AI Notebooks - Tutorial - Use a pretrained ResNet model for image classification

How to use an existing model to do Transfer Learning?

Last updated 6th June, 2022.

Objective

This tutorial will allow you to use Transfer Learning to train an existing model on a custom dataset thanks to OVHcloud AI Notebooks.

At the end of this tutorial, you will have learned to master OVHcloud AI Notebooks and be able to predict the scores obtained by students as a function of the number of hours worked.

To show how Transfer Learning can be useful, ResNet50 will be trained on a custom dataset.

USE CASE: Flower Classification

To classify flower images, the Flower Classification dataset will be used. It is available on Kaggle.

Contains information from Flower Classification, which is made available here under the CC0: Public Domain.

Definition

Transfer learning

In Machine Learning, the aim of Transfer Learning is to use the knowledge already acquired to solve a completely new, but related problem. In other words, the technique is to transfer the notions learned on a source dataset to be more efficient in the processing of a new target dataset.

For this tutorial, the goal is to do image classification. An existing model is used: ResNet50.

ResNet50 model

ResNet is a Residual neural Network structure. It is an innovative neural network created for image classification.

The ResNet model architecture allows the training error to be reduced with a deeper network through connection skip.

Residual neural networks ignore some connections and make double or triple layer jumps that contain non-linearities (ReLU).

image

With this method, performance is generally improved.

ResNet has many variants that work on the same concept but have different numbers of layers. Resnet50 is used to refer to the variant that can work with fifty neural network layers. It was trained on more than a million images from the ImageNet database.

Requirements

Instructions

First, you have to create 2 object containers in your OVHcloud Object Storage.

  • The first one contains the non-pre-processed data (base images).
  • The second object container is empty. It is intended to receive the data once processed and split for training.

To know more about how to push your data to Object Storage, please refer to the dedicated documentation.

Then, you can launch your notebook from the OVHcloud Control Panel or via the ovhai CLI.

Launching a Jupyter notebook with "Miniconda" via UI

To launch your notebook from the OVHcloud Control Panel, refer to the following steps.

Code editor

Choose the Jupyterlab code editor.

Framework

In this tutorial, the TensorFlow framework is used.

For this tutorial, you can use the following TensorFlow framework version: tf2.4-py38-cuda11.0-v22-4.

Resources

You can choose the number of CPUs or GPUs you want.

Here, using 1 GPU is sufficient.

Attach a Git container or repository

  1. Attach the object container (from the Object Storage) that contains your dataset for image classification.
  • Mount directory: /workspace/data
  • Permission: read only (RO)
  1. Attach an empty object container to store your data, once it has been processed and split for training, validation and test.
  • Mount directory: /workspace/data-split
  • Permission: read write (RW)
  1. Attach an empty object container to store your model after training.
  • Mount directory: /workspace/saved_model
  • Permission: read write (RW)

To learn more about how to use and manage your data in a notebook with UI, check this documentation.

Launching a Jupyter notebook with TensorFlow via CLI

If you want to launch it with the CLI, choose the jupyterlab editor and the tensorflow framework.

For this tutorial, you can use the following TensorFlow framework version: tf2.4-py38-cuda11.0-v22-4.

To know more about how to use and manage your data in a notebook with the CLI, refer to this documentation.

Choose the number of GPUs (<nb-gpus>) to use in your notebook and use the following command.

ovhai notebook run tensorflow jupyterlab \
        --name <notebook-name> \
        --framework-version tf2.4-py38-cuda11.0-v22-4 \
        --volume <my-data>@<region>/:/workspace/data:RO:cache \
        --volume <my-model>@<region>/:/workspace/saved_model:RW \
        --gpu <nb-gpus> 

You can then reach your notebook’s URL once the notebook is running.

Accessing the notebook

Once the repository has been cloned, find your notebook by following this path: ai-training-examples > notebooks > computer-vision > image-classification > tensorflow > notebook-resnet-transfer-learning-image-classification.ipynb.

A preview of this notebook can be found on GitHub here.

Feedback

Please send us your questions, feedback and suggestions to improve the service:


Did you find this guide useful?

Please feel free to give any suggestions in order to improve this documentation.

Whether your feedback is about images, content, or structure, please share it, so that we can improve it together.

Your support requests will not be processed via this form. To do this, please use the "Create a ticket" form.

Thank you. Your feedback has been received.


These guides might also interest you...

OVHcloud Community

Access your community space. Ask questions, search for information, post content, and interact with other OVHcloud Community members.

Discuss with the OVHcloud community

In accordance with the 2006/112/CE Directive, modified on 01/01/2015, prices incl. VAT may vary according to the customer's country of residence
(by default, the prices displayed are inclusive of the UK VAT in force).