Deep Learning Implementation: CNN on
MNIST Dataset
Problem Statement
The goal of this project is to build a Convolutional Neural Network (CNN) to classify
handwritten digits using the MNIST dataset. The MNIST dataset contains 70,000 grayscale
images of handwritten digits (0–9) that are 28x28 pixels in size. This implementation
demonstrates how deep learning techniques can achieve high accuracy on image
classification tasks.
Dataset Overview
The MNIST dataset includes 60,000 training images and 10,000 testing images. Each image
is labeled with the corresponding digit. We normalize the image data and reshape it to fit
the input requirements of a CNN.
CNN Model Architecture
We use TensorFlow and Keras to build the CNN model. The architecture consists of the
following layers:
- Input Layer: 28x28 grayscale image
- Convolutional Layer (32 filters, 3x3 kernel)
- MaxPooling Layer (2x2)
- Convolutional Layer (64 filters, 3x3 kernel)
- MaxPooling Layer (2x2)
- Flatten Layer
- Dense Layer (128 units, ReLU)
- Output Layer (10 units, Softmax)
Python Code (Using TensorFlow/Keras)
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.utils import to_categorical
# Load and preprocess data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape((60000, 28, 28, 1)).astype('float32') / 255
x_test = x_test.reshape((10000, 28, 28, 1)).astype('float32') / 255
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
# Build the CNN model
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
# Compile and train
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
Training Results
The CNN model achieved high accuracy on both training and validation datasets. Below is
the plot showing the loss and accuracy over 10 epochs.
Conclusion
This implementation demonstrates the effectiveness of CNNs in image classification tasks.
The model achieved over 97% accuracy on the MNIST dataset, validating the suitability of
deep learning for digit recognition problems.