Spaces:
Sleeping
Sleeping
Final try
Browse files- tasks/image.py +15 -13
tasks/image.py
CHANGED
@@ -114,36 +114,38 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
114 |
logging.info(f"Loading model {model_name}")
|
115 |
model = YOLO(Path(model_path, model_name), task="detect")
|
116 |
device_name = device("cuda" if is_available() else "cpu")
|
|
|
117 |
|
|
|
|
|
|
|
|
|
|
|
118 |
predictions = []
|
119 |
-
|
120 |
pred_boxes = []
|
121 |
-
|
122 |
-
|
123 |
logging.info(f"Inference start on device: {device_name}")
|
124 |
-
for example in test_dataset:
|
125 |
-
|
126 |
-
annotation =
|
127 |
-
has_smoke = len(annotation) > 0
|
128 |
-
true_labels.append(int(has_smoke))
|
129 |
|
130 |
# Make prediction
|
131 |
results = model.predict(example["image"], device=device_name, conf=THRESHOLD, verbose=False, imgsz=IMGSIZE)[0]
|
132 |
pred_has_smoke = len(results) > 0
|
133 |
predictions.append(int(pred_has_smoke))
|
134 |
-
|
135 |
# If there's a true box, parse it and add box prediction
|
136 |
if has_smoke:
|
137 |
# Parse all true boxes from the annotation
|
138 |
image_true_boxes = parse_boxes(annotation)
|
139 |
true_boxes_list.append(image_true_boxes)
|
140 |
|
141 |
-
# Append
|
142 |
-
|
143 |
-
if results.boxes.cls.numel()!=0:
|
144 |
pred_boxes.append(results.boxes[0].xywhn.tolist()[0])
|
145 |
else:
|
146 |
-
pred_boxes.append([0,0,0,0])
|
147 |
|
148 |
#--------------------------------------------------------------------------------------------
|
149 |
# YOUR MODEL INFERENCE STOPS HERE
|
|
|
114 |
logging.info(f"Loading model {model_name}")
|
115 |
model = YOLO(Path(model_path, model_name), task="detect")
|
116 |
device_name = device("cuda" if is_available() else "cpu")
|
117 |
+
model.to(device_name)
|
118 |
|
119 |
+
# Preprocessing the annotations before the loop to avoid repeated parsing
|
120 |
+
annotations = [example.get("annotations", "").strip() for example in test_dataset]
|
121 |
+
true_labels = [int(len(ann) > 0) for ann in annotations]
|
122 |
+
|
123 |
+
# Initialize lists
|
124 |
predictions = []
|
125 |
+
true_boxes_list = []
|
126 |
pred_boxes = []
|
127 |
+
|
|
|
128 |
logging.info(f"Inference start on device: {device_name}")
|
129 |
+
for i, example in enumerate(test_dataset):
|
130 |
+
has_smoke = true_labels[i]
|
131 |
+
annotation = annotations[i]
|
|
|
|
|
132 |
|
133 |
# Make prediction
|
134 |
results = model.predict(example["image"], device=device_name, conf=THRESHOLD, verbose=False, imgsz=IMGSIZE)[0]
|
135 |
pred_has_smoke = len(results) > 0
|
136 |
predictions.append(int(pred_has_smoke))
|
137 |
+
|
138 |
# If there's a true box, parse it and add box prediction
|
139 |
if has_smoke:
|
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 |
+
# Append bounding box for the prediction
|
145 |
+
if results.boxes.cls.numel() != 0:
|
|
|
146 |
pred_boxes.append(results.boxes[0].xywhn.tolist()[0])
|
147 |
else:
|
148 |
+
pred_boxes.append([0, 0, 0, 0])
|
149 |
|
150 |
#--------------------------------------------------------------------------------------------
|
151 |
# YOUR MODEL INFERENCE STOPS HERE
|