Transfer Learning of CNN Model Using Google Colab — part I

Nir Barazida
5 min readSep 1, 2020

--

In this blog post, I will explore how to perform transfer learning on a CNN image recognition (VGG-19) model using ‘Google Colab’. The model includes binary classification and multi-class classification of leaf images.

What Is ‘Google Colab’

‘Google Colab’ research project created to help machine learning education and research. It’s a Jupyter notebook environment that requires no setup to use and runs entirely in the cloud. The main advantage of ‘Google Colab’ is that it provides FREE GPU usage which allows the user to train very complex models in a fast and easy way.

from https://imgflip.com/

The Model

I will use a model that I created during the Israel Tech Challenge (ITC) Hackathon in the summer of 2020. ITC conducts a hackathon twice a year to its fellows, which have to produce a product within 48 hours. The main subject of the Hackathon was sustainability and my team chose to create an app that makes an assessment of a tree condition using a picture of its leaves. To do so, I created a model that is trained on images of sick and healthy trees. quickly I realized that Machine Learning tools will not produce the necessary accuracy so I chose to use a Deep Learning model where VGG-19 is the base model and I added layers to it.

The full dataset can be found in this GitHub repository.

Work-Flow

  1. Connect to ‘Google Colab’ to ‘Google Drive’ and clone the database to it.
  2. Load the database to run time.
  3. Create a Binary model.
  4. Create a Multi-Class Model.

Let's Get Started

1. Connect to Google drive and clone the database to it

First, we will choose our hardware accelerator as a GPU.
Go to the ‘Run time’ tab, choose ‘change runtime type’ and select GPU.
To make sure that we are using GPU I’ll run the next cell:

config.list_physical_devices('GPU')> [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

To work with ‘Google Colab’ we need to create a directory where we store our database. ‘Google Colab’ provides it’s users with 2 options. The first, to upload the data to the run time which will be deleted once the runtime is over. The second, to upload the database to ‘Google Drive’ and to Mount a connection between the ‘Google Colab’ notebook and the Drive repository. Because of using multiple runtimes, I will choose the second option.

Run the next cell and follow the instructions.

‘Google Colab’ supports git commands and lets us clone, push and pull files.

We’ll start by changing the directory on our Google Drive to the place we want to clone the directory and only then we’ll clone it.

2. Load the database to run time

Now that the data is stored on my ‘Google Drive’ I will load it to the run time. First, let’s check what folders the database stores and how may images every category holds.

Choose the directories that will be loaded to the run time

Load to run time

Important:
VGG-19 model default input, like many other models, is the size (224,224). Thus, I will rescale the images to the necessary size. One can also trim the image but will risk losing valuable information.

Sample of the database

3. Create a Binary model

I believe a good data scientist should start with the simplest problem and as they succeed they can add complexity to their model. Thus, I will choose to start with a simple Binary Model and after achieving good results I will carry on to create a more complex model, such as the Multi-Class model.

The Label Encoder classes are: ['healthy' 'sick']
Sample of the binary dataset labels:
First 10 images[1 1 1 1 1 1 1 1 1 1]
Last 10 images[0 0 0 0 0 0 0 0 0 0]

Split the data to train, validation and test sets
Because of the low sample size, there is a risk that the model will overfit.
To prevent it I will use as many images as possible for the training of the model. Thus will have a small test set.
Make sure to use stratify to distribute the categories equally between the data sets.

The train categories distribution is:  {0.0: 5927, 1.0: 14703}
The test categories distribution is: {0.0: 823, 1.0: 2043}
The validation categories distribution is: {0.0: 1482, 1.0: 3676}
train set: ((20630, 224, 224, 3), (20630,))
Validation set: ((5158, 224, 224, 3), (5158,))
test set: ((2866, 224, 224, 3), (2866,))

Insights:

  • All the categories are spread equally in the different data sets.
  • All images have the same size (224,224) with 3 colors (RGB).
  • The size of the labels and image sets is equal.

data augmentation

Data augmentation is a strategy that enables practitioners to significantly increase the diversity of data available for training models, without actually collecting new data.

  • Because we have a small dataset we would like to generate ‘Artificial images’ from the original ones. We will do it using `ImageDataGenerator` that will manipulate every image (using horizontal_flip, rotation, zoom, etc.) to generate images that the model hasn’t seen before.
  • For every epoch, the `ImageDataGenerator` will create a new image from the base image with a different manipulation.

There is no need to perform data augmentation on the test set because we will only run it one time.

In the next blog post, I will show how to import the VGG-19 base model, add relevant layers, train, save and load the model, and finally make a prediction!
waiting for you in part II

--

--

Nir Barazida

Data Scientist at DAGsHub. Leading the advocacy and outreach activity worldwide. We are building the next GitHub for Data Science.