COVID-19 X-Ray Detection using Keras

In this blog, we will detect whether the X-Ray is of a COVID-19 patient or a Normal patient using Keras module in Python. It is a binary image classification task. We will use Kaggle Dataset for the model training.

So, let’s dive into it.

Code

We would be importing all the necessary libraries for our model.

import os
from keras.layers import Dense,Dropout,Conv2D,MaxPool2D,Flatten,GlobalAveragePooling2D
from keras.models import Model
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
import matplotlib.pyplot as plt

Now, we will do the preprocessing of the data. We will do it using the Keras ImgeDataGenerator. Also, we are going to do the Data Augmentation task such as Zoom, Width, and Horizontal shift, etc to avoid overfitting of the Data. We will make the image size to (150,150) and class model to categorical as we have 2 classes.

train=ImageDataGenerator(rescale=1/255.0,
                         rotation_range=20,
                         shear_range=0.4,
                         zoom_range=0.1,
                        horizontal_flip=True)
training=train.flow_from_directory(
    "xray_dataset_covid19/train",
    target_size=(150,150),
    class_mode="categorical"
)
testing=ImageDataGenerator(rescale=1/255.0)
testing=train.flow_from_directory(
    "xray_dataset_covid19/test",
    target_size=(150,150),
    class_mode="categorical"
)
OUTPUT:
Found 148 images belonging to 2 classes.
Found 40 images belonging to 2 classes.

From the output we can see that very few images are there to train our model, so we have used Data Augmentation.

We can plot some images to see how the normal X-Ray Scan looks like, for that matplotlib is used.

image=os.listdir("xray_dataset_covid19/train/NORMAL")

for i in imag[:5]:
  x=os.path.join("xray_dataset_covid19/train/NORMAL",i)
  img=image.load_img(x)
  img=image.img_to_array(img)/255.0
  plt.imshow(img)
  plt.axis("off")
  plt.show()
  • Similarly, we can also see the Covid-19 X-Ray Scans.
image=os.listdir("xray_dataset_covid19/train/PNEUMONIA")
for i in imag[:5]:
  x=os.path.join("xray_dataset_covid19/train/PNEUMONIA",i)
  img=image.load_img(x)
  img=image.img_to_array(img)/255.0
  plt.imshow(img)
  plt.axis("off")
  plt.show()

We will use the Sequential model with layers as Convolutional, Maxpooling, Flatten, and Dropouts to reduce overfitting.

The shape of the image passed is (150,150,3) as we have changed the size of the image to 150,150 and 3 is the number of channels as it is the colored image and has 3 channels RGB.

We will use Relu activations for the hidden layers and Sigmoid for the output layer.

NOTE: Sigmoid is better activation function than Softmax in Binary classification.

model=Sequential()
model.add(Conv2D(32,(3,3),activation='relu',input_shape=(150,150,3)))

model.add(MaxPool2D(2,2))
model.add(Conv2D(32,(3,3),activation='relu'))
model.add(MaxPool2D(2,2))

model.add(Flatten())

model.add(Dense(2,activation='sigmoid'))
model.summary()
OUTPUT:
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 148, 148, 32)      896       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 74, 74, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 72, 72, 32)        9248      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 36, 36, 32)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 41472)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 82946     
=================================================================
Total params: 93,090
Trainable params: 93,090
Non-trainable params: 0

 

Now we will compile our model by using Adam as the Optimizer and Binary loss entropy as the loss.

model.compile(optimizer="Adam",loss="binary_crossentropy",metrics=['accuracy'])

Finally, we will train the model on the given dataset for 15 epochs.

his=model.fit_generator(training,epochs=15)
OUTPUT:
Epoch 1/15
5/5 [==============================] - 9s 2s/step - loss: 0.6921 - accuracy: 0.6284
Epoch 2/15
5/5 [==============================] - 8s 2s/step - loss: 0.5085 - accuracy: 0.8311
Epoch 3/15
5/5 [==============================] - 8s 2s/step - loss: 0.3829 - accuracy: 0.8784
Epoch 4/15
5/5 [==============================] - 8s 2s/step - loss: 0.3050 - accuracy: 0.8851
Epoch 5/15
5/5 [==============================] - 8s 2s/step - loss: 0.2834 - accuracy: 0.8784
Epoch 6/15
5/5 [==============================] - 8s 2s/step - loss: 0.2267 - accuracy: 0.8986
Epoch 7/15
5/5 [==============================] - 8s 2s/step - loss: 0.2401 - accuracy: 0.8851
Epoch 8/15
5/5 [==============================] - 8s 2s/step - loss: 0.1965 - accuracy: 0.9257
Epoch 9/15
5/5 [==============================] - 8s 2s/step - loss: 0.1687 - accuracy: 0.9392
Epoch 10/15
5/5 [==============================] - 8s 2s/step - loss: 0.3254 - accuracy: 0.8784
Epoch 11/15
5/5 [==============================] - 8s 2s/step - loss: 0.2622 - accuracy: 0.9054
Epoch 12/15
5/5 [==============================] - 8s 2s/step - loss: 0.2570 - accuracy: 0.8649
Epoch 13/15
5/5 [==============================] - 8s 2s/step - loss: 0.2132 - accuracy: 0.9054
Epoch 14/15
5/5 [==============================] - 8s 2s/step - loss: 0.1912 - accuracy: 0.9392
Epoch 15/15
5/5 [==============================] - 8s 2s/step - loss: 0.1659 - accuracy: 0.9324

After training, we can check the training accuracy of the model.

model.evaluate(testing)

OUTPUT:
[0.019918538630008698, 0.949999988079071]

We got 94.9% training accuracy and 1.9% loss.

We can also plot the graph of trainin accuracy and training loss.

plt.plot(his.history['accuracy'],label='accuracy')
plt.plot(his.history['loss'],label='loss')
plt.legend()
plt.show()

Since we have few images of the data we can’t check the accuracy on the test set. In order to predict the image, you can reshape the shape of the image to the shape training image and then call model.predict function on the image.

CONCLUSION

In this blog, we have learnt to do binary image classification using COVID-19 X-Ray scan dataset.

If you have any doubts or suggestions related to the topic, feel free to comment below.

 

Leave a Reply

Your email address will not be published.