File size: 4,936 Bytes
921f471
 
 
 
 
 
 
 
 
17d2f7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaf193e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17d2f7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
---
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

```bash
pip install -r requirements.txt
```

### 2. Prepare the Model

Run the optimization scripts in order:

```bash
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

```bash
uvicorn app.main:app --host 0.0.0.0 --port 7860
```

### 4. Test the API

```bash
# 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

```bash
docker build -t image-classifier .
docker run -p 7860:7860 image-classifier
```

## Testing

```bash
pytest tests/ -v
```

## API Endpoints

### GET /health

Health check endpoint.

**Response:**
```json
{
  "status": "ok"
}
```

### POST /predict

Image classification endpoint.

**Request:**
- Content-Type: `multipart/form-data`
- Body: `file` (image file)

**Response:**
```json
{
  "label": "tabby, tabby cat",
  "score": 0.8234,
  "label_id": 281,
  "inference_time_ms": 45.123
}
```

**Error Codes:**
- `400`: Corrupted or invalid image
- `413`: File too large (max 10MB)
- `415`: Unsupported media type
- `500`: 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:
1. Runs unit tests on every push/PR
2. Deploys to Hugging Face Spaces on main branch (requires `HF_TOKEN` secret)

### Setup Hugging Face Deployment

1. Create a Hugging Face Space
2. Generate an access token with write permissions
3. Add `HF_TOKEN` to GitHub repository secrets
4. Update `.github/workflows/ci-cd.yml` with 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:

```bash
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](https://huggingface.co/spaces/PhonePixelGhost/Image_Classification_Service)

## Local Development

### Adding New Features

1. Update code in `app/`
2. Add tests in `tests/`
3. Run tests: `pytest tests/ -v`
4. 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