Page19/20
Transfer Learning & Model Deployment Β· Page 1 of 2
Transfer Learning
Transfer Learning
The Problem: Training from Scratch
ResNet-50 with ImageNet pre-training:
- 50 layers
- ~25 million parameters
- Trained on 1.2 million images
- Training time: Days on GPU
You have 1000 images:
- Training from scratch: Slow, prone to overfitting
- You don't have enough data!
Solution: Use the pre-trained model!
Transfer Learning: Reuse & Adapt
Idea: A model trained on ImageNet learned general visual features!
Pre-trained ResNet:
Layer 1-48: General features (edges, textures, shapes)
Layer 49-50: ImageNet-specific (1000 classes)
Your task (e.g., classify dog breeds):
Layer 1-48: Keep frozen (already good!)
Layer 49-50: Replace and train (only 10 layers!)
Two Approaches
1. Feature Extraction (Frozen Backbone)
Pre-trained CNN (ImageNet)
β
Remove last layers (frozen - don't train)
β
Add new classification head (train this!)
β
Fine-tune only the head (fast!)
Use when: Limited data, similar task.
2. Fine-Tuning (Unfreeze Layers)
Pre-trained model
β
Unfreeze last few layers
β
Train entire model with low learning rate
β
Adapt all layers to your data
Use when: Plenty of data, task significantly different.
Which Layers to Unfreeze?
Layer 1-10: Very general (edges, colors) β Keep frozen
Layer 11-30: Medium level (shapes, textures) β Maybe freeze
Layer 31-50: Task-specific (objects) β Definitely unfreeze
Rule of thumb:
- Different task: Freeze most, train last few
- Similar task: Unfreeze more layers
- Lots of data: Fine-tune everything
- Little data: Freeze most
Real-World Example: Dog Breed Classification
Pre-trained: ImageNet (1000 classes, general objects)
Your task: Dog breeds (120 classes)
Approach:
1. Load ResNet-50 (pre-trained on ImageNet)
2. Remove last layer (was "1000 classes")
3. Add new last layer ("120 dog breeds")
4. Train new layer (quick!)
5. Optionally fine-tune last few layers (medium effort)
Result: 95% accuracy in hours!
Without transfer learning: 50% accuracy, days of training
Popular Pre-Trained Models
| Model | Task | Best For |
|---|---|---|
| ResNet-50 | ImageNet | General image classification |
| BERT | Wikipedia | Text understanding |
| GPT-2 | Wikipedia + WebText | Text generation |
| YOLO | COCO | Object detection |
| MobileNet | ImageNet | Mobile/edge devices |
Most available through: TensorFlow Hub, Hugging Face, PyTorch Hub
main.py
Loading...
OUTPUT
βΆClick "Run Code" to executeβ¦