COVID-19 Detection from Chest X-Ray using CNN-part 1

Kunnathradhesh
3 min readOct 2, 2020

Hey, Everyone This is Radhesh…Hope you all are doing good

As this is my First Blog, I would be sharing my Experience regarding this project which I am currently working on!!!! So let's get Started….

Lets first understand why are we using Chest X-Ray for detection of the Coronavirus…

  1. Blood Tests take time to conduct. It takes ~5 hours per patient
  2. We can easily Build Deep learning Models which can be classified using Image Classification Models & segmentation Techniques.
  3. And the Best part is that we have the dataset.

I'm sharing the link of the dataset that I have taken for this project

COVID X-Ray Image Dataset —https://github.com/ieee8023/covid-chestxray-dataset for positive cases.

In the above-mentioned link, it not only consists of COVID chest x-ray but also some other infectious diseases like pneumonia, sars, etc..Now our task is to collect the COVID chest X rays which are Posterior-Anterior (PA) projection …I have written a few lines of code in order to extract the images from the metadata.csv file

import pandas as pdfilepath="covid-chestxray-dataset/metadata.csv"
imagespath="covid-chestxray-dataset/images"
count=0
for (i,row) in df.iterrows():
if row["finding"]=="COVID-19" and row["view"]=="PA":
count+=1
print("no of covid images that are PA ",count)
no of covid images that are PA 180

Now we have collected COVID Images, Next, we will collect Normal Chest X-ray images from here

Kaggle X-Ray Chest Images — https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia for negative cases.

for i in range(180):
image_name=image_names[i]
image_path=os.path.join(kaggle_file_path,image_name)
target_path=os.path.join(target_normal_dir,image_name)

shutil.copy2(image_path,target_path)
print("copying image ",i)

Great…Lets start working on our model

TRAIN_PATH = "CovidDataset/Train"VAL_PATH = "CovidDataset/Test"

Let's import some modules Now

import numpy as npimport matplotlib.pyplot as pltimport kerasfrom keras.layers import *from keras.models import *from keras.preprocessing import image

I have seen many people in recent times working on COVID detection and most of them have worked with VGG16 as their pre-trained model. The reason why I'm not using is that we are having around 350 images in total and VGG is having around 140 million parameters. If we train a VGG network it is easily going to overfit and it is going to be difficult to work with VGG. Cool….let's get started….

Here we are Building a CNN based Model in Keras to build the model architecture

It is a layered architecture and we will be creating 3 or 4 CNN layers followed by the Classification layer.

The sequential model will have a conv2d layer with 32 number of filters in the beginning….Now you might be wondering what does this mean? let me tell you it means that we are going to extract 32 different types of features in the first layer and let's take the kernel size as 3 X 3 as it is the standard size, with “relu” as the activation function. As this is the first layer we need to specify the input size as 224 X 224 X 3

In a similar way, we have the second layer having 64 filters which means that 2 Conv layers stacked on top of each other. It represents 5 X 5 filters followed by max-pooling and dropout.

Now we will add more Conv layers…let me show the model architecture

model = Sequential()model.add(Conv2D(32,kernel_size=(3,3),activation='relu',input_shape=(224,224,3)))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(128,(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss=keras.losses.binary_crossentropy,optimizer='adam',metrics=['accuracy'])

Here we are performing Binary Classification so we need to use Sigmoid as the activation in the final layer and the optimizer as Adam.

That's all for this blog…In Part 2 we shall get to know more on how to train the data… Thank you, for reading this article…

— — — — — — — — — — —Kunnath Radhesh — — — — — — — ——

--

--