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, automated image classification required hand-crafted features: SIFT descriptors, HOG features, Gabor filters. These required domain expertise, extensive tuning, and still plateaued in accuracy. AlexNet's 2012 ImageNet victory — reducing top-5 error from 26% to 16% using a deep CNN trained on GPUs — showed that networks could learn features directly from data.
Today image classification is the foundational vision task: a solved benchmark and the starting point for transfer learning into every downstream vision problem.
Think of It Like This
Sorting mail by the picture on the envelope
A postal worker sorts envelopes by glancing at the stamp: a bird stamp goes to the ornithology society, a landscape goes to the travel bureau. They don't analyze every pixel — they recognize patterns instantly. Image classification teaches a neural network to do the same: build a hierarchy of features (edges → textures → shapes → objects), then output a class label.
How It Actually Works
The standard architecture
A CNN classifier has three parts:
1. Feature extractor (CNN backbone): A stack of convolution + ReLU + pooling layers transforms an input image of shape into a feature map of shape . Each layer learns increasingly abstract patterns. Early layers detect edges; middle layers detect textures and parts; deep layers detect object-level structures.
2. Global average pooling (GAP): Reduces the spatial feature map to a vector of length by averaging each channel over spatial dimensions. Makes the classifier resolution-independent and reduces parameter count dramatically versus a flat fully-connected layer.
3. Classifier head: A single linear layer maps the -dimensional feature vector to class logits. A softmax converts logits to a probability distribution: .
Training objective
Cross-entropy loss between the predicted distribution and the one-hot label :
Where is the true class index. Minimizing this is equivalent to maximizing the log probability of the correct class.
Evaluation metrics
- Top-1 accuracy: The highest-probability class matches the label.
- Top-5 accuracy: The true label is among the five highest-probability predictions. Used on ImageNet because multiple labels can be valid for an image (a photo of a labrador is also a dog).
Transfer learning
Training a ResNet-50 on ImageNet from scratch takes days on 8 GPUs. Most practitioners download pre-trained weights and fine-tune:
- Feature extraction: freeze the backbone, train only the new head.
- Fine-tuning: unfreeze the last few backbone layers and train with a small learning rate.
This works because early CNN layers learn universal features (edges, textures) that transfer across domains — medical images, satellite imagery, product photos.
Code
import torchfrom torchvision import models, transformsfrom PIL import Image
# Load a pre-trained ResNet-50 (ImageNet weights)model = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V2)model.eval()
# Standard ImageNet preprocessingpreprocess = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),])
# Load and classify an imageimage = Image.open("dog.jpg").convert("RGB")input_tensor = preprocess(image).unsqueeze(0) # add batch dimension: [1, 3, 224, 224]
with torch.no_grad(): logits = model(input_tensor) probs = torch.softmax(logits, dim=1)
# Top-5 predictionstop5_probs, top5_idx = probs.topk(5)print("Top-5 predictions:")for prob, idx in zip(top5_probs[0], top5_idx[0]): print(f" Class {idx.item():>4}: {prob.item():.3f}")
# Fine-tuning for a custom dataset (binary: cat vs dog)num_classes = 2model.fc = torch.nn.Linear(model.fc.in_features, num_classes) # replace head
# Freeze backbone, train only the new headfor param in model.parameters(): param.requires_grad = Falsefor param in model.fc.parameters(): param.requires_grad = True
optimizer = torch.optim.Adam(model.fc.parameters(), lr=1e-3)Watch Out For
Shortcut learning and texture bias
CNNs trained on natural images learn texture shortcuts rather than shape features. A classifier can be fooled by adversarial examples that change textures imperceptibly to humans but dramatically to the model. Evaluate on augmented and out-of-distribution data to detect shortcut reliance before deployment.
Label distribution shift at test time
A classifier trained on 90% cats and 10% dogs will be confidently miscalibrated when deployed on a balanced dataset. The softmax probabilities are not reliable confidence scores out-of-the-box. Use temperature scaling or Platt scaling to calibrate predictions on the actual deployment distribution.
The Quick Version
- Image classification assigns one class label to a fixed-size image from a predefined set.
- Architecture: CNN feature extractor → global average pooling → linear + softmax.
- Training minimizes cross-entropy loss; evaluation uses top-1 or top-5 accuracy.
- AlexNet (2012) launched the deep learning era; ResNet (2015) solved the vanishing gradient problem that blocked very deep networks.
- Transfer learning from ImageNet pre-trained weights dramatically reduces training time and data requirements for new domains.