Image Classification
Given a fixed-size image, predict which class it belongs to from a predefined set, the task that launched the deep learning revolution when AlexNet cut the ImageNet error rate nearly in half in 2012.
Why Does This Exist?
Before 2012, classification needed hand-built descriptors with domain tuning that plateaued in accuracy. AlexNet's 2012 ImageNet win cut top-5 error from percent to percent with a deep CNN on GPUs, showing networks could learn features from data. The task is now the foundation of vision: a solved benchmark and the source of pretrained weights for object detection and every downstream head.
This page covers the classifier, its loss, and its metrics. Data practices live in ImageNet, localization in object detection, and per-pixel labels in semantic segmentation.
Think of It Like This
Sorting mail by the picture on the envelope
A postal worker glances at each stamp and routes it: bird stamp to the ornithology society, landscape to the travel bureau. They do not measure every millimeter of ink, they recognize stacked patterns at a glance. A classifier does the same with learned layers from edges to textures to shapes, then one label out.
Where it stops: the worker fails on an envelope with two stamps of equal size, just as one-label classification fails on multi-object scenes that need detection or multi-label heads instead.
How It Actually Works
A CNN backbone turns input into features , with early layers reading edges, middle layers textures and parts, and deep layers object structures. Global average pooling averages each channel over space into one -vector, making the head resolution-independent with far fewer weights than flattening. A linear layer maps it to logits , and softmax gives .
Training minimizes cross-entropy for true class , which maximizes the log probability of the right answer. Evaluation uses top-1 accuracy plus top-5 on ImageNet, where several labels can be valid. Pretrained backbones transfer because early layers learn universal edges and textures, so new tasks freeze the backbone and train a head, then unfreeze late layers at a small learning rate.
Worked example
Take logits for three classes with truth class . Exponentials are , , , summing to . Probabilities are , , , summing to . Loss is , and top-1 picks class correctly.
Code
Softmax plus cross-entropy with only the standard library:
import math
logits = [2.0, 1.0, 0.1]truth = 0exps = [math.exp(z) for z in logits]total = sum(exps)probs = [e / total for e in exps]loss = -math.log(probs[truth])print(f"probs {[round(p, 3) for p in probs]}, loss {round(loss, 3)}")# -> probs [0.659, 0.242, 0.099], loss 0.417print(f"top1 {max(range(len(probs)), key=lambda k: probs[k])}")# -> top1 0Probabilities give loss with top-1 class , matching the hand computation.
Watch Out For
Trusting texture shortcuts and raw softmax
Symptom: high validation accuracy collapses on new backgrounds, because the network keyed on textures instead of shape and the softmax reads as confidence it is not. Fix: test on augmented and out-of-distribution splits, and calibrate with temperature scaling on the deployment mix before shipping scores.
Ignoring label shift between train and serving
Symptom: a model trained on percent cats stays overconfident on dogs when the serving mix turns balanced, since the head absorbed the training prior. Fix: reweight or recalibrate on the deployment distribution and monitor per-class rates instead of one global number.
The Quick Version
- Image classification assigns one label per fixed-size image from a predefined set.
- The stack is backbone features to global pooling to linear logits to softmax.
- The fixture logits give with loss .
- AlexNet cut ImageNet top-5 error from percent to percent in 2012.
- Transfer from pretrained weights beats training from scratch on small new domains.