Covid-19 Detection by analyzing Chest X-Ray Images-I

An attempt to detect Covid-19 in a patient by analyzing the Chest X-ray images using the ResNet50 Deep Learning Model

Shaunak Varudandi
Geek Culture

--

Photo by National Cancer Institute on Unsplash

Introduction

Coronavirus (COVID-19) is an infectious disease caused by related RNA viruses that affect mammals and birds. After the first case was reported in Wuhan, China in December 2019, Coronavirus became a global pandemic that has caused around 3.8 million deaths (as of 18th June) around the world. One of the techniques to diagnose if a patient is suffering from Covid-19 is to study their chest x-rays to look for evidence of probable infection and later, administer the requisite treatment to cure them of the disease.

I decided to automate this process of diagnosis by leveraging the power of pre-trained Deep Learning models. The deep learning model would analyze the image of a chest x-ray and help faster diagnose the Covid-19 virus in a patient.

Problem Statement

To develop a chest x-ray classifier that appropriately analyzes an x-ray and determines whether a patient is positive or negative for Covid-19.

Workflow

To start with this project, I had to get hold of a good set of chest x-ray images that can be used for training the deep learning model. By good, I mean a data set that satisfies quantity as well as quality. Quantity is important since it is paramount for the deep learning model to generalize as much as possible. On the other hand, quality is important since low-quality images would make it difficult for the deep learning model to pick up defining training points from the images during the training phase, thereby considerably reducing the model accuracy.

Readers who are interested in implementing the chest x-ray classifier project can download the dataset from the following link.

Data Pre-processing and Data Augmentation

Having procured the ideal data-set for the project I started analyzing it. The first observation I had was that the number of images present was still not enough to adequately train the deep learning model. Additionally, the images had to be converted into a format that will be accepted by the deep learning model during the training phase.

Fortunately, I had a way to overcome both the aforementioned deficiencies in my dataset via a single procedure. I used the ImageDataGenerator library from Keras to perform Data Augmentation. At the same time, I imported preprocess_input from the ResNet50 package in Keras. Preprocess_input helps in converting the images into a format that is accepted by the ResNet50 model as input. I then passed this package as an input to the parameter “preprocess_function” that is present in the ImageDataGenerator package.

Such a technique helped in creating additional images which helped me train my deep learning model adequately. Simultaneously, it helped me process the images and get them ready for the training phase.

The final count that I achieved was 7800 images for the training set, 1008 images for the validation set, and 800 images for the test set. A total of 9608 images in my entire data set.

Model Training & Model Assessment

Step 1: Train the Deep Learning Model.

The data pre-processing and the data augmentation stage helped in generating the requisite data needed to adequately train and test the deep learning model. Now it was time to train the deep learning model using the training and the validation data.

To do so, I first downloaded the ResNet50 deep learning model from Keras after which I added a Flatten layer and a Dense Layer. Additionally, I made sure that the upper layers of the ResNet50 model are not retrieved while downloading the entire model and I decided to set the layer.trainable parameter to false.

Next, I used two more packages provided by Keras that are, EarlyStopping and ModelCheckpoint. EarlyStopping was used to ensure that I do not overfit the model and training stops when there is no increase in the validation accuracy. Whereas, ModelCheckPoint was used to save the model immediately when there was an increase in the validation accuracy at the end of an epoch.

Lastly, while compiling the model before starting the training process, I set the learning rate for the Adam optimizer to 1e-1. The reason being that a learning rate of 1e-1 provided me with an optimum result in terms of accuracy, precision and recall. I decided to train the model for 30 epochs but the training process stopped after the 13th epoch as there was no increase in the validation accuracy. This ensured that I do not overfit my deep learning model.

Step 2: Assess Model Performance using Test Data.

As soon as the training was completed and the best model was saved, it was time to test the deep learning model. As mentioned before, I had around 800 chest x-ray images that I could use for testing the model performance.

Firstly, I decided to calculate the Accuracy of the trained model. The evaluate method helped me assess the model accuracy which came out to be 91.25%.

Next, I decided to calculate the Precision as well as the Recall for my trained model. According to the use case, I decided to put more emphasis on recall since my model mustn't classify the x-ray of a patient suffering from covid as normal. This would mean that the patient would have the wrong information that could cost him/her their life. I decided to use classification_report from the sklearn package and calculated the precision as well as the recall scores for the trained model.

(Image by Author)

As we can see clearly in the above image, the recall score for the covid label is higher than the recall score for the normal label and that is exactly what I wanted to achieve. During the training phase, I did achieve marginally higher accuracies for different learning rates, but that would mean a compromise on the recall score which I was not ready to make. Therefore, I decided to stay with a 1e-1 learning rate which achieved a respectable accuracy and a really good recall score.

Additionally, I did plot the Confusion Matrix which allowed me to have look at the number of images that were classified correctly and the number of images that were incorrectly classified.

(Image by Author)

Lastly, I plotted the ROC curve and calculated the AUC score to be certain that the model accuracy is not some false indication of the model’s performance. I wanted to be sure that the deep learning model trained on the training data is not some dumb model that predicts every patient is positive for covid. Firstly, I calculated the AUC score which came out to be 0.903 and later plotted the ROC Curve which bolstered the fact that our deep learning model was indeed a good model that classifies based on the training points which it captured during the training stage.

(Image by Author)

Conclusion

Having already worked on 2 NLP projects, I thought about taking up a new challenge and working on a project that includes data that is comprised of images. Chest X-Ray Classifier to detect Covid-19 gave me this opportunity and helped me work with a pre-trained model (ResNet50) as well as learn about its core functioning. Additionally, it helped me try out different techniques which are used to determine a models performance and made me cognizant about the importance of every model performance test.

The next step in this project is to deploy the Deep Learning model as a backend to a web application that classifies an X-Ray belonging to the class Covid or Normal. A detailed walkthrough of all the steps needed to deploy the Deep Learning model on Google Cloud Platform’s App Engine Sevice can be found in Part II of this blog. Be sure to check that one out as well.

The workflow followed by me for this project can be found on my Github page. I hope you enjoyed reading my blog.

--

--

Shaunak Varudandi
Geek Culture

MBA (Co-op) student at the DeGroote School of Business || Aspiring Business Data Analyst.