Skip to content

Farid-Jam/Digit-Predictor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MNIST Digit Recognizer: From Scratch to GUI

An end-to-end Machine Learning project featuring a Deep Neural Network built from scratch using NumPy. This repository includes pre-trained weights, allowing you to run the interactive drawing application immediately.

Features

  • Inference-Ready: Includes mnist_weights.pkl so you can use the model without training.
  • Neural Network from Scratch: Logic for forward/backpropagation and gradient descent implemented without ML frameworks (no TensorFlow/PyTorch).
  • Interactive GUI: A Tkinter-based whiteboard to draw digits and get real-time predictions.
  • Optimized Preprocessing: Automatic image scaling and inversion to match the MNIST dataset format.

Technical Architecture

1. Model Structure

The model is a 2-layer dense neural network:

  • Input Layer: 784 neurons (28x28 pixel images).
  • Hidden Layer: 64 neurons with ReLU activation.
  • Output Layer: 10 neurons (digits 0-9) with Softmax activation.

2. Performance

  • Accuracy: ~95% on MNIST test data.
  • Training Method: Stochastic Gradient Descent (SGD).

How to Use

Prerequisites

Ensure you have Python installed along with these dependencies:

pip install numpy pandas matplotlib pillow

Running the App

  1. Clone the Repo: Ensure the mnist_weights.pkl file is in the same directory as your main script.
  2. Launch: Run the Python script containing the Tkinter code.
  3. Interact: * Draw a digit (0-9) on the white canvas using your mouse.
    • Click Predict to see the model's result.
    • Click Clear to wipe the canvas for a new attempt.

Preprocessing Pipeline

To ensure the model recognizes your drawings accurately, the GUI performs these automated steps whenever you click Predict:

  • Capture: Saves the current canvas drawing as a PostScript file.
  • Convert: Changes the image to Grayscale (L mode).
  • Invert: Flips the colors to white-on-black to match the MNIST training standard.
  • Resize: Squashes the drawing down to exactly 28x28 pixels.
  • Normalize: Divides pixel values by 255 to feed the network values between 0 and 1.

Retraining the Model

If you wish to retrain the model from scratch or experiment with hyperparameters:

  1. Data Setup: Ensure you have the mnist_train.csv file located in a folder named data/.
  2. Execute Training: Run the gradient_descent function within the training script.
  3. Save: The script will automatically overwrite the existing mnist_weights.pkl with your newly optimized parameters.

About

Developing a neural network from scratch and utilizing the MNIST dataset to accurately predict digits on a 28 x 28 grid

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors