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.

Image for post
Image for post
COVID-19 X-ray Dataset (Above 4 images are infected X-rays and below 4 images are normal X-rays)

Dataset:

  1. 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

Image for post
Image for post

2. Data Preparation and Preprocessing

Image for post
Image for post

To load our data, we grab paths to images in the dataset directory (Lines 22–25).

Image for post
Image for post

Then for each image path, we:

  1. Load the image and preprocess it by converting to RGB channel and resizing it to 224 X 224 pixels.
  2. Extract the class label (either covid or normal) from the path.
  3. Update our data and labels list respectively.
Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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).

Image for post
Image for post
Classification Report Obtained
Image for post
Image for post
Confusion Matrix Obtained
Image for post
Image for post

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%.

Image for post
Image for post

We, then plot our training accuracy and loss (Lines 118–123).

Image for post
Image for post
Training accuracy Vs Validation accuracy
Image for post
Image for post
Training loss Vs Validation loss

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.

Written by

Data Science enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store