Spaces:
Sleeping
Sleeping
Update tasks/image.py
Browse files- tasks/image.py +30 -13
tasks/image.py
CHANGED
@@ -5,6 +5,7 @@ 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
|
@@ -14,7 +15,7 @@ load_dotenv()
|
|
14 |
|
15 |
router = APIRouter()
|
16 |
|
17 |
-
DESCRIPTION = "
|
18 |
ROUTE = "/image"
|
19 |
|
20 |
def parse_boxes(annotation_string):
|
@@ -67,6 +68,17 @@ def compute_max_iou(true_boxes, pred_box):
|
|
67 |
max_iou = max(max_iou, iou)
|
68 |
return max_iou
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
@router.post(ROUTE, tags=["Image Task"],
|
71 |
description=DESCRIPTION)
|
72 |
async def evaluate_image(request: ImageEvaluationRequest):
|
@@ -99,20 +111,28 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
99 |
# YOUR MODEL INFERENCE CODE HERE
|
100 |
# Update the code below to replace the random baseline with your model inference
|
101 |
#--------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
predictions = []
|
104 |
true_labels = []
|
105 |
pred_boxes = []
|
106 |
true_boxes_list = [] # List of lists, each inner list contains boxes for one image
|
107 |
|
108 |
-
|
|
|
|
|
109 |
# Parse true annotation (YOLO format: class_id x_center y_center width height)
|
110 |
annotation = example.get("annotations", "").strip()
|
111 |
has_smoke = len(annotation) > 0
|
112 |
true_labels.append(int(has_smoke))
|
113 |
|
114 |
-
# Make
|
115 |
-
|
|
|
116 |
predictions.append(int(pred_has_smoke))
|
117 |
|
118 |
# If there's a true box, parse it and make random box prediction
|
@@ -121,15 +141,12 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
121 |
image_true_boxes = parse_boxes(annotation)
|
122 |
true_boxes_list.append(image_true_boxes)
|
123 |
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
random.random() * 0.5 # height (max 0.5)
|
131 |
-
]
|
132 |
-
pred_boxes.append(random_box)
|
133 |
|
134 |
#--------------------------------------------------------------------------------------------
|
135 |
# YOUR MODEL INFERENCE STOPS HERE
|
|
|
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
|
|
|
15 |
|
16 |
router = APIRouter()
|
17 |
|
18 |
+
DESCRIPTION = f"MountAIn Small model 640"
|
19 |
ROUTE = "/image"
|
20 |
|
21 |
def parse_boxes(annotation_string):
|
|
|
68 |
max_iou = max(max_iou, iou)
|
69 |
return max_iou
|
70 |
|
71 |
+
def load_model(path_to_model, model_type="YOLO"):
|
72 |
+
if model_type == "YOLO":
|
73 |
+
model = YOLO(path_to_model)
|
74 |
+
else:
|
75 |
+
raise NotImplementedError
|
76 |
+
return model
|
77 |
+
|
78 |
+
def get_boxes_list(predictions):
|
79 |
+
return [box.tolist() for box in predictions.boxes.xywhn]
|
80 |
+
|
81 |
+
|
82 |
@router.post(ROUTE, tags=["Image Task"],
|
83 |
description=DESCRIPTION)
|
84 |
async def evaluate_image(request: ImageEvaluationRequest):
|
|
|
111 |
# YOUR MODEL INFERENCE CODE HERE
|
112 |
# Update the code below to replace the random baseline with your model inference
|
113 |
#--------------------------------------------------------------------------------------------
|
114 |
+
|
115 |
+
PATH_TO_MODEL = f"models/best-mountain-s.pt"
|
116 |
+
model = load_model(PATH_TO_MODEL)
|
117 |
+
|
118 |
+
print(f"Model info: {model.info()}")
|
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 |
+
# Make model prediction
|
134 |
+
model_preds = model(example['image'])[0]
|
135 |
+
pred_has_smoke = len(model_preds) > 0
|
136 |
predictions.append(int(pred_has_smoke))
|
137 |
|
138 |
# If there's a true box, parse it and make random box prediction
|
|
|
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 |
# YOUR MODEL INFERENCE STOPS HERE
|