COVID-19 / Pneumonia Prediction using Deep Learning
By: Snehit Vaddi
Tags: AI, Deep Learning, Keras, Machine Learning, Covid 19

Predicting COVID-19 using X-ray images

In this tutorial, we will use X-ray images to analyze 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

Link for complete code GITHUB REPO

Your downloaded data set folder should look like this:

figure 1: Example of a directory structure of X-ray images of COVID-19 positive patients. Using X-ray images we train CNN (Convolutional Neural Network) to detect COVID-19 using Keras and Tensorflow

1.1 Importing modules

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

Here is 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.

“from keras.preprocessing.image import ImageDataGenerator”

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 colored channels.

The model type that we will use is Sequential as it is the easiest way to build a model in Keras. A 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 3x3 matrix, the dot product of the 3x3 matrix of the image, and a 3x3 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. The pooling layer reduces the size of the image; pool_size of (2,2) takes each 2x2 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 backpropagation 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.

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 me, please do share your learnings. After all, knowledge only grows with sharing!

⚠Creator Disclaimer

All results from this open-source code should only be used for research/academic/personal purposes only. As the models are trained on the open-source datasets, any form of commercial use is strictly prohibited. Please contact us for any further queries.

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

Github: https://github.com/snehitvaddi