Spaces:
Sleeping
Sleeping
Update api_server.py
Browse files- api_server.py +59 -39
api_server.py
CHANGED
@@ -2,7 +2,7 @@ import os
|
|
2 |
import time
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
-
|
6 |
from pathlib import Path
|
7 |
|
8 |
# Disable tensorflow warnings
|
@@ -10,35 +10,37 @@ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
|
10 |
|
11 |
from tensorflow import keras
|
12 |
from flask import Flask, jsonify, request, render_template
|
|
|
13 |
|
14 |
-
load_type = '
|
15 |
-
"""
|
16 |
-
local;
|
17 |
-
remote_hub_download;
|
18 |
-
remote_hub_from_pretrained;
|
19 |
-
remote_hub_pipeline; - needs config.json and this is not easy to grasp how to do it with custom models
|
20 |
-
https://discuss.huggingface.co/t/how-to-create-a-config-json-after-saving-a-model/10459/4
|
21 |
-
"""
|
22 |
|
23 |
-
|
24 |
MODEL_DIR = "./artifacts/models"
|
|
|
25 |
|
26 |
-
# Load the saved model into memory
|
27 |
if load_type == 'local':
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
elif load_type == 'remote_hub_download':
|
30 |
from huggingface_hub import hf_hub_download
|
31 |
|
32 |
-
|
|
|
|
|
|
|
33 |
elif load_type == 'remote_hub_from_pretrained':
|
34 |
-
#
|
35 |
os.environ['TRANSFORMERS_CACHE'] = str(Path(MODEL_DIR).absolute())
|
36 |
-
from huggingface_hub import
|
37 |
-
model = from_pretrained_keras(REPO_ID, cache_dir=MODEL_DIR)
|
38 |
-
elif load_type == 'remote_hub_pipeline':
|
39 |
-
from transformers import pipeline
|
40 |
|
41 |
-
model =
|
|
|
42 |
else:
|
43 |
raise AssertionError('No load type is specified!')
|
44 |
|
@@ -46,7 +48,7 @@ else:
|
|
46 |
app = Flask(__name__)
|
47 |
|
48 |
|
49 |
-
# API route for prediction
|
50 |
@app.route('/predict', methods=['POST'])
|
51 |
def predict():
|
52 |
"""
|
@@ -76,27 +78,35 @@ def predict():
|
|
76 |
# Get pixels out of file
|
77 |
image_data = Image.open(file)
|
78 |
|
79 |
-
# Check image shape
|
80 |
-
if image_data.size != (28, 28):
|
81 |
-
|
82 |
|
83 |
# Preprocess the image
|
84 |
processed_image = preprocess_image(image_data)
|
85 |
|
86 |
-
# Make a prediction
|
87 |
-
|
88 |
-
|
89 |
-
#
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
# Calculate latency in milliseconds
|
94 |
latency_ms = (time.time() - start_time) * 1000
|
95 |
|
96 |
-
# Return the
|
97 |
response = {
|
98 |
-
'
|
99 |
-
'pred_proba': float(proba),
|
100 |
'ml-latency-ms': round(latency_ms, 4)
|
101 |
}
|
102 |
|
@@ -108,16 +118,26 @@ def predict():
|
|
108 |
|
109 |
# Helper function to preprocess the image
|
110 |
def preprocess_image(image_data):
|
111 |
-
"""Preprocess image for Model Inference
|
112 |
|
113 |
-
:param image_data: Raw image
|
114 |
-
:return: image: Preprocessed Image
|
115 |
"""
|
116 |
-
#
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
-
#
|
120 |
-
image = image.
|
121 |
|
122 |
return image
|
123 |
|
|
|
2 |
import time
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
+
import torchvision.transforms as transforms
|
6 |
from pathlib import Path
|
7 |
|
8 |
# Disable tensorflow warnings
|
|
|
10 |
|
11 |
from tensorflow import keras
|
12 |
from flask import Flask, jsonify, request, render_template
|
13 |
+
import torch
|
14 |
|
15 |
+
load_type = 'local'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
MODEL_NAME = "yolo11_detect_best_241018_1.pt"
|
18 |
MODEL_DIR = "./artifacts/models"
|
19 |
+
#REPO_ID = "1vash/mnist_demo_model"
|
20 |
|
21 |
+
# Load the saved YOLO model into memory
|
22 |
if load_type == 'local':
|
23 |
+
# 本地模型路徑
|
24 |
+
model_path = f'{MODEL_DIR}/{MODEL_NAME}'
|
25 |
+
if not os.path.exists(model_path):
|
26 |
+
raise FileNotFoundError(f"Model file not found at {model_path}")
|
27 |
+
# 使用 torch 來載入 YOLO 模型
|
28 |
+
model = torch.load(model_path)
|
29 |
+
model.eval() # 設定模型為推理模式
|
30 |
elif load_type == 'remote_hub_download':
|
31 |
from huggingface_hub import hf_hub_download
|
32 |
|
33 |
+
# 從 Hugging Face Hub 下載模型
|
34 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_NAME)
|
35 |
+
model = torch.load(model_path)
|
36 |
+
model.eval()
|
37 |
elif load_type == 'remote_hub_from_pretrained':
|
38 |
+
# 使用 Hugging Face Hub 預訓練的模型方式下載
|
39 |
os.environ['TRANSFORMERS_CACHE'] = str(Path(MODEL_DIR).absolute())
|
40 |
+
from huggingface_hub import from_pretrained
|
|
|
|
|
|
|
41 |
|
42 |
+
model = from_pretrained(REPO_ID, filename=MODEL_NAME, cache_dir=MODEL_DIR)
|
43 |
+
model.eval()
|
44 |
else:
|
45 |
raise AssertionError('No load type is specified!')
|
46 |
|
|
|
48 |
app = Flask(__name__)
|
49 |
|
50 |
|
51 |
+
# API route for prediction(YOLO)
|
52 |
@app.route('/predict', methods=['POST'])
|
53 |
def predict():
|
54 |
"""
|
|
|
78 |
# Get pixels out of file
|
79 |
image_data = Image.open(file)
|
80 |
|
81 |
+
# # Check image shape
|
82 |
+
# if image_data.size != (28, 28):
|
83 |
+
# return "Invalid image shape. Expected (28, 28), take from 'demo images' folder."
|
84 |
|
85 |
# Preprocess the image
|
86 |
processed_image = preprocess_image(image_data)
|
87 |
|
88 |
+
# Make a prediction using YOLO
|
89 |
+
results = model(processed_image)
|
90 |
+
|
91 |
+
# Process the YOLO output
|
92 |
+
detections = []
|
93 |
+
for det in results.xyxy[0]: # Assuming results are in xyxy format (xmin, ymin, xmax, ymax, confidence, class)
|
94 |
+
x_min, y_min, x_max, y_max, confidence, class_idx = det
|
95 |
+
width = x_max - x_min
|
96 |
+
height = y_max - y_min
|
97 |
+
detection = {
|
98 |
+
"label": int(class_idx),
|
99 |
+
"confidence": float(confidence),
|
100 |
+
"bbox": [float(x_min), float(y_min), float(width), float(height)]
|
101 |
+
}
|
102 |
+
detections.append(detection)
|
103 |
|
104 |
# Calculate latency in milliseconds
|
105 |
latency_ms = (time.time() - start_time) * 1000
|
106 |
|
107 |
+
# Return the detection results and latency as JSON response
|
108 |
response = {
|
109 |
+
'detections': detections,
|
|
|
110 |
'ml-latency-ms': round(latency_ms, 4)
|
111 |
}
|
112 |
|
|
|
118 |
|
119 |
# Helper function to preprocess the image
|
120 |
def preprocess_image(image_data):
|
121 |
+
"""Preprocess image for YOLO Model Inference
|
122 |
|
123 |
+
:param image_data: Raw image (PIL.Image)
|
124 |
+
:return: image: Preprocessed Image (Tensor)
|
125 |
"""
|
126 |
+
# Define the YOLO input size (example 640x640, you can modify this based on your model)
|
127 |
+
input_size = (640, 640)
|
128 |
+
|
129 |
+
# Define transformation: Resize the image, convert to Tensor, and normalize pixel values
|
130 |
+
transform = transforms.Compose([
|
131 |
+
transforms.Resize(input_size), # Resize to YOLO input size
|
132 |
+
transforms.ToTensor(), # Convert image to PyTorch Tensor (通道數、影像高度和寬度)
|
133 |
+
transforms.Normalize([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]) # Normalization (if needed)
|
134 |
+
])
|
135 |
+
|
136 |
+
# Apply transformations to the image
|
137 |
+
image = transform(image_data)
|
138 |
|
139 |
+
# Add batch dimension (1, C, H, W) since YOLO expects a batch
|
140 |
+
image = image.unsqueeze(0)
|
141 |
|
142 |
return image
|
143 |
|