10

Pneumonia Detection using Deep Learning

 3 years ago
source link: https://mc.ai/pneumonia-detection-using-deep-learning/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A Pandemic in Modern Times

With the recent outbreak of COVID-19 also known as the coronavirus, it does seem like history is repeating itself and we are going back in time to the 1900s during the spanish influenza. The coronavirus is a deadly virus that has claimed hundreds of thousands of lives in countries around the world. Older adults and people who have severe underlying medical conditions or prior cases of pneumonia seem to be at higher risk for developing more serious complications from the virus. With rising deaths and limited medical resources, doctors and medical professionals around the world are working around the clock to treat patients and prevent the spread of the virus. Severe forms of the virus can cause pneumonia leading to greater risk of death. It is crucial to have quick and accurate detection of pneumonia so patients can receive treatment in a timely manner especially in impoverished regions.

With the growing technological advancements we have in this day and age, it is possible to use tools based on deep learning frameworks to detect pneumonia based on chest x-ray images. The challenge here would be to aid the diagnosis process which allows for expedited treatment and better clinical outcomes.

The Data

The dataset that will be used for this project will be the Chest X-Ray Images (Pneumonia) from Kaggle. The dataset consists of training data, validation data, and testing data. The training data consists of 5,216 chest x-ray images with 3,875 images shown to have pneumonia and 1,341 images shown to be normal. The validation data is relatively small with only 16 images with 8 cases of pneumonia and 8 normal cases. The testing data consists of 624 images split between 390 pneumonia cases and 234 normal cases.

As shown from the training data, we can see that the data is very imbalanced. There appears to be almost three times more pneumonia cases than normal cases which makes sense because this is medical data. Since the validation data is a small sample, I will just combine it with the training data and shuffle the new dataset before applying a training/validation split.

After exploring some of the images in the dataset, I noticed that some images were grayscale 1-channel and others were 3-channel. Before doing any transforms and data loading, the data needs to be consistent so I decided to convert all single channel images to 3-channel images. This is done simply by repeating the single channel values 3 times as shown in my code below for the PneumoniaDataset class.

For the training set and validation set, I decided to do a 80/20 split. For the data transforms, I chose to resize the images to a (256, 256) size and use a 90 degree affine transformation. Data transformation or augmentation is a powerful technique which helps in almost every case for improving the robustness of a model. This technique can prove to be even more helpful when the dataset is imbalanced. You can generate different samples of undersampled classes in order to try to balance the overall distribution. For the sake of simplicity, I will just use the original dataset. For the batch size, I will use a size of 16 since a lower batch size will help prevent our model from overfitting. Let’s take a quick look at some of the images in our training and validation sets.

After carefully looking at the images, it would be hard for anyone besides medical professionals to distinguish between a normal case and a pneumonia case. If we’re able to build a strong model, it would certainly help medical professionals alot in their work which hopefully in the end helps them save time and money.

The Model

Our initial model will be a simple 8-layer convolutional neural network with max pooling and a ReLU activation function. Let’s see how well the model does with the validation and testing set.

We can see that our model does quite well with the training and validation datasets but failed to classify not even half of the images from the testing set correctly. Our model is definitely overfitting so we need to introduce some regularization techniques that can help with this issue. Let’s define a new model with batch normalization and dropout, two common regularization techniques that are very useful in preventing overfitting.

With the introduction of batch normalization, dropout, and a few more layers, this model proves to be much better than the previous model. It is now able to classify more than half of the images in the testing dataset correctly. However, this is still not good enough to be used in the real world comfortably. The models that we’ve used so far are not complex enough to capture the intricacies of the images. To solve this issue, we use the optimization technique of transfer learning where we repurpose complex models trained for one purpose on another. In this case, we will use a pretrained residual network that is trained on the ImageNet dataset and repurpose that model to our chest x-rays dataset. We will use a 34 layer residual network with dropout in the last layer and see how well it compares to the previous models.

A testing accuracy of about 83% which is definitely an improvement from the prior two models. This shows the power and effectiveness of using transfer learning to solve general image problems. While the testing accuracy has increased significantly, it is still questionable whether or not this is good enough to be deployed into the real world. Let’s try the model on a few test images.

The model clearly isn’t great but it’s still impressive at what it does. It’s definitely better than me at classifying whether the above two images are diseased or normal.

Final Words

In this project, we’ve discovered the power of transfer learning and how well it can improve the performance of a deep learning model. The final model certainly could’ve been improved more by further adding regularization and optimization techniques as well as tweaking hyper-parameters. Also, training on an imbalanced dataset is not optimal so generating new images to balance the distribution would also prove to be useful in this case.

References


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK