A Neural Networks based approach to detect Covid-19 through Chest X-Ray
We require Non-Conventional approaches to fight this deadly virus!
We are facing 2nd wave of deadly Corona Virus (COVID-19) that has caused a pandemic situation across the world, effecting more than 200 countries and territories. The virus is easy to spread through person to person or by touching infected surface. The primary symptoms of a Covid-19 infection are fever, dry cough, lose of taste/smell and shortness of breath . In some regions it is still hard and expensive to identify Covid-19 patient due to lack of testing kits and tremendous burden placed on healthcare centers. In test process samples are collected from swab in the nose or back of throat then laboratory checks the sample on the swab for the virus’s genetic material and after one or two days find out the result of the test. The process is painful, time taking and costly.
Covid-19 effects on patient’s body in certain patterns, specially it attacks and damages certain areas of human chest and lungs. The idea is to develop a deep neural network based solution to automatically detect Covid-19 with the help of chest X-Ray images of patients, some organizations have created public repositories which contain authentic chest X-Ray images datasets of Covid-19 patients.
I have designed and trained a Convolutional neural network over thousands of Covid-19 effected as well as normal X-ray images.The model takes X-ray image of a suspected patient and provide binary classification to predict(Covid-19 positive or Normal). The article present step by step demonstration of aforementioned project that is implemented using Keras that is a high-level Python neural networks library that runs on the top layer of TensorFlow.
The dataset used in this project has been taken from two different sources Kaggle and Gituhub making a dataset of 3500 images with distribution of 70% train, 15% val, 15% test and having 50% distribution of Covid-19 vs Normal X-Ray images.
- Training set → 2450 images
- Validation set → 550 images
- Test set → 550 images
Data Source 1: Kaggle
The dataset from kaggle consists of X-Ray(PA-CXR) images of Covid-19, bacterial and viral pneumonia patients and Normal people without having any disease.
Data Source 2: Github
The source of second Dataset is a gituhub repository which contains an open dataset of chest X-Ray and CT images of patients that are positive to COVID-19 or other viral and bacterial pneumonias.
TensorFlow is an open-source library for data flow and differentiable programming across a range of tasks i.e. machine learning/deep learning applications such as neural networks. Keras is an open-source neural-network library written in Python, capable of running on top of TensorFlow, Microsoft Cognitive Toolkit, R, Theano, or PlaidML. It is industry-strength framework that can be scaled to large clusters of GPUs or an entire TPU pod and Designed to enable fast experimentation with deep neural networks. Here i have used Keras for implementation of binary classifier that will classify an X-Ray image as Covid-19 infected or Normal .
</> → Lets get ready for deep dive into the code
If you plan to implement this code, I assume that you are already loaded with Keras and Tensorflow installation in your workstation. →No? follow this quick guide to Anaconda, Jupyter Notebook, TensorFlow and Keras for Deep Learning, and get here right back
If you don’t want to setup the environment on your local machine an alternate way is to use a cloud service for machine learning such as Google Colab which I am also using for this demonstration
The foremost step is to import following libraries which will be required for further process
Mount google drive with google Colab
The best way to fetch data over google colab notebook is to place the dataset on google drive and mount the drive with colab, after running following code you will be asked for credentials and and access code will be provided to put on notebook console to authorize the access.
Fetch and transform data with ImageDataGenerator
After authorization we are ready to fetch our data from drive. Well there can be numerous ways to do it but using ImageDataGenerator library of Keras is the finest way that I always prefer. Its function flow_from_directory will not only fetch the images into iterative batches but also provide data augmentation and apply basic transformation to whole dataset of images i.e. re-scaling, re-shaping, zooming, and flipping according to your requirements and then convert them into numerical format in order to directly pass into the model.
Defining Class Labels and Break ImageDataGenerator Iterators into X,Y training and testing sets
Visualize the Dataset before training the model
Set some Important Parameters to pass into Model
Channel based transformation
Dataset Transformation on the basis of data format i.e. channels_first or channels_last and converting numerical data into float
A convolutional neural network based model containing 6 hidden Layers has been designed and trained to evolve with 16 Epochs after compilation over transformed dataset.
The network contains 6 Layers involving
- 2 Conv2D Layers having Relu as activation function, kernal size of (3,3) and 32 as filter.
- 1 MaxPooling2D Layer having pool size of (2,2).
- 1 Flattern layer to collapse the spatial dimensions of the input into the channel dimension.
- 2 Dense Layers having Relu and softmax as activation functions respectively
Defining and Compiling Model
Model Fitting and Evaluation on Test set
Visualizing the results, (Actual class vs predicted class over 100 patients)
Results having 81% accuracy have been achieved over test set after 16 Epochs, The neural network based model successfully detects Covid-19 patient and classify an X-Ray image as (Covid-19 or Normal)
There can be numerous solutions to one problem and everyone can play his role by contributing his bit to the community. In this article i demonstrated my work on convolutional neural network based solution to detect Covid-19 patient, a binary classifier is developed that takes an X-Ray image as input and output its prediction as Covid-19 or Normal with 81% accuracy.
P.S → You are highly welcome to contribute in this project though my github repository, or share your suggestions/feedback in comment section.
Enjoy Your day!