Skip to content

ejaifeobuks/Containerized-Real-Time-ML-Inference-Service-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง Containerized Real-Time ML Inference Service

License Python TensorFlow React Docker

A full-stack machine learning application that classifies handwritten digits (0-9) using a neural network trained on the MNIST dataset. Features a modern React frontend with canvas drawing, a robust FastAPI backend, and complete Docker containerization.

๐ŸŽฏ Project Overview

This project demonstrates an end-to-end machine learning deployment pipeline:

  • Neural Network: Trained on the MNIST dataset (60,000 training images) achieving ~98% accuracy.
  • Interactive Frontend: React-based UI allowing users to draw digits directly in the browser.
  • Real-time Inference: FastAPI backend that processes images and serves predictions instantly.
  • Production Ready: Fully containerized with Docker and Nginx for easy deployment.

๐Ÿ—๏ธ Architecture

graph LR
    User[User] -->|Draws Digit| Frontend[React Frontend :3000]
    Frontend -->|POST /predict| Backend[FastAPI Backend :8000]
    Backend -->|Preprocess| Processor[Image Processor]
    Processor -->|Inference| Model[TensorFlow Model]
    Model -->|Probabilities| Backend
    Backend -->|JSON Response| Frontend
Loading

๐Ÿš€ Quick Start

Option 1: Using Docker (Recommended)

The easiest way to run the application is using Docker Compose.

  1. Clone the repository:

    git clone <repository-url>
    cd neural-network-project
  2. Start the application:

    docker-compose up --build
  3. Access the application:

  4. Stop the application:

    docker-compose down

Option 2: Local Development

Backend Setup

  1. Create and activate a virtual environment (Python 3.11+).
  2. Install dependencies:
    cd backend
    pip install -r requirements.txt
  3. Run the server:
    uvicorn predict:app --reload

Frontend Setup

  1. Install dependencies:
    cd frontend
    npm install
  2. Start the dev server:
    npm run dev

๐Ÿง  Model & Preprocessing

Model Architecture

The model is a feedforward neural network built with Keras:

  • Input: 784 neurons (flattened 28x28 image)
  • Hidden Layer 1: 128 neurons (ReLU)
  • Hidden Layer 2: 64 neurons (ReLU)
  • Output: 10 neurons (Softmax)

Smart Preprocessing Pipeline

To ensure the model works with user drawings, the backend implements a robust preprocessing pipeline that mimics the MNIST dataset preparation:

  1. Grayscale Conversion: Converts input to single channel.
  2. Auto-Cropping: Detects the digit's bounding box and crops empty whitespace.
  3. Centering & Padding: Centers the digit on a 28x28 canvas with 20% padding.
  4. Inversion: Inverts colors (Canvas is black-on-white, MNIST is white-on-black).
  5. Normalization: Scales pixel values to [0, 1].

๐Ÿ“‚ Project Structure

neural-network-project/
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ predict.py              # FastAPI application & logic
โ”‚   โ”œโ”€โ”€ requirements.txt        # Python dependencies
โ”‚   โ”œโ”€โ”€ Dockerfile              # Backend container config
โ”‚   โ””โ”€โ”€ .dockerignore
โ”œโ”€โ”€ frontend/
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ App.jsx             # Main React component
โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ”œโ”€โ”€ package.json            # Node dependencies
โ”‚   โ”œโ”€โ”€ Dockerfile              # Frontend container config
โ”‚   โ”œโ”€โ”€ nginx.conf              # Nginx configuration for serving React
โ”‚   โ””โ”€โ”€ vite.config.js          # Vite build config
โ”œโ”€โ”€ Model_v1/
โ”‚   โ””โ”€โ”€ my_model.keras          # Trained TensorFlow model
โ”œโ”€โ”€ Neural_Network_Project.ipynb # Jupyter notebook for training
โ”œโ”€โ”€ docker-compose.yml          # Orchestration config
โ””โ”€โ”€ README.md

๐Ÿ”ง API Reference

POST /predict

Uploads an image file for digit classification.

Request: multipart/form-data

  • file: The image file (PNG/JPEG)

Response:

{
  "predicted_digit": 7,
  "confidence": 0.985,
  "probabilities": [0.001, 0.000, ..., 0.985, ..., 0.002]
}

๐Ÿ› Troubleshooting

"Model not found" error: Ensure the Model_v1 directory contains my_model.keras. If running locally, make sure you are in the correct directory.

"Port already in use": Stop any running containers (docker ps -> docker stop <id>) or change ports in docker-compose.yml.

Accuracy issues: Try drawing the digit larger and in the center. The preprocessing handles most cases, but very thin lines or tiny drawings might be harder to recognize.

๐Ÿ”ฎ Future Improvements

  • Add Convolutional Neural Network (CNN) for better accuracy.
  • Implement user feedback loop to save misclassified images for retraining.
  • Add support for mobile touch drawing.

๐Ÿ“„ License

This project is for educational purposes. The MNIST dataset is publicly available and free to use.

๐Ÿ“ง Contact

Author: Ogheneobukome Ejaife


๐Ÿš€ Quick Start Commands

# 1. Create environment
conda create -n tensorflow-env python=3.11 -y && conda activate tensorflow-env

# 2. Install dependencies
pip install tensorflow scikit-learn numpy matplotlib jupyter ipykernel

# 3. Run notebook
code Neural_Network_Project.ipynb

# 4. Select tensorflow-env kernel in VS Code and run all cells!

Happy Learning! ๐ŸŽ‰ This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors