Handwriting Reader
Write a digit or a PIN code and a neural network reads it. Or train one yourself.
Write a digit
WaitingReal handwriting from the MNIST test set, which the pretrained network never trained on.
What the network reads
PretrainedWaiting for a digit
Write in the pad, or pick a sample. It reads as you write.
- 0–
- 1–
- 2–
- 3–
- 4–
- 5–
- 6–
- 7–
- 8–
- 9–
Couriers still sort parcels by PIN codes written by hand. Write one digit in each box, as on a delivery slip: the network reads all six and points to the digit a person should check before the parcel goes to the wrong city.
empty
empty
empty
empty
empty
empty
PIN code read
––– –––
Write in the boxes, or write the sample PIN.
The layers, from your digit to the answer
- Input28 × 28 × 1
Your digit, cropped, scaled to 20 px and centred, as MNIST's digits were.
- Convolution: 16 filters, 3 × 328 × 28 × 16 · 160 weights
Slides 16 small patterns over the image; each map lights up where its pattern appears.
- Max pooling, 2 × 214 × 14 × 16
Keeps the strongest response in each 2 × 2 patch, so the maps shrink to 14 × 14 and small shifts matter less.
- Convolution: 32 filters, 3 × 314 × 14 × 32 · 4,640 weights
Combines the earlier maps into 32 larger parts: strokes, curves, corners and loops.
- Max pooling, 2 × 27 × 7 × 32
Keeps the strongest response in each 2 × 2 patch, so the maps shrink to 7 × 7 and small shifts matter less.
- Flatten1568
Lays the maps out as one list of 1,568 numbers.
- Dense: 64 units64 · 100,416 weights
Each of the 64 units weighs all the evidence for a combination of parts.
- Dropout, 25%64
Switches off 25% of the units at random during training, so no single unit is relied on. Off when reading.
- Output: 10 digits10 · 650 weights
One score per digit, turned into probabilities that add up to 100%.
105,866 weights in all, 423 kB as 32-bit numbers.
The pretrained network's first layer: 16 filters and what they find
Each small square is a 3 × 3 filter the network learnt by itself from the training digits: violet where it wants ink, amber where it wants none. Under it is that filter's map of your digit: the brighter, the stronger the match. Some light up along whole strokes, others only along edges facing one way.
The filters appear once the network has loaded.
Train a fresh copy of the same network, starting from random weights, on handwriting from MNIST. It runs on your graphics chip through WebGL, in small steps so the page stays usable.
Waiting for TensorFlow.js…
The charts fill in batch by batch.
Loss is how wrong the network is on each batch of 64 digits; it should fall. Accuracy is the share it gets right; it should climb. The 7,000 digits (1.2 MB) download when you start.
Measured on MNIST's 10,000 test digits, which the network never trained on, by the script that trained it. It went through the 60,000 training digits 15 times, each time shifted, turned, slanted and thickened a little at random, in 9 min 48 s on a laptop's graphics chip.
Which digits it confuses
Rows: the digit written. Columns: what it read. The diagonal is right.
| Written ↓ · Read → | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 977 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 1 |
| 1 | 0 | 1129 | 1 | 0 | 2 | 0 | 1 | 1 | 1 | 0 |
| 2 | 1 | 0 | 1029 | 0 | 0 | 0 | 0 | 2 | 0 | 0 |
| 3 | 0 | 0 | 0 | 1004 | 0 | 3 | 0 | 1 | 2 | 0 |
| 4 | 0 | 0 | 0 | 0 | 980 | 0 | 0 | 0 | 0 | 2 |
| 5 | 1 | 0 | 0 | 5 | 0 | 883 | 2 | 0 | 0 | 1 |
| 6 | 2 | 3 | 0 | 0 | 2 | 0 | 949 | 0 | 2 | 0 |
| 7 | 0 | 1 | 5 | 1 | 0 | 0 | 0 | 1017 | 1 | 3 |
| 8 | 0 | 0 | 2 | 1 | 0 | 1 | 2 | 1 | 965 | 2 |
| 9 | 0 | 0 | 0 | 0 | 4 | 2 | 0 | 2 | 3 | 998 |
The commonest slips: a 5 read as 3 (5 times), a 7 read as 2 (5 times) and a 9 read as 4 (4 times).
The first 12 test digits it got wrong
Some of these would make a person pause too. Under each: what was written, what it read and how sure it was.
Test accuracy after each pass
Epochs along the bottom. The learning rate falls in later passes, so the last steps are small refinements.
The pretrained network learnt from MNIST's 60,000 training digits. Everything runs in this browser tab; nothing you write leaves it.
Things to try
- Draw a 7 with a bar through it, then without.
- Write a PIN code and see which digit it asks you to check.
- Train your own network, then read the same digit with both.
Where I'd use it
- PIN codes on parcels
- Cheque amounts
- Meter readings
Want this on your own data? Tell me about your project.