This project builds a Convolutional Neural Network (CNN) using TensorFlow/Keras to classify handwritten digits from the MNIST dataset. CNNs are the go-to architecture for image data — they learn spatial patterns using filters that slide across the image.
The MNIST dataset contains 70,000 grayscale images of handwritten digits (0-9):
- Training set: 60,000 images
- Test set: 10,000 images
- Each image is 28x28 pixels (grayscale)
- 10 classes: digits 0 through 9
To build a CNN that learns spatial features (edges, curves, shapes) from handwritten digit images, achieving higher accuracy than a plain feedforward network by leveraging the 2D structure of the data.
- CNNs preserve spatial structure — unlike ANNs that flatten images and lose spatial info
- Convolutional layers learn local patterns: edges, curves, loops — the building blocks of digits
- Pooling layers reduce dimensionality while keeping important features
- CNNs achieve >99% accuracy on MNIST — state-of-the-art for simple image classification
- Python
- Jupyter Notebook
pandas,numpyfor data handlingmatplotlib,seabornfor visualizationtensorflow/kerasfor building and training the CNN
- Conv2D Layer 1: 32 filters, 3x3 kernel, ReLU activation
- MaxPooling2D: 2x2 pool size
- Conv2D Layer 2: 64 filters, 3x3 kernel, ReLU activation
- MaxPooling2D: 2x2 pool size
- Flatten: Convert 2D feature maps to 1D
- Dense Layer: 128 neurons, ReLU activation
- Dropout: 30% for regularization
- Output Layer: 10 neurons, Softmax activation
Total parameters: ~100,000
-
Data Loading & Exploration:
- Loaded MNIST from
tf.keras.datasets - Visualized sample digits with their labels
- Checked image dimensions and class balance
- Loaded MNIST from
-
Data Preprocessing:
- Reshaped images to (28, 28, 1) — adding channel dimension
- Normalized pixel values to [0, 1]
- One-hot encoded labels
-
Modeling:
- Built CNN with Conv2D + MaxPooling2D layers
- Used categorical crossentropy loss and Adam optimizer
- Trained for 10 epochs
-
Evaluation & Visualization:
- Evaluated accuracy and loss on test set
- Plotted training vs validation curves
- Visualized correct and incorrect predictions
- CNN achieved >99% accuracy on MNIST — significantly better than a plain ANN
- The model learned meaningful filters: edge detectors, curve detectors, etc.
- Training converged quickly — fewer epochs needed than ANN
- Fewer parameters than the ANN but better accuracy — showing the power of convolutions
ML-DL-Projects/
├── Deep-Learning/
│ └── CNN/
│ ├── dataset/ # (MNIST loaded via tf.keras)
│ ├── images/
│ │ ├── training_history.png
│ │ └── sample_predictions.png
│ ├── notebooks/
│ │ └── CNN_Example.ipynb
│ └── README.md
├── uv.lock
├── pyproject.toml
To run this project locally:
cd ML-DL-Projects
uv syncjupyter notebookThen open CNN_Example.ipynb.
- Add more convolutional layers (deeper network) and see how it affects accuracy
- Experiment with different filter sizes and numbers
- Use data augmentation (rotation, zoom, shift) to improve robustness
- Apply the same CNN architecture to Fashion MNIST for a harder challenge
Feel free to reach out if you have questions or want to collaborate!