This repository implements a Convolutional Neural Network (CNN) for recognizing handwritten digits from the MNIST dataset using PyTorch. The project also includes an interactive UI built with the Godot engine that allows users to draw digits and see real-time predictions performed by the CNN model running in the background.
Model weights (in .pth format) are excluded from this repository,
and instead can be downloaded from huggingface, and placed in the trained_models folder.
The CNN is a simple architecture with the following layers:
- Conv2d layer with 10 filters (kernel size 5) + ReLU + MaxPooling
- Conv2d layer with 20 filters + Dropout2d + ReLU + MaxPooling
- Fully connected layer with 50 units + ReLU
- Dropout + Output fully connected layer with 10 units (one per digit) + LogSoftmax
- Dataset: MNIST handwritten digits, automatically downloaded and loaded using torchvision.
- Training batch size: 64
- Testing batch size: 1000
- Optimizer: Adam
- Loss function: Negative Log Likelihood Loss (NLLLoss)
- Training epochs: 3
- Training loss and test accuracy are plotted during training (example accuracy on test set: ~97%).
The Godot engine project in godot_project/ provides a user interface for interactively drawing digits.
- The UI writes handwritten digit data to a named pipe file
model_input.pipe. - The Python script
predict.pycontinuously reads frommodel_input.pipe, runs the CNN model prediction, and writes the predictions tomodel_output.pipe. - This setup allows real-time interaction where the user can see predictions instantly as digits are drawn.
-
Clone the repository
-
Create a virtual environment and install the dependencies:
python3.11 -m venv venv source venv/bin/activate pip install -r requirements.txt -
Train the model (optional if you want to retrain):
- Run
Model_training.ipynbin Jupyter to train and save the model totrained_models/model.pth. - OR download model from huggingface to
trained_modelsfolder
- Run
-
Run the prediction server:
python predict.py
-
Open the Godot project in Godot Engine 4.5 and edit project from
godot_project/folder.- The UI will interact with
predict.pyvia http requests for real-time digit recognition.
- The UI will interact with
To run in docker, first build a docker image:
docker build -t handwritten-img .Then run docker container with port redirected:
docker run -d --name handwritten -p 8001:8000 handwritten-img