Skip to content

Commit 621f8e4

Browse files
authored
Merge pull request #1500 from roboflow/fix/101-coco-algorithm
101 AP algo: do not interpollate precision
2 parents 96bdcb3 + 7a1c314 commit 621f8e4

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

supervision/metrics/mean_average_precision.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,15 @@ def _compute_average_precision(recall: np.ndarray, precision: np.ndarray) -> flo
222222
Returns:
223223
(float): Average precision.
224224
"""
225-
extended_recall = np.concatenate(([0.0], recall, [1.0]))
226-
extended_precision = np.concatenate(([1.0], precision, [0.0]))
227-
max_accumulated_precision = np.flip(
228-
np.maximum.accumulate(np.flip(extended_precision))
229-
)
230-
interpolated_recall_levels = np.linspace(0, 1, 101)
231-
interpolated_precision = np.interp(
232-
interpolated_recall_levels, extended_recall, max_accumulated_precision
233-
)
234-
average_precision = np.trapz(interpolated_precision, interpolated_recall_levels)
225+
if len(recall) == 0 and len(precision) == 0:
226+
return 0.0
227+
228+
recall_levels = np.linspace(0, 1, 101)
229+
precision_levels = np.zeros_like(recall_levels)
230+
for r, p in zip(recall[::-1], precision[::-1]):
231+
precision_levels[recall_levels <= r] = p
232+
233+
average_precision = (1 / 100 * precision_levels).sum()
235234
return average_precision
236235

237236
@staticmethod
@@ -307,9 +306,9 @@ def _average_precisions_per_class(
307306

308307
false_positives = (1 - matches[is_class]).cumsum(0)
309308
true_positives = matches[is_class].cumsum(0)
310-
true_negatives = total_true - true_positives
309+
false_negatives = total_true - true_positives
311310

312-
recall = true_positives / (true_positives + true_negatives + eps)
311+
recall = true_positives / (true_positives + false_negatives + eps)
313312
precision = true_positives / (true_positives + false_positives)
314313

315314
for iou_level_idx in range(matches.shape[1]):

0 commit comments

Comments
 (0)