Detecting COVID-19 in X-ray images using Deep Learning
Developing a model that can help in fighting the COVID-19 pandemic.
In this tutorial, you will learn how to detect COVID-19 in X-ray images using Tensorflow and Keras.
Like most people in the world right now, I’m concerned about COVID-19. But instead of sitting idle and doing nothing about the Covid pandemic, I decided to focus on the aspects that can tackle this problem or atleast can give some ray of hope. Therefore, I created a Web Application that could detect COVID-19 in X-ray images.
In this tutorial, which is my first, I will try to keep it simple. Therefore, In this tutorial we will go through only the model creation part not the Web Application.
Those who are interested in full project code, can check out my GitHub repository.
- The COVID-19 X-ray image dataset we will be using is taken from this GitHub repository. Inside this repository, you will find example of COVID-19 cases, as well as MERS, SARS and ARDS. Therefore, in order to create a sample dataset for COVID-19 cases, I parsed the metadata.csv file found in the above repository. The procedure I followed to parse is beyond the scope of this tutorial. If you are interested on how to parse the data, check my GitHub repository.
- Next, X-ray images of healthy patients were collected from Kaggle’s Chest X-ray images (Pneumonia) dataset. From this dataset, I sampled only 99 images because our dataset of COVID-19 infected patients contain only 99 images. So, if i would have sampled more images, then there would be a case of class imbalance.
I have included my sampled dataset in my GitHub repository, so you do not have to recreate it.
Now, let's start the model creation part.
1. Import necessary libraries
2. Data Preparation and Preprocessing
With our imports taken care of, next we will prepare our data.
To load our data, we grab paths to images in the dataset directory (Lines 22–25).
Then for each image path, we:
- Load the image and preprocess it by converting to RGB channel and resizing it to 224 X 224 pixels.
- Extract the class label (either covid or normal) from the path.
- Update our data and labels list respectively.
We then scale pixel intensities to the range [0,1] and convert both our data and labels to Numpy array format (Lines 50–51).
Next, we will one-hot encode our labels and create our training/ testing split (80% for training and 20% for testing) (Lines 54–59).
In order to ensure that our model generalizes, we perform data augmentation on images (Lines 62–66).
3. Model Creation and Tuning
Instantiate the VGG16 network with weights pre-trained on ImageNet, leaving off the fully connected layer head (Line 71).
From there, we construct a new fully connected layer head consisting of pooling and fully connected dense layers and append it on top of VGG16 (Lines 74–81).
We then freeze the weights of VGG16 such that only the fully connected layer head will be trained (Lines 84–85).
4. Compiling and Training the Model
We are now ready to compile and train our COVID-19 model.
Compile the network with Adam optimizer. And we will use “binary crossentropy” loss as this is a 2 class classification problem (Line 87).
Now, we will start our neural network training process by passing our chest X-ray data via data augmentation object (Lines 90–93).
5. Model Evaluation
For evaluation, we first make predictions on the testing set and grab the prediction indices (Lines 96–97).
We, then generate classification report and confusion matrix using scikit-learn library (Lines 100–103).
Now, we will use the confusion matrix to derive accuracy, sensitivity and specificity and print each of these performance metrics (Lines 104–113).
If you are following the above code, you should get accuracy ~ 98%, sensitivity ~ 95% and specificity = 100%.
We, then plot our training accuracy and loss (Lines 118–123).
Finally, we save our COVID-19 classifier model (Line 126).
That’s it, now we have our own COVID-19 detector up and running.
The complete code for this article can be downloaded from this repository: https://github.com/paramattri/Covid-19-Detector.
I hope you enjoyed the blog post.
This is my first post, so let me know where can I improve.
Thank You and Stay Safe.