Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.

I3eka/SVHN-Digit-Classifier-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SVHN Digit Classifier API

Overview

This project is a simple FastAPI-based web server that uses a pre-trained ResNet-18 model to classify handwritten or street-view house number digits from images. The model is trained on the SVHN (Street View House Numbers) dataset, which consists of digits 0-9. It provides an API endpoint to upload an image and receive the predicted digit.

Features

  • FastAPI server for easy deployment and testing.
  • Image upload and prediction using PyTorch and torchvision.
  • Pre-trained model loaded from models/resnet18_svhn.pth.
  • Supports RGB images resized to 32x32 pixels.
  • Error handling for invalid inputs.

Project Structure

  • server_model.py: Main FastAPI application with the prediction endpoint.
  • models/resnet18_svhn.pth: Pre-trained ResNet-18 model weights for SVHN.
  • models/cifar_net.pth: Additional model (not currently used).
  • notebooks/notebook.ipynb: Jupyter notebook for model training or experimentation (currently minimal).

Requirements

  • Python 3.8+
  • FastAPI
  • Uvicorn (for running the server)
  • PyTorch
  • Pillow (PIL)
  • torchvision

Install dependencies:

pip install fastapi uvicorn torch torchvision pillow

Setup and Running

  1. Clone or navigate to the project directory:

    cd D:\Projects\project
    
  2. Ensure the model files are in the models/ directory. If not present, you'll need to train or download the resnet18_svhn.pth model.

  3. Run the server:

    uvicorn server_model:app --reload --host 0.0.0.0 --port 8000
    • --reload: Enables auto-reload during development.
    • The server will be available at http://localhost:8000.
  4. Test the API:

    • Visit http://localhost:8000/docs for interactive API documentation (Swagger UI).
    • Use the /predict POST endpoint to upload an image file.
    • Example curl request:
      curl -X POST "http://localhost:8000/predict" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "file=@path/to/your/image.jpg"
    • Response: {"prediction": "5"} (example).

Model Details

  • Architecture: ResNet-18 with BasicBlock, adapted for 32x32 input images.
  • Classes: Digits 0-9.
  • Preprocessing: Resize to 32x32, normalize with mean/std (0.5, 0.5, 0.5).
  • Inference: Runs on CPU (model loaded with map_location="cpu").

Training the Model

If you need to train the model yourself:

  1. Use the notebooks/notebook.ipynb as a starting point.
  2. Download the SVHN dataset using torchvision.
  3. Train using standard PyTorch training loop with the ResNet architecture defined in server_model.py.

Troubleshooting

  • If the model file is missing, the server will fail to load. Ensure models/resnet18_svhn.pth exists.
  • For GPU support, modify the model loading to use CUDA if available.
  • Image format: Supports JPEG, PNG, etc., converted to RGB.

License

This project is open-source. Feel free to use and modify.


Built with ❤️ using FastAPI and PyTorch

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published