borch

Tutorial · 4

Image classifier

A real dataset this time: CIFAR-10, ten classes of 32×32 photographs. Look at the images, train a small convolutional network on your own GPU, and read the accuracy per class rather than as one number.

This tutorial needs data. The subset is not in the repository — binaries stay out of git here, the same as vendor/pyodide. Build it once with python3 site/fetch_data.py (add --download if you do not already have the CIFAR binaries). It is 2,000 training and 500 test images, JPEG-compressed to about 1MB, so the pixels are not bit-identical to the original — read the accuracy below as "does it learn", not as a number to compare against a paper.

1 · Look at the data first

Before any model. Half of what goes wrong in a vision project is visible here — wrong channel order, wrong scaling, labels off by one.

2 · A small convolutional network

Two convolution-pool stages and a linear head — the same shape as the CNN in lesson 5, sized for three input channels. Bigger networks do better on CIFAR; this one is chosen to train while you watch.

3 · Train it

Batches of 64 from a DataLoader, Adam, cross entropy. Watch the loss curve rather than the numbers — what you want to see is a slope, and if it goes flat at the top the learning rate is the first thing to change.

4 · Accuracy, per class

One number hides which classes the model cannot tell apart. Cats and dogs are the usual pair; at this size ships and planes often are too, because both are a small object on a large flat background.

5 · The mistakes

Sixteen of them, with what the model said. This is the same move as the quickstart and it stays useful at every size — a model's errors tell you more about the data than its successes do.

What this does not show. Ten epochs on 2,000 images is not a CIFAR result; the repository's own benchmark trains ResNet-18 on the full set and reports 64.6% after ten epochs with augmentation. What you just measured is that the whole chain — data, convolution, autograd, optimizer — works in a browser tab, on your GPU, with nothing installed.