Published in


Transfer Learning with 5 lines of code

Step-by-step guide of training accurate models with limited number of images.

In the field of modern Artificial Intelligence, Deep Learning has proved to be single most accurate methods to train intelligent models which can match and effectively augment human intelligence in performing tasks and solving problems. Deep Learning models have been extensively used computer vision, Natural Language processing , Speech and robotics. To leverage Deep Learning in creating intelligent systems, the following uncompromising factors must be in place:

  • Sufficient (sometimes large) training data
  • High Performance Computing using NVIDIA GPUs
  • A well fine-tuned deep neural network.

Most researchers, developers and companies that work on using Deep Learning for their projects and products in specific domains and uses are often faced with 2 common challenges. They are:

  • Insufficient Data: For most custom use cases, it is hard to find thousands of training data needed to create a deep learning model that will achieve desired accuracy in practical applications.
  • Cost of Training: Using powerful compute resources like NVIDIA GPUs to train can be very expensive due to the need to train for hours or days depending on tasks involved.

To solve these problems, Transfer learning is used extensively in training custom deep learning models. Transfer learning refers to the process of using an initially trained model (pre-trained) to facilitate the training of a new model in performing a new task. The advantages of using transfer learning are:

  • training very accurate models with limited training data
  • reducing training cost and time

In this tutorial, we will show you how you can train deep learning models for object recognition in images using transfer learning in just 5 lines of Python code. This is made possible with the latest version of ImageAI (v2.0.3) which allows you to train new image recognition models using pre-trained models.

Let’s get started by following the steps below.

Step 1 — Install ImageAI and its dependencies

i) Download and install Python 3.6 from official Python Language website

ii) Install Tensorflow via PIP using the command below

(pip3 install tensorflow==1.13.1)

iii) Install Keras using the command below

(pip3 install keras==2.2.4)

iv) Install OpenCV using the command below

(pip3 install opencv-python)

v) Install Numpy using the command below

(pip3 install numpy==1.16.1)

vi) Finally, install ImageAI (v2.0.3) using the command below

pip3 install imageai --upgrade

Step 2 — Download Sample Dataset and pre-trained model

For the purpose of this tutorial, we have collected a sample Fruits mini-dataset with 5 different kind of fruits which are apple, banana, mango, orange and strawberry. To demonstrate the benefits of transfer learning, this mini-dataset has only 400 images per object (300 for training and 100 for testing) as opposed to 1000 images of more that is required to train a fairly accurate deep learning model.

Download the dataset via the link below and and unzip it.

The structure of the dataset is as seen below

fruits//train//apple// 300 images of applefruits//train//banana// 300 images of bananafruits//train//mango// 300 images of mangofruits//train//orange// 300 images of orangefruits//train//strawberry// 300 images of strawberryfruits//test//apple// 100 images of applefruits//test//banana// 100 images of bananafruits//test//mango// 100 images of mangofruits//test//orange// 100 images of orangefruits//test//strawberry// 100 images of strawberry

Also, for the pre-trained model we will be using to perform transfer learning, download an already trained ImageNet model via the link below.

Then, create a Python file with the name and write the code below into it.

Let’s break down the code above:

— We imported ImageAI training class in the first line

— We created an instance of the class and set the Model type to ResNet in the second and third line

— We specified the path to the folder containing the fruits mini-dataset.

— We started the training and specified the following parameters:

  • num_objects: This refers to the number of objects in our fruits mini-dataset.
  • num_experiments: This refers to the number of times we want the model to be trained. Because we are using transfer learning, we will be training.
  • enhance_data: This is used to fine-tune the data during training for maximum accuracy
  • save_full_model: This is used to ensure the model is saved in a way we do not need to specify the model type when performing prediction
  • batch_size: This is the number of images that will be processed at once in a batch during training until all the training images are covered per experiment
  • show_network_summary: This is to show the structure of the ResNet architecture we are using
  • transfer_from_model: This is where we specified the path to the pre-trained model we are using for transfer learning
  • initial_num_objects: This is the number of objects the pre-trained model was trained to recognize which in this case is the 1000 classes in ImageNet
  • transfer_with_full_training: This is used to instruct ImageAI to retrain the entire layers in the ResNet model from the pre-trained model.

Run the file and let the 50 experiments be done. Once it is done, ImageAI will have saved a number of trained models in the fruits/models folder and also a JSON file in the fruits/json folder.

Step3 — Run Prediction Code

Now that you are done training, take the model with the highest accuracy as well as the JSON file to perform a prediction on a picture using the code below.

NB: A sample model has been provided below during the training done for this article. The training achieved over 86% accuracy despite the limited number of images we used to train.

Sample Code:

Sample Image and Result

Prediction Result: [‘strawberry’] : [‘100.0’]

So far, you have learnt how to use train your own deep learning models by leveraging transfer learning to work with limited number of images and for less the compute cost and training time of training from scratch.

For more of this and all the features provided by ImageAI, visit the ImageAI repository and documentations linked below.

If you find this article helpful and enjoyed it, kindly give it a clap. Also, feel free to share it with friends and colleagues.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Moses Olafenwa

Moses Olafenwa

Software Engineer @BabylonHealth, Prev. @Microsoft. A self-Taught computer programmer, Deep Learning, AI Engineer.