NEHA SINGH's other Models Reports

Major Concepts

 

Sign-Up/Login to access Several ML Models and also Deploy & Monetize your own ML solutions for free

Models Home » Domain Usecases » Health Care and Pharmaceuticals » Covid Prediction Using Chest X-Ray

Covid Prediction Using Chest X-Ray

Models Status

Model Overview



Abstract




In this use case, I will guide you on how to predict Covid19 using a Chest X-Ray of the patient. We all know the techniques used these days are through the blood which takes a comparatively longer time of doctor, and also it is expensive, whereas X-Ray is quite cheap and then feeding the image to our model, it will automatically predict covid.

Source of covid x-ray images:

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

Source of normal images:

https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia Normal Images: 195

Training Images:
387


Testing Images:
4


Model:CNN

Validation Accuracy:
98%


Training Accuracy:
88%



You can download my dataset used in this model from here:

https://drive.google.com/drive/folders/1dc5o0mpUBFXS9zCQJ4L3yP2RAhzCFuO9?usp=sharing


You can download it from Google Drive and then store it in your drive.


1. CNN Model

It is a Deep Learning algorithm to which we will provide an input image, then it will assign importance (learnable weights and biases) to various aspects/objects in the image and finally be able to differentiate objects and images.


Coding

Let's mount google drive to google collab. It will ask you for a key, go to the link provided copy it, and then enter. 

Mounted at /content/gdrive


For feeding the data(images) to the CNN model, we need to distribute the dataset into two parts, Training and Validation Folder. So, that the model can learn the difference between covid and non-covid images.

Content in google collab can be found in "/content/gdrive" folder

Train and Validation folder:

Now, we have a path to the training dataset in TRAIN_PATH, and the validation dataset in VAL_PATH.


Importing the required libraries:

We will need various libraries like NumPy, matplotlib, Keras, Keras layers, Keras preprocessing, and Keras model.


import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.layers import *
from keras.models import *
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array

Defining CNN Model in Keras

This model has layered architecture, and every layer has some filters. So, we will 4 CNN layers followed by classification layers.



Model is sequential
Convolutional layer with 32 number of filters, which is less as a lower layer of filter detect filter in a small part of the image. We will be taking kernel size as 3,3, with activation function as relu, and as this is the first layer, we will provide the input shape as 224,224,3
Second Convolutional layer, with 64 filters, standard kernel size, and relu as activation function.

After the filters have passed over the image, a feature map is generated for each filter. These are then taken through an activation function, which decides whether a certain feature is present at a given location in the image.

It is a Simple Way to Prevent Neural Networks from Overfitting, to make the model flexible. Now, we will add Dense Layer, Flatten Layer


We will be compiling our model, with binary_crossentropy as loss, adam as optimizer, and accuracy metrics

model.compile(loss=keras.losses.binary_crossentropy,optimizer='adam',metrics=['accuracy'])

Checking the summary of the model:


model.summary()


We have 12,000,450 parameters.


Training model from scratch:


# Train from scratch
train_datagen = image.ImageDataGenerator(
rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
)

test_dataset = image.ImageDataGenerator(rescale=1./255)

We have first rescaled the image by 1/255, which will help in normalizing, zooming range will be 20%, the horizontal flip will be true but not vertical as we should not invert the image as all other images will have vertical orientation only.
We will only rescale the test dataset by 1/255.


Train Generator:


train_generator = train_datagen.flow_from_directory(
'/content/gdrive/MyDrive/Neha Singh/Projects/ML/Covid_Dataset/CovidDataset/Train',
target_size = (224,224),
batch_size = 32,
class_mode = 'categorical')

Found 387 images belonging to 2 classes.

Now, we are creating a data generator using a method of train_generator object called as flow from the directory. 
We are providing the directory, the target size of the image, batch size, and class mode will be binary as we are doing binary classification.

It will pick images from the directory, convert images to target size, with the batch size of 32.


Checking the classes of train_generator


train_generator.class_indices

{'Covid': 0, 'Normal': 1}

Validation Generator:
In the same way, we will create the validation data generator


validation_generator = test_dataset.flow_from_directory(
'/content/gdrive/MyDrive/Neha Singh/Projects/ML/Covid_Dataset/CovidDataset/Val',
target_size = (224,224),
batch_size = 32,
class_mode = 'binary')

Found 84 images belonging to 2 classes.

CNN Model

We will create the model, train it using train_generator, steps per epoch will be the length of train_generator, epochs=10, validation data, and validation steps.


hist = model.fit(
train_generator,
steps_per_epoch=len(train_generator),
epochs = 10,
validation_data = validation_generator,
validation_steps=len(validation_generator)
)


As you can see here that the accuracy is 88% in the last epoch.


Testing the model:

Loading one image from google drive
Converting it to array for testing it. Then, scaling it by 1/255 factor. 
Now, we will predict class using the model.


import glob
test = []
for i in glob.glob("/content/gdrive/MyDrive/Neha Singh/Projects/ML/Covid_Dataset/CovidDataset/Test/covid15/*"):
testing = load_img(i, target_size=(224,224))
testing
x = img_to_array(testing)
x = x/255
x = np.expand_dims(x, axis=0)

print(model.predict_classes(x))
print(model.predict(x))#confidence



Saving our Model:


model.save('/content/gdrive/MyDrive/Neha Singh/Projects/ML/Covid_Dataset/Model/model.h5')

For the next time, you should upload the model directly and test it:


saved_model = load_model('/content/gdrive/MyDrive/Neha Singh/Projects/ML/Covid_Dataset/Model/model.h5')

Testing using the normal image:


import glob
test = []
for i in glob.glob("/content/gdrive/MyDrive/Neha Singh/Projects/ML/Covid_Dataset/CovidDataset/Test/normal15/*"):
testing = load_img(i, target_size=(224,224))
testing
x = img_to_array(testing)
x = x/255
x = np.expand_dims(x, axis=0)

print(model.predict_classes(x))
print(model.predict(x))#confidence
# 13 right # 17 wrong


We can find that some of the predictions are done wrong. But those are comparatively less.


2. VGG16 Model

Let's work on the VGG16 Model


VGG Layers are present in a Keras application, and VGG 16 is an inbuilt feature extraction to which we just need to pass the input shape and weights. We will not train the model at the front layers, but at the last layers, that's why vgg.trainable is false.


VGG = keras.applications.VGG16(input_shape=(224,224,3), include_top=False, weights='imagenet')
VGG.trainable = False

As you can see in the image we have three dense layers, we will firstly flatten the layers, then provide size and dimension for those three layers.
For compiling we are using optimizer s adam, loss parameter as categorical_crossentrophy, and accuracy metrics.


model_vgg = keras.Sequential(
[
VGG,
keras.layers.Flatten(),
keras.layers.Dense(units=256,activation="relu"),
keras.layers.Dense(units=256,activation="relu"),
keras.layers.Dense(units=2,activation="softmax")
]
)

model_vgg.compile(optimizer='adam', loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])

Let's see the model summary:


model_vgg.summary()


You can see we have here 21.203.778 parameters found.

Creating the test data generators:


trData_vgg = ImageDataGenerator()
trainData_vgg = trData_vgg.flow_from_directory('/content/gdrive/MyDrive/Neha Singh/Projects/ML/Covid_Dataset/CovidDataset/Train', target_size=(224,224))

tsData_vgg = ImageDataGenerator()
testData_vgg = tsData_vgg.flow_from_directory('/content/gdrive/MyDrive/Neha Singh/Projects/ML/Covid_Dataset/CovidDataset/Val', target_size=(224,224))

Found 252 images belonging to 2 classes.
Found 84 images belonging to 2 classes.


import tensorflow as tf
import keras

tf.config.run_functions_eagerly(True)

Model creation:
For VGG16, we will train the model by feeding it with training data, steps per epoch, epochs, validation data, and validation steps.


hist1 = model_vgg.fit(
trainData_vgg,
steps_per_epoch=len(train_generator),
epochs = 10,
validation_data = testData_vgg,
validation_steps = 10
)


You can see the accuracy came out to be 100%

Let's plot the graph of accuracy:

Here we are firstly importing the library, then we will make a graph using the hist1 model history using accuracy, and loss. The model title is Model Accuracy, Y label as Accuracy, X label as Epoch. Then, finally showing the graph.

import matplotlib.pyplot as plt
plt.plot(hist1.history["accuracy"])
plt.plot(hist1.history["loss"])
plt.title("Model Accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epoch")
plt.legend(["Accuracy", "Loss"])
plt.show()​




Let's save the VGG16 model:
model_vgg.save('/content/gdrive/MyDrive/Neha Singh/Projects/ML/Covid_Dataset/Model/model_vgg.h5')​



Testing our model with Covid image:

Here we are importing images from google drive, then converting them to the array, and showing the image. Later, we will insert the array into a new axis and pass it to predict function. Finally showing the result.


from keras.preprocessing import image
img = image.load_img('/content/gdrive/MyDrive/Neha Singh/Projects/ML/Covid_Dataset/CovidDataset/Val/Covid/E63574A7-4188-4C8D-8D17-9D67A18A1AFA.jpeg', target_size=(224,224))
img = np.asarray(img)
plt.imshow(img)
img = np.expand_dims(img, axis=0)

model_vgg.predict_classes(img)


As you can see output is 0: so, this image is a covid patient's image and our model worked well.






0 comments