Spaces:
Sleeping
Sleeping
Update tasks/image.py
Browse files- tasks/image.py +51 -45
tasks/image.py
CHANGED
@@ -2,11 +2,11 @@ from fastapi import APIRouter
|
|
2 |
from datetime import datetime
|
3 |
from datasets import load_dataset
|
4 |
import numpy as np
|
5 |
-
from sklearn.metrics import accuracy_score
|
6 |
import random
|
7 |
import os
|
|
|
8 |
|
9 |
-
from ultralytics import YOLO # Import YOLO
|
10 |
from .utils.evaluation import ImageEvaluationRequest
|
11 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
12 |
|
@@ -15,11 +15,10 @@ load_dotenv()
|
|
15 |
|
16 |
router = APIRouter()
|
17 |
|
18 |
-
|
|
|
19 |
ROUTE = "/image"
|
20 |
|
21 |
-
yolo_model = YOLO("best.pt")
|
22 |
-
|
23 |
def parse_boxes(annotation_string):
|
24 |
"""Parse multiple boxes from a single annotation string.
|
25 |
Each box has 5 values: class_id, x_center, y_center, width, height"""
|
@@ -70,6 +69,16 @@ def compute_max_iou(true_boxes, pred_box):
|
|
70 |
max_iou = max(max_iou, iou)
|
71 |
return max_iou
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
@router.post(ROUTE, tags=["Image Task"],
|
74 |
description=DESCRIPTION)
|
75 |
async def evaluate_image(request: ImageEvaluationRequest):
|
@@ -92,7 +101,7 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
92 |
|
93 |
# Split dataset
|
94 |
train_test = dataset["train"].train_test_split(test_size=request.test_size, seed=request.test_seed)
|
95 |
-
test_dataset =
|
96 |
|
97 |
# Start tracking emissions
|
98 |
tracker.start()
|
@@ -102,52 +111,44 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
102 |
# YOUR MODEL INFERENCE CODE HERE
|
103 |
# Update the code below to replace the random baseline with your model inference
|
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(
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
120 |
if has_smoke:
|
|
|
|
|
121 |
image_true_boxes = parse_boxes(annotation)
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
results = yolo_model.predict(image) # INFERENCE - prediction
|
131 |
|
132 |
-
if len(results[0].boxes):
|
133 |
-
pred_box = results[0].boxes.xywhn[0].cpu().numpy().tolist()
|
134 |
-
predictions.append(1)
|
135 |
-
pred_boxes.append(pred_box)
|
136 |
-
else:
|
137 |
-
predictions.append(0)
|
138 |
-
pred_boxes.append([])
|
139 |
|
140 |
-
filtered_true_boxes_list = []
|
141 |
-
filtered_pred_boxes = []
|
142 |
-
|
143 |
-
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
|
144 |
-
if true_boxes and pred_boxes_entry:
|
145 |
-
filtered_true_boxes_list.append(true_boxes)
|
146 |
-
filtered_pred_boxes.append(pred_boxes_entry)
|
147 |
-
|
148 |
-
|
149 |
-
true_boxes_list = filtered_true_boxes_list
|
150 |
-
pred_boxes = filtered_pred_boxes
|
151 |
|
152 |
#--------------------------------------------------------------------------------------------
|
153 |
# YOUR MODEL INFERENCE STOPS HERE
|
@@ -156,8 +157,10 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
156 |
# Stop tracking emissions
|
157 |
emissions_data = tracker.stop_task()
|
158 |
|
159 |
-
# Calculate classification
|
160 |
classification_accuracy = accuracy_score(true_labels, predictions)
|
|
|
|
|
161 |
|
162 |
# Calculate mean IoU for object detection (only for images with smoke)
|
163 |
# For each image, we compute the max IoU between the predicted box and all true boxes
|
@@ -175,6 +178,8 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
175 |
"submission_timestamp": datetime.now().isoformat(),
|
176 |
"model_description": DESCRIPTION,
|
177 |
"classification_accuracy": float(classification_accuracy),
|
|
|
|
|
178 |
"mean_iou": mean_iou,
|
179 |
"energy_consumed_wh": emissions_data.energy_consumed * 1000,
|
180 |
"emissions_gco2eq": emissions_data.emissions * 1000,
|
@@ -186,4 +191,5 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
186 |
"test_seed": request.test_seed
|
187 |
}
|
188 |
}
|
|
|
189 |
return results
|
|
|
2 |
from datetime import datetime
|
3 |
from datasets import load_dataset
|
4 |
import numpy as np
|
5 |
+
from sklearn.metrics import accuracy_score, precision_score, recall_score
|
6 |
import random
|
7 |
import os
|
8 |
+
from ultralytics import YOLO
|
9 |
|
|
|
10 |
from .utils.evaluation import ImageEvaluationRequest
|
11 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
12 |
|
|
|
15 |
|
16 |
router = APIRouter()
|
17 |
|
18 |
+
# MODEL_TYPE = "YOLOv11n"
|
19 |
+
DESCRIPTION = "YOLOv11"
|
20 |
ROUTE = "/image"
|
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"""
|
|
|
69 |
max_iou = max(max_iou, iou)
|
70 |
return max_iou
|
71 |
|
72 |
+
def load_model(path_to_model, model_type="YOLO"):
|
73 |
+
if model_type == "YOLO":
|
74 |
+
model = YOLO(path_to_model)
|
75 |
+
else:
|
76 |
+
raise NotImplementedError
|
77 |
+
return model
|
78 |
+
|
79 |
+
def get_boxes_list(predictions):
|
80 |
+
return [box.tolist() for box in predictions.boxes.xywhn]
|
81 |
+
|
82 |
@router.post(ROUTE, tags=["Image Task"],
|
83 |
description=DESCRIPTION)
|
84 |
async def evaluate_image(request: ImageEvaluationRequest):
|
|
|
101 |
|
102 |
# Split dataset
|
103 |
train_test = dataset["train"].train_test_split(test_size=request.test_size, seed=request.test_seed)
|
104 |
+
test_dataset = train_test["test"]
|
105 |
|
106 |
# Start tracking emissions
|
107 |
tracker.start()
|
|
|
111 |
# YOUR MODEL INFERENCE CODE HERE
|
112 |
# Update the code below to replace the random baseline with your model inference
|
113 |
#--------------------------------------------------------------------------------------------
|
114 |
+
|
115 |
+
|
116 |
+
PATH_TO_MODEL = f"best.pt"
|
117 |
+
model = load_model(PATH_TO_MODEL)
|
118 |
+
|
119 |
+
print(f"Model info: {model.info()}")
|
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 = model(example['image'])[0]
|
134 |
+
pred_has_smoke = len(model_preds) > 0
|
135 |
+
predictions.append(int(pred_has_smoke))
|
136 |
+
|
137 |
+
# If there's a true box, parse it and make random box prediction
|
138 |
if has_smoke:
|
139 |
+
|
140 |
+
# Parse all true boxes from the annotation
|
141 |
image_true_boxes = parse_boxes(annotation)
|
142 |
+
true_boxes_list.append(image_true_boxes)
|
143 |
+
|
144 |
+
try:
|
145 |
+
pred_box_list = get_boxes_list(model_preds)[0] # With one bbox to start with (as in the random baseline)
|
146 |
+
except:
|
147 |
+
print("No boxes found")
|
148 |
+
pred_box_list = [0, 0, 0, 0] # Hacky way to make sure that compute_max_iou doesn't fail
|
149 |
+
pred_boxes.append(pred_box_list)
|
|
|
150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
#--------------------------------------------------------------------------------------------
|
154 |
# YOUR MODEL INFERENCE STOPS HERE
|
|
|
157 |
# Stop tracking emissions
|
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)
|
164 |
|
165 |
# Calculate mean IoU for object detection (only for images with smoke)
|
166 |
# For each image, we compute the max IoU between the predicted box and all true boxes
|
|
|
178 |
"submission_timestamp": datetime.now().isoformat(),
|
179 |
"model_description": DESCRIPTION,
|
180 |
"classification_accuracy": float(classification_accuracy),
|
181 |
+
"classification_precision": float(classification_precision),
|
182 |
+
"classification_recall": float(classification_recall),
|
183 |
"mean_iou": mean_iou,
|
184 |
"energy_consumed_wh": emissions_data.energy_consumed * 1000,
|
185 |
"emissions_gco2eq": emissions_data.emissions * 1000,
|
|
|
191 |
"test_seed": request.test_seed
|
192 |
}
|
193 |
}
|
194 |
+
|
195 |
return results
|