This repository contains code for a Convolutional Neural Network (CNN) implemented in TensorFlow/Keras for the task of digit recognition on the MNIST dataset. The model achieves high accuracy and has been trained and evaluated on the MNIST dataset, a benchmark dataset for handwritten digit recognition.
To run the code in this repository, you need to have the following dependencies installed:
pip install numpy matplotlib keras
Make sure you have the TensorFlow library installed. If not, you can install it using:
pip install tensorflow
The MNIST dataset is automatically downloaded and loaded using TensorFlow's mnist.load_data()
function. It consists of 60,000 training images and 10,000 testing images, each of size 28x28 pixels.
The loaded images are pre-processed as follows:
- Normalized to the range [0, 1].
- Reshaped to include a channel dimension (for compatibility with CNN architecture).
- One-hot encoded the labels.
The CNN model architecture consists of the following layers:
- Convolutional layer with 32 filters, kernel size (3, 3), and ReLU activation.
- MaxPooling layer with pool size (2, 2).
- Dropout layer with a dropout rate of 0.25.
- Convolutional layer with 64 filters, kernel size (3, 3), and ReLU activation.
- MaxPooling layer with pool size (2, 2).
- Dropout layer with a dropout rate of 0.25.
- Flatten layer to transform the 2D feature maps to a 1D vector.
- Fully connected (Dense) layer with 128 units and ReLU activation.
- Fully connected (Dense) layer with 10 units and softmax activation.
The model is compiled using categorical cross-entropy loss and the Adam optimizer. It is then trained for 10 epochs on the training data with a batch size of 32. Validation data is used during training to monitor model performance on unseen data.
The trained model is evaluated on the test set, and both the loss and accuracy are printed.
The README provides a visual representation of the training history, plotting both training and testing accuracy over the epochs.
A single test sample is selected, and the trained model predicts the digit. The actual and predicted digits, along with the image, are displayed.
Feel free to use and modify the code according to your needs. Happy coding!