This project demonstrates how to train a neural network using TensorFlow on the MNIST dataset (handwritten digits), save the trained model, and use it to predict digits from new image files.
- Trains a simple yet effective deep neural network on the MNIST dataset.
- Saves the trained model to disk.
- Loads the saved model for future predictions.
- Predicts digit values from custom image inputs (
.pngfiles). - Visualizes each processed image along with its prediction.
- Input Layer: Flatten (28x28 pixels)
- Hidden Layers:
- Dense Layer (128 units, ReLU)
- Dense Layer (128 units, ReLU)
- Output Layer: Dense Layer (10 units, Softmax for digit classification 0β9)
Install the dependencies using pip:
pip install numpy opencv-python matplotlib tensorflowproject/
βββ digits/
β βββ digit1.png
β βββ digit2.png
β βββ ...
βββ mnist_model.h5
βββ main.py
- Train the Model (Optional) If you want to retrain the model from scratch, uncomment and run the training section in mnist_classifier.py:
(x_train, y_train), (x_test, y_test) = mnist.load_data()
model.fit(x_train, y_train, epochs=3)
model.save('mnist_model.h5')
- Add Custom Digit Images Save grayscale images (28x28 pixels recommended) of digits to the digits/ folder.
Name them sequentially as digit1.png, digit2.png, etc.
- Run Prediction
python main.py
The script will load mnist_model.h5, preprocess each image, and display the predicted digit.
Predictions are shown with the corresponding image using matplotlib.
Images should be in grayscale (cv2.IMREAD_GRAYSCALE is used).
Recommended to use white digits on black background.
Each image will be:
Resized to 28x28.
Inverted (black to white) to match MNIST data format.
Normalized to [0, 1] range.
Prediction for image 1: 1
Prediction for image 2: 2
Displays each image along with its predicted digit.