This project implements a Simple Feed Forward Neural Network and a Convolutional Neural Network (CNN) for handwritten digit recognition using the MNIST dataset. The project includes a terminal-based interface that allows users to draw digits in real time using their mouse. Once a digit is drawn, it is processed and classified by the trained model.
- Python 3.8+
- Libraries:
- PyTorch
- torchvision
- tqdm
- numpy
- matplotlib
- jupyter
- g++ or clang (C++11 or later)
- LibTorch (PyTorch C++ API)
- ncurses library
- cmake
-
Clone the Repository
git clone https://github.com/Xzanam/draw-and-recognize-mnist.git cd draw-and-recognize-mnist
-
Install the required Python packages:
The training code is provided in mnist.ipynb
. Follow these steps:
-
Open the notebook:
jupyter notebook mnist.ipynb
-
Run the cells to train the model on the MNIST dataset. mnist.ipynb consists of training code for both Feed Forward Neural Network and Convolutional Neural Network. The notebook itself is self-explanatory. After training, the model is exported as 'model_cnn.pt' using PyTorch's JIT.
To compile the C++ interface, follow these steps:
-
Create a build directory: Open your terminal and navigate to the project folder. Create a separate directory for the build files to keep things organized:
mkdir build cd build
-
Run CMake: Use CMake to generate the necessary build files. Make sure you are inside the
build
directory:cmake ..
-
Compile the code: Now, compile the code using
make
:make
After these steps, an executable named character_recog
should be created in the build
directory, ready to run with your trained model.
Launch the application with:
./character_recog <path_to_the_trained_model>
In my case
./character_recog ../mnist_CNN.pt
- Draw: Use your mouse to draw a digit directly in the terminal.
- Predict: Press
p
to classify the drawn digit. - Clear: Press
c
to erase the current drawing and start over. - Quit: Press
q
to exit the application.
digit_recog.mp4
-
libtorch.so: No such file or directory
- This error usually indicates that LibTorch is not installed or the paths provided during compilation are incorrect.
- Solution: Verify that LibTorch is installed.
-
Mouse not working
- If the mouse input is not functioning as expected, it might be due to a missing or incompatible
ncurses
installation. - Solution: Ensure that
ncurses
is properly installed on your system and that your terminal supports mouse input.
- If the mouse input is not functioning as expected, it might be due to a missing or incompatible