Spaces:
Sleeping
Sleeping
feat(model): use yolov6.pt
Browse files- requirements.txt +2 -0
- tasks/image.py +116 -8
requirements.txt
CHANGED
|
@@ -13,3 +13,5 @@ ultralytics==8.3.68
|
|
| 13 |
ultralytics-thop==2.0.14
|
| 14 |
#opencv-python==4.11.0.86
|
| 15 |
python-dotenv==1.0.0
|
|
|
|
|
|
|
|
|
| 13 |
ultralytics-thop==2.0.14
|
| 14 |
#opencv-python==4.11.0.86
|
| 15 |
python-dotenv==1.0.0
|
| 16 |
+
onnxruntime==1.19.2
|
| 17 |
+
matplotlib==3.8.1
|
tasks/image.py
CHANGED
|
@@ -112,25 +112,123 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
| 112 |
# Update the code below to replace the random baseline with your model inference
|
| 113 |
#--------------------------------------------------------------------------------------------
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
predictions = []
|
| 121 |
true_labels = []
|
| 122 |
pred_boxes = []
|
| 123 |
true_boxes_list = [] # List of lists, each inner list contains boxes for one image
|
| 124 |
|
| 125 |
n_examples = len(test_dataset)
|
|
|
|
| 126 |
for i, example in enumerate(test_dataset):
|
| 127 |
print(f"Running {i+1} of {n_examples}")
|
|
|
|
| 128 |
# Parse true annotation (YOLO format: class_id x_center y_center width height)
|
| 129 |
annotation = example.get("annotations", "").strip()
|
|
|
|
|
|
|
|
|
|
| 130 |
has_smoke = len(annotation) > 0
|
| 131 |
true_labels.append(int(has_smoke))
|
| 132 |
|
| 133 |
-
model_preds =
|
| 134 |
pred_has_smoke = len(model_preds) > 0
|
| 135 |
predictions.append(int(pred_has_smoke))
|
| 136 |
|
|
@@ -142,12 +240,20 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
| 142 |
true_boxes_list.append(image_true_boxes)
|
| 143 |
|
| 144 |
try:
|
| 145 |
-
pred_box_list
|
|
|
|
|
|
|
|
|
|
| 146 |
except:
|
| 147 |
print("No boxes found")
|
| 148 |
-
|
| 149 |
-
pred_boxes.append(
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
|
| 153 |
#--------------------------------------------------------------------------------------------
|
|
@@ -158,6 +264,8 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
| 158 |
emissions_data = tracker.stop_task()
|
| 159 |
|
| 160 |
# Calculate classification metrics
|
|
|
|
|
|
|
| 161 |
classification_accuracy = accuracy_score(true_labels, predictions)
|
| 162 |
classification_precision = precision_score(true_labels, predictions)
|
| 163 |
classification_recall = recall_score(true_labels, predictions)
|
|
|
|
| 112 |
# Update the code below to replace the random baseline with your model inference
|
| 113 |
#--------------------------------------------------------------------------------------------
|
| 114 |
|
| 115 |
+
import cv2
|
| 116 |
+
import onnxruntime
|
| 117 |
+
import matplotlib.pyplot as plt
|
| 118 |
+
|
| 119 |
+
#PATH_TO_MODEL = 'models/best_YOLOv11n_1280.onnx'
|
| 120 |
+
PATH_TO_MODEL = 'models/best_yolov6n_1280.pt'
|
| 121 |
+
INFERENCE_ENGINE_TYPE = 'pt'
|
| 122 |
+
INPUT_SIZE = 1280
|
| 123 |
+
|
| 124 |
+
def preprocessor(frame):
|
| 125 |
+
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Only when read from file
|
| 126 |
+
x = cv2.resize(frame, (INPUT_SIZE, INPUT_SIZE))
|
| 127 |
+
image_data = np.array(x).astype(np.float32) / 255.0 # Normalize to [0, 1] range
|
| 128 |
+
image_data = np.transpose(image_data, (2, 0, 1)) # (H, W, C) -> (C, H, W)
|
| 129 |
+
image_data = np.expand_dims(image_data, axis=0) # Add batch dimension
|
| 130 |
+
return image_data
|
| 131 |
+
|
| 132 |
+
class Inference:
|
| 133 |
+
def __init__(self, model, image):
|
| 134 |
+
self.session = onnxruntime.InferenceSession(model,#, providers=["CPUExecutionProvider"]
|
| 135 |
+
providers=["CUDAExecutionProvider"]
|
| 136 |
+
)
|
| 137 |
+
model_inputs = self.session.get_inputs()
|
| 138 |
+
input_shape = model_inputs[0].shape
|
| 139 |
+
self.image = image
|
| 140 |
+
self.input_width = input_shape[2]
|
| 141 |
+
self.input_height = input_shape[3]
|
| 142 |
+
self.classes = {0: 'smoke'}
|
| 143 |
+
|
| 144 |
+
def detector(self, image_data):
|
| 145 |
+
ort = onnxruntime.OrtValue.ortvalue_from_numpy(image_data)
|
| 146 |
+
return self.session.run(["output0"], {"images": ort})
|
| 147 |
+
|
| 148 |
+
def postprocessor(self, results, frame, confidence, iou):
|
| 149 |
+
img_height, img_width = frame.shape[:2]
|
| 150 |
+
outputs = np.transpose(np.squeeze(results[0]))
|
| 151 |
+
rows = outputs.shape[0]
|
| 152 |
+
boxes = []
|
| 153 |
+
final_boxes = []
|
| 154 |
+
final_scores = []
|
| 155 |
+
scores = []
|
| 156 |
+
class_ids = []
|
| 157 |
+
x_factor = img_width / self.input_width
|
| 158 |
+
y_factor = img_height / self.input_height
|
| 159 |
+
|
| 160 |
+
max_of_max_scores = 0
|
| 161 |
+
for i in range(rows):
|
| 162 |
+
classes_scores = outputs[i][4:]
|
| 163 |
+
max_score = np.amax(classes_scores)
|
| 164 |
+
if max_score >= confidence:
|
| 165 |
+
class_id = np.argmax(classes_scores)
|
| 166 |
+
x, y, w, h = outputs[i][0], outputs[i][1], outputs[i][2], outputs[i][3]
|
| 167 |
+
# Calculate the scaled coordinates of the bounding box
|
| 168 |
+
left = int(x * x_factor) / img_width
|
| 169 |
+
top = int(y * y_factor) / img_height
|
| 170 |
+
width = int(w * x_factor) / img_width
|
| 171 |
+
height = int(h * y_factor) / img_height
|
| 172 |
+
class_ids.append(class_id)
|
| 173 |
+
scores.append(max_score)
|
| 174 |
+
boxes.append([left, top, width, height])
|
| 175 |
+
max_of_max_scores = max(max_of_max_scores, max_score)
|
| 176 |
+
# Apply non-maximum suppression to filter out overlapping bounding boxes
|
| 177 |
+
indices = cv2.dnn.NMSBoxes(boxes, scores, confidence, iou)
|
| 178 |
+
for i in indices:
|
| 179 |
+
box = boxes[i]
|
| 180 |
+
score = scores[i]
|
| 181 |
+
class_id = class_ids[i]
|
| 182 |
+
final_boxes.append(box)
|
| 183 |
+
final_scores.append(score)
|
| 184 |
+
return frame, final_boxes, final_scores
|
| 185 |
+
|
| 186 |
+
def pipeline(self):
|
| 187 |
+
if isinstance(self.image, str):
|
| 188 |
+
frame = cv2.imread(self.image)
|
| 189 |
+
else:
|
| 190 |
+
frame = np.array(self.image)
|
| 191 |
+
preprocessed = preprocessor(frame)
|
| 192 |
+
detected = self.detector(preprocessed)
|
| 193 |
+
frame, boxes, scores = self.postprocessor(detected, frame, 0.20,0.20)
|
| 194 |
+
return frame, boxes, scores
|
| 195 |
|
| 196 |
+
def predict(inference_engine_type, image, path_to_model):
|
| 197 |
+
if inference_engine_type == 'pt':
|
| 198 |
+
print("INFO - Using pytorch model")
|
| 199 |
+
inference_engine = YOLO(path_to_model)
|
| 200 |
+
boxes = inference_engine.predict(image)[0].boxes.xywhn.tolist()
|
| 201 |
+
confidences = inference_engine.predict(image)[0].boxes.conf.tolist()
|
| 202 |
+
return boxes, confidences
|
| 203 |
+
|
| 204 |
+
elif inference_engine_type == 'onnx':
|
| 205 |
+
print("INFO -Using onnx model")
|
| 206 |
+
inference_engine = Inference(path_to_model, image)
|
| 207 |
+
_, boxes, scores = inference_engine.pipeline()
|
| 208 |
+
return boxes, scores
|
| 209 |
+
else:
|
| 210 |
+
raise ValueError(f"Invalid inference engine type: {inference_engine_type}")
|
| 211 |
+
|
| 212 |
+
print("Starting inference")
|
| 213 |
predictions = []
|
| 214 |
true_labels = []
|
| 215 |
pred_boxes = []
|
| 216 |
true_boxes_list = [] # List of lists, each inner list contains boxes for one image
|
| 217 |
|
| 218 |
n_examples = len(test_dataset)
|
| 219 |
+
n_boxes = []
|
| 220 |
for i, example in enumerate(test_dataset):
|
| 221 |
print(f"Running {i+1} of {n_examples}")
|
| 222 |
+
|
| 223 |
# Parse true annotation (YOLO format: class_id x_center y_center width height)
|
| 224 |
annotation = example.get("annotations", "").strip()
|
| 225 |
+
n_annotations = len(annotation.split("\n"))
|
| 226 |
+
n_boxes.append(n_annotations)
|
| 227 |
+
|
| 228 |
has_smoke = len(annotation) > 0
|
| 229 |
true_labels.append(int(has_smoke))
|
| 230 |
|
| 231 |
+
model_preds, scores = predict(INFERENCE_ENGINE_TYPE, example['image'], PATH_TO_MODEL)
|
| 232 |
pred_has_smoke = len(model_preds) > 0
|
| 233 |
predictions.append(int(pred_has_smoke))
|
| 234 |
|
|
|
|
| 240 |
true_boxes_list.append(image_true_boxes)
|
| 241 |
|
| 242 |
try:
|
| 243 |
+
print("pred_box_list", model_preds) # With one bbox to start with (as in the random baseline)
|
| 244 |
+
model_preds = model_preds[0]
|
| 245 |
+
if len(model_preds) < 1:
|
| 246 |
+
model_preds = [0, 0, 0, 0]
|
| 247 |
except:
|
| 248 |
print("No boxes found")
|
| 249 |
+
model_preds = [0, 0, 0, 0] # Hacky way to make sure that compute_max_iou doesn't fail
|
| 250 |
+
pred_boxes.append(model_preds)
|
| 251 |
|
| 252 |
+
if i == 100000:
|
| 253 |
+
from collections import Counter
|
| 254 |
+
n_box_distr = Counter(n_boxes)
|
| 255 |
+
print(n_box_distr)
|
| 256 |
+
break
|
| 257 |
|
| 258 |
|
| 259 |
#--------------------------------------------------------------------------------------------
|
|
|
|
| 264 |
emissions_data = tracker.stop_task()
|
| 265 |
|
| 266 |
# Calculate classification metrics
|
| 267 |
+
print("true_labels", true_labels)
|
| 268 |
+
print("predictions", predictions)
|
| 269 |
classification_accuracy = accuracy_score(true_labels, predictions)
|
| 270 |
classification_precision = precision_score(true_labels, predictions)
|
| 271 |
classification_recall = recall_score(true_labels, predictions)
|