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

Hey, Everyone Welcome Back!!! This is Radhesh…Hope you all are doing good
This is the 2nd blog I hope that you have gone through my First Blog...If not you can check it out…https://medium.com/@kunnathradhesh/covid-19-detection-from-chest-x-ray-using-cnn-part-1-65a5f27fcc7e
This is the summary of our previous model
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 222, 222, 32) 896
_________________________________________________________________
conv2d_1 (Conv2D) (None, 220, 220, 64) 18496
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 110, 110, 64) 0
_________________________________________________________________
dropout (Dropout) (None, 110, 110, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 108, 108, 64) 36928
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 54, 54, 64) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 54, 54, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 52, 52, 128) 73856
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 26, 26, 128) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 26, 26, 128) 0
_________________________________________________________________
flatten (Flatten) (None, 86528) 0
_________________________________________________________________
dense (Dense) (None, 64) 5537856
_________________________________________________________________
dropout_3 (Dropout) (None, 64) 0
_________________________________________________________________
dense_1 (Dense) (None, 1) 65
=================================================================
Total params: 5,668,097
Trainable params: 5,668,097
Non-trainable params: 0
_________________________________________________________________
Now we will use the Keras image data generator library to make the data ready for the Model, we also rescale the data and perform some data augmentation such as shear, zooming, horizontal flip, etc
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)train_generator = train_datagen.flow_from_directory(
'CovidDataset/Train',
target_size = (224,224),
batch_size = 32,
class_mode = 'binary')
Now we will train the model
hist = model.fit_generator(
train_generator,
steps_per_epoch=6,
epochs = 10,
validation_data = validation_generator,
validation_steps=2
)
Epoch 1/10
6/6 [==============================] - 9s 2s/step - loss: 0.6833 - accuracy: 0.6146 - val_loss: 0.6807 - val_accuracy: 0.6333
Epoch 2/10
6/6 [==============================] - 8s 1s/step - loss: 0.6296 - accuracy: 0.6510 - val_loss: 0.5913 - val_accuracy: 0.9333
Epoch 3/10
6/6 [==============================] - 8s 1s/step - loss: 0.4616 - accuracy: 0.7760 - val_loss: 0.4218 - val_accuracy: 0.9333
Epoch 4/10
6/6 [==============================] - 8s 1s/step - loss: 0.3298 - accuracy: 0.8698 - val_loss: 0.2801 - val_accuracy: 0.9667
Epoch 5/10
6/6 [==============================] - 8s 1s/step - loss: 0.3021 - accuracy: 0.8854 - val_loss: 0.1959 - val_accuracy: 0.9500
Epoch 6/10
6/6 [==============================] - 8s 1s/step - loss: 0.2848 - accuracy: 0.8698 - val_loss: 0.1728 - val_accuracy: 0.9667
Epoch 7/10
6/6 [==============================] - 8s 1s/step - loss: 0.2362 - accuracy: 0.8698 - val_loss: 0.1508 - val_accuracy: 0.9833
Epoch 8/10
6/6 [==============================] - 8s 1s/step - loss: 0.1897 - accuracy: 0.9479 - val_loss: 0.1357 - val_accuracy: 0.9667
Epoch 9/10
6/6 [==============================] - 8s 1s/step - loss: 0.1858 - accuracy: 0.9271 - val_loss: 0.1632 - val_accuracy: 1.0000
Epoch 10/10
6/6 [==============================] - 8s 1s/step - loss: 0.4694 - accuracy: 0.8385 - val_loss: 0.1054 - val_accuracy: 0.9833
so we got maximum accuracy of 94 for the training set and 98 for validation set
model.evaluate_generator(train_generator)[0.12117815017700195, 0.9642857313156128]model.evaluate_generator(validation_generator)[0.1054450049996376, 0.9833333492279053]
we can try various other ways just by increasing the epochs and also put some checkpoints to save the best model when the accuracy is high
We can even try plotting a confusion matrix as well
y_actual=[]
y_test=[]
for i in os.listdir("./CovidDataset/Val/Normal/"):
img=image.load_img("./CovidDataset/Val/Normal/"+i,target_size=(224,224))
img=image.img_to_array(img)
img=np.expand_dims(img,axis=0)
p=model.predict_classes(img)
y_test.append(p[0,0])
y_actual.append(1)for i in os.listdir("./CovidDataset/Val/Covid/"):
img=image.load_img("./CovidDataset/Val/Covid/"+i,target_size=(224,224))
img=image.img_to_array(img)
img=np.expand_dims(img,axis=0)
p=model.predict_classes(img)
y_test.append(p[0,0])
y_actual.append(0)y_actual=np.array(y_actual)
y_test=np.array(y_test)from sklearn.metrics import confusion_matrix
cm=confusion_matrix(y_actual,y_test)import seaborn as sns
sns.heatmap(cm,cmap="plasma",annot=True)

Now we have come to the end of this Project…I have tried implementing it in a simpler way using this CNN model but you can even try using transfer learning but the ImageNets datasets are different from X-Ray Images so we need to fine-tune all the layers in the model to get great results.
Thank you, everyone
Kunnath Radhesh
