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
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.
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
)
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()
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.