Detection Of Pneumonia From Chest X-Ray Images

Herambh Dakshinamoorthy
Geek Culture
Published in
6 min readAug 28, 2021

Discover The True Power Of Deep Learning As It Predicts If a Person Is Suffering From Pneumonia With an Accuracy of 93%

Photo by CDC on Unsplash

To view the complete code click here

What is Pneumonia?

Pneumonia is an infection that inflames the air sacs (alveoli) in one or both lungs, caused by bacteria, viruses or fungi. The air sacs get filled with fluid or pus, causing cough with phlegm and difficulty breathing.

The chest X-ray of a person with Pneumonia has areas of abnormal opacification which is not always clearly visible to the human eye. This makes it difficult to make accurate predictions and save lives. This is where we can see the beauty of Deep Learning.

What is Deep Learning?

Deep Learning is a subset of Artificial Intelligence that uses Neural Networks that imitates the workings of the human brain in processing data and creating patterns to make decisions.

The Neural Network consists of many layers which are interconnected and capture certain non-linear and linear relationships. Each layer is responsible for learning specific features and the model gets better and better as the number of layers increases.

Method

This project uses PyTorch a Python native, which integrates easily with other Python packages, which makes this a simple and easy to learn choice.

Here, I am using Residual Networks (ResNets) via Transfer Learning, a popular deep learning neural network model.

Here are the steps involved:

  1. Downloading the Dataset
  2. Importing the Required Libraries
  3. Data Analysis and Visualization
  4. Data Augmentation and Normalization
  5. Preparing the Training and Validation Datasets
  6. Setting Up the GPU
  7. Creating Model Class
  8. Train and Evaluate the Model
  9. Accuracy and Loss Plots
  10. Plot Predictions against Actual Labels
  11. Conclusion

Downloading the Dataset

The first step would be to download the data that we are intending to use. The dataset is available at https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia. Using the opendatasets library from Jovian, I downloaded the data from Kaggle.

Importing the Required Libraries

Photo by Zaini Izzuddin on Unsplash

We will require a number of libraries such as Pandas, Matplotlib, Numpy and other PyTorch utilities to streamline our workflow.

Data Analysis and Visualization

Photo by Carlos Muza on Unsplash

In order to use our dataset, we must first examine it and take necessary actions for it to best fit our model.

While exploring the data, I came something significant. There were more number of images for the Pneumonia class than that for Normal. This may produce faulty results as our model may give more weightage to the Pneumonia class. In order to combat this, we apply Data Augmentation.

This is how a sample of images that our model is trained on looks like!

As it can be seen, it is very hard to exactly tell whether the person is suffering from Pneumonia or not just from the X-Ray.

Data Augmentation and Normalization

by Shubham Goyal from Towards Data Science

We want our model to learn features which it is specifically targeted to and not anything present in the background. This is the reason we apply Data Augmentation. This helps us to generalize the data and prevent it from learning any unwanted features.

Normalizing the data prevents the values from any one channel from disproportionately affecting the losses and gradients while training, simply by having a higher or wider range of values that others.

Preparing the Training and Validation Datasets

Now it’s time to prepare our training and validation dataset. Here we split the train dataset into 75% train and 25% validation.

Training set — used to train the model i.e. compute the loss and adjust the weights of the model using gradient descent.

Validation set — used to evaluate the model while training, adjust hyperparameters (learning rate etc.) and pick the best version of the model.

We can now create data loaders to help us load the data in batches. We shuffle the training data loader to ensure that the batches generated in each epoch are different. This randomization helps generalize & speed up the training process.

A batch of Training Data from the DataLoader

Setting Up the GPU

Photo by Andy Holmes on Unsplash

The Graphical Processing Units (GPUs) contain hundreds of cores that are optimized for performing expensive matrix operations on floating point numbers in a short time, which makes them ideal for training deep neural networks with many layers.

To seamlessly use a GPU, if one is available, we define a couple of helper functions (get_default_device & to_device) and a helper class DeviceDataLoader to move our model & data to the GPU as required.

Creating Model Class

We will be using Residual Networks in our model. A Residual Network adds a residual block to the CNN, which adds the original input back to the output feature map obtained by passing the input through one or more convolutional layers. This small change produces a drastic improvement in performance. It allows deeper networks to be trained without hurting performance as some layers can be skipped.

You can refer to this blogpost to get a better understanding of how ResNet works: https://towardsdatascience.com/residual-blocks-building-blocks-of-resnet-fd90ca15d6ec

Representation of ResNet50

Train and Evaluate the Model

Negotiation Experts

Finally, it’s time to train and evaluate our model on the entire train and validation sets.

Configurations like batch size, learning rate, etc. need to picked in advance while training models, and are called hyperparameters. Picking the right hyperparameters is what makes a great model stand apart from the good ones.

Accuracy and Loss Plots

Our model has an accuracy of 100% on the training set and 98.88% on the validation set which is a pretty decent result.

Plot Predictions against Actual Labels

Let’s have a look at a few images with their respective labels and what our model has predicted.

Our model has a test accuracy of 93% which is far better than human predictions. Thus, we can be more sure about our predictions and save more lives.

Conclusion

I hope that this post has unveiled the potential power of Deep Learning. With all the fast paced advancements that are being made in Technology, we will soon have processors strong enough to perform this in a fraction of a second with unbelievably high accuracies.

To view the complete code click here

--

--

Herambh Dakshinamoorthy
Geek Culture

Passionate Mechanical Engineering Undergrad merging Physics, Math, and AI to revolutionize the Automotive Industry for a cleaner, sustainable future.