Transfer learning and Image classification using Keras on Kaggle kernels.
By: Rising Odegua
Tags: Machine Learning, Keras, Transfer Learning, Image Classification, Inception Model

In my last post, we trained a convnet to differentiate dogs from cats. We trained the convnet from scratch and got an accuracy of about 80%. Not bad for a model trained on very little dataset (4000 images).

But in real world/production scenarios, our model is actually under-performing.

Although we suggested tuning some hyperparameters — epochs, learning rates, input size, network depth, backpropagation algorithms e.t.c — to see if we could increase our accuracy.

Well, I did try…

And truth is, after tuning, re-tuning, not-tuning , my accuracy wouldn’t go above 90% and at a point It was useless.

Of course having more data would have helped our model; But remember we’re working with a small dataset, a common problem in the field of deep learning.

But alas! there is a way….

Image from pixabay

Transfer learning walks in….

But then you ask, what is Transfer learning?

Well, TL (Transfer learning) is a popular training technique used in deep learning; where models that have been trained for a task are reused as base/starting point for another model.

To train an Image classifier that will achieve near or above human level accuracy on Image classification, we’ll need massive amount of data, large compute power, and lots of time on our hands. This I’m sure most of us don’t have.

Knowing this would be a problem for people with little or no resources, some smart researchers built models, trained on large image datasets like ImageNet, COCO, Open Images, and decided to share their models to the general public for reuse.

Image from Marvel comics

This means you should never have to train an Image classifier from scratch again, unless you have a very, very large dataset different from the ones above or you want to be an hero or thanos.

I know you have questions, like…

Why does transfer learning work?

Well Transfer learning works for Image classification problems because Neural Networks learn in an increasingly complex way. i.e The deeper you go down the network the more image specific features are learnt.

A neural network learns to detect objects in increasing level of complexity | Image source: cnnetss.com

Let’s build some intuition to understand this better. In a neural network trying to detect faces,we notice that the network learns to detect edges in the first layer, some basic shapes in the second and complex features as it goes deeper.

So the idea here is that all Images have shapes and edges and we can only identify differences between them when we start extracting higher level features like-say nose in a face or tires in a car. Only then can we say, okay; this is a person, because it has a nose and this is an automobile because it has a tires.

The take-away here is that the earlier layers of a neural network will always detect the same basic shapes and edges that are present in both the picture of a car and a person.

Now, taking this intuition to our problem of differentiating dogs from cats, it means we can use models that have been trained on huge dataset containing different types of animals.

This works because these models have learnt already the basic shape and structure of animals and therefore all we need to do, is teach it (model) the high level features of our new images.

All I’m trying to say is that we need a network already trained on a large image dataset like ImageNet (contains about 1.4 million labeled images and 1000 different categories including animals and everyday objects).

Since this model already knows how classify different animals, then we can use this existing knowledge to quickly train a new classifier to identify our specific classes (cats and dogs).

I mean a person who can boil eggs should know how to boil just water right?

Now that we have an understanding/intuition of what Transfer Learning is, let’s talk about pretrained networks.

There are different variants of pretrained networks each with its own architecture, speed, size, advantages and disadvantages.

Keras comes prepackaged with many types of these pretrained models. Some of them are:

and many more. Detailed explanation of some of these architectures can be found here.

We’ll be using the InceptionResNetV2 in this tutorial, feel free to try other models.

InceptionResNetV2 model | Image source: researchgate

The InceptionResNetV2 is a recent architecture from the INCEPTION family. It works really well and is super fast for many reasons, but for the sake of brevity, we’ll leave the details and stick to just using it in this post.

If you’re interested in the details of how the INCEPTION model works then go here.