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.
- 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.
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).
- Python 3.8+
- FastAPI
- Uvicorn (for running the server)
- PyTorch
- Pillow (PIL)
- torchvision
Install dependencies:
pip install fastapi uvicorn torch torchvision pillow-
Clone or navigate to the project directory:
cd D:\Projects\project -
Ensure the model files are in the
models/directory. If not present, you'll need to train or download theresnet18_svhn.pthmodel. -
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.
-
Test the API:
- Visit
http://localhost:8000/docsfor interactive API documentation (Swagger UI). - Use the
/predictPOST 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).
- Visit
- 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").
If you need to train the model yourself:
- Use the
notebooks/notebook.ipynbas a starting point. - Download the SVHN dataset using torchvision.
- Train using standard PyTorch training loop with the ResNet architecture defined in
server_model.py.
- If the model file is missing, the server will fail to load. Ensure
models/resnet18_svhn.pthexists. - For GPU support, modify the model loading to use CUDA if available.
- Image format: Supports JPEG, PNG, etc., converted to RGB.
This project is open-source. Feel free to use and modify.
Built with ❤️ using FastAPI and PyTorch