metadata
title: Image Classification Service
emoji: πΌοΈ
colorFrom: indigo
colorTo: purple
sdk: docker
pinned: false
High-Throughput Image Classification Service
A production-ready image classification API using ResNet-18 with ONNX optimization, FastAPI, and CI/CD pipeline.
Features
- Optimized Model: ResNet-18 converted to ONNX with dynamic quantization (~70% size reduction)
- High Performance: ProcessPoolExecutor for concurrent request handling
- Production Ready: Docker containerization, comprehensive error handling
- CI/CD Pipeline: Automated testing and deployment to Hugging Face Spaces
- Comprehensive Testing: pytest unit tests with 100% endpoint coverage
Project Structure
image-classification-service/
βββ app/
β βββ __init__.py
β βββ main.py # FastAPI application
β βββ model.py # ONNX inference logic
β βββ schemas.py # Pydantic models
βββ models/
β βββ resnet18_quantized.onnx # Optimized model
βββ tests/
β βββ test_api.py # Unit tests
βββ scripts/
β βββ 01_baseline_test.py # PyTorch baseline benchmark
β βββ 02_export_onnx.py # Export to ONNX
β βββ 03_quantize.py # Dynamic quantization
β βββ 04_benchmark_onnx.py # ONNX benchmark
βββ .github/
β βββ workflows/
β βββ ci-cd.yml # GitHub Actions pipeline
βββ Dockerfile
βββ .dockerignore
βββ requirements.txt
βββ README.md
Quick Start
1. Install Dependencies
pip install -r requirements.txt
2. Prepare the Model
Run the optimization scripts in order:
cd scripts
python 01_baseline_test.py # Measure PyTorch baseline
python 02_export_onnx.py # Export to ONNX
python 03_quantize.py # Apply quantization
python 04_benchmark_onnx.py # Compare performance
cd ..
3. Run the API
uvicorn app.main:app --host 0.0.0.0 --port 7860
4. Test the API
# Health check
curl http://localhost:7860/health
# Predict
curl -X POST "http://localhost:7860/predict" \
-H "accept: application/json" \
-F "file=@/path/to/image.jpg"
Docker Deployment
Build and Run
docker build -t image-classifier .
docker run -p 7860:7860 image-classifier
Testing
pytest tests/ -v
API Endpoints
GET /health
Health check endpoint.
Response:
{
"status": "ok"
}
POST /predict
Image classification endpoint.
Request:
- Content-Type:
multipart/form-data - Body:
file(image file)
Response:
{
"label": "tabby, tabby cat",
"score": 0.8234,
"label_id": 281,
"inference_time_ms": 45.123
}
Error Codes:
400: Corrupted or invalid image413: File too large (max 10MB)415: Unsupported media type500: Inference error
Performance Metrics
| Format | File Size | Avg Latency | P95 Latency |
|---|---|---|---|
| PyTorch | ~45 MB | baseline | baseline |
| ONNX | ~45 MB | ~20% faster | - |
| ONNX Quantized | ~12 MB | ~40% faster | - |
Run benchmark scripts to get actual measurements on your hardware
CI/CD Pipeline
The GitHub Actions workflow automatically:
- Runs unit tests on every push/PR
- Deploys to Hugging Face Spaces on main branch (requires
HF_TOKENsecret)
Setup Hugging Face Deployment
- Create a Hugging Face Space
- Generate an access token with write permissions
- Add
HF_TOKENto GitHub repository secrets - Update
.github/workflows/ci-cd.ymlwith your Space URL
Model Details
- Base Model: microsoft/resnet-18 (Hugging Face)
- Task: Image Classification (ImageNet-1k)
- Input: RGB images (224x224)
- Output: 1000 class probabilities
- Optimization: ONNX + Dynamic Quantization (QUint8)
Cloud API Usage
The service is deployed on Hugging Face Spaces. You can access the API directly using the following endpoint:
Endpoint: https://phonepixelghost-image-classification-service.hf.space/predict
1. Using cURL
You can test the API from your terminal using this command:
curl -X POST https://phonepixelghost-image-classification-service.hf.space/predict \
-F "file=@test.jpg"
2. Interactive UI
Visit the Space URL to use the modern web interface: Hugging Face Space Demo
Local Development
Adding New Features
- Update code in
app/ - Add tests in
tests/ - Run tests:
pytest tests/ -v - Update documentation
Performance Testing
Use JMeter or similar tools to test throughput:
- Concurrent users: 10, 50, 100
- Measure: TPS, P95 latency, error rate
License
MIT
Acknowledgments
- Model: microsoft/resnet-18 from Hugging Face
- Framework: FastAPI, ONNX Runtime