πŸ“š Study Notes / Home / Neural Nets / Exam Notes / Transfer Learning
Exam Notes Β· W5S1

Transfer Learning & CNN Applications

Reuse a CNN trained on ImageNet instead of training from scratch. Feature extraction vs fine-tuning, when to use which, then detection (Faster R-CNN, YOLO) and segmentation (FCN, U-Net) β€” all built on pre-trained backbones.

1 Why transfer learning


  • Training from scratch (e.g. ResNet-152 on ImageNet) needs ~1.2M labeled images, 8 GPUs, weeks. Reality: a few hundred images, one laptop/Colab GPU, a deadline.
  • Transfer learning: use knowledge learned on a large dataset (ImageNet) to solve a different task with limited data.
  • Two approaches: (1) Feature extraction β€” freeze the pre-trained net, train a new head. (2) Fine-tuning β€” unfreeze some layers, adapt with a small LR.
  • In practice you almost never train from scratch β€” transfer learning is the default.

2 What CNN layers learn


  • Early layers = generic: edges, colors, textures β†’ transfer everywhere.
  • Later layers = task-specific: patterns β†’ object parts β†’ specialized for the original task.
  • Hierarchy: Layer 1 (edges/colors) β†’ L2 (textures) β†’ L3 (patterns) β†’ L4–5 (object parts).
  • This is why you keep early layers and only retrain the end for a new task.

3 Feature extraction


  • Use the frozen pre-trained CNN as a fixed feature extractor; it outputs a feature vector (e.g. 512-dim) that you feed to a new trainable classifier.
  • Steps: load pre-trained model β†’ freeze all layers (requires_grad=False) β†’ replace final FC head β†’ train only the new head.
  • Fast, needs little data, trains only ~0.1% of parameters yet hits high accuracy.
model = resnet50(weights='IMAGENET1K_V1')
for param in model.parameters():
    param.requires_grad = False        # freeze backbone
model.fc = nn.Linear(2048, num_classes) # new head (trainable)
optimizer = Adam(model.fc.parameters(), lr=0.001)

Note: a frozen layer still runs in the forward pass to produce features; its weights just never update in backprop.

4 Fine-tuning


  • Fine-tuning: unfreeze some pre-trained layers and train end-to-end with a small learning rate.
  • Differential LR: new head LR β‰ˆ 1e-3; unfrozen pre-trained layers get 10–100Γ— smaller LR (β‰ˆ 1e-4); deeper-frozen layers LR = 0.
  • Critical: a large LR on pre-trained layers destroys useful features in the first epoch (catastrophic forgetting). Small LR adapts gently without erasing ImageNet knowledge.

Three strategies:

  • Conservative β€” unfreeze only last 1–2 blocks. Safe for small data, minimal forgetting.
  • Aggressive β€” unfreeze all layers, very small LR. Best for large data / very different domains.
  • Gradual unfreezing β€” start head-only, unfreeze more each epoch. Safest for beginners.

Practical tips: always train the head first for 1–2 epochs before unfreezing; keep batch norm in eval mode while fine-tuning; monitor val loss (overfits fast on small data); use early stopping.

Must-know for exam Feature extraction = freeze backbone, train new head (small/similar data). Fine-tuning = unfreeze + small LR (more data / different domain). Use a 10–100Γ— smaller LR for pre-trained layers to avoid catastrophic forgetting. Rule of thumb: start frozen, unfreeze only if accuracy plateaus.

5 When to use which β€” the 2Γ—2


Small datasetLarge dataset
Similar domainFeature extractionFine-tune all layers
Different domainFine-tune last layersFine-tune all (or scratch)
  • Rule of thumb: start with feature extraction; unfreeze more if needed.
  • Early layers (edges, textures) are the most likely to transfer across different image domains.

6 Domain shift


  • Domain shift: model trained on domain A, deployed on domain B β†’ performance drops. E.g. studioβ†’phone photos, USβ†’rural clinic X-rays, syntheticβ†’real-world.
  • Simplest fix: fine-tune on a small labeled set from the target domain β€” even 100–500 target samples can significantly close the gap.

7 Object detection


  • Detection = WHAT + WHERE: class labels and bounding boxes (x1,y1,x2,y2). Uses: self-driving, security, medical, retail.
  • Naive sliding window: classify a window at every position & scale β†’ millions of forward passes β†’ too slow.
  • Faster R-CNN (two-stage): Stage 1 Region Proposal Network (RPN) proposes ~2000 regions from shared backbone features; Stage 2 classifies each region + refines the box. Backbone computed once, reused.
  • YOLO (single-shot): divide image into an SΓ—S grid; each cell predicts B boxes + confidence and C class probabilities. One forward pass, real-time (30+ FPS); slightly weaker on small objects.
  • IoU = area of overlap / area of union between predicted & ground-truth box. IoU > 0.5 β‰ˆ a "correct" detection.
Two-stage (R-CNN)Single-shot (YOLO)
SpeedSlowerReal-time
AccuracyHigherSlightly lower
Small objectsBetterWeaker
Use caseMedical, high-stakesVideo, robotics

Both use pre-trained CNN backbones β€” transfer learning is the foundation. YOLO dominates industry (speed beats the last 1% accuracy); R-CNN family for high-stakes accuracy.

8 Segmentation


  • Progression: Classification ("cat") β†’ Detection (box) β†’ Segmentation (pixel mask).
  • Semantic: classify every pixel (all cats one color). Instance: distinguish individuals (cat 1 vs cat 2).
  • Encoder–decoder architectures (FCN, U-Net): encoder downsamples to build semantic features; decoder upsamples back to full resolution (transposed conv or bilinear interp + conv).
  • Skip connections (U-Net) pass high-res spatial detail from encoder to decoder. Without them the decoder only has the compressed bottleneck β†’ blurry boundaries.
  • Why: medical imaging / autonomous driving need exact boundaries, not rectangles.

β˜… Likely exam questions


Q1. 300 labeled flower photos, 5 species β€” feature extraction or fine-tuning?

Feature extraction. 300 images is very small and flowers are similar to ImageNet, so freeze ResNet and train only a new FC head. Fine-tuning would overfit with so little data.

Q2. Why a much smaller LR for pre-trained layers than the new head?

A large LR destroys pre-trained features in the first few updates (catastrophic forgetting). A small LR (10–100Γ— smaller) adapts them gently without erasing useful ImageNet knowledge.

Q3. What problem does the RPN in Faster R-CNN solve vs sliding window?

Sliding window tests millions of positions/scales, each a forward pass. The RPN shares backbone features and proposes only ~2000 regions β€” orders of magnitude faster.

Q4. Purpose of U-Net skip connections β€” and what happens without them?

They pass high-resolution spatial detail (edges, boundaries) straight from encoder to decoder. Without them the decoder only has the compressed bottleneck and produces blurry boundaries.

Q5. ImageNet model does poorly on X-rays despite fine-tuning β€” name it and the fix.

Domain shift. Simplest fix: fine-tune on a small labeled set from the target domain (even 100–500 X-ray samples helps significantly).

Q6. Which CNN layers transfer best across different domains, and why?

Early layers β€” they learn generic edges/colors/textures that are universal. Later layers are task-specific and transfer less.

Q7. Compare YOLO vs Faster R-CNN.

YOLO: single-shot, one forward pass, real-time, slightly weaker on small objects β†’ video/robotics. Faster R-CNN: two-stage (RPN + classify), slower but higher accuracy and better on small objects β†’ medical/high-stakes. Both use pre-trained backbones.