Convolutional neural networks (CNNs): from pixels to a digit
Build a convolutional network for MNIST digits. How do learned filters and pooling turn pixels into a vote?
Builds on More inputs, more neurons.
A sorting office gets thousands of handwritten postcodes every hour. A person reads a sloppy 7 or a 1 with a flag on top without thinking. We want a machine to do it. Last time, the network took one number: the month. Now it gets a picture.
What the machine sees
To a computer, a picture is a grid of pixels, each a number for its brightness. A handwritten digit from the MNIST data set (LeCun, Cortes and Burges, 1998) is 28 by 28: 784 numbers. Move across it.
How do we show it to a network?
A network takes numbers in, and a picture is 784 numbers. So the simplest idea: make every pixel one input, laid out in a line, and give the neuron a weight for each.
Where the simplest idea breaks
Digits are labels, not amounts. A single-number answer treats them as points on a line, so a picture caught between a 4 and a 9 might get the answer 6.5. Slide it.
The answer is not a digit. A classifier should score each possible label instead; its error measure should treat every wrong class as wrong, rather than ranking mistakes by numerical distance.
One score per digit
So change the question. Instead of “which number?”, ask ten questions: “how much does this look like a 0?”, “a 1?”, and so on. One neuron per digit, each with weights for every pixel. The biggest score wins, and no digit is nearer to any other.
More neurons, more features
To read digits well, we want many questions: is there a stroke along the top, a loop, a diagonal? Each neuron can learn a different feature. Let us count what that costs.
More neurons means more features, but every neuron looks at every pixel, so each one brings 784 weights of its own. The cost explodes: a grid of 26 by 26 neurons needs over half a million weights, for one layer, and this is only a tiny 28 by 28 picture.
Idea one: look locally
A stroke or corner is visible in a few neighbouring pixels. Let each neuron look at a 3 by 3 patch. Hover over the neurons to see each patch.
Each neuron now needs nine weights instead of 784, but every position still learns its own edge detector.
Idea two: share the weights
An edge is an edge wherever it is. Share the same nine weights across positions and slide them over the picture.
One filter instead of 676 sets of weights: from 529,984 weights down to nine, plus a bias.
One filter, slid across the picture
Those shared weights are a filter, a tiny pattern detector. Sliding it across the digit is a convolution; its results form a feature map. A ReLU keeps positive results. Try a filter or enter your own nine numbers.
More filters, more features
One filter asks one question. Run several over the same picture and stack their feature maps. Add the eight filters learned by the digit reader one at a time.
Eight filters cost only 8 × 10 = 80 weights. A single neuron of the first design cost 785.
Then shrink
The block now has 5,408 numbers, most saying “nothing here”. Max-pooling shrinks it by keeping the biggest number in each 2 by 2 block.
Stack another round
Repeat with filters on the pooled maps: the next layer can find curves, corners and crossings from the edges below.
Flatten, then the vote
Finally, flatten the maps into a list. Ten neurons, one per digit, each score the 400 values.
Each neuron gives a raw score, which can be any size. To turn the ten scores into probabilities that add up to 1, we use softmax (John Bridle, 1990):
Softmax raises each score to a power, then divides by the total so the probabilities add to 1. Drag the scores.
Only the gaps between scores matter. Training is the same loop as ever: guess, measure, send the error back, nudge.
Try it
The reader has 5,258 weights and biases and gets about 98.9% of 10,000 unseen digits right. Draw a digit and watch it go through every layer.
Try drawing a letter. It still names a digit, often confidently. It has only ever seen digits, so every picture is one of ten.
Where this comes from
LeNet-5 (LeCun et al., 1998) read digits with convolutions and pooling. The data set, MNIST, comes from the same group. The softmax is John Bridle’s (1990).
Next
Convolutions build in two useful assumptions: nearby pixels belong together, and a pattern means the same wherever it appears. The final vote works for words too: a language model scores its vocabulary, then uses softmax to estimate the next word.
Next: the same idea on colour photographs and a thousand classes, in classifying images.
Sources: LeCun, Bottou, Bengio and Haffner, “Gradient-based learning applied to document recognition” (1998); Bridle, “Probabilistic interpretation of feedforward classification network outputs” (1990). The digit reader is a small network trained on MNIST; the sizes are worked out from its layers.