Spaces:
Sleeping
Sleeping
Final submission
Browse files- tasks/image.py +14 -15
tasks/image.py
CHANGED
@@ -116,36 +116,35 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
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 |
-
|
126 |
pred_boxes = []
|
127 |
-
|
|
|
128 |
logging.info(f"Inference start on device: {device_name}")
|
129 |
-
for
|
130 |
-
|
131 |
-
annotation = annotations
|
|
|
|
|
132 |
|
133 |
# Make prediction
|
134 |
-
results = model.predict(example["image"],
|
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
|
145 |
-
|
|
|
146 |
pred_boxes.append(results.boxes[0].xywhn.tolist()[0])
|
147 |
else:
|
148 |
-
pred_boxes.append([0,
|
149 |
|
150 |
#--------------------------------------------------------------------------------------------
|
151 |
# YOUR MODEL INFERENCE STOPS HERE
|
|
|
116 |
device_name = device("cuda" if is_available() else "cpu")
|
117 |
model.to(device_name)
|
118 |
|
|
|
|
|
|
|
|
|
|
|
119 |
predictions = []
|
120 |
+
true_labels = []
|
121 |
pred_boxes = []
|
122 |
+
true_boxes_list = [] # List of lists, each inner list contains boxes for one image
|
123 |
+
|
124 |
logging.info(f"Inference start on device: {device_name}")
|
125 |
+
for example in test_dataset:
|
126 |
+
# Parse true annotation (YOLO format: class_id x_center y_center width height)
|
127 |
+
annotation = example.get("annotations", "").strip()
|
128 |
+
has_smoke = len(annotation) > 0
|
129 |
+
true_labels.append(int(has_smoke))
|
130 |
|
131 |
# Make prediction
|
132 |
+
results = model.predict(example["image"], conf=THRESHOLD, verbose=False, imgsz=IMGSIZE)[0]
|
133 |
pred_has_smoke = len(results) > 0
|
134 |
predictions.append(int(pred_has_smoke))
|
135 |
+
|
136 |
# If there's a true box, parse it and add box prediction
|
137 |
if has_smoke:
|
138 |
# Parse all true boxes from the annotation
|
139 |
image_true_boxes = parse_boxes(annotation)
|
140 |
true_boxes_list.append(image_true_boxes)
|
141 |
|
142 |
+
# Append only one bounding box if at least one fire is detected
|
143 |
+
# Note that multiple boxes could be appended
|
144 |
+
if results.boxes.cls.numel()!=0:
|
145 |
pred_boxes.append(results.boxes[0].xywhn.tolist()[0])
|
146 |
else:
|
147 |
+
pred_boxes.append([0,0,0,0])
|
148 |
|
149 |
#--------------------------------------------------------------------------------------------
|
150 |
# YOUR MODEL INFERENCE STOPS HERE
|