Skip to main content
Author: Sasank Chilamkurthy In this tutorial, you will learn how to train a convolutional neural network for image classification using transfer learning. Read more about transfer learning at the CS231n notes.

Why Transfer Learning?

In practice, very few people train an entire Convolutional Network from scratch (with random initialization), because it is relatively rare to have a dataset of sufficient size. Instead, it is common to:
  1. Pretrain a ConvNet on a very large dataset (e.g., ImageNet with 1.2 million images and 1000 categories)
  2. Use the ConvNet either as an initialization or a fixed feature extractor for the task of interest

Two Major Transfer Learning Scenarios

1. Finetuning the ConvNet

Instead of random initialization, initialize the network with a pretrained network (e.g., trained on ImageNet). Rest of training proceeds as usual.

2. ConvNet as Fixed Feature Extractor

Freeze weights for all layers except the final fully connected layer. Replace the last layer with a new one with random weights and train only this layer.

Example: Ants vs Bees Classification

Dataset

A small dataset with ~120 training images each for ants and bees, and 75 validation images per class. This is normally too small to generalize if trained from scratch, but transfer learning allows reasonable generalization.

Data Augmentation

Training Function

Results Comparison

The fixed feature extractor approach is faster (gradients not computed for most of the network) and achieves better accuracy on this small dataset.

When to Use Each Approach

Further Learning

References

PyTorch reference