13 / 15 PROJECT

Every project

MNIST From-Scratch Image Classifier

A handwritten digit pipeline written from scratch in Python: a multi-class SVM, a KNN, the preprocessing and the dimensionality reduction, on NumPy and SciPy rather than sklearn.

Read the source

The MNIST set visualised: a grid of white handwritten numerals on black, one small greyscale image per digit. This is the input the pipeline was built around.
99.8%
accuracy on noisy test data
95%
variance kept through PCA
2
classifiers written from scratch

Two classifiers, written by hand

The task is MNIST, the standard set of handwritten digits, and the pipeline attacks it twice. The first classifier is a multi-class support vector machine assembled one-vs-all from ten binary machines, trained by mini-batch gradient descent on a hinge loss. The second is k-nearest neighbours, which trains not at all and measures distance to everything it has already seen. Both are Jack's own code, on NumPy and SciPy.

Before the classifier sees anything

Most of the work happens before the classifier sees anything. Each digit is threshold-masked to separate ink from background and blurred with a Gaussian kernel, and the training set is enlarged with rotations, flips and added noise, so damaged digits are seen in training rather than first met at test time. PCA then cuts the dimensionality, keeping 95% of the variance.

There is no library underneath to upgrade, so whatever closed that gap was written.

The climb to 99.8

The recorded progression runs from 70% to 99.8%, and the headline figure is quoted on noisy test data rather than clean, which is the harder of the two claims. There is no library underneath to upgrade, so whatever closed that gap was written. Models are persisted and scored through an evaluation pipeline, and the code carries full type annotations and documentation.

The pad on this site

The digit pad on this site's first plate is a separate model, worth keeping apart from the pipeline above. It is a 784 to 64 to 10 network with a ReLU and a softmax, trained on synthesised strokes rather than on MNIST, and it runs in the browser as two matrix multiplies. What it borrows is the normalisation: crop to the ink, scale the long side to 20 pixels, then shift so the centre of mass lands in the middle of a 28 by 28 field.

What it does

  1. Custom multi-class SVM with One-vs-All strategy
  2. Mini-batch gradient descent with hinge loss optimization
  3. PCA dimensionality reduction retaining 95% variance
  4. Advanced data augmentation (rotation, flip, noise)
  5. Robust preprocessing with threshold masking and Gaussian blur
  6. Complete type annotations and comprehensive documentation
  7. Performance progression from 70% to 99.8% accuracy
  8. Model persistence and evaluation pipeline

Built with

  • Python
  • NumPy
  • SciPy
  • AI
  • Machine Learning
  • Computer Vision
  • Mathematics