Transfer Learning & Fine-Tuning: Backbone Freezing, Linear Probing & Discriminative LRs
Why nobody trains ResNets or ViTs from scratch on 1,000 images: the Dataset-Size × Domain-Shift matrix, the 2-stage LP-FT (Linear Probe → Fine-Tune) recipe, and layer-wise Discriminative Learning Rates!
Training a vision backbone from random weights on 800 medical X-rays overfits miserably at 65% accuracy — fine-tuning a pretrained ConvNeXt/ViT hits 94%+ in 3 minutes!
95% of real-world industry Deep Learning is Transfer Learning: starting from strong pretrained weights and adapting safely without catastrophic forgetting.
Using LP-FT (freeze & warm up head first, then unfreeze with discriminative LRs) reliably beats naive end-to-end fine-tuning on out-of-distribution test sets!
Classic applied DL interview question: 'You replace the classification head of a pretrained ResNet with a random Linear layer and immediately fine-tune all layers at lr = 1e-3. What goes wrong?'
What happens if you fine-tune an entire pretrained backbone immediately alongside a randomly initialized classification head at a high LR?
- Choose between Frozen Backbone (Linear Probing), Partial Unfreezing, and Full Fine-Tuning based on dataset size and domain shift
- Apply the LP-FT (Linear Probe then Fine-Tune) 2-stage recipe to prevent random head gradients from distorting pretrained features
- Configure PyTorch Optimizer Parameter Groups for layer-wise Discriminative Learning Rates (
1e-5early →1e-3head) - Avoid the classic BatchNorm Eval Trap when freezing a pretrained CNN backbone
early: 1e-5, mid: 1e-4, head: 1e-3 inside torch.optim.AdamW([...]).Learning to play tennis when you already play badminton is Transfer Learning: you don't re-learn how to run or track a ball (your frozen early layers!). You keep your core coordination intact and gently adapt your wrist swing (top layers + head)!
1. The Dataset-Size × Domain-Shift Matrix
- Small data + Similar domain: Freeze backbone (
requires_grad=False) and train ONLY the linear head. - Small data + Shifted domain (e.g., X-rays, satellite): Linear probe first, then gently unfreeze only the top 1–2 blocks with heavy augmentation.
- Large data: Use LP-FT to fine-tune the full model with Discriminative LRs!
Backbone Freezing Strategy Selector
2. The 2-Stage LP-FT Recipe (Linear Probe → Fine-Tune)
Never shock a pretrained backbone with random head gradients! Run Stage 1 (Linear Probe) with the backbone frozen for 1–3 epochs, then enter Stage 2 (Fine-Tune) by unfreezing the backbone at a 10×–50× smaller learning rate.
3. Discriminative Learning Rates in PyTorch
Early layers detect universal edges and textures that barely need to move (1e-5); deeper semantic layers adapt moderately (1e-4); the new task head moves fastest (1e-3). Pass multiple param-group dicts directly to AdamW!
When freezing a pretrained ResNet backbone (param.requires_grad = False), why should you ALSO keep frozen BatchNorm layers in .eval() mode?
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
YES! Always match the exact input preprocessing (mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) that the pretrained backbone was originally trained with!
LP-FT Two-Stage Transfer Learning Schedule
Warm up the random head first so huge initial gradients never destroy pretrained backbone weights.
Medical Imaging, Manufacturing QC & Domain Classification
Turns 500–5,000 labeled domain images into production-grade >95% classifiers in minutes using pretrained ViT/ConvNeXt backbones.
optimizer = torch.optim.AdamW([
{"params": model.early_blocks.parameters(), "lr": 1e-5},
{"params": model.late_blocks.parameters(), "lr": 1e-4},
{"params": model.head.parameters(), "lr": 1e-3},
], weight_decay=0.01)
transforms attached to the pretrained weights (weights.transforms() in torchvision or timm.data.resolve_model_data_config)!