Improve your model accuracy by Transfer Learning.

Anchit Jain
Data Science 101
Published in
5 min readJul 9, 2018

In this blog post, I’ll help you in understanding how we can work with real-life implementations of images or object recognition with one of the most vital design methodologies of deep learning that is TRANSFER LEARNING. Before we dive into the pool of transfer learning, let us warm up with some opening concepts that we need to understand.

The traditional machine learning models give a lot of pain when we do not have sufficient labeled data for the specific task or domain we care about to train a reliable model. For example, I need to train handwritten text recognition using some model trained on IAM data set. Transfer learning allows us to deal with these scenarios by leveraging the already existing labeled data of some related task or domain. We try to store this knowledge gained in solving the source task in the source domain and apply it to our problem of interest.

In short, transfer learning is nothing but learning from others knowledge.

Transfer learning vs. Machine learning

This figure clearly reflects the beauty of transfer learning as you can see that you can train your own customized model with minimal labeled dataset while you can see there is a lack of such feature in machine learning.

For your better understanding, I have implemented the working of a simple convolutional neural network on image classification. In this blog, we will learn about various methodologies of transfer learning and will check the accuracy on each one of them. Let us have a quick look at a process of CNN.

  1. Loading data using python libraries.
  2. Preprocess of data which includes reshaping, one-hot encoding and splitting.
  3. Constructing the model layers of CNN followed by model compiling, model training.
  4. Evaluating the model on test data.
  5. Finally, predicting the correct and incorrect labels.

For detail explanation of each step, you can refer my last blog on image classification using CNN. Let us begin with the traditional approach of machine learning using the standard CNN model.

First things first. Loading and Pre-processing the data. You can download the dataset from here. and follow the given folder directory structure to access the dataset- data/train/cats, data/train/dogs, data/validation/cats, data/validation/dogs. Where each folder of cats and dogs in train folder contains 1000 images respectively and each folder of cats and dogs in validation folder contains 400 images respectively.

Done so far? It’s time to train our model.No worries I have another gist that will help you to train. But before you scroll down make sure that you have understood each and every layer thoroughly because this will help in understanding the twist we gonna make during transfer learning.

Moving ahead we need to check how good we are.

CNN model output with accuracy 68.75%

Once we are done with training the model and with an accuracy of 68.75% after 50 epochs but this is not where we can stop. Even experimenting with hidden layers, number of neurons in hidden layers and drop out rates. We could not manage to substantially increase my training accuracy. Then how can we achieve this accuracy up to 90% or more? Yes, it is possible and this is were transfer learning comes into play. Embedding the concept of transfer learning we will see how freezing the layers of existing model will trigger the performance of the model.

Moving ahead, we will use pre-trained image recognition model called INCEPTION V3 which will help us in two ways extracting features from existing CNN and classification of fully connected softmax layers.

The entire process will cover the following scenarios.

  1. training a neural network from scratch(see above)
  2. using bottleneck features of inception v3 model.
  3. fine tuning on a newly trained model.

Working more on model layers we will see that how a change in CNN layers using inception v3 models will help to achieve good results in our accuracy. Once we succeed in building the model from scratch then working on inception v3 model won’t trouble you much. Let’s check it out.

Running the above code will give you the following result after running the 10 epochs.

Bottleneck features with 92.93% accuracy

If you can notice in above lines code, the functionality of the entire code are divided into two-phase bottleneck featuring and Fine tuning

‘Bottleneck’ is an informal term we often use for the layer just before the final output layer that actually does the classification.

Fine-Tuning is training the variables of an imported module together with those of the model around or in simple words we can say that it is the process in which the parameters of a trained model must be adjusted very precisely while we are trying to validate that model.

Once defining the inceptionv3 layers with bottleneck featuring we are about to compile the model and using the process of data augmentation on training and test data we can help our model to grab more features of an image by eventually increasing the size of the database. Data augmentation also plays one of the most vital roles in increasing the accuracy of any model.

And we are all set for training our model with new accuracy.

So far we have now achieved a good accuracy on this model by freezing the base layers and unfreezing the top layers of the model. What if we can further improve the accuracy from 92 % to 97.88 %. By using the process of fine-tuning in the above code we can reach to this accuracy. After tuning very precisely the entire model we were able to achieve the following result.

Fine tuning with accuracy of 97.88 %

SUMMARY :Using these two process of an inceptionv3 model we have learned that how we can improve the accuracy of any model and below summary table will help you to identify the distinguished accuracy among them.

Summary

You can access the full code here.

Thank you for you patience…………Claps (Echoing)

--

--

Anchit Jain
Data Science 101

Machine learning engineer. Loves to work on Deep learning based Image Recognition and NLP. Writing to share because I was inspired when others did.