This repository contains a simple pipeline for training a neural network on the classic MNIST dataset.
It demonstrates:
- Downloading and preprocessing the data
- Visualising the training/test images (optional)
- Building and training a neural network
- Evaluating a trained model
- Saving and reloading the trained model for interference
Project Structure
.
├── README.md
├── data
│ └── MNIST
│ └── mnist.npz
├── main.py
├── models
│ ├── __init__.py
│ ├── create_model.py
│ └── saved_models
│ ├── __init__.py
│ └── mnist_model.keras
├── requirements.txt
├── scripts
│ ├── download_data.py
│ ├── evaluate.py
│ ├── train.py
│ └── visualise.py
└── web_app
├── app.py
├── static
│ └── styles
│ └── style.css
└── templates
├── index.html
└── results.html
- Python 3.7+
- TensorFlow (tested with TensorFlow 2.12+)
- NumPy
- Matplotlib
- argparse (bundled with standard Python)
All required packages are listed in requirements.txt
-
Clone the repository:
git clone https://github.com/hub-mm/MNIST_handwritten_digit_recognition.git
-
Create Virtual Environment:
python -m venv venv source venv/bin/activate # on macOS/Linux .\venv\Scripts\activate # on Windows
-
Install Dependencies:
pip install --upgrade pip pip install -r requirements.txt
-
Run main.py script to train and evaluate the model:
python main.py --epochs 3 --visualise
Argument Description Default --epochs Number of epochs to train the model 3 --visualise If set, shows a sample image from the dataset (flag) --dataset_path Path to store/load the MNIST dataset ./data/MNIST
-
Train for 5 epochs and visualise a sample:
python main.py --epochs 5 --visualise
-
Train for 10 epochs without visualising:
python main.py --epochs 10
-
Specify a custom dataset path:
python main.py --epochs 2 --dataset_path /tmp/mnist
-
Use Default:
python main.py
- Download the MNIST dataset to ./data/MNIST/ if not already present
- Train a simple neural network for 3 epochs
- Evaluate on the test set (10k images)
- Save the model the ./models/saved_models/mnist_model.keras
- Reload the model, compile it, predict the first test image, and display the predicted label vs. the true label
-
Simple Web Page: contains a landing page and result page.
-
Ability to upload Images: png, jpg, jpeg, etc. (gets converted to png).
-
Output: prediction made by the trained model of what number the image is showing.
- Can only guarantee accuracy for MNIST type of images.
- Only works with single numbers.
- Work best with black background and white number.
Run main scripts again after training with default parameters:
python main.py
Click on link in terminal or access via: http://127.0.0.1:5000 if default parameters are kept.
-
Fork the repository
-
Create a feature branch:
git checkout -b feature/my_new_feature
-
Commit your changes:
git commit -m 'Add some feature'
-
Push to the branch:
git push origin feature/my_new_feature
-
Open a pull request describing what changed and why
Thank you for taking the time to check this work out!