Skip to content
AI360XpertBeta

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.

A backbone turns pixels into features, global pooling squeezes them to one vector, and a linear layer plus softmax picks one class with logits 2.0 1.0 0.1
A backbone turns pixels into features, global pooling squeezes them to one vector, and a linear layer plus softmax picks one class with logits 2.0 1.0 0.1

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 2626 percent to 1616 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 H×W×3H \times W \times 3 into features h×w×Ch \times w \times C, 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 CC-vector, making the head resolution-independent with far fewer weights than flattening. A linear layer maps it to KK logits zz, and softmax gives p^k=ezk/∑jezj\hat{p}_k = e^{z_k} / \sum_j e^{z_j}.

Training minimizes cross-entropy L=−∑kyklog⁡p^k=−log⁡p^y∗L = -\sum_k y_k \log \hat{p}_k = -\log \hat{p}_{y^*} for true class y∗y^*, 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 z=[2.0,1.0,0.1]z = [2.0, 1.0, 0.1] for three classes with truth class 00. Exponentials are e2.0≈7.389e^{2.0} \approx 7.389, e1.0≈2.718e^{1.0} \approx 2.718, e0.1≈1.105e^{0.1} \approx 1.105, summing to 7.389+2.718+1.105=11.2127.389 + 2.718 + 1.105 = 11.212. Probabilities are 7.389/11.212≈0.6597.389 / 11.212 \approx 0.659, 2.718/11.212≈0.2422.718 / 11.212 \approx 0.242, 1.105/11.212≈0.0991.105 / 11.212 \approx 0.099, summing to 1.01.0. Loss is −log⁡(0.659)≈0.417-\log(0.659) \approx 0.417, and top-1 picks class 00 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 0

Probabilities [0.659,0.242,0.099][0.659, 0.242, 0.099] give loss 0.4170.417 with top-1 class 00, 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 9090 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 [2.0,1.0,0.1][2.0, 1.0, 0.1] give [0.659,0.242,0.099][0.659, 0.242, 0.099] with loss 0.4170.417.
  • AlexNet cut ImageNet top-5 error from 2626 percent to 1616 percent in 2012.
  • Transfer from pretrained weights beats training from scratch on small new domains.