Pre-trained model
- Machine Learning course
- Advanced
- Lesson 64/78
- 5 min
A pre-trained model is trained on a large dataset with shared weights.
Pretrained models
torchvision.models offers architectures like ResNet, EfficientNet and ViT with ImageNet weights; for text, Hugging Face is a similar library. The weights are downloaded on first use and cached:
from torchvision.models import resnet18, ResNet18_Weights
agirlik = ResNet18_Weights.IMAGENET1K_V1
model = resnet18(weights=agirlik)
onisleme = agirlik.transforms() # the model's preprocessing in training
print(sum(p.numel() for p in model.parameters())) # ~11.7 millionThe transforms() that come with the weights must be used; the model expects input with the same size and normalisation it was trained with.
Normalisation and size
ImageNet preprocessing is typically: resizing (256), centre cropping (224), scaling to [0, 1] and per-channel normalisation:
ort = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)
std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)
x = torch.rand(8, 3, 224, 224) # images in the [0, 1] range
x_norm = (x - ort) / std # per channel via broadcastingview(1, 3, 1, 1) makes the per-channel values broadcast along the right axis of the (N, C, H, W) tensor.
Practice ImageNet normalisation
Normalise a (2, 3, 4, 4) image whose pixels are all 0.5 with the ImageNet mean and std values. Print the result's shape and each channel's first pixel value as a list rounded with round(..., 3).
Starter code
import torch
x = torch.full((2, 3, 4, 4), 0.5)
# buraya yazSample solution
import torch
x = torch.full((2, 3, 4, 4), 0.5)
ort = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)
std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)
x_norm = (x - ort) / std
print(x_norm.shape)
print([round(v, 3) for v in x_norm[0, :, 0, 0].tolist()])Expected output
torch.Size([2, 3, 4, 4]) [0.066, 0.196, 0.418]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why do we normalise the input for ImageNet models with the same mean and std?
- To make the image prettier
- It isn't needed
- For the GPU
- The model saw this distribution in training; different input lowers performance Correct answer
Explanation: The learned weights are tuned to a particular input scale.
2 Bring the channel means into a broadcastable shape.
ort = torch.tensor([0.485, 0.456, 0.406]).____(1, 3, 1, 1)
ort = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)
Explanation: view(1, 3, 1, 1) puts the values on the channel axis.
3 Match the preprocessing steps.
| Term | Meaning |
|---|---|
Resize(256) |
Resizing |
CenterCrop(224) |
Centre crop |
ToTensor |
To the [0, 1] range |
Normalize |
Per-channel standardisation |