A console-based neural network application for handwritten digit recognition using the MNIST dataset. Implemented from scratch with no external libraries.
- Interactive menu system for training, testing, and prediction
- Custom neural network architecture with configurable hidden layers
- Model serialization
- Train on MNIST CSV dataset
- Test accuracy on validation set
- Predict digits from user .bmp images
- Confidence scores with visual probability bars
Use the provided batch file:
compile.bat
Or compile manually:
g++ -std=c++17 -O2 main.cpp neural_net.cpp mnist_loader.cpp image_loader.cpp -o digit_recognition.exe
Then run:
digit_recognition.exe
Training and testing data are already provided in the data folder:
data/mnist_train.csv
data/mnist_test.csv
If you'd like to replace them with your own dataset, make sure they take the same name, and that they follow the CSV format below.
- First column: label (0-9)
- Remaining 784 columns: pixel values (0-255)
- Optional header row (auto-detected and skipped)
The MNIST dataset in CSV format can be downloaded from various sources online. Each row should contain one digit sample with 785 values total (1 label + 784 pixels). The training set typically contains 60,000 samples and the test set contains 10,000 samples.
To test with custom images:
- Create a BMP image, or edit the 'test_digit.bmp` already provided.
- Draw your digit with:
- White foreground (the digit itself)
- Black background
- The size doesn't (will be resized to 28x28)
Trained models are saved in the models folder with .mdl extension.
With recommended settings (2 hidden layers of 128 and 64 neurons, 10 epochs):
- Training accuracy: 95-98%
- Test accuracy: 95-97%
- Training time: varies by hardware (progress bar shows real-time status)
- Architecture: Feedforward neural network
- Hidden activation: Sigmoid
- Output activation: Softmax
- Loss function: Cross Entropy
- Optimization: Minibatch gradient descent with backpropagation
- Weight initialization: Xavier initialization
- Input normalization: Pixels scaled to [0, 1]
- Data augmentation: Real-time augmentation during training to combat domain shift
- Neural Networks: 3Blue1Brown - Neural Networks Series
- But what is a neural netowrk?: 3Blue1Brown - But what is a neural network? | Deep learning chapter 1
- How to Create a Neural Network: Sebastian Lague - How to Create a Neural Network (and Train it to Identify Doodles)
- MNIST Dataset: LeCun, Y., Cortes, C., & Burges, C. (1998). The MNIST database of handwritten digits.
- Cross-Entropy Loss: Cross entropy on Wikipedia - Used for multi-class classification problems with softmax output.