Optical Character Recognition
Draw a digit and watch a neural network I wrote from scratch in NumPy read it in real time.
- Python
- NumPy
- Flask
- MNIST
Draw a digit, 0 to 9
What the network sees
Your drawing, shrunk to 28 × 28 pixels
Its answer
? Loading the network…
This demo runs in your browser, using the weights from my trained model. Draw in the middle of the pad for the best results.
This is a handwritten digit recognizer you can try at the top of this page. You draw a digit from 0 to 9, and a neural network guesses what it is while you are still drawing. The network is written from scratch in NumPy. There is no TensorFlow or PyTorch involved: the forward pass, the loss, the gradients and the weight updates are all implemented by hand.
The network
The model is a fully connected network with one hidden layer. A 28 × 28 pixel image is flattened into 784 inputs, passed through 300 hidden neurons with ReLU activation, and reduced to 10 outputs. A softmax turns those outputs into a probability for each digit.
Training uses stochastic gradient descent, one image at a time, on the 60,000 images in the MNIST training set. For each image the network runs a forward pass, measures its error with cross-entropy loss, works out the gradient of every weight and bias through backpropagation, and takes a small step in the direction that reduces the error. The learning rate follows a schedule, and the model is checked against the separate MNIST test set as it trains. When training finishes, the weights are saved to a file so the web app can load them without retraining.
On the 10,000 test images the network has never seen, it reads 97.5% correctly.
From model to web app
The trained model is served by a small Flask app deployed on Render. The page has three panels: the drawing canvas, a view of what the network sees, and the prediction.
A few times a second, the browser shrinks the drawing down to 28 × 28 pixels and sends it to the server. The server converts it to grayscale, inverts it so it matches MNIST’s white-on-black format, and runs it through the network. The response contains the probability for all ten digits, which the page draws as a bar chart. If the network is less than 50% sure of its top guess, the page says the digit is unknown instead of guessing.
The “what the network sees” panel shows the 28 × 28 image that was actually sent. It makes the model’s behaviour easier to understand: a digit that looks clear at full size can lose a lot of detail at that resolution, and seeing the real input helps explain a wrong guess. Drawing works with both mouse and touch, so the demo is usable on a phone.
The demo on this page works the same way without the server. The same trained weights are loaded into the browser, and the same calculation runs in JavaScript, so the answer updates the moment you draw.
What I learned
- A neural network is not magic. It is multiplication and addition repeated many times, and “learning” is nudging numbers until the answers get better.
- The input has to look like what the network was trained on. That is why the drawing is shrunk and its colours are flipped before the network sees it.
- Showing what the network sees is the quickest way to understand a wrong answer.
- A model is much more fun to share once people can try it for themselves.