Keras is a Deep Learning library that, as Data Scientists, we might come across often. It is the simplest to implement and the easiest to learn deep learning framework, and if that wasn’t enough, the icing on the cake is that ever since the evolution of Tensorflow 2.0, it became even easier to learn and build with Keras as it comes bundled up with Tensorflow.
This article describes a project that will help you nail down the basics and get up to speed with them in no time at all.
So with that said, let’s get around to building a deep neural network for performing multi class classification on Covid-19 Chest X-Rays.
The data
We will be using this dataset available on Kaggle, available under Public domain, under the same heading that I’ve mentioned in the previous part.
For the classification problem, we have three categories of X-Rays – Normal, Pneumonic, and Covid inflicted. Our goal is to quickly develop a model and with it, learn the basics of Keras deep learning library.

Keep the downloaded data under the 'data/'
folder for convenience from here onwards.
Let’s get going by importing our required packages:
from tensorflow.keras.layers import Conv2D, Flatten, Dense, MaxPooling2D, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing import image
from tensorflow.keras.callbacks import EarlyStopping
import os
Typically, we will go with the following structure for our project:
image_classification_project/
classifier.ipynb
data/
train/
normal/
covid/
pneumonic/
valid/
normal/
covid/
pneumonic/
test/
normal/
covid/
pneumonic/
Now, we proceed to building our model and importing our data.
Importing the data
Spin up your local environment or better yet, a colab notebook with your runtime set to ‘GPU".
Let’s start by defining the training and testing Data Generator objects:
# Data Generators for some data preprocessing and augmentation
train_datagen = image.ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = image.ImageDataGenerator(rescale = 1./255)
The ImageDataGenerator class provides an easy way to load, preprocess, and augment images in batches for our image classification tasks.
We now move on to importing the data from the directories we defined in the previous section:
# importing data
training_set= train_datagen.flow_from_directory(os.path.join(data, "train"),
target_size = (128, 128),
batch_size = 32,
class_mode='binary')
test_set = test_datagen.flow_from_directory(os.path.join(data, "valid"),
target_size = (128, 128),
batch_size = 32,
class_mode='binary')
If you’re working with a different directory structure, please make sure to change the bold parts in the above code snippet as per your own particular setup.
You shall get the following output with the execution of the above lines:
Found 4830 images belonging to 3 classes.
Found 1039 images belonging to 3 classes.
Now, we are ready to inspect our training indices.
print(training_set.class_indices, test_set.class_indices)
You’ll see this output upon the execution:
({'covid': 0, 'normal': 1, 'pneumonia': 2}, {'covid': 0, 'normal': 1, 'pneumonia': 2})
This pertains to the way our three image classes can be referenced later on and it helps seeing that they are the same for both train and test sets.
Now, let’s get on to building the model!
The model
We start with the Sequential object and add the 2D Convolution layers with 32 filters (or neurons) with 3×3 kernel size.
Our input shape for every image is to be: 128×128 pixels and 3 represents that our images are RGB.
We define the activation function to be ‘ReLU’.
# the model
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape = (128, 128, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(32, (3, 3), input_shape = (128, 128, 3), activation = 'relu'))
model.add(Flatten())
model.add(Dense(units = 256, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 128, activation = 'relu'))
model.add(Dense(units = 3, activation = 'softmax'))
model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
model.summary()
We then add a Pooling(MaxPooling2D) layer, another convolution layer and a fully connected (Dense) layer. The Dropout layer is intended to make sure our model does not overfit.
Our last layer is another Dense layer, also functioning as our output layer which outputs 3 class probabilities which add up to 1, with the help of the softmax activation function.
Early Stopping
The Tensorflow documentation describes early stopping as:
Stop training when a monitored metric has stopped improving.
Since we want to minimize our validation loss, we monitor it so that our patience parameter can define at which epoch to stop the training in case it doesn’t improve over as many epochs.
early_stopping = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4)
This is a callback we will include in the next step when we fit (or train) the model.
Training the model
Fitting the model is as simple as calling the fit function on the model with our necessary parameters such as the batch size for stochastic gradient descent updates, and number of epochs we want the whole training to occur in.
# Model training
classifier = model.fit(training_set,
batch_size = 128,
epochs = 10,
validation_data = test_set,
callbacks=[early_stopping])
You will notice that we also specify our callback as the early stopping object that we defined earlier.

As you can see, our callback has had the necessary effect of not overtraining our model and stopping at the 9th epoch itself.
As a bonus, you can now save your model with the below Keras’ in-built save function:
model_save_full_path = os.path.join(ROOT, 'saved_models', 'conv2d_version_1_acc_79_84')
model.save(model_save_full_path)
Next steps
Thank you for reading!
If you found this tutorial helpful and followed along, here are the things that I recommend doing after this tutorial:
— Build a confusion matrix to visually represent the number of labels classified correctly and incorrectly.
— Explore other metrics such as precision, recall, and F1 score for evaluating the model.
— Gauge how the model performs on the test dataset.
If you’d like to see the next parts of the tutorial in which I describe the above further steps and even build a frontend for the classifier, follow me and stay tuned!
Get my FREE "From Model to Production eBook" here in which I teach the quickest way to build a deep learning model and deploy it as an API.
Happy learning! 🙂