A deep learning project using a Convolutional Neural Network (CNN) to classify handwritten digits (0-9) from the MNIST dataset. This project demonstrates image classification with Keras/TensorFlow.
This project trains a CNN on the MNIST dataset and provides a script to test custom digit images. The model achieves high accuracy on standard MNIST data and can predict digits from user-provided images.
The CNN includes:
- 2 Convolutional Layers with ReLU activation and max pooling for feature extraction.
- Flatten Layer to transition to dense layers.
- 2 Dense Layers (64 units with ReLU, 10 units with Softmax for classification).
- Trained with Adam optimizer and sparse categorical crossentropy loss.
Note: The model is trained on MNIST data with white digits on a black background. Test images are automatically inverted if they have a white background to match this format.
MNSITcode.py: Trains the model on MNIST and saves it asmnist_model.keras.Test_Model.py: Loads the model and predicts digits fromimage.png.MNIST_Complete.py: Combined script for training and testing.mnist_model.keras: Saved trained model (generated after runningMNSITcode.py).
-
Install Dependencies:
pip install tensorflow numpy matplotlib pillow
-
Train the Model:
python MNSITcode.py
This trains for 10 epochs with validation and saves the model.
-
Test on Custom Image:
- Place a 28x28 grayscale image named
image.pngin the directory. - Run:
python Test_Model.py
- The script handles image inversion if needed and outputs the prediction with confidence.
- Place a 28x28 grayscale image named
- Training Accuracy: ~90-99% on MNIST test set.
- Custom Prediction: Works best with clear, centered digits. Includes automatic preprocessing for color inversion.
- Ensure
image.pngandimage2.pngis grayscale; the script resizes and preprocesses it. - For low-confidence predictions, check image quality or retrain with more epochs.
- Model uses
.kerasformat for compatibility.