Renato Sortino
commited on
Commit
·
25a9dc3
1
Parent(s):
5607a63
Added model evaluation code
Browse files- tasks/image.py +26 -23
tasks/image.py
CHANGED
@@ -1,20 +1,24 @@
|
|
1 |
-
|
|
|
2 |
from datetime import datetime
|
3 |
-
|
4 |
import numpy as np
|
|
|
|
|
|
|
|
|
5 |
from sklearn.metrics import accuracy_score, precision_score, recall_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 |
|
12 |
-
from dotenv import load_dotenv
|
13 |
load_dotenv()
|
14 |
|
15 |
router = APIRouter()
|
16 |
|
17 |
-
DESCRIPTION = "
|
18 |
ROUTE = "/image"
|
19 |
|
20 |
def parse_boxes(annotation_string):
|
@@ -90,6 +94,10 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
90 |
# Split dataset
|
91 |
train_test = dataset["train"].train_test_split(test_size=request.test_size, seed=request.test_seed)
|
92 |
test_dataset = train_test["test"]
|
|
|
|
|
|
|
|
|
93 |
|
94 |
# Start tracking emissions
|
95 |
tracker.start()
|
@@ -111,25 +119,20 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
111 |
has_smoke = len(annotation) > 0
|
112 |
true_labels.append(int(has_smoke))
|
113 |
|
114 |
-
# Make
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
117 |
|
118 |
-
# If there's a true box,
|
119 |
-
|
|
|
120 |
# Parse all true boxes from the annotation
|
121 |
image_true_boxes = parse_boxes(annotation)
|
122 |
true_boxes_list.append(image_true_boxes)
|
123 |
-
|
124 |
-
# For baseline, make one random box prediction per image
|
125 |
-
# In a real model, you might want to predict multiple boxes
|
126 |
-
random_box = [
|
127 |
-
random.random(), # x_center
|
128 |
-
random.random(), # y_center
|
129 |
-
random.random() * 0.5, # width (max 0.5)
|
130 |
-
random.random() * 0.5 # height (max 0.5)
|
131 |
-
]
|
132 |
-
pred_boxes.append(random_box)
|
133 |
|
134 |
#--------------------------------------------------------------------------------------------
|
135 |
# YOUR MODEL INFERENCE STOPS HERE
|
@@ -137,7 +140,7 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
137 |
|
138 |
# Stop tracking emissions
|
139 |
emissions_data = tracker.stop_task()
|
140 |
-
|
141 |
# Calculate classification metrics
|
142 |
classification_accuracy = accuracy_score(true_labels, predictions)
|
143 |
classification_precision = precision_score(true_labels, predictions)
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
from datetime import datetime
|
4 |
+
|
5 |
import numpy as np
|
6 |
+
import torch
|
7 |
+
from datasets import load_dataset
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
from fastapi import APIRouter
|
10 |
from sklearn.metrics import accuracy_score, precision_score, recall_score
|
|
|
|
|
11 |
|
12 |
+
from ultralytics import YOLOv10
|
13 |
+
|
14 |
+
from .utils.emissions import clean_emissions_data, get_space_info, tracker
|
15 |
from .utils.evaluation import ImageEvaluationRequest
|
|
|
16 |
|
|
|
17 |
load_dotenv()
|
18 |
|
19 |
router = APIRouter()
|
20 |
|
21 |
+
DESCRIPTION = "YOLO-EFD"
|
22 |
ROUTE = "/image"
|
23 |
|
24 |
def parse_boxes(annotation_string):
|
|
|
94 |
# Split dataset
|
95 |
train_test = dataset["train"].train_test_split(test_size=request.test_size, seed=request.test_seed)
|
96 |
test_dataset = train_test["test"]
|
97 |
+
|
98 |
+
model = YOLOv10("best.pt")
|
99 |
+
device = "cuda"
|
100 |
+
model = model.to(device)
|
101 |
|
102 |
# Start tracking emissions
|
103 |
tracker.start()
|
|
|
119 |
has_smoke = len(annotation) > 0
|
120 |
true_labels.append(int(has_smoke))
|
121 |
|
122 |
+
# Make prediction with model
|
123 |
+
image = example['image']
|
124 |
+
with torch.inference_mode():
|
125 |
+
pred = model(image)[0]
|
126 |
+
smoke_detected = pred.boxes.xywhn.shape[0] > 0
|
127 |
+
predictions.append(int(smoke_detected))
|
128 |
|
129 |
+
# If there's a true box, and at least one box is predicted, parse them
|
130 |
+
# If one of the two boxes is empty, mIoU computation fails
|
131 |
+
if has_smoke and smoke_detected:
|
132 |
# Parse all true boxes from the annotation
|
133 |
image_true_boxes = parse_boxes(annotation)
|
134 |
true_boxes_list.append(image_true_boxes)
|
135 |
+
pred_boxes.append(pred.boxes.xywhn.tolist()[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
#--------------------------------------------------------------------------------------------
|
138 |
# YOUR MODEL INFERENCE STOPS HERE
|
|
|
140 |
|
141 |
# Stop tracking emissions
|
142 |
emissions_data = tracker.stop_task()
|
143 |
+
|
144 |
# Calculate classification metrics
|
145 |
classification_accuracy = accuracy_score(true_labels, predictions)
|
146 |
classification_precision = precision_score(true_labels, predictions)
|