6 min read

Using X-Ray Images to detect COVID-19 Patients

Using X-Ray Images to detect COVID-19 Patients

A quick introduction to an experiment on classifying chest x-ray images of COVID-19 using FastAI


SARS-CoV-2 or Severe Acute Respiratory Syndrome Coronavirus 2 has taken the world by storm. Nations are locked down to prevent the spread, economies are taking a nose dive, unemployment rates are going through the roof and amidst all of this healthcare system is being stress tested all around the world. We’ve all seen numerous exponential curves and how we all need to play our part to flatten the curve to prevent overwhelming the healthcare system. There are numerous things we need to do together to battle this virus including staying home, social distancing, good hygiene and following all the directives issued by WHO and your respective governments.

The real heroes of this battle are our healthcare providers and their battleship is the healthcare system and infrastructure. There is immense planning and wartime execution going on at hospitals and healthcare systems. Contact tracing, isolation, testing at a large scale, intensive care and so on. This article talks about the use of technology in the field of radiology to augment testing capabilities.

Before I proceed I want to be absolutely clear that the ideas presented in this article are just some experiments performed while sitting in front of a desk and by no means the idea or the result produced is remotely close to being used in the real scenario. Deploy-able machine learning systems need rigorous clinical and field testing. My intent is not to spread or aid in spreading misinformation or to suggest that deep learning is the magic wand we all need. Please read this article as purely an exercise in augmenting your own deep learning knowledge and a fun experiment to do and learn about FastAI. Lets begin!

Fast AI

Fast.ai is a brainchild of Rachel Thomas and Jeremy Howard. The idea behind FastAI is to democratize AI to reach people from all domains. They have created a complex but simple to use library which does all the heavy lifting for you so that you can focus on building the model and domain specific nuances and not worry too much about setting things up. Fast AI offers much more abstraction than Keras. Keras as you might know is built over Tensorflow whereas Fast AI is built over PyTorch. To illustrate the level of abstraction provided by Fast AI compared to Keras, a 31 line code in Keras to setup resnet50 model requires just 5 lines of code in Fast AI! Fast AI allows you to build learning models at a blazingly fast pace. I’ve recently started learning fast.ai and classifying x-ray images is one of the 3–4 different classifier models I was able to create in a couple of days.

Source: xkcd.com

X-Ray dataset

The X-Ray dataset I’ve used for this experiment is taken from an open source x-ray and CT dataset.

ieee8023/covid-chestxray-dataset
We are building an open database of COVID-19 cases with chest X-ray or CT images. - ieee8023/covid-chestxray-dataset

The dataset is created from images from research publications from China and other Countries in the world. We will use this dataset to build our classifier.

Setting up Fast AI and data

There is detailed documentation on setting up Fast AI on fast.ai. If you take up a course on their website Practical deep learning for coders it will guide you through the setup process. You will need to have a kaggle account or you can use a Colab environment or other paid options like Amazon Sagemaker Azure etc. Basically you will need a machine with an Nvidia GPU. If you have the patience (3–5 days) and enthusiasm to setup your own machine by all means do that. But the above environments help you to get going in less than couple of hours.

OK once you are done setting Fast AI up lets move on to importing fast ai libraries and creating our dataset.

Download the x-ray dataset from the link above and save it in a folder. There is a csv file, metadata.csv, which describes the details about the x-ray images. We will read this file and using this data we will segregate the images according to their labels. I am using only X-ray images (not CT scans) with a Posteroanterior (PA) views.

covid_images and non_covid_images will give you a list of files corresponding to the positive and negative class. Put these files into two different folders named covid_positive and covid_negative. The names of these folders will serve as your class labels. The ImageDataBunch method reads the files inside the labeled folders and splits it into training and validation sets, resizes them, normalizes them all in one shot! show_batch method allows you to view your data.

This is all the setup you need to to to get started with FastAI you can now jump into building your model.

Training and Validating the model

We will use transfer learning to train our model. If you are not familiar with transfer learning I would recommend to have a peek at this article. I am using a Resnet50 to build the classifier.

We will use precision and recall as metrics for our model. cnn_learner downloads the specified cnn model. Here we are downloading resnet50. You also specify your databunch to the cnn_learner. Once you have the model downloaded we start the learning using fit_one_cycle() method. You can read about fit one cycle in detail here. fit_one_cycle outputs the training and validation loss for the number of epochs you choose to run.

Since we are using transfer learning at this point we are just training the last few layers of our resnet50. To train the complete neural network we need to use learn.unfreeze(). This allows all the layers of the resnet50 to be trained. FastAI also provides very convenient methods to figure out learning rates. the lr_find() method helps you to choose a good learning rate so you can train the model quicker. The learning rate finder plots loss vs learning rates.

After choosing an appropriate learning rate you can run training for a certain number of epochs to see how your model is performing.

Once you are satisfied with the training metrics you can save your model again and plot confusion matrix and mis-classifications to further tune your model.interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()

All this can be done in a couple of hours using FastAI! Machine learning projects need a very fast experiment cycle and fast ai enables you to do exactly that. I am not going into details of model optimization in this article the intent here is to just introduce you to the FastAI library and give a feeler. You can create your own datasets and build image recognition models using this workflow.

There is a lot of work going on in the world to tackle this pandemic and you can do your part by contributing to competitions in Kaggle, in opensource github databases and spreading the right information and arresting the spread of misinformation. And yes keep learning!


AI Graduate aims to organize and build a community for AI that not only is open source but also looks at the ethical and political aspects of it. More such experiment driven simplified AI concepts will follow. If you liked this or have some feedback or follow-up questions please comment below.