Transfer Learning using Inception-v3 for Image Classification

Tejan Irla
Analytics Vidhya
Published in
4 min readOct 5, 2019
Photo by Pixabay on pexels.com

In my previous post, I worked on a subset of the original Dogs vs. Cats Dataset (3000 images sampled from the original dataset of 25000 images) to build an image classifier capable of classifying images of Dogs and Cats with 82% accuracy.

This project is in continuation with the previous one where we try to improve our performance and is based on the TensorFlow in Practice Specialization.

When we have a relatively small dataset, a super-effective technique is to use Transfer Learning where we use a pre-trained model. This model has been trained on an extremely large dataset, and we would be able to transfer weights which were learned through hundreds of hours of training on multiple high powered GPUs.

Many such models are open-sourced such as VGG-19 and Inception-v3. They were trained on millions of images with extremely high computing power which can be very expensive to achieve from scratch.

We are using the Inception-v3 model in the project.

Transfer Learning has become immensely popular because it considerably reduces training time, and requires a lot less data to train on to increase performance.

Let's begin.

Get the data (3000 total images)

Import the Inception-v3 model

We are going to use all the layers in the model except for the last fully connected layer as it is specific to the ImageNet competition.

Make all the layers non-trainable (We can retrain some of the lower layers to increase performance. Keep in mind that this may lead to overfitting)

We use binary_crossentropy as the loss metric as we have 2 target classes (it's a binary classification problem)

Our optimizer is RMSprop with a learning rate of 0.0001 (We can experiment with this; Adam and Adagrad optimizers would also work well)

After rescaling the images, and using Image Augmentation, we flow them in batches of 20 using train_datagen and test_datagen. Details can be found in my previous post.

We have 2000 training images and a 1000 for validation.

Let’s Train

After about 35 minutes, we were able to achieve an accuracy of 94%. A callback can very easily be implemented after we reach a certain accuracy.

This is the kind of result we were hoping for using Transfer Learning; Building upon a pre-trained model and using it in our custom application which was able to achieve great performance after training on just 2000 images.

Another approach to this problem would be not using all the layers of Inception-v3.

Here we were able to achieve an accuracy of 96% using a callback.

As you can see, Transfer Learning has great applications in Computer Vision.

Not everyone can afford weeks of training in addition to access to millions of images to build a high performing CNN model for their custom use case, and this is where Transfer Learning comes in. You get the power and the flexibility to retrain some of the lower layers (as per your target classes) using a reasonable amount of resources. Here’s an article aiming towards deploying such models through IOS apps.

Connect with me on LinkedIn. You can find the full code here.

Cheers !!

--

--