Artificial Neural Networks, Part 5 — Implementing CNN using Tensorflow and Keras
By: Saket Chaturvedi
Tags: Machine Learning, Deep Learning, Data Science, Image Classification, Python

Photo by Arseny Togulev on Unsplash

In the earlier post, Artificial Neural Networks, Part 4 — Convolution Neural Networks, we looked into the theory behind CNNs. In this post, we will go through the practical implementation using Keras and Tensorflow. The dataset is taken from Kaggle — Chest X-Ray Images. There are about 5864 images and 2 categories (PNEUMONIA / NORMAL).

Let us get straight into the implementation.

The first step starts with importing the required libraries

There are three folders in the dataset, train, test, and, validation. For this demo, we will work with the train and test datasets. Lets set up the file paths —

Let’s look at the images we are working with.

Image for Normal Lungs

Pneumonia Infected Lungs

The images are grayscale, hence when we look at the shape, there will not be any details on the color scale.

Given that the images in the training and testing data are of different scales, we will find out the average dimensions and reshape the images to be of the same sizes.

Below is a plot showing the different dimensions of the images. We will reshape all the images to be of the size (1400,1400).

The circle represents that the mean will be somewhere in this area.

As it is always said that the more data the better, it will be better to have more images in our arsenal to train. In a real-world scenario, it can get difficult to get more image data, we can mitigate this by creating transformed image data from existing images. This can be accomplished using the ImageDataGenerator class.

In the above function, we are creating images that are transformed images from our existing dataset. Below is an example of how the transformation will occur.

Original

Transformed

Now we are ready to create our CNN for training the image classification model. In our CNN, there are 5 convolution layers, followed by 3 Dense layers. Why did I use this architecture? Because it improved the validation accuracy and I was trying to use the AlexNet architecture. Here is the model summary —

For training the model, we will leverage the flow_from_directory() method to create batches of images that will be supplied to the network for training. This will help with reducing memory usage. BUT, this will increase the training time. We will also use Early Stopping, to break the training cycle when there is no improvement in the validation accuracy.

These methods are available in the Tensorflow library.

Moving on to training the model using the data —

It took about an hour and a half for the training to complete. We provided the number of epochs as 10, but with early stopping the training stopped at epoch 5 with a validation accuracy of 82.5%.

Let us predict on our test set data —

Keeping the threshold at 0.5, when comparing with the actuals, we get the following classification report —

‘NORMAL’: 0, ‘PNEUMONIA’: 1

It looks like out of all the images which have Pneumonia, our model is able to detect 91% correctly. Not bad!

We can definitely improve the model and I would like to get any feedback and suggestions for improvements.

The full code can be accessed here — Pneumonia_Prediction_CNN