A self-contained PyTorch + Tkinter app that trains a Multi-Layer Perceptron on the MNIST dataset and lets you draw a digit with the mouse to see what the model predicts.
- Phase 1 — Data. Loads the MNIST IDX files from
data/, normalizes images to[0, 1], renders a 5×5 sample grid + pixel-intensity histogram, and splits the 60k training set into 50k train / 10k validation. The 10k test set is held out. - Phase 2 — Model & Training. Trains an MLP (
784 → 512 → 256 → 128 → 10, ReLU + Dropout, He init) with Adam (lr=1e-3) for up to 20 epochs. Early-stops if validation loss does not improve for 5 epochs. Saves the best weights and plots loss/accuracy curves. - Phase 2 — Evaluation. Reports test accuracy, confusion matrix heatmap, and per-class precision/recall/F1.
- Phase 3 — Debug & Improve. Renders 20 sample test predictions (correct in green, wrong in red). If test accuracy is below 97 %, runs up to 2 improvement attempts (BatchNorm variant, longer training) and keeps the best weights.
- Frontend. Tkinter window with a collapsible step viewer on the left and a 280×280 drawing canvas on the right. Draw a digit, hit Predict, and see the predicted class plus a 10-bar confidence chart.
.
├── CLAUDE.md # Project spec
├── README.md
├── requirements.txt
├── data/ # MNIST IDX files (not in git)
│ ├── train-images.idx3-ubyte
│ ├── train-labels.idx1-ubyte
│ ├── t10k-images.idx3-ubyte
│ └── t10k-labels.idx1-ubyte
├── outputs/ # Generated artifacts (not in git)
│ ├── best_model.pth
│ ├── data_visualization.png
│ ├── training_curves.png
│ ├── confusion_matrix.png
│ └── sample_predictions.png
└── src/
├── data.py # IDX parsing, viz, splits, DataLoaders
├── model.py # MLP + MLP_BN architectures
├── train.py # Training loop, early stopping, curves
├── evaluate.py # Accuracy, confusion matrix, classification report
├── debug.py # Sample predictions + improvement attempts
└── app.py # Tkinter app — entry point
- Python 3.10+
- The four MNIST IDX files placed in
data/(uncompressed, with the names shown above). - Packages from
requirements.txt:torch,torchvision,numpy,matplotlib,scikit-learn,Pillow.
A working Tk install is needed for the GUI. On Linux: sudo apt install python3-tk.
git clone https://github.com/Adamex-SVK/NumberRecognitionNN.git
cd NumberRecognitionNN
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txtPlace the four MNIST IDX files in data/ if they are not there already.
python src/app.pyWhat you will see:
- The window opens. The left panel lists every pipeline step as
…. - A background thread runs Phase 1 → 2 → 3. Each step flips to
⏳while it works and✓when it finishes. The training row shows per-epochtrain_loss,val_loss,val_acc. - When training is done, the best weights are written to
outputs/best_model.pthand the Predict button becomes active. - Draw a digit on the white canvas. Click Predict to see the predicted class and a confidence bar chart for all 10 digits. Clear wipes the canvas. Retrain runs the full pipeline again from scratch.
- Click any step in the tree, then Open output to view its saved PNG (data viz, training curves, confusion matrix, or sample predictions).
After the first successful run, outputs/best_model.pth is cached. Re-launching the app skips training and goes straight to the canvas. Use Retrain to force a fresh run.
- All randomness is seeded with
42for reproducibility. - Training takes a few minutes on CPU; under a minute on GPU (CUDA is used automatically when available).
- The drawing pipeline crops the digit to its bounding box, pads it to a square with a small margin, and resizes to 28×28 to mimic MNIST's centered framing — this matters a lot for prediction quality.