Machine Learning
Pre-trained model
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 million
The 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 broadcasting
view(1, 3, 1, 1) makes the per-channel values broadcast along the right axis of the (N, C, H, W) tensor.
Exercise 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).
Show example 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()])
Test yourself
-
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
Show answer
The model saw this distribution in training; different input lowers performance — The learned weights are tuned to a particular input scale.
-
Bring the channel means into a broadcastable shape.
ort = torch.tensor([0.485, 0.456, 0.406]).____(1, 3, 1, 1)
Show answer
ort = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)
-
Match the preprocessing steps.
- Resize(256)
- Resizing
- CenterCrop(224)
- Centre crop
- ToTensor
- To the [0, 1] range
- Normalize
- Per-channel standardisation
Next lesson: Fine-tuning