Tudor IustinSubscribe

Handwritten digit recognition with a hybrid CNN-SVM

Combining neural network feature extraction with support vector machines on MNIST.

Training curves and confusion matrix from the handwritten digit recognition notebook

A handwritten digit looks simple to a person, but a classifier has to account for variation in stroke width, shape and position. This project explores a hybrid approach: use a convolutional neural network to learn a useful representation, then train a support vector machine to classify that representation.

The experiment uses MNIST, a dataset of 28 × 28 greyscale digit images. The repository describes 60,000 training images and 10,000 test images, with a standard CNN as the baseline.

Two models with different jobs

The CNN learns spatial patterns from the images. Its architecture includes two convolutional layers with ReLU activations, max pooling, dropout and a dense layer. Rather than relying only on the network’s final softmax classifier, the hybrid model extracts dense-layer features and passes them into an SVM. The SVM uses an RBF kernel to form non-linear decision boundaries in that learned feature space.

From pixels to feature vectors

The workflow begins by normalising pixel values to the range from zero to one. The CNN is trained with the Adam optimiser for 20 epochs, using batches of 32 images. Dropout and augmentation are used to help regularise learning. Once the CNN has learned image features, those representations become the inputs for the SVM stage.

  • Normalise the image data and prepare the training and test sets.
  • Train the CNN to learn spatial features.
  • Extract the learned dense-layer representations.
  • Tune and fit an RBF-kernel SVM with scikit-learn’s GridSearchCV.
  • Evaluate predictions using accuracy, precision, recall, F1 and a confusion matrix.

Reading the reported results

The README reports a best hybrid accuracy of 99.45%, with precision, recall and F1 also reported as 99.45%. These are results from this MNIST experiment, rather than a guarantee for other handwriting datasets. The figure above comes directly from the project notebook and includes learning curves, a confusion matrix and performance metrics. It is useful to inspect where mistakes occur as well as the headline accuracy.

Tools behind the experiment

TensorFlow and Keras handle the neural network, while scikit-learn provides the SVM and hyperparameter search. NumPy, pandas, Matplotlib and Seaborn support data handling and analysis. The documented development environment uses NVIDIA AI Workbench with an RTX 6000 Ada GPU. The repository includes the notebook and a fuller project report for readers who want to follow the experiment.

What I would investigate next

This experiment gives a concrete way to compare learned features with different classification heads. Useful follow-up work would test other CNN architectures, compare SVM kernels under the same evaluation protocol, and evaluate handwriting from outside MNIST. A high benchmark score does not establish robustness to unfamiliar handwriting or image capture conditions; those require separate data and experiments.

Explore the repository on GitHub ↗

← All posts