Teach Your Computer to Read Handwritten Digits

A Deep Learning Project

✍️✨Introduction📝🤖

Hey tech enthusiasts! 🌟 

Today, we’re diving into a super fun and practical machine learning project: Handwritten Digit Recognition. Imagine teaching your computer to read your messy notes! 📝 

In this project, we'll use Python and the MNIST dataset to build a neural network that can recognise handwritten digits from 0 to 9.

Let's get started on this exciting journey!

 

What You Will Learn

  • How to load and preprocess the MNIST dataset.

  • Basics of training a neural network for classification.

  • How to evaluate the model’s performance.

  • How to preprocess and predict digits from custom images.

 

Let’s break it down step by step! 🚀

Tools You Will Need

To run this project, you'll need the following tools:

  1. Python: Make sure you have Python installed. You can download it from here.

  2. PyCharm: A powerful Integrated Development Environment (IDE) for Python development. Download it from here.

Step-by-Step Guide

1. Install Required Libraries

 First, ensure you have the necessary libraries installed. Run this in your terminal:

 

   pip install numpy scikit-learn pillow matplotlib

 

2. Import Libraries and Load Dataset

  import numpy as np

   from sklearn.datasets import fetch_openml

   from sklearn.model_selection import train_test_split

   from sklearn.neural_network import MLPClassifier

   from sklearn.metrics import accuracy_score

   from PIL import Image

   import matplotlib.pyplot as plt

 

 Explanation:

  • numpy: For numerical operations.

  • fetch_openml: To load the MNIST dataset.

  • train_test_split: To split the dataset into training and testing sets.

  • MLPClassifier: A Multi-layer Perceptron model for classification.

  • accuracy_score: To evaluate the model's performance.

  • PIL: To handle image processing.

  • matplotlib.pyplot: For plotting images.

 

3. Load MNIST Dataset

   mnist = fetch_openml('mnist_784', version=1, as_frame=False)

   X, y = mnist.data / 255.0, mnist.target.astype(int)

 

 Explanation:

  • fetch_openml('mnist_784'): Fetches the MNIST dataset.

  • mnist.data / 255.0: Normalizes pixel values to be between 0 and 1.

  • mnist.target.astype(int): Converts labels to integers.

 

4. Split the Data

   X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

 

 Explanation:

  • train_test_split(): Splits data into 80% training and 20% testing sets.

  • random_state=42: Ensures reproducibility of results.

 

5. Train the Model

   model = MLPClassifier(hidden_layer_sizes=(128, 64), max_iter=1000, random_state=42, verbose=False)

   model.fit(X_train, y_train)

 

 Explanation:

  • MLPClassifier(hidden_layer_sizes=(128, 64)): Creates a neural network with two hidden layers (128 and 64 neurons).

  • max_iter=1000: Sets the maximum number of iterations.

  • model.fit(X_train, y_train): Trains the model on the training data.

 

6. Evaluate the Model

   y_pred = model.predict(X_test)

   accuracy = accuracy_score(y_test, y_pred)

   print(f"Model accuracy on test set: {accuracy:.4f}")

 

 Explanation:

  • model.predict(X_test): Predicts labels for the test data.

  • accuracy_score(y_test, y_pred): Calculates the accuracy of the model.

 

7. Preprocess Custom Images

 

   def preprocess_image(image_path):

       img = Image.open(image_path).convert('L')  # Convert to grayscale

       if img.size != (28, 28):

           img = img.resize((28, 28), Image.LANCZOS)

       img_array = np.array(img)

       if np.mean(img_array) > 127:

           img_array = 255 - img_array

       img_array = img_array / 255.0

       img_array = img_array.flatten()

       return img_array

 

 Explanation:

  • Image.open(image_path).convert('L'): Opens the image and converts it to grayscale.

  • img.resize((28, 28), Image.LANCZOS): Resizes the image to 28x28 pixels if it's not already.

  • np.array(img): Converts the image to a numpy array.

  • 255 - img_array: Inverts the image if it's white on black.

  • img_array / 255.0: Normalizes pixel values.

  • img_array.flatten(): Flattens the array for model input.

 

Subscribe to keep reading

This content is free, but you must be subscribed to CodeCraft by Dr. Christine Lee to continue reading.

Already a subscriber?Sign In.Not now