MNIST Digit Recognition
Handwritten digit recognition with a CNN trained using Java DJL and PyTorch. An interactive demo running entirely in the browser with ONNX Runtime Web.
Try It
Test it directly in your browser. No backend is required.
Open demoHow It Works
The model was trained on 60,000 MNIST images using Java’s DJL (Deep Java Library) with a PyTorch backend. It was then exported to ONNX format (1.6 MB). The demo runs entirely in the browser using ONNX Runtime Web and WebAssembly.
Preprocessing steps
- Read a 280×280 image from the canvas.
- Crop the drawing to its bounding box.
- Resize it to fit within 20×20 pixels.
- Center it on a 28×28 canvas, matching MNIST’s format.
- Normalize it with mean 0.1307 and standard deviation 0.3081.
- Pass a tensor of shape [1, 1, 28, 28] to the model.
- Apply softmax to obtain a probability distribution.
Model Architecture
- Two convolutional layers with 32 and 64 filters.
- Max pooling and dropout.
- Two fully connected layers.
- Softmax output over ten classes, digits 0 through 9.