Deep learning
Transfer learning for vision
Reuse a network trained on a large image dataset and adapt it to your own labels. Early layers carry over, and the head and normalization usually need to change.
Labeled images are expensive to collect. Transfer learning avoids starting from random weights. You take a network trained on a large dataset such as ImageNet, replace its final layer with one for your classes, and train from there. The early layers have learned edges and textures that transfer to many tasks, so you need far less labeled data.
The recipe
- Replace the head. Swap the 1000-way ImageNet classifier for a layer with
Koutputs, one per class of yours. - Choose how much to train. Either freeze the backbone and train only the head, or train everything with a smaller learning rate on the early layers than on the head.
- Augment to match your domain. Crops, flips and color jitter help, as long as they keep the labels valid.
Sketch, not run here: this needs torch and torchvision installed.
import torchvision as tv
import torch
weights = tv.models.ResNet18_Weights.DEFAULT
model = tv.models.resnet18(weights=weights)
model.fc = torch.nn.Linear(model.fc.in_features, num_classes)Then train with a low learning rate on the backbone and a higher one on model.fc.
Choosing how much to fine-tune
For a small dataset, start with a frozen backbone and a linear head. This is a linear probe. It trains quickly and gives a baseline, so you can measure how much fine-tuning adds. For a large dataset, or a domain far from ImageNet, full fine-tuning usually wins.
Example
A satellite-imagery team has a few hundred labeled crops. They freeze a ResNet, train only a new head, and get a baseline in minutes. Then they unfreeze the last block with a small learning rate. If the gain is small, the frozen model was already good enough.
What transfers and what does not
| Part | Carries over? | Note |
|---|---|---|
| Early convolution layers | Usually yes | Edges, colors and textures transfer widely |
| Late layers | Partly | Often unfreeze at least the last block |
| Final classifier | No | Replace it for your labels |
| Normalization statistics | Only if your data matches | Recompute them for different image types |
Transfer can also fail on the input itself. Single-channel images fed to a three-channel network need a choice: repeat the channel, or retrain the first layer. Repeating the channel is a surprisingly good baseline.
Common mistakes
- Keeping the ImageNet head. Its 1000 outputs do not match your classes.
- Using ImageNet normalization on other data. Single-channel X-rays need their own mean and standard deviation.
- Mismatched preprocessing. A random crop in training with a center crop at serving shifts the input distribution. Use the same transform in both.
Check yourself
- Which layer of a pretrained ResNet should you replace first, and why? The final classifier, because its outputs are for ImageNet's 1000 classes, not yours.
- Why train a linear probe before full fine-tuning? It is a fast baseline, and the gap to full fine-tuning tells you whether the extra work is worth it.
Key takeaways
- Start from a pretrained backbone unless your domain is very different and your dataset is very large.
- Replace the head, then decide how much of the backbone to train.
- Early features transfer well. Normalization and the classifier often do not.
- Keep preprocessing identical between training and serving.
Further learning
Pooling and cheap convolutions shape the cost of these backbones: pooling and cheap convolutions.
Related
Convolutional networks
How CNNs use small shared filters, stacked layers and residual connections to learn images, and why transfer learning from pretrained models is the default.
Residual connections and ResNet
A residual block adds its input back to its output, so each block only needs to learn a change. That keeps very deep networks trainable and gives gradients a path that skips the weights.
Supervised fine-tuning and PEFT
Instruction tuning, chat formats, data quality, LoRA/QLoRA math, and the tooling that turns a base model into a useful assistant.