Build a Web App to detect Pneumonia from Chest X-Ray Images Part 1: Data collection and Model…

Original article was published on Deep Learning on Medium

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.

Difference between tradition and transfer learning algorithms (source: Medium)

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:

  1. VGG16, VGG19
  2. ResNet50, ResNet101, Resnet152 etc
  3. Xception
  4. Inceptionv3
  5. InceptionResNetv2 and the list go on. You can all of these here.

You can also read about using them here.

A small breather

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.

Graph for model training

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!!