Skip to content

How it's builtDeep learning

Handwriting Reader

A small convolutional neural network reads handwritten digits in your browser with TensorFlow.js. I trained it on MNIST's 60,000 training digits; it reads 99.31% of the 10,000 test digits correctly. You can also train your own copy, live, and watch it learn.

The problem

Couriers, banks and utilities still read digits written by hand: PIN codes on parcels and delivery slips, amounts on cheques, readings on meter cards. Keying them in is slow and dull, and one misread PIN code sends a parcel to the wrong city.

A network that reads each digit and says how sure it is does most of the keying, and sends a person only the digits worth checking. The PIN code tab works that way: it reads all six boxes, then names the digit to check.

The data

MNIST, by Yann LeCun and Corinna Cortes: 70,000 handwritten digits of 28 × 28 pixels, 60,000 for training and 10,000 for testing, written by American Census Bureau staff and secondary-school students. It is licensed CC BY-SA 3.0; I downloaded it from a public mirror.

MNIST's digits were scaled to fit a 20 × 20 box, keeping their shape, then centred by their centre of mass in the 28 × 28 field. Whatever you write goes through the same steps before the network sees it. Getting that right matters more than any change to the network.

For training in your browser I packed 6,000 training digits (600 of each) and 1,000 test digits (100 of each), picked at random with a fixed seed, into one 1.2 MB PNG image and a list of labels. They download only when you start training.

The model

A convolutional neural network with 105,866 weights: two convolution layers of 16 and 32 filters, each 3 × 3 and each followed by max pooling, then a dense layer of 64 units, dropout, and ten outputs, one per digit. Convolutions suit images: the same small filters slide over the whole digit, so a stroke is recognised wherever it falls. The "Inside the network" tab shows the first layer's filters at work on your digit.

Training. I trained it with TensorFlow.js inside a browser, on an integrated laptop graphics chip through WebGL: 15 passes over the 60,000 training digits in batches of 128, with the Adam optimiser and a learning rate that falls in the later passes. It took about 10 minutes.

Augmentation. Before each pass, every training digit was shifted by up to 2.5 pixels, turned by up to 12°, resized and slanted a little, and sometimes made bolder or lighter, at random. MNIST's digits are neat; digits drawn with a mouse or a finger are not, and the distortions teach the network to cope.

Your own run trains the same network from random weights on the browser's digits: 6,000 digits for 2 passes to start with, in batches of 64, one batch per animation frame, so the page stays responsive and the charts update as it learns.

The pipeline

  1. 01
    Write

    Your strokes are kept as points and drawn as thick, smooth ink into a 280 × 280 grid, the same on any screen.

  2. 02
    Crop and scale

    Cropped to the ink and scaled so the longer side is 20 pixels, keeping the digit's shape.

  3. 03
    Centre

    Placed in a 28 × 28 field with its centre of mass in the middle, as MNIST's digits were.

  4. 04
    Find strokes

    16 filters find edges and strokes; 32 more combine them into curves, corners and loops.

  5. 05
    Decide

    A dense layer weighs the evidence, and ten outputs give a probability for each digit.

  6. 06
    Check

    In a PIN code, the least sure digit is flagged for a person when the network is under 90% sure.

Results

Test accuracy
99.31%
69 mistakes in 10,000 test digits
Distorted test digits
98.9%
93.2% when trained without distortions
Network size
423 kB
105,866 weights
A visitor's first run
96.1%
6,000 digits, 2 passes, 22 s

Measured by the script that trained the network, with the same code the demo runs, on MNIST's 10,000 test digits (the visitor's run on the browser's 1,000). The training time is for an integrated laptop graphics chip; a phone takes longer, and a browser without WebGL much longer.

For a client

For a client I would train on their own paperwork: slips scanned or photographed in their depots, labelled by the people who key them in today. Their pens, paper and handwriting differ from MNIST's, and that difference matters more than the network.

A real slip needs more than one digit read: finding the PIN code box, splitting it into digits (or reading the whole sequence with a network built for that), and checking the result against the list of valid PIN codes, which catches many misreads on its own.

Digits under a confidence threshold go to a person, and every correction becomes new training data. I would track the share of slips that need a person, by depot and by month, and retrain when it creeps up.

The network is 423 kB, small enough to run on a phone or a scanner in the depot, offline, as it runs here.

Stack

PartWhat it does hereLicence
TensorFlow.jsRuns the network in your browser on WebGL (or the processor), trains your own copy, and trained the pretrained oneApache 2.0
MNISTHandwritten digits by Yann LeCun and Corinna Cortes: all 60,000 training digits for the pretrained network, 7,000 packed for your own runCC BY-SA 3.0
My CNNThe pretrained network: 105,866 weights, served from this siteMine
My preprocessing and training codeTurns strokes into MNIST-style input, distorts training digits, packs and reads the training image, and runs the training loop; TypeScript, unit-testedMine
PreactThe interactive demoMIT
AstroThe page, built as static HTMLMIT

Privacy

Everything happens in your browser. What you write is never uploaded: the network reads it on your device, and your own training run happens there too.

The page fetches TensorFlow.js and the 423 kB network from this site when the demo scrolls into view, and the training digits only if you start training.

Limits

  • It reads one digit per box. Joined-up numbers, letters and whole forms need more: finding and separating the digits first.
  • It always answers with one of ten digits, even for a scribble or a letter. Its confidence is the only warning, and it can be confidently wrong.
  • MNIST is American handwriting, so styles that are rare in it, such as a 1 with a long flag, read less reliably.
  • Your own run sees far fewer digits, far fewer times and without distortions, so expect it to score a few points below the pretrained network and to be thrown more easily by writing that does not look like MNIST's.