Categories
Blog

COVID-19 / Pneumonia Prediction using Deep Learning

Currently, the entire world is gripped by the fear of novel coronavirus. As of June 2020, there were over 8 million cases recorded worldwide. The pandemic has not only brought life across the globe to a standstill, but also affected the mental health of millions. What makes it worse is the fact that most people do not know the exact way they contract the virus. Some experts, including the WHO, say that the virus spreads through either direct or indirect contact with a person – droplets that spread when an infected person sneezes or coughs is one of the ways by which this virus spreads.

Medical professionals all over the world have issued several guidelines and precautionary measures to prevent the rise of contagious coronavirus, like staying indoors, washing hands frequently, not touching one’s face, wearing masks etc.   It is unfortunate that this crisis has affected those who live in remote areas the most. Daily wage earners or people who do not have a decent household income have proven to be the most vulnerable.  In India, the average cost of a coronavirus test is more than Rs 4,500 (in private hospitals). Time taken to get the test results also depends on many factors, like where one lives, population density of the area, etc.

It is also true that the one thing that can help nations nip this virus in the bud is conducting tests on infected persons as early as possible. So, in this blog, we will look at how we can predict coronavirus test results using X-ray images. 

Disclaimer

This blog post on COVID-19 prediction is a proof-of-concept and for educational purposes only. It is not meant to replace professional medical advice.

Predicting COVID-19 using X-ray images

Predicting COVID-19 / Pneumonia / Normal using X-Ray scans with Keras and Tensorflow

In this tutorial, we will use X-ray images to analyse the health of the patient’s lungs. COVID-19 virus usually attacks the epithelial cells of human beings (cells that line the various surfaces of the human body). So, the X-ray scan will help us identify the affected epithelial cells on the surface of the respiratory tract. Also, since X-ray scans are easier to get than a CT scan, so, we opted for the same for our tutorial. 

Step 1: Training Keras model to predict COVID-19 / Pneumonia / Normal cases

For the first step, you can go ahead and download the training and testing dataset from the link below:
Drive Link

Your downloaded data set folder should look like this:

1.1 Importing modules

We need cv2 and matplotlib to perform image manipulations and visualisations, OS, and glob for file system management. Here are the list of images in the “dataset/test/covid” folder:

Here are the list of images in the “dataset/test/covid” folder.

1.2 Data cleaning, naming, and structuring (optional)

  • If you have your own dataset, you can create a separate folder for each label and name the folder with the image label. Eg: All COVID images can be moved into the ‘covid’ folder.

Step 2: Image Manipulation and Augmentation

It is usually a good idea to manipulate the image using rotation, resizing, and scaling. This helps make the model more robust  to different images that our dataset may not have. We can use the ImageDataGenerator to do this automatically for us.

In general, datasets are represented with two CSV files–one for training and the other for testing. The entire training set can fit into RAM and no data augmentation is required there. However,  real-world datasets are rarely that simple!(I’m sure all of you who’re reading this are nodding in agreement right now.) Real-world datasets are often too large to fit into memory and they also tend to undergo overfitting of the ML model.

To avoid this data augmentation is performed. Here, generators are used for various purposes, like to load images in batches, to augment images, and to save memory consumption.

2.1 Initialising ImageDataGenerator object

  • rotation_range=30, # Rotate the image by 30 degrees
  • width_shift_range=0.1, # Shift the image width by maximum 10%
  • height_shift_range=0.1, # Shift the image height by maximum 10%
  • rescale=1/255, # Rescale the image by normalizing it
  • shear_range=0.2, # Shear means cutting away a part of the image (maximum 20%)
  • zoom_range=0.2, # Zoom into the image  by 20% maximum
  • horizontal_flip=True, # Allow horizontal flipping
  • fill_mode=’nearest’ # Fill in missing pixels with the nearest filled value

Here is a preview of the transformed image:

2.2 Generating Batches

flow_from_directory: This method takes the path of a directory and generates batches of augmented data and additional parameters, like target_size, batch_size, and class_mode.

In this case, we will assign batch_size as 16 (based on my dataset size). Since our model predicts multiple classes, that is three,  class_mode is assigned as categorial.

Step 3: Building the CNN with Keras

The next step would be to import modules that are required to design a convolutional neural network. The network inputs images of size (150,150,3) where 3 represents the three coloured channels.

The model type that we will use is Sequential as it is the easiest way to build a model in Keras. Sequential model also allows you to build a model layer by layer.

Our first two layers are Conv2D layers. The first layer consists of 32 filters and the second convolution layer consists of 64 filters. So, a kernel of size (3,3) means there is a 3×3 matrix, dot product of 3×3 matrix of image and 3×3 matrix of random numbers. This output is given as input for the next layer. This is repeated for all 64 filters.

The activation function we will use for our first two layers is the ReLU, or Rectified Linear Activation. This activation function has been proven to work well in neural networks.

After each convolution layer, a pooling layer will be added. Pooling layer reduces the size of the image; pool_size of (2,2) takes each 2×2 matrix of input image pixels and replaces it with the maximum value of that matrix.

In between the Conv2D layers and the dense layer, there is a ‘Flatten’ layer. Flatten serves as a connection between the convolution and dense layers.

‘Dense’ is the layer type we will use in our output layer. Dense is a standard layer type that is used in many cases for neural networks.

Step 4: Training the model

Since we are using generators, the model is trained using fit_generator; fit_generator method first accepts a batch of a dataset, performs back propagation on it, and then updates the weights in the model. This process is repeated for the number of epochs specified.

4.1 Results and Visualisation

We find that our model has an accuracy of 85% in the training set.

Model Accuracy
Model Loss

Conclusion : 

To this end, we have made the code, model, and results available here. We shall also update the  dataset and model as and when more data gets publicly accessible. We are, in fact, looking for a better method to evaluate the entire test set. 

Please note that we have also discovered that our  model loss is spiking towards the end of training and we are looking for a possible solution.

NOTE: If any of you managed to get a better metric than us, better metrics than me, please do share your learnings.  After all, knowledge only grows with sharing! 

References

towardsdatascience.com/using-deep-learning-to-detect-ncov-19-from-x-ray-images-1a89701d1acd

https://jovian.ml/v-snehith999/corona-pneumonia-normal-keras

Author:

– Snehit Vaddi

I am a Machine Learning enthusiast. I teach machines how to see, listen, and learn.

Linkedin: https://www.linkedin.com/in/snehit-vaddi-73a814158/

Github: https://github.com/snehitvaddi

Leave a Reply

Your email address will not be published. Required fields are marked *