Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

🖼️ MNIST Digit Classification with Convolutional Neural Network (CNN)

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.


📁 Dataset Overview

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

🎯 Objective

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.


🧠 Why CNN?

  • 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

🧪 Tools & Libraries Used

  • Python
  • Jupyter Notebook
  • pandas, numpy for data handling
  • matplotlib, seaborn for visualization
  • tensorflow / keras for building and training the CNN

📊 Model Architecture

  • 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


📊 Process Overview

  1. Data Loading & Exploration:

    • Loaded MNIST from tf.keras.datasets
    • Visualized sample digits with their labels
    • Checked image dimensions and class balance
  2. Data Preprocessing:

    • Reshaped images to (28, 28, 1) — adding channel dimension
    • Normalized pixel values to [0, 1]
    • One-hot encoded labels
  3. Modeling:

    • Built CNN with Conv2D + MaxPooling2D layers
    • Used categorical crossentropy loss and Adam optimizer
    • Trained for 10 epochs
  4. Evaluation & Visualization:

    • Evaluated accuracy and loss on test set
    • Plotted training vs validation curves
    • Visualized correct and incorrect predictions

📌 Key Results

  • 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

📂 Folder Structure

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

🚀 Getting Started

To run this project locally:

Step 1: Set up environment

cd ML-DL-Projects
uv sync

Step 2: Run notebook

jupyter notebook

Then open CNN_Example.ipynb.


💡 Future Ideas

  • 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

📬 Contact

Feel free to reach out if you have questions or want to collaborate!