Day 37: Transfer learning: fine-tuning a pretrained ResNet/EfficientNet
Standing on a pretrained model's shoulders
A ResNet trained on ImageNet's millions of images has already learned the universal visual vocabulary — edges, textures, shapes, object parts. Transfer learning reuses that: take the pretrained network, replace only its final classification layer with one for your classes, and fine-tune. You inherit months of learning and millions of images' worth of visual understanding, then adapt it to garments with a few thousand examples. This is *the* technique that makes the ≥90% Garment Classifier achievable without a massive dataset.
import torch.nn as nn
from torchvision import models
model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT)
# option A: freeze the backbone, train only the new head (fast, small data)
for param in model.parameters():
param.requires_grad = False
# replace the 1000-class ImageNet head with an 8-class garment head
model.fc = nn.Linear(model.fc.in_features, 8) # only this is trainable now
# option B: unfreeze later and fine-tune the whole net at a low LR for more accuracyFreeze, then fine-tune
Two strategies, often combined: feature extraction freezes the pretrained backbone and trains only the new head — fast and safe on small data. Fine-tuning then unfreezes some or all backbone layers and trains them at a *low* learning rate, letting the general features adapt slightly to your domain. Start frozen to get a baseline, then unfreeze if you need more accuracy. Use a small learning rate when unfrozen, or you'll wreck the pretrained weights.
Why this is the practitioner default
Almost no one trains vision models from scratch anymore — transfer learning is faster, needs far less data, and reaches higher accuracy. Being able to explain *why* (the backbone already learned general features; you only adapt the last mile) is exactly the practical judgment interviewers probe for in an applied AI engineer.
Key terms
- Transfer learning
- Reusing a model pretrained on a large dataset and adapting it to a new, smaller task.
- Feature extraction
- Freezing a pretrained backbone and training only a new task-specific head on top of its features.
- Fine-tuning
- Unfreezing pretrained layers and training them at a low learning rate to adapt them to the new task.
When fine-tuning (unfreezing) a pretrained ResNet backbone, why should you use a small learning rate?