Sub topic 5
Choosing a Pretrained Architecture
This chapter assumes you know basic Python, PyTorch tensors and modules, and the ideas of loss, gradient descent, epochs, and convolutional neural networks. You will use those ideas to choose a practical starting model before replacing its classification head.
Why architecture choice matters
A pretrained model has already learned visual patterns from a large dataset, usually ImageNet. Early layers often detect edges, colors, and textures. Later layers combine those patterns into shapes and object parts. Transfer learning reuses these features instead of learning every feature from random weights.
The architecture determines more than accuracy. It affects:
- Memory use during training and inference.
- Prediction speed.
- The number of parameters that can overfit a small dataset.
- How easily you can find and replace the final classification layer.
- Whether the model fits your deployment device.
A larger model is not automatically better. A model that cannot fit in memory or runs too slowly may be a poor choice even if its benchmark accuracy is higher.
Common choices in torchvision
For a first image classifier, start with a model that has a clear final layer and reliable pretrained weights.
| Architecture | Useful property | Good starting situation |
|---|---|---|
| ResNet-18 | Small, familiar, and easy to modify | Small datasets and learning experiments |
| ResNet-50 | More capacity than ResNet-18 | Harder visual differences and more data |
| MobileNet V3 | Low memory and fast inference | Phones, embedded devices, or APIs with strict latency |
| EfficientNet-B0 | Strong accuracy for its size | When you want a compact general-purpose model |
ResNet models use residual connections. A residual connection lets a layer add its learned change to the original signal. This makes deeper networks easier to optimize. MobileNet reduces computation with depthwise separable convolutions, which process spatial patterns and channels more cheaply than standard convolutions.
Match the model to the constraints
Use ResNet-18 as a baseline when your dataset is small or you are still checking the data pipeline. Its simpler structure also makes freezing and unfreezing layers straightforward.
Choose a larger model when the baseline underfits. Underfitting means both training and validation performance remain too low. More capacity can help, but it also increases computation and the risk of memorizing the training images.
Choose a mobile architecture when inference speed or memory matters more than the last few accuracy points. Measure performance on the actual target device. Desktop benchmark results do not predict phone or embedded-device latency reliably.
The pretrained model also expects a particular input size and normalization. Read the weight metadata instead of guessing. The metadata can provide the validation transform associated with those weights.
import torch
from torchvision.models import ResNet18_Weights, resnet18
weights = ResNet18_Weights.DEFAULT
model = resnet18(weights=weights)
preprocess = weights.transforms()
print("input transform:", preprocess)
print("parameters:", sum(p.numel() for p in model.parameters()))Gotchas when comparing models
Do not compare validation scores if the models use different data splits, augmentations, or preprocessing. Those differences can matter as much as the architecture.
Do not select weights trained for a different task without checking their input requirements. Classification weights are a reasonable starting point for classification. Detection and segmentation models have different output structures.
A model name can hide different versions. Record the exact architecture, weights, input transform, class order, and software versions with your experiment. This makes later results reproducible.
Exercise
You have 800 labeled images, a GPU with limited memory, and no deployment requirement yet. Which starting architecture is a sensible choice, and why?
Show answer
Start with pretrained ResNet-18. It is compact, easy to modify, and provides a useful baseline before testing larger or faster alternatives.