Spaces:
Running
Running
Update det-metrics.py
Browse files- det-metrics.py +81 -5
det-metrics.py
CHANGED
@@ -131,14 +131,18 @@ class DetectionMetric(evaluate.Metric):
|
|
131 |
# save parameters for later
|
132 |
self.payload = payload
|
133 |
self.model_names = payload.models if payload else ["custom"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
self.iou_thresholds = (
|
135 |
iou_threshold if isinstance(iou_threshold, list) else [iou_threshold]
|
136 |
)
|
137 |
self.area_ranges = [v for _, v in area_ranges_tuples]
|
138 |
self.area_ranges_labels = [k for k, _ in area_ranges_tuples]
|
139 |
-
self.class_agnostic = class_agnostic
|
140 |
-
self.iou_type = iou_type
|
141 |
-
self.box_format = bbox_format
|
142 |
|
143 |
# initialize coco_metrics
|
144 |
self.coco_metric = PrecisionRecallF1Support(
|
@@ -147,7 +151,7 @@ class DetectionMetric(evaluate.Metric):
|
|
147 |
area_ranges_labels=self.area_ranges_labels,
|
148 |
class_agnostic=self.class_agnostic,
|
149 |
iou_type=self.iou_type,
|
150 |
-
box_format=self.
|
151 |
)
|
152 |
|
153 |
# initialize evaluation metric
|
@@ -248,7 +252,7 @@ class DetectionMetric(evaluate.Metric):
|
|
248 |
area_ranges_labels=self.area_ranges_labels,
|
249 |
class_agnostic=self.class_agnostic,
|
250 |
iou_type=self.iou_type,
|
251 |
-
box_format=self.
|
252 |
)
|
253 |
return results
|
254 |
|
@@ -414,3 +418,75 @@ class DetectionMetric(evaluate.Metric):
|
|
414 |
references = [{"boxes": [[1.0, 2.0, 3.0, 4.0]], "labels": [0], "area": [1.0]}]
|
415 |
|
416 |
return predictions, references
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
# save parameters for later
|
132 |
self.payload = payload
|
133 |
self.model_names = payload.models if payload else ["custom"]
|
134 |
+
self.iou_threshold = iou_threshold
|
135 |
+
self.area_ranges_tuples = area_ranges_tuples
|
136 |
+
self.class_agnostic = class_agnostic
|
137 |
+
self.iou_type = iou_type
|
138 |
+
self.bbox_format = bbox_format
|
139 |
+
|
140 |
+
# postprocess parameters
|
141 |
self.iou_thresholds = (
|
142 |
iou_threshold if isinstance(iou_threshold, list) else [iou_threshold]
|
143 |
)
|
144 |
self.area_ranges = [v for _, v in area_ranges_tuples]
|
145 |
self.area_ranges_labels = [k for k, _ in area_ranges_tuples]
|
|
|
|
|
|
|
146 |
|
147 |
# initialize coco_metrics
|
148 |
self.coco_metric = PrecisionRecallF1Support(
|
|
|
151 |
area_ranges_labels=self.area_ranges_labels,
|
152 |
class_agnostic=self.class_agnostic,
|
153 |
iou_type=self.iou_type,
|
154 |
+
box_format=self.bbox_format,
|
155 |
)
|
156 |
|
157 |
# initialize evaluation metric
|
|
|
252 |
area_ranges_labels=self.area_ranges_labels,
|
253 |
class_agnostic=self.class_agnostic,
|
254 |
iou_type=self.iou_type,
|
255 |
+
box_format=self.bbox_format,
|
256 |
)
|
257 |
return results
|
258 |
|
|
|
418 |
references = [{"boxes": [[1.0, 2.0, 3.0, 4.0]], "labels": [0], "area": [1.0]}]
|
419 |
|
420 |
return predictions, references
|
421 |
+
|
422 |
+
|
423 |
+
def compute_from_payload(self, payload: Payload):
|
424 |
+
"""
|
425 |
+
Compute the metric from the payload.
|
426 |
+
Args:
|
427 |
+
payload (Payload): The payload to compute the metric from.
|
428 |
+
**kwargs: Additional keyword arguments.
|
429 |
+
Returns:
|
430 |
+
dict: The computed metric results with the following format:
|
431 |
+
{
|
432 |
+
"model_name": {
|
433 |
+
"overall": {
|
434 |
+
"all": {"tp": ..., "fp": ..., "fn": ..., "f1": ...},
|
435 |
+
... # more area ranges
|
436 |
+
},
|
437 |
+
"per_sequence": {
|
438 |
+
"sequence_name": {
|
439 |
+
"all": {...},
|
440 |
+
... # more area ranges
|
441 |
+
},
|
442 |
+
... # more sequences
|
443 |
+
}
|
444 |
+
},
|
445 |
+
... # more models
|
446 |
+
}
|
447 |
+
|
448 |
+
Note:
|
449 |
+
- If the metric does not support area ranges, the metric should store the results under the `all` key.
|
450 |
+
- If a range area is provided it will be displayed in the output. if area_ranges_tuples is None, then all the area ranges will be displayed
|
451 |
+
"""
|
452 |
+
results = {}
|
453 |
+
|
454 |
+
for model_name in payload.models:
|
455 |
+
results[model_name] = {"overall": {}, "per_sequence": {}}
|
456 |
+
|
457 |
+
# per-sequence loop
|
458 |
+
for seq_name, sequence in payload.sequences.items():
|
459 |
+
# create new payload only with specific sequence and model
|
460 |
+
sequence_payload = Payload(
|
461 |
+
dataset=payload.dataset,
|
462 |
+
gt_field_name=payload.gt_field_name,
|
463 |
+
models=[model_name],
|
464 |
+
sequences={seq_name: sequence}
|
465 |
+
)
|
466 |
+
module = DetectionMetric(
|
467 |
+
area_ranges_tuples=self.area_ranges_tuples,
|
468 |
+
iou_threshold=self.iou_threshold,
|
469 |
+
class_agnostic=self.class_agnostic,
|
470 |
+
bbox_format=self.bbox_format,
|
471 |
+
iou_type=self.iou_type,
|
472 |
+
payload=sequence_payload
|
473 |
+
)
|
474 |
+
results[model_name]["per_sequence"][seq_name] = module.compute()[model_name]["metrics"]
|
475 |
+
|
476 |
+
# overall per-model loop
|
477 |
+
model_payload = Payload(
|
478 |
+
dataset=payload.dataset,
|
479 |
+
gt_field_name=payload.gt_field_name,
|
480 |
+
models=[model_name],
|
481 |
+
sequences=payload.sequences
|
482 |
+
)
|
483 |
+
module = DetectionMetric(
|
484 |
+
area_ranges_tuples=self.area_ranges_tuples,
|
485 |
+
iou_threshold=self.iou_threshold,
|
486 |
+
class_agnostic=self.class_agnostic,
|
487 |
+
bbox_format=self.bbox_format,
|
488 |
+
iou_type=self.iou_type,
|
489 |
+
payload=model_payload
|
490 |
+
)
|
491 |
+
results[model_name]["overall"] = module.compute()[model_name]["metrics"]
|
492 |
+
return results
|