Transfer Learning in 5 minutes with VGG16

Monika Nowakowska
5 min readJan 20, 2021

Have your favorite pet’s photo close at hand, we will need it!

Is it a dog or is it a ghost?

Photo by Karsten Winegeart on Unsplash

Transfer Learning is a very popular machine learning concept in which the model uses the wisdom of other (previously trained) models instead of self learning from scratch. It is the most used idea in image recognition and deep learning in general, where gigantic datasets are required to achieve satisfactory results.

Use what you know, Adapt it & Keep learning. This is the essence of what Transfer Learning does. Today, I will show you how!

Preface

1. What is Transfer Learning?

2. Why should you care about Transfer Learning?

3. What’s VGG16?

4. Transfer Learning in practice

What is Transfer Learning?

Janelle Shane in her fantastic book “You look like a thing and I love you”, called transfer learning: “Piggybacking on other progress”. She couldn’t put it more accurately.

As a matter of a fact, artificial intelligence models learn very slowly. If you want to train your model with decent results, you need hundreds of thousands of data to solve countless possible scenarios. Otherwise, with very little, messy data you can be sure your model will almost certainly overfit. Unfortunately, this is a very common and undesirable scenario. Little data means too few cases to analyze and therefore too few real-life examples to be taught.

Why should you care about Transfer Learning?

Simply because learning from scratch is not effective at all. Nobody wants to waste time reinventing the wheel. Imagine how depressing it must be to learn math from scratch every time you move on to the next grade. It is human nature to learn from mistakes. Every time we move to a higher class, our math level is naturally a bit higher, because we are richer in the knowledge already acquired. As a consequence, learning takes less time, and we gain the ability to solve more complex tasks.

What is VGG16?

VGG16 is a convolutional neural network model proposed by the University of Oxford. The VGG model achieved 92.7% test accuracy in ImageNet competition. ImageNet is a famous database created by Fei-Fei Li who began collecting data in 2006. It’s a huge database of images designed for visual object recognition research. It consists of 1,4 million dataset labels from 1000 categories.

The VGG16 is just one of the pre-trained models that you can choose from. Here are many other image classification models that you can import from the Keras library. Note that the architecture for VGG16 and VGG19 is exactly the same, the only difference is VGG19 is a bit bigger (more layers)

import keras.applications
dir(keras.applications)

Transfer Learning in practice

Now, let’s turn this knowledge into more practical experience.

My lovely doggo — Molly, will take part in today’s experiment. For those of you who don’t know, this is the shih tzu breed. But, don’t trust my word! See what VGG16 has to say about it.

Molly in the park — Image by Author

Step-1: Import libraries

The VGG16 model is included in the Keras package. It can be imported from the keras.applications module.

Step-2: Download VGG16

Step-3: Input data

Imageio library helps to read an image data. According to Keras documentation, the input_shape parameter has to be 224x 224 pixels in RGB (3 channels).

If your image has different values (as is my case), we can fix it by adding the target_size parameter

Now, we are good to go to the final step

Step-4: Prediction

Just because I want to see the 5 most similar results, I used the “top” parameter.

Step-5: Visualization

Step-6: Conclusions

The Shih Tzu wins the competition with a staggering 89% probability. Lhasa is right behind him, taking second place with 9,4%. Pekinese, Tibetan Terrier, and Pug scored less than 1%.

As you can see, short-nosed, flat-faced doggos, can be difficult to distinguish even for humans. As it turned out today, using a previously trained model, we can flawlessly classify the breed of our dog with a surprisingly high result. Moreover, I completed this test in less than 5 minutes (writing the code included). As you can already guess, this would never be possible without implementing the Transfer Learning concept.

Final Thoughts

The main purpose of this article was to show how quickly and easily the Transfer Learning concept can be implemented. Even if you don’t have enough training data, you are not in a losing position. You just need to use one of the already trained models and apply it to any problem you want to solve. Using your own photos, you can try to classify not only dogs, but really anything you want. Give it a try!

So… is it a dog or is it a ghost? :)

Thank you for reading!

--

--