Spaces:
Sleeping
Sleeping
Update tasks/image.py
Browse files- tasks/image.py +40 -23
tasks/image.py
CHANGED
@@ -5,7 +5,7 @@ import numpy as np
|
|
5 |
from sklearn.metrics import accuracy_score
|
6 |
import random
|
7 |
import os
|
8 |
-
|
9 |
from .utils.evaluation import ImageEvaluationRequest
|
10 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
11 |
|
@@ -17,6 +17,8 @@ router = APIRouter()
|
|
17 |
DESCRIPTION = "Random Baseline"
|
18 |
ROUTE = "/image"
|
19 |
|
|
|
|
|
20 |
def parse_boxes(annotation_string):
|
21 |
"""Parse multiple boxes from a single annotation string.
|
22 |
Each box has 5 values: class_id, x_center, y_center, width, height"""
|
@@ -102,35 +104,50 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
102 |
|
103 |
predictions = []
|
104 |
true_labels = []
|
105 |
-
pred_boxes = []
|
106 |
-
true_boxes_list = []
|
107 |
|
108 |
for example in test_dataset:
|
109 |
-
#
|
|
|
110 |
annotation = example.get("annotations", "").strip()
|
111 |
-
has_smoke = len(annotation) > 0
|
112 |
-
true_labels.append(int(has_smoke))
|
113 |
-
|
114 |
-
# Make random classification prediction
|
115 |
-
pred_has_smoke = random.random() > 0.5
|
116 |
-
predictions.append(int(pred_has_smoke))
|
117 |
|
118 |
-
|
|
|
|
|
|
|
|
|
119 |
if has_smoke:
|
120 |
-
# Parse all true boxes from the annotation
|
121 |
image_true_boxes = parse_boxes(annotation)
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
#--------------------------------------------------------------------------------------------
|
135 |
# YOUR MODEL INFERENCE STOPS HERE
|
136 |
#--------------------------------------------------------------------------------------------
|
|
|
5 |
from sklearn.metrics import accuracy_score
|
6 |
import random
|
7 |
import os
|
8 |
+
from ultralytics import YOLO
|
9 |
from .utils.evaluation import ImageEvaluationRequest
|
10 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
11 |
|
|
|
17 |
DESCRIPTION = "Random Baseline"
|
18 |
ROUTE = "/image"
|
19 |
|
20 |
+
model = YOLO("test_best.pt")
|
21 |
+
|
22 |
def parse_boxes(annotation_string):
|
23 |
"""Parse multiple boxes from a single annotation string.
|
24 |
Each box has 5 values: class_id, x_center, y_center, width, height"""
|
|
|
104 |
|
105 |
predictions = []
|
106 |
true_labels = []
|
107 |
+
pred_boxes = []
|
108 |
+
true_boxes_list = []
|
109 |
|
110 |
for example in test_dataset:
|
111 |
+
# Extract image and annotations
|
112 |
+
image = example["image"]
|
113 |
annotation = example.get("annotations", "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
|
116 |
+
has_smoke = len(annotation) > 0
|
117 |
+
true_labels.append(1 if has_smoke else 0)
|
118 |
+
|
119 |
+
|
120 |
if has_smoke:
|
|
|
121 |
image_true_boxes = parse_boxes(annotation)
|
122 |
+
if image_true_boxes:
|
123 |
+
true_boxes_list.append(image_true_boxes)
|
124 |
+
else:
|
125 |
+
true_boxes_list.append([])
|
126 |
+
else:
|
127 |
+
true_boxes_list.append([])
|
128 |
+
|
129 |
+
results = model.predict(image, verbose=False) # INFERENCE - prediction
|
130 |
+
|
131 |
+
if len(results[0].boxes):
|
132 |
+
pred_box = results[0].boxes.xywhn[0].cpu().numpy().tolist()
|
133 |
+
predictions.append(1)
|
134 |
+
pred_boxes.append(pred_box)
|
135 |
+
else:
|
136 |
+
predictions.append(0)
|
137 |
+
pred_boxes.append([])
|
138 |
+
|
139 |
+
filtered_true_boxes_list = []
|
140 |
+
filtered_pred_boxes = []
|
141 |
|
142 |
+
for true_boxes, pred_boxes_entry in zip(true_boxes_list, pred_boxes): # Only see when annotation(s) is/are both on true label and prediction
|
143 |
+
if true_boxes and pred_boxes_entry:
|
144 |
+
filtered_true_boxes_list.append(true_boxes)
|
145 |
+
filtered_pred_boxes.append(pred_boxes_entry)
|
146 |
+
|
147 |
+
|
148 |
+
true_boxes_list = filtered_true_boxes_list
|
149 |
+
pred_boxes = filtered_pred_boxes
|
150 |
+
|
151 |
#--------------------------------------------------------------------------------------------
|
152 |
# YOUR MODEL INFERENCE STOPS HERE
|
153 |
#--------------------------------------------------------------------------------------------
|