This repository demonstrates how to build a Convolutional Neural Network (CNN) using TensorFlow to perform digit recognition on the MNIST dataset. The notebook walks through all the necessary steps, from importing libraries to saving the trained model. The filename for the main code is main.ipynb
.
This project utilizes the MNIST dataset to recognize handwritten digits (0-9) using a CNN. The notebook explains step-by-step how the network is built, trained, and evaluated.
The dataset used is the MNIST (Modified National Institute of Standards and Technology) dataset:
- Training Data: 60,000 grayscale images (28x28 pixels).
- Testing Data: 10,000 grayscale images (28x28 pixels).
Each image represents a handwritten digit, and the goal is to classify each image into one of 10 classes (0-9).
Essential libraries such as TensorFlow, NumPy, and Matplotlib are imported for building and visualizing the model.
The MNIST dataset is loaded and its shapes are printed to verify the dimensions of the data.
Several sample images from the dataset are displayed using Matplotlib for better understanding.
- The pixel values of the images are normalized to the range [0, 1].
- The dataset is reshaped to include a channel dimension (required by CNNs).
A CNN architecture is defined using TensorFlow:
- 2 Convolutional Layers with MaxPooling.
- 1 Dense Hidden Layer.
- 1 Output Layer using Softmax activation.
The model is compiled with:
- Optimizer: Adam
- Loss Function: Sparse Categorical Crossentropy
- Metric: Accuracy
A graphical representation of the model is generated using the plot_model
function.
The model is trained on the training dataset for 10 epochs with validation data.
The trained model's accuracy is tested on the test dataset.
- Plots of training/validation accuracy and loss over epochs are generated to visualize the model's learning process.
The trained model is saved in HDF5 format (mnist_cnn_model.h5
) for future use.
- The model achieved a test accuracy of approximately 99%, demonstrating excellent performance on the MNIST dataset.
- Plots of accuracy and loss confirm effective training with minimal overfitting.
-
Clone the repository:
git clone https://github.com/KartikAg13/digit_recognition.git cd digit_recognition
-
Open the Jupyter Notebook
main.ipynb
:jupyter notebook main.ipynb
-
Follow the step-by-step instructions in the notebook to:
- Train the model.
- Evaluate its performance.
- Save the trained model.
The trained model is saved as mnist_cnn_model.h5
in the repository. You can load it in another program to make predictions on new data:
from tensorflow.keras.models import load_model
model = load_model('mnist_cnn_model.h5')
predictions = model.predict(new_data)
This concludes the steps for recognizing digits using a CNN. Happy learning!