Skip to content

This project uses the MNIST dataset to classify handwritten digits

Notifications You must be signed in to change notification settings

NathanCordeiro/MNIST-Neural-Network-Classifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MNIST Neural Network Classifier

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.


πŸ“Œ Features

  • 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 (.png files).
  • Visualizes each processed image along with its prediction.

🧠 Model Architecture

  • 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)

πŸ› οΈ Requirements

Install the dependencies using pip:

pip install numpy opencv-python matplotlib tensorflow

πŸ“ Project Structure

project/
β”œβ”€β”€ digits/
β”‚   β”œβ”€β”€ digit1.png
β”‚   β”œβ”€β”€ digit2.png
β”‚   └── ...
β”œβ”€β”€ mnist_model.h5
└── main.py

πŸš€ How to Run

  1. 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')
  1. 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.

  1. 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.


πŸ“· Image Format Guidelines

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.


πŸ“Š Sample Output

Prediction for image 1: 1
Prediction for image 2: 2

Displays each image along with its predicted digit.


About

This project uses the MNIST dataset to classify handwritten digits

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages