Fix Python 3.9 compatibility - replace union operator with Union type
Browse files- inference_server.py +127 -51
inference_server.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
from fastapi import FastAPI, File, UploadFile
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
from typing import List
|
| 4 |
import torch
|
| 5 |
from transformers import AutoImageProcessor, AutoModel
|
| 6 |
from ultralytics import YOLO
|
|
@@ -8,68 +8,144 @@ import faiss
|
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
import io
|
|
|
|
| 11 |
|
| 12 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Load models and index ONCE at startup
|
| 15 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 16 |
-
processor = AutoImageProcessor.from_pretrained('facebook/dinov2-base')
|
| 17 |
-
dino_model = AutoModel.from_pretrained('facebook/dinov2-base').to(device)
|
| 18 |
-
yolo_model = YOLO("models/deepfashion2_yolov8s-seg.pt")
|
| 19 |
-
faiss_index = faiss.read_index("index/jersey_index.faiss")
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
class FeaturesRequest(BaseModel):
|
| 30 |
-
features: List[float]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
@app.post("/dino")
|
| 33 |
async def dino_inference(file: UploadFile = File(...)):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
@app.post("/faiss")
|
| 43 |
async def faiss_search(request: FeaturesRequest):
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
@app.post("/yolo")
|
| 67 |
async def yolo_inference(file: UploadFile = File(...)):
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from typing import List, Union
|
| 4 |
import torch
|
| 5 |
from transformers import AutoImageProcessor, AutoModel
|
| 6 |
from ultralytics import YOLO
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
import io
|
| 11 |
+
import os
|
| 12 |
|
| 13 |
+
app = FastAPI(
|
| 14 |
+
title="Devam Jersey Server",
|
| 15 |
+
description="Jersey similarity and detection server using YOLOv8 and DINOv2",
|
| 16 |
+
version="1.0.0"
|
| 17 |
+
)
|
| 18 |
|
| 19 |
# Load models and index ONCE at startup
|
| 20 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Initialize models with error handling
|
| 23 |
+
try:
|
| 24 |
+
processor = AutoImageProcessor.from_pretrained('facebook/dinov2-base')
|
| 25 |
+
dino_model = AutoModel.from_pretrained('facebook/dinov2-base').to(device)
|
| 26 |
+
print(f"β
DINOv2 model loaded successfully on {device}")
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"β Error loading DINOv2 model: {e}")
|
| 29 |
+
processor = None
|
| 30 |
+
dino_model = None
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
yolo_model = YOLO("models/deepfashion2_yolov8s-seg.pt")
|
| 34 |
+
print("β
YOLOv8 model loaded successfully")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"β Error loading YOLOv8 model: {e}")
|
| 37 |
+
yolo_model = None
|
| 38 |
+
|
| 39 |
+
# Initialize FAISS index and metadata
|
| 40 |
+
faiss_index = None
|
| 41 |
+
index_to_path = {}
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
if os.path.exists("index/jersey_index.faiss"):
|
| 45 |
+
faiss_index = faiss.read_index("index/jersey_index.faiss")
|
| 46 |
+
print("β
FAISS index loaded successfully")
|
| 47 |
+
else:
|
| 48 |
+
# Create a dummy index if the real one doesn't exist
|
| 49 |
+
print("β οΈ FAISS index not found, creating dummy index")
|
| 50 |
+
dimension = 768 # DINOv2 base dimension
|
| 51 |
+
faiss_index = faiss.IndexFlatIP(dimension)
|
| 52 |
+
faiss_index.add(np.random.random((100, dimension)).astype('float32'))
|
| 53 |
+
|
| 54 |
+
if os.path.exists("index/jersey_metadata.npy"):
|
| 55 |
+
loaded_data = np.load("index/jersey_metadata.npy", allow_pickle=True)
|
| 56 |
+
if isinstance(loaded_data, dict):
|
| 57 |
+
index_to_path = {int(k): v for k, v in loaded_data.items()}
|
| 58 |
+
elif isinstance(loaded_data, np.ndarray):
|
| 59 |
+
index_to_path = {i: str(item) for i, item in enumerate(loaded_data)}
|
| 60 |
+
print("β
Jersey metadata loaded successfully")
|
| 61 |
+
else:
|
| 62 |
+
print("β οΈ Jersey metadata not found, using dummy data")
|
| 63 |
+
index_to_path = {i: f"jersey_{i}.jpg" for i in range(100)}
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"β Error loading FAISS index or metadata: {e}")
|
| 67 |
|
| 68 |
class FeaturesRequest(BaseModel):
|
| 69 |
+
features: Union[List[float], List[List[float]]]
|
| 70 |
+
|
| 71 |
+
@app.get("/")
|
| 72 |
+
async def root():
|
| 73 |
+
return {
|
| 74 |
+
"message": "Devam Jersey Server",
|
| 75 |
+
"status": "running",
|
| 76 |
+
"endpoints": ["/dino", "/faiss", "/yolo"],
|
| 77 |
+
"models_loaded": {
|
| 78 |
+
"dino": dino_model is not None,
|
| 79 |
+
"yolo": yolo_model is not None,
|
| 80 |
+
"faiss": faiss_index is not None
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
|
| 84 |
@app.post("/dino")
|
| 85 |
async def dino_inference(file: UploadFile = File(...)):
|
| 86 |
+
if dino_model is None:
|
| 87 |
+
raise HTTPException(status_code=500, detail="DINOv2 model not loaded")
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
image_bytes = await file.read()
|
| 91 |
+
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
|
| 92 |
+
with torch.no_grad():
|
| 93 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 94 |
+
outputs = dino_model(**inputs)
|
| 95 |
+
features = outputs.last_hidden_state.mean(dim=1).detach().cpu().numpy()[0]
|
| 96 |
+
return {"features": features.tolist()}
|
| 97 |
+
except Exception as e:
|
| 98 |
+
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 99 |
|
| 100 |
@app.post("/faiss")
|
| 101 |
async def faiss_search(request: FeaturesRequest):
|
| 102 |
+
if faiss_index is None:
|
| 103 |
+
raise HTTPException(status_code=500, detail="FAISS index not loaded")
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
features = request.features
|
| 107 |
+
if isinstance(features[0], list):
|
| 108 |
+
vector = np.array(features, dtype=np.float32)
|
| 109 |
+
else:
|
| 110 |
+
vector = np.array([features], dtype=np.float32)
|
| 111 |
+
|
| 112 |
+
if vector.shape[1] != faiss_index.d:
|
| 113 |
+
error_msg = f"Feature vector length {vector.shape[1]} does not match FAISS index dimension {faiss_index.d}"
|
| 114 |
+
return {"error": error_msg}
|
| 115 |
+
|
| 116 |
+
faiss.normalize_L2(vector)
|
| 117 |
+
distances, indices = faiss_index.search(vector, 15)
|
| 118 |
+
results = []
|
| 119 |
+
for i, (distance, idx) in enumerate(zip(distances[0], indices[0])):
|
| 120 |
+
if idx in index_to_path:
|
| 121 |
+
key = idx
|
| 122 |
+
results.append({
|
| 123 |
+
"rank": i + 1,
|
| 124 |
+
"distance": float(distance),
|
| 125 |
+
"file_path": index_to_path[key],
|
| 126 |
+
"full_path": f"catalogue/{index_to_path[key]}"
|
| 127 |
+
})
|
| 128 |
+
return {"results": results}
|
| 129 |
+
except Exception as e:
|
| 130 |
+
raise HTTPException(status_code=500, detail=f"Error searching FAISS index: {str(e)}")
|
| 131 |
|
| 132 |
@app.post("/yolo")
|
| 133 |
async def yolo_inference(file: UploadFile = File(...)):
|
| 134 |
+
if yolo_model is None:
|
| 135 |
+
raise HTTPException(status_code=500, detail="YOLOv8 model not loaded")
|
| 136 |
+
|
| 137 |
+
try:
|
| 138 |
+
image_bytes = await file.read()
|
| 139 |
+
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
|
| 140 |
+
results = yolo_model(image, device=0 if torch.cuda.is_available() else 'cpu', verbose=False)[0]
|
| 141 |
+
polygons = []
|
| 142 |
+
if hasattr(results, 'masks') and results.masks is not None and hasattr(results.masks, 'xy'):
|
| 143 |
+
for mask in results.masks.xy:
|
| 144 |
+
polygons.append(mask.tolist())
|
| 145 |
+
return {"polygons": polygons}
|
| 146 |
+
except Exception as e:
|
| 147 |
+
raise HTTPException(status_code=500, detail=f"Error processing image with YOLO: {str(e)}")
|
| 148 |
+
|
| 149 |
+
if __name__ == "__main__":
|
| 150 |
+
import uvicorn
|
| 151 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|