Transfer Learning & Fine-tuning
Why Transfer Learning?
Consider this: a ResNet-50 trained on ImageNet has already learned to detect edges, textures, shapes, and high-level object concepts from 14 million images. These features are not specific to ImageNet — edges look like edges whether you are classifying dogs or detecting tumors in medical scans. Throwing away all that learned knowledge and starting from random weights is like hiring an experienced chef and making them re-learn how to hold a knife. Training from scratch requires:- Massive datasets (typically 100K+ labeled examples for reasonable accuracy)
- Enormous compute (days to weeks on GPUs)
- Careful hyperparameter tuning (learning rate, augmentation, etc.)
- Start with ImageNet/web-scale features (lower layers already detect edges, textures, patterns)
- Adapt to your specific task with much less data (often 100-1000 examples is enough)
- Get SOTA results with minimal compute (hours instead of weeks)
The Transfer Learning Spectrum
Feature Extraction
The simplest form of transfer learning: treat the pretrained model as a fixed feature extractor. Freeze all pretrained weights (they do not update during training) and only train a new classification head on top. This works surprisingly well when your target domain is similar to the pretraining domain and you have limited data — with as few as 50-100 examples per class, you can often get 85-95% accuracy. Freeze pretrained backbone, only train new classifier:Fine-tuning (Gradual Unfreezing)
Discriminative Learning Rates
Different learning rates for different layers:Transfer Learning for Vision Transformers
Transfer Learning for NLP
Using Hugging Face Transformers
Parameter-Efficient Fine-tuning (PEFT)
LoRA (Low-Rank Adaptation)
The insight behind LoRA: when you fine-tune a pretrained model, the weight updates tend to be low-rank — meaning the actual “change” to each weight matrix can be approximated by multiplying two much smaller matrices. Instead of updating the full weight matrix (which might be 4096 x 4096 = 16 million parameters), LoRA freezes and trains two small matrices (4096 x 8) and (8 x 4096), so the effective update is with only 65,536 trainable parameters. That is a 99.6% reduction. Think of it like adjusting a building’s plumbing. Full fine-tuning rebuilds every pipe. LoRA installs small bypass valves at key junctions — same water flow, fraction of the construction cost. Only train small rank decomposition matrices:Adapter Layers
Domain Adaptation Techniques
When target domain differs significantly:Best Practices
Common pitfall — forgetting to adjust data normalization: Pretrained models expect inputs normalized with the pretraining dataset statistics (e.g., ImageNet mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]). If you normalize with your own dataset’s statistics, the pretrained features will receive out-of-distribution inputs and perform poorly. Always use the pretraining normalization, even if your domain is very different.
Exercises
Exercise 1: Feature Extraction vs Fine-tuning
Exercise 1: Feature Extraction vs Fine-tuning
Compare accuracy on a 500-sample dataset using frozen backbone vs full fine-tuning.
Exercise 2: Discriminative Learning Rates
Exercise 2: Discriminative Learning Rates
Implement layer-wise learning rates and compare with uniform learning rate.
Exercise 3: LoRA Implementation
Exercise 3: LoRA Implementation
Add LoRA adapters to a ViT model. Compare trainable parameters and final accuracy.
What’s Next
Module 21: Model Deployment
Export models to ONNX, TorchScript, and deploy to production.