Lesson 5.4core3 h

Transfer learning and training CNNs in practice

Start from pretrained weights, then fine-tune with a modern recipe

Almost nobody trains vision models from scratch anymore. A backbone pretrained on ImageNet (or on billions of web images, as with CLIP and DINOv2) already knows edges, textures, parts and objects; fine-tuning adapts that knowledge to your 2,000 X-rays or 500 product photos in minutes. The same logic — pretrain broadly, adapt cheaply — is exactly what LoRA does for LLMs (8.2 Fine-tuning and PEFT). This lesson gives you the recipe, the augmentations and the debugging habits that make fine-tuning reliable, and prepares the project in 5.10 Project: train and deploy an image classifier.

You'll be able to

  • Explain why early features transfer and choose between feature extraction and fine-tuning based on data size and domain similarity
  • Write a complete timm/PyTorch fine-tuning loop with discriminative learning rates, a schedule and mixed precision
  • Select and justify data augmentations, including mixup/CutMix, and avoid label-destroying ones
  • Handle class imbalance and tiny datasets using weighting, embeddings and foundation backbones
  • Diagnose a fine-tuning run with confusion matrices, top losses and calibration

Key ideas

  1. Early layers learn general edge/texture detectors; later layers are task-specific — so pretrained initialization helps everywhere and most when data is scarce.
  2. Small data + similar domain → freeze and train a head (or logistic regression on embeddings); large data → fine-tune everything with a small LR; in between, discriminative LRs and gradual unfreezing.
  3. A reliable recipe: timm backbone, fresh head, pretraining mean/std, AdamW with 10× lower LR for the backbone, warmup + cosine, label smoothing, bf16 autocast, EMA.
Show 4 more key ideas
  1. Augmentation is the strongest regularizer: random resized crops, flips, RandAugment, mixup/CutMix — but only transformations that preserve the label.
  2. For imbalance: class weights or weighted sampling plus threshold tuning; for tiny datasets: frozen foundation-model embeddings + kNN/logistic regression.
  3. Normalize with the pretraining statistics, mind train/test resolution, and scale LR with batch size.
  4. Debug with confusion matrices, top-loss images and calibration before touching the architecture.

Suggested path through this lesson