9th Jun 2020

Application of AI and ML for detection of COVID-19

Artificial Intelligence, Machine Learning

Written By, Ayaz Saiyed


Humans around the globe are in panic and suffering from the pandemic of the 21st century i.e COVID 19 – the word itself speaks everything.

Governments are locking down countries, but daily the number of infected patients continues to rise. As on May 20, 2020 while writing this blog, currently there are more than 4.89 million cases across the world.


Image Credit – Coronavirus Resource Center

So this blog explores how machine learning (ML) can play a wide role during this pandemic situation by helping the doctors and society in general to detect such viruses conditions..

Yeahh, it’s possible !!

A machine can detect covid patients from x-rays on the basis of some hefty algorithms and trained models !!

Excited ? Let’s see how all it works.

How it works !

It all starts with – Training a model

If you have a good amount of labelled data ( dataset ), then that is the key for solving the problems using machine learning.

In our case, we are going to use an X-ray dataset of the patients who were reported as COVID positive and negative.

Unfortunately, currently, COVID-19 lung scan datasets are limited. So we had referred to one of the good collections of COVID-19 open-source dataset. It consists of x-ray images made available publically for research use. Once we have a filtered dataset, we can train a model on a large number of COVID positive v/s COVID negative x rays images, using appropriate algorithms, and then use that algorithm to detect COVID from the input x rays.

This is how Artificial Intelligence (AI) can detect the COVID-19 from the images.

Confused? Let’s move closer to the actual process.

How can trained models detect COVID-19 from just an image ?

To train a model accurately, we should have a filtered x-ray dataset, with proper balanced images,

for example :

  • 100 x-ray images ( Covid Positive )
  • 100 x-ray images ( Covid Negative )

In our case, we filtered 140 total images from the large dataset , and splitted them into 70 for positive patients and 70 for negative patients.

Covid Negative


Covid Positive

Once we have a filtered dataset, we can train the model using the hefty libraries of python like tensorflow and keras.

Steps for training Covid-19 detection model

  1. Installing the required libraries
  2. Extract the class labels ( either Covid or Normal )
  3. Loading the image and converting it to RGB channel and resizing it as per the need , in our scenarios 224*224 pixels as shown above, to make it ready for the Convolutional Neural Network.
  4. Initialize VGGNET model and set it up for fine tuning.
  5. Compiling the network with Adam optimizer , and using binary_crossentropy loss as it is a 2-class problem, rather than using categorical crossentropy.
  6. And in the end to follow some basic rules and algorithms of training a model.

These were few overall steps to train a model, for entire code stay tuned at our github repository.


After passing through all such processes of training, in the end we will have a trained model ( Prediction Model ), which will be able to discriminate between the x-ray of covid patients and of normal patients.

Trained model is the kind of brain made using machine learning.


AI based Covid-19 detection


In addition , we have made a model which can detect whether the uploaded image is an x-ray image or not.

So it can be helpful during the training process, as if a doctor or user uploads a non x-ray image in a dataset during a new training process, then the algorithm will throw out that image and will take only x-ray images into consideration for training of the model.

” Yeahh, machine is brilliant if it is trained well like humans .”

Covid-19 detection using Artificial Intelligence

Above is the demonstration of detecting COVID-19 from the input x-rays, using the trained model and image classification algorithms. It can detect whether the x-ray belongs to the covid patient or normal patient.

X-ray detection using pre-trained model

Above is the demo of detecting the x-rays from thousands of images using trained models on x-rays, and storing those detected images in an x-ray named folder for training use.

So, in conclusion we can say,

“In a list of all superheroes like doctors, cops, covid workers, Artificial Intelligence will be playing its role soon.”

Written By,

Python Developer at Yudiz Solutions Pvt. Ltd