Pneumonia Detection using Quantum Transfer Learning

Pneumonia Detection using Quantum Transfer Learning

Quantum Machine Learning for Medical and HealthCare

In this blog, we leverage transfer learning, a powerful machine learning technique, to enhance the performance of an image classifier. Classical and quantum networks are merged to create a hybrid architecture that gives practical usefulness of Quantum Machine Learning.

Transfer learning in a CNN is a machine learning technique that uses a pre-trained model to train a new model on a different task. Transfer learning is like borrowing knowledge from a smart friend. If they're good at solving one problem, they can help you solve a similar problem too, with just a little extra guidance.

  • Network 'A' is trained on a dataset Da for a given task Ta.

  • Remove some final layers of the network. Now the remaining network A' becomes a feature extractor.

  • We connect a trainable network B at the end of A'. This trainable network B can be classical or a variational quantum circuit. For this blog we use network B as a Variational Quantum Circuit.

  • We keep the weights or the trainable parameters of A' as constant and only train the network B with a dataset Db for a new task Tb.

About the Model:

The Quantum Circuit used is as follows:

A graphical representation of the full data processing pipeline:

  • As pre-trained network A we use ResNet18, a deep residual neural network which is pre-trained on the ImageNet dataset.

  • After removing its final layer we obtain A'. This will act as a pre-processing block which maps any input high-resolution image into 512 abstract features.

  • These features are classified by a 4-qubit “dressed quantum circuit” B. A variational quantum circuit sandwiched between two classical layers.

  • The hybrid model is trained, keeping A' constant, on the Pneumonia dataset containing images of chest X-Rays of Pneumonia and Normal patients.

About the DataSet:

The dataset used is named as 'Chest XRay Images (Pneumonia)' on Kaggle. The dataset is organized into 3 folders (train, test, val) and contains subfolders for each image category (Pneumonia/Normal). There are 5,863 X-Ray images (JPEG) and 2 categories (Pneumonia/Normal). For further detail refer the below link:

https://www.kaggle.com/datasets/paultimothymooney/chest-xray-pneumonia?select=chest_xray

Code:

Let's see the code for the above model.

  • Importing the libraries:

  • Initializing the hyperparameters:

  • Setting up the device on which the neural network is to be trained. Here we use only CPU. Also setting up the device for Quantum Node in Pennylane QML.

  • We resize each image to a specific size since all the images in the dataset are of different sizes which is not suitable for a neural network.

  • Importing dataset:

  • Get the class names:

  • Loading the data:

The torch.utils.data.DataLoader class is used in the PyTorch library to create a Python iterable over a dataset. It provides features such as batching, shuffling, and parallel loading of data, making it convenient for training and evaluating models with large datasets.

  • Creating Quantum Variational Circuit

  • Defining Dressed Quantum Circuit (Quantum Circuit sandwiched between 2 classical layers):

We can now define a custom torch.nn.Module representing a dressed quantum circuit.

This is a concatenation of:

  • A classical pre-processing layer (nn.Linear).

  • A classical activation function (torch.tanh).

  • A constant np.pi/2.0 scaling.

  • The previously defined quantum circuit (quantum_net).

  • A classical post-processing layer (nn.Linear).

Build the Classical-Quantum Network:

Setting up the Loss function and Optimizer:

Define the Training function:

Train the Model:

Tuning few other hyperparameters like quantum circuit depth, image size, number of epochs just gave the same accuracy.

However other classical CNN models and classical transfer learning methods gives better accuracy in less training time on the same dataset. Hence proving a quantum advantage over classical model will need a lot of research and time.

Credit for the above blog:

I have gained the information of the above blog from article written by Andrea Mari on Pennylane Website. Link to the article:

https://pennylane.ai/qml/demos/tutorial_quantum_transfer_learning/