Build a Web App to detect Pneumonia from Chest X-Ray Images Part 1: Data collection and Model training
Hello Everyone
I hope you all enjoyed the last blog where we went about how to make a Facial Expression Recognition model and use it in various ways. Today we will be building a CNN to predict pneumonia from chest X-Ray images with the help of a technique called Transfer Learning and in the next story, we will be serving it as a web app. If you have not read the first blog you can read it here. So without further talks,
Let’s Get Going
Pre-requisites
This blog post series also requires some pre-requisites, but don’t worry we will provide resources.
- Basics of Machine Learning.
- Tensorflow(Keras) framework and Deep Learning (especially CNN). You can learn it here
- Experience with Flask (will be useful in the next story). You can learn it here.
Collecting Data and Analysing it
In this project, we will be using the Chest X-Ray Images (Pneumonia) from Kaggle. This dataset consists of chest X-Ray images for both healthy and pneumonic type. The dataset is over 1GB in size so it may take time to download based on your internet speed. After downloading and extracting the data you will have a folder Pneumonia_Detection with following contents
D:.
└───chest_xray
├───test
│ ├───NORMAL
│ └───PNEUMONIA
├───train
│ ├───NORMAL
│ └───PNEUMONIA
└───val
├───NORMAL
└───PNEUMONIA
As you can see the dataset is aligned beautifully for making data pipelines.
Let’s do data analysis of it
We will look at distribution as well as some images from the dataset.
As you can see that the dataset is not that much balanced with Pneumonic images more than the healthy images Especially in case of the training set. We will be needing Data Augmentation to cope with this problem. You can read more about data augmentation here.
Now let’s have a look at some healthy and pneumonic chest x-ray images.
Can you spot the difference in the above images? Just kidding 😂😂. Let our deep learning model do this.
The code for generating the above results is listed below:
Building the Model and Training
So now we will start building the model. So for this task, I have used a technique called transfer learning.
Transfer Learning is a technique of using the parameters of a model trained on some other (maybe similar) dataset on our problem statement. We can either use it for just making predictions or we can train some layers of it to predict our own dataset.
As you can see from the above image, in traditional ML approaches we do not take the knowledge from one learning system to another even if the dataset 2 is similar to dataset 1. But in Transfer learning, we take the knowledge(parameters) from another system and can use it to train on our dataset. This technique helps reduces training time as you are just updating the parameters according to your dataset/problem statement.
For an in-depth knowledge of transfer learning, you can read this excellent blog post from Medium.
Tensorflow(Keras) supports transfer learning with a variety of models with pre-trained weights. Some of them are:
- VGG16, VGG19
- ResNet50, ResNet101, Resnet152 etc
- Xception
- Inceptionv3
- InceptionResNetv2 and the list go on. You can all of these here.
You can also read about using them here.
OK. Let’s begin.
The code listing is given below.
Line 15–22: In this section of the code, we load in the VGG16 model using the applications module of Keras. On line 15, we initialize VGG16 with include_top = False, this parameter is used to load the full model or model without the last fully connected layers. We also have to specify the weights type and input shape. After this, we flatten the output of the model and add a Dense layer to predict whether the person is healthy (0) or pneumonia (1).
The rest of the code is quite self-explanatory. We initialize the data generator for training the model (27–45), compile and train the model(51–52), and plot the loss and accuracy curves (58–68).
After training, I got an accuracy of around 93%. You can play with different models to get better accuracy. The performance of the model is shown in the following image.
Congratulations on completing the first part of the project
I hope you got the basics of transfer learning and this will help you to build DL models quickly for your projects.
Thanks for being here with me till the end. I have just started writing blog posts so please do comment below about the blog, your comments mean a lot to bring the best content to you.
You can follow me on Twitter, LinkedIn, Facebook
If you find yourself with any doubts or want to share your valuable suggestion do comment below
Stay tuned for the second part of the series where we will serve this model as a Web App. Until then Keep Practising.
Thank You!!