MNIST Digit Recognition

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.

Visit project
JavaDJLPyTorchONNXReactMachine Learning

Try It

Test it directly in your browser. No backend is required.

Open demo

How 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

  1. Read a 280×280 image from the canvas.
  2. Crop the drawing to its bounding box.
  3. Resize it to fit within 20×20 pixels.
  4. Center it on a 28×28 canvas, matching MNIST’s format.
  5. Normalize it with mean 0.1307 and standard deviation 0.3081.
  6. Pass a tensor of shape [1, 1, 28, 28] to the model.
  7. 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.

Commands

Pages

Links