Computer Vision Lab Lessons Image classification
Image classification
From the image to the class: preprocessing, Top-1 and Top-5, and the limits of models trained on ImageNet.
This content was prepared automatically from documented sources and its human scientific review is not yet complete. If you find an error, let us know.
Introduction
Classification is the simplest deep vision task: giving the whole image a single class from a fixed list.
Learning objectives
- Understand the model's preprocessing.
- Read Top-1 and Top-5.
- Know the limits of fixed classes.
Prerequisites
CNN structure.
Scientific explanation
The model receives a 224×224 image normalised with the ImageNet mean and standard deviation, and outputs 1000 scores that Softmax turns into probabilities. Top-1 accuracy is the share of images whose correct class came first, and Top-5 is the share where it is among the first five. The model knows only the ImageNet classes, so if you show it a medical image it gives the nearest class it knows, with a confidence that may be high.
In-depth explanation
Formulating the problem
We want a function f_\theta: \mathbb R^{3\times H\times W}\to\Delta^{K-1} that gives a probability distribution over K classes, and we learn \theta by minimising the cross-entropy on the training data, hoping it generalises to new data from the same distribution.
\theta^* = \arg\min_\theta \frac1N\sum_{n=1}^{N} -\log f_\theta(x_n)_{y_n} + \lambda\lVert\theta\rVert_2^2Modern training tricks
| Technique | Effect |
|---|---|
| Data augmentation (random crop, flip, RandAugment) | Better robustness and generalisation |
| Mixup / CutMix | Strong regularisation by mixing images and labels |
| Label smoothing | Reduces overconfidence: the target is 0.9 instead of 1 |
| Learning-rate scheduling (cosine) with warm-up | Stable convergence |
| AdamW and weight decay | Regularisation suited to transformers |
| EMA of the weights | A more stable final model |
Evaluation beyond accuracy
- Top-1 and Top-5: is the correct class first, or among the first five?
- Confusion matrix: which classes get mixed up?
- Balanced accuracy and per-class F1: essential with class imbalance.
- Calibration: does 80% confidence mean being right in 80% of cases? It is measured with ECE.
- Robustness: performance on data from a different distribution (another hospital, another camera).
\mathrm{ECE} = \sum_{b=1}^{B}\frac{|S_b|}{N}\big|\mathrm{acc}(S_b) - \mathrm{conf}(S_b)\big|A worked example of the confusion matrix
100 images: 90 healthy and 10 diseased. The model classifies every image as “healthy”: accuracy is 90%, but sensitivity to the disease is 0%. And balanced accuracy = (100\%+0\%)/2 = 50\%, which is the same as guessing. This is why accuracy alone is not enough.
In the lab
The classification lab shows the top five classes with their probabilities. Try an image that belongs to no ImageNet class, and notice that the model still gives a high probability to some class: Softmax does not know “I don't know”.
Review questions
- Why is the model evaluated on a test set that was not used to choose the hyperparameters?
- What is label smoothing good for?
- Compute the sensitivity and specificity of a model: TP=8, FN=2, TN=85, FP=5.
Mathematical equations
x_norm = (x/255 − μ)/σ where μ = (0.485, 0.456, 0.406) and σ = (0.229, 0.224, 0.225).
Practical example
MobileNetV2 usually classifies the coffee cup in the samples as “espresso”, “cup” or “coffee mug”, because these are the nearby ImageNet classes.
Implementation steps
- Open the classification lab and choose a sample.
- Run the classification and read the top five.
- Look at “What the model actually sees”.
Expected results
You see the 224×224 centre crop and the top five classes with their probabilities.
Common mistakes
Giving the model images outside its domain, or forgetting that the crop may push the object out of the frame.
Practical applications
Image sorting, visual search, feature extraction.
References
- J. Deng et al. (2009).ImageNet: A large-scale hierarchical image database. CVPR 2009. doi:10.1109/CVPR.2009.5206848
- O. Russakovsky et al. (2015).ImageNet Large Scale Visual Recognition Challenge. International Journal of Computer Vision 115. doi:10.1007/s11263-015-0816-y
- PyTorch / torchvision (2024).Models and pre-trained weights. pytorch.org. pytorch.org
