Detecting COVID-19 using images from COVID-19 Radiography Database from Kaggle

Screen Shot 2020-09-28 at 8 59 47 PM

Screen Shot 2020-09-28 at 9 24 33 PM

The purpose of this project was to create a resnet18 convolutional neural network using PyTorch that would be capable of classifying images from the COVID-19 Radiography Database Dataset from Kaggle.com click here.

Screen Shot 2020-09-28 at 9 26 50 PM

I started the project by first importing all the necessary libraries and modules. We need to make sure we are importing the most up-to-date PyTorch version available. PyTorch is an open source machine learning library based on the Torch library. It is commonly employed for computer vision and natural language processing tasks. PIL will be used to display the images from our training and test sets. We will also be utilizing torchvision to perform the image transformations and augmentations on our training set during the training phase of the project.

Screen Shot 2020-09-28 at 9 12 39 PM

Next, came the training and test set preparation along with creating a custom dataset, if need be. It consists of cycling through the relevant folders and deriving the necessary images to create the training and test sets to be used in the project. The class is created from torch.utils.data.Dataset. Below is the framework of the class that will need to be generated:

Screen Shot 2020-09-28 at 9 37 08 PM

DataLoaders need to be created that will aid in populating the sets from the images in each of the folders. These essentially fetch examples to feed the model during the training process. The DataLoaders iterate over the folders containing the images and tell us how many images are in each. Allowing us to correctly segmentate the relevant images into their respective categories.

This is for training:

Screen Shot 2020-09-28 at 9 42 21 PM

This is for testing:

Screen Shot 2020-09-28 at 9 42 31 PM

For reference, this is a visual representation of the training and test batch sizes we will be utilizing:

Screen Shot 2020-09-28 at 9 58 19 PM

We then proceed to the visualization phase of the project where we create a helper function that forms part of the training loop. This function converts the tensorflow images back to numpy arrays. It also undoes the normalization performed in the previous step using mean and standard deviation (std). The image pixel values are converted back to their orginal values and displayed using subplot.

Screen Shot 2020-09-28 at 10 05 30 PM

Screen Shot 2020-09-28 at 10 05 52 PM

Then comes the model creation juncture of the project. We use the resnet18 model imported from torchvision.models. It comes ready with pre-trained weights, having been trained on the ImageNet Dataset. This image database is comprised of hundreds and thousands of images. This makes it quite a robust model to play with and serves our purpose perfectly. Not without first adjusting one of the parameters found in the fully-connected layer of the resnet18 model architecture. The resnet18 model comes with a pre-defined out_features parameter of 1000 and we only need 3 (normal, viral, covid). We also set the desired optimization and learning rate.

Screen Shot 2020-09-28 at 10 17 53 PM

The most interesting and fun part is the training phase of the project. Wherein, we witness the runnning of our epochs and the training of our model. We see validation loss and accuracy scores for every 20 steps ran. The model is ran until we reach a classification accuracy of >95% as per our specified acceptance threshold.

Screen Shot 2020-09-28 at 10 21 22 PM

Screen Shot 2020-09-28 at 10 21 48 PM

These are our final predictions illicited by our COVID-19 resnet18 PyTorch Image Classifier …

Screen Shot 2020-09-28 at 10 25 17 PM

It is not 100% accurate, so we must expect some mistakes …

Screen Shot 2020-09-28 at 10 25 27 PM

Key takeways:

1. This resnet18 image classifier can play an essential role in clinical treatments and teaching tasks. Doctors and medical practitioners can use the predictions illicited by the model to guide their intuition and make more informed decisions.

2. The model can be trained even further by providing it with more pre-labeled medical image datasets. A significant amount of medical expertise is needed to correctly label the images that will be fed to the model, but eventually the model will be very adept when classifying novel data.

3. Over time, the model will be able to correctly classify vast amounts of data with significant accuracy and minimal validation loss. This resnet18 image classifier can then be utilized an a myriad of ways, including but not limited to, the detection of new strains of COVID. This would undoubtably lead to noteworthy discoveries that will lead to advances in the medical field.

Screen Shot 2020-09-28 at 10 56 14 PM

COVID-19 PyTorch Image Classifier