← All projects

Case study · Computer vision

  • Image classification
  • CNN & transfer learning
  • Vision Transformers

Who is in this photo?

3,023 news photos of 62 public figures. Three generations of computer vision answer the same question under the same rules: hand-designed edge features, a CNN that learns its own, and a network pretrained on a million images, then fine-tuned. Then a look inside how a Vision Transformer reads a face.

62people to tell apart
17.6%score from always guessing the most common face
86.8%test accuracy, fine-tuned MobileNetV2

Step 01 · Faces as numbers

Every face is 35,250 numbers.

01 · aLook before you train

Each photo is a 125 × 94 grid of pixels with three colour channels: a (125, 94, 3) tensor of values between 0 and 1. The people are far from equal: one man has 530 photos, the rarest have 20. A model that always answers “George W Bush” scores 17.6%, which is why the main score here is macro-F1, where every person counts the same.

Photos per person · the ten most common

George W Bush530
Colin Powell236
Tony Blair144
Donald Rumsfeld121
Gerhard Schroeder109
Ariel Sharon77
Hugo Chavez71
Junichiro Koizumi60
Jean Chretien55
John Ashcroft53

52 more people have between 20 and 52 photos each.

A stratified 70 / 15 / 15 split

Training2,116
Validation453
Test (touched once)454

All 62 people appear in every split, but that leaves only about 34 training photos per person, and as few as 14. The rarest people have just 3 photos to be tested on, so one mistake moves their score by 33 points.

01 · bHand-designed features

Edges carry identity. Fixed rules don't adapt.

Classical vision describes a face with rules a person chose. Grayscale, contrast equalisation, blur, Sobel gradients and Canny edges show where identity lives: around the eyes, nose, mouth and jaw. They also show the weakness: Canny's fixed thresholds draw a rich sketch of one face and almost nothing of another.

Three faces shown six ways: colour, grayscale, equalised, blurred, Sobel gradient magnitude and Canny edges; Canny finds few edges on the first face
Six views of three training faces, from the notebook.
Pixel values per face35,250
HOG gradient features5,040

HOG (histograms of edge directions in 8 × 8 cells) shrinks each face 7×. A linear SVM on top reached 75.3% on validation in 36 seconds on a CPU: about 4× the majority baseline, before any deep learning.

Step 02 · Let the model learn the features

405,790 parameters, learned from scratch.

A CNN learns its own filters instead of using hand-picked ones. Mine has four convolution blocks (32 → 64 → 128 → 256 filters, each with batch normalisation and pooling), global average pooling and dropout. With so few photos per person, it also needs augmentation: random flips, small rotations, shifts, brightness and contrast changes, applied to training images only.

Run:real epochs

Solid: training accuracy (augmentation and dropout on, so it understates). Dashed: validation. Hover to read any epoch.

84.1%validation accuracy, 95.6% top-5
0.786validation macro-F1, against HOG's 0.685
126best epoch of 141; early stopping restored it
385 straining time on a GPU, 10× HOG's
The shaky start

For the first eight or so epochs, validation accuracy sat near 2% while training improved, and validation loss spiked to almost 6. The likely culprit is batch normalisation: its running statistics lag behind fast-changing weights. Seven learning-rate cuts later the curves settled. A small min_delta on early stopping would have saved the last 70 nearly flat epochs; the next model uses one.

Step 03 · Borrow what's already learned The hard part

Pretrained alone wasn't enough.

MobileNetV2 has already learned edges, textures and shapes from over a million ImageNet photos. First I froze all of it and trained only a new 62-way head. It was worse than my own small CNN. Flip the switch to see what fine-tuning the top 40 layers did.

69.1%

validation accuracy · 79,422 trainable parameters

Generic ImageNet features (cats, cars, textures) can't tell two grey-suited men apart.

Mean recall by how many training photos a person had (test set)

Rare 14–18 · HOG58.7%
Rare · CNN65.6%
Rare · MobileNetV275.4%
Medium 19–26 · HOG60.6%
Medium · CNN78.4%
Medium · MobileNetV275.3%
Common 27+ · HOG78.2%
Common · CNN86.4%
Common · MobileNetV291.7%

Pretraining helped most where data was thinnest: +10 points over the CNN on the rarest people.

Not better everywhere

Against the CNN, fine-tuned MobileNetV2 was better on 24 people, worse on 16 and the same on 22. The average gain hides that churn. It also overfits more: 99% training accuracy against 88% on validation, because 1.7 million trainable parameters can memorise 2,116 photos. Early stopping with a minimum improvement caught it at epoch 27 of fine-tuning.

Step 04 · How a Vision Transformer sees

Cut the face into words. Let every word look at every other.

04 · aPatches and attention, by hand

A ViT doesn't slide filters. It cuts the image into 16 × 16 patches, flattens each into a token, and lets every token score every other one. I did it by hand on one face: 48 tokens of 768 values, a 48 × 48 attention matrix whose rows each sum to exactly 1.

A face resized to 128 by 96 pixels with an 8 by 6 grid of numbered 16-pixel patches
48 patch tokens from one face.
A 48 by 48 attention matrix with a bright diagonal, and overlays showing which patches a forehead patch and a mouth patch attend to
Hand-computed attention: the bright diagonal is each patch matching itself. With identity projections this shows the mechanism, not learned behaviour.
Why I didn't train one

ViT-Base/16 has 85.8 million parameters: 211 times my CNN and 37 times MobileNetV2. With 2,116 training photos it would have nothing to learn from. Knowing when not to use the fashionable model is part of the job.

04 · bThe cost of attention

Halve the patch, 16× the work.

Every token attends to every token, so the work grows with the square of the token count. For a standard 224 × 224 image in ViT-Base (12 layers × 12 heads), pick a patch size:

197tokens (patches + 1 class token)
38,809attention scores per head
5,588,496scores per image, all 144 heads

Pure arithmetic: tokens = (224 ÷ patch)² + 1, and each head scores tokens × tokens pairs.

Attention rollout from a pretrained ViT-Base on five faces: hot spots on eyes, nose and lips, plus bright spots in image corners
A pretrained ViT's attention on five validation faces: partly on eyes, nose and lips, partly on empty corners. An illustration, not proof of what it understands.

Step 05 · The final exam

Probably better. Not proven better.

05 · aOne look at the test set

The 454 test photos played no part in training, early stopping or choosing models. Each finished model was scored on them once. The bar is test accuracy; the tick is the validation score it predicted.

Sort:
    test accuracy (60–95% scale)validation accuracy

    Every model scored within 1.3 points of its validation estimate, so choices made on the validation set didn't overfit it.

    05 · bIs the gap real?

    Resample the test set 1,000 times.

    A 4-point lead on 454 photos could be luck. Bootstrapping redraws the test set with replacement 1,000 times and records each difference. An interval that crosses zero means the lead can't be told apart from noise.

    MobileNetV2 − HOG · accuracy+11.2 [7.0, 15.6]
    CNN − HOG · accuracy+7.3 [2.9, 11.7]
    MobileNetV2 − CNN · accuracy+4.0 [0.2, 7.7]
    MobileNetV2 − CNN · macro-F1 ×100+4.0 [−1.2, 10.2]
    −50+5+10+15+20

    Points of difference with 95% bootstrap intervals. Both neural networks clearly beat HOG. MobileNetV2's accuracy lead over the CNN only just clears zero, and its macro-F1 lead doesn't.

    Ten test faces MobileNetV2 got wrong, each with true and predicted name and a confidence between 93 and 100 percent
    The ten most confident mistakes: 93% to 100% sure, and wrong. A closed-set model has no way to say “I don't know”.
    • Learned beats handmade

      Both neural networks beat HOG by 7 to 11 points, with intervals well clear of zero.

      The clear result
    • Transfer learning, modestly

      Best on every metric and on the rarest people, trained faster than the CNN, but 5.8× larger, and its lead isn't proven.

      The pick
    • Data is the real limit

      About 34 photos per person caps every model. Possible near-duplicate news photos across splits may flatter all three scores.

      The caveat

    The lesson

    A better number isn't a better model until the interval says so.

    Python · TensorFlow / Keras · scikit-learn · scikit-image · OpenCV · PyTorch + Hugging Face (ViT). Labeled Faces in the Wild (Huang et al., 2007).