Transfer Learning and Fine-Tuning

Start from a model pretrained on millions of images and adapt it to your task with a few hundred labelled examples.

Intermediate · about 8 hours · difficulty 3/5

Learn first: Image Data Pipelines and Augmentation

Training a vision model from scratch is rarely the right call. A backbone pretrained on ImageNet or a large web dataset has already learned general features (edges, textures, parts), and you only need to teach it your classes. This is the single most practical technique in applied deep learning.

Two strategies

  • Feature extraction: freeze the backbone, replace the final layer, and train only the new head. Fast, works with very little data.
  • Fine-tuning: unfreeze some or all layers and continue training with a small learning rate. Better accuracy when you have more data or your images look different from the pretraining set (medical scans, satellite images).

Practical recipe

  1. Load a pretrained model from torchvision, timm or the Hugging Face Hub.
  2. Apply the same preprocessing it was trained with.
  3. Train the new head for a few epochs with the backbone frozen.
  4. Unfreeze and fine-tune with a lower learning rate, often using discriminative learning rates (smaller for early layers).
  5. Use a learning rate finder and a one-cycle or cosine schedule.

Foundation models

Models such as CLIP and DINOv2 produce embeddings good enough for zero-shot classification or a simple linear probe. Try them as a baseline before fine-tuning anything.

Pitfalls

Forgetting to switch batch normalization layers correctly between train and eval mode, using a learning rate so high it destroys pretrained features, and leakage when near-duplicate images land in both train and test.

Good enough to move on when you can fine-tune a pretrained backbone on a custom dataset of a few hundred images and beat a from-scratch model by a clear margin.

Key concepts

  • Pretrained backbones
  • Feature extraction with frozen layers
  • Full fine-tuning
  • Discriminative learning rates
  • timm model library
  • CLIP zero-shot classification
  • Linear probing embeddings

Mini project

Collect about 300 photos of 3 to 5 categories you care about, fine-tune a pretrained ResNet or ConvNeXt, compare it with CLIP zero-shot, and publish the model with a small Gradio demo.

Check yourself

When would you freeze the whole backbone versus fine-tune every layer, and why is a lower learning rate used when fine-tuning?

Free resources