Training image classifiers
Good image classifiers come from good data practice as much as good architectures. Multiply your data with augmentation, read a confusion matrix to find what the model gets wrong, and use transfer learning to reach high accuracy with only hundreds of labeled images.
In this lecture you will
- Explain how data augmentation improves generalization
- Read a confusion matrix and compute per-class precision and recall
- Apply transfer learning — freezing a pretrained backbone and fine-tuning it
- Plan the data work for a real image-classification project
You now know what a CNN is. Turning one into a classifier that works reliably on real photos is mostly about data and evaluation: how to make the most of a limited set of labeled images, how to see what the model gets wrong, and how to borrow knowledge from models trained on millions of images.
Augmentation: more data for free
A model should recognize a car whether it faces left or right, in morning or evening light, centered or slightly cropped. Rather than collecting photos of every variation, we can generate them: every time an image is used in training, apply a random, label-preserving transformation.
Each training epoch sees a different random version of every photo. The model cannot memorize exact pixels, so it has to learn what stays the same: the objects.
- Flip
- Mirror the image horizontally. Safe for most natural scenes, wrong for text and anything with meaningful left–right orientation.
- Rotate / crop / zoom
- Small geometric changes teach the model that position and scale do not change the class.
- Color and lighting jitter
- Random brightness, contrast and saturation changes teach robustness to cameras and lighting.
- Cut-out
- Blank out a random patch so the model cannot rely on one small detail and must use the whole object.
- Test-time augmentation
- Predict on several augmented versions of a test image and average the results — a cheap accuracy boost.
Reading a confusion matrix
A single accuracy number hides a lot. A confusion matrix counts, for every true class (rows), how often each class was predicted (columns). Click any cell to see what it means.
Most errors cluster in two pairs: cats and dogs, cars and trucks. That tells you where to collect more examples or add features — something overall accuracy could never show.
From the matrix we can compute, for each class:
Which matters more depends on the cost of each mistake. A defect detector that misses cracks (low recall) is dangerous; one that flags too many good parts (low precision) is merely expensive.
Transfer learning: stand on the shoulders of ImageNet
Training a CNN from scratch needs hundreds of thousands of labeled images. Few companies have that for their own problem — but the early and middle layers of any vision model learn similar things: edges, textures, shapes, parts. Transfer learning reuses them.
- Start from a pretrained backboneA network trained on ImageNet — 1.2 million photos in 1,000 classes — has learned general visual features in its blocks.
- Replace the headThrow away the 1,000-class output layer and attach a small new head for your own classes: here, OK, scratch and dent.
- Freeze the backbone, train the headLock the backbone’s weights and train only the new head. It takes minutes, and the general features are reused unchanged.
- Fine-tune the top blocksUnfreeze the last blocks and continue with a small learning rate, so the high-level features adapt to your images without forgetting what they know.
- Better results from less dataWith the same 600 labeled photos, a fine-tuned pretrained model typically far outperforms one trained from scratch.
import torch
from torch import nn
from torchvision import models
model = models.resnet50(weights="IMAGENET1K_V2") # pretrained backbone
for p in model.parameters():
p.requires_grad = False # 1. freeze everything
model.fc = nn.Linear(model.fc.in_features, 3) # 2. new head: OK / scratch / dent
optimizer = torch.optim.AdamW(model.fc.parameters(), lr=1e-3)
# ... train the head for a few epochs ...
for p in model.layer4.parameters(): # 3. unfreeze the top block
p.requires_grad = True
optimizer = torch.optim.AdamW(
[{"params": model.fc.parameters(), "lr": 1e-4},
{"params": model.layer4.parameters(), "lr": 1e-5}] # small learning rates
)
Classification answers what is in an image. Many applications also need where — counting products on a shelf, finding every pedestrian, locating a defect. That is object detection, the subject of the next lecture.
Key takeaways
- Augmentation creates new, realistic training images by flipping, rotating, cropping and changing lighting, so the model learns what does not matter.
- A confusion matrix shows exactly which classes are mistaken for which — far more useful than accuracy alone.
- Transfer learning reuses a backbone pretrained on millions of images; fine-tuning it needs far less data and compute.
- Most accuracy gains in practice come from better, more representative data and careful evaluation.
Check your understanding
4 questions · answers explainedQ1Why should horizontal flips NOT be used when training a model to read text in images?
Q2In a confusion matrix, where do you find the correct predictions?
Q3Precision for “dog” is 83% and recall is 80%. What does the recall tell you?
Q4You have 500 labeled images of your products. What is usually the best starting point?