19/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

ModelTaskBest For
ResNet-50ImageNetGeneral image classification
BERTWikipediaText understanding
GPT-2Wikipedia + WebTextText generation
YOLOCOCOObject detection
MobileNetImageNetMobile/edge devices

Most available through: TensorFlow Hub, Hugging Face, PyTorch Hub

main.py
Loading...
OUTPUT
β–ΆClick "Run Code" to execute…