Skip to content

Commit a3c4e9d

Browse files
authored
Add AP and MAP to polygon metrics (#200)
* Add map to polygon metrics * fix * bump version * fix docstring * bump patch version
1 parent e730bcf commit a3c4e9d

File tree

8 files changed

+567
-43
lines changed

8 files changed

+567
-43
lines changed

nucleus/metrics/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from .base import Metric, MetricResult
22
from .polygon_metrics import (
3+
PolygonAveragePrecision,
34
PolygonIOU,
5+
PolygonMAP,
46
PolygonMetric,
57
PolygonPrecision,
68
PolygonRecall,

nucleus/metrics/filters.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
from nucleus.prediction import PredictionList
44

55
from .polygon_utils import (
6-
BoxOrPolygonAnnotation,
6+
BoxOrPolygonAnnoOrPred,
77
polygon_annotation_to_geometry,
88
)
99

1010

1111
def polygon_area_filter(
12-
polygons: List[BoxOrPolygonAnnotation], min_area: float, max_area: float
13-
) -> List[BoxOrPolygonAnnotation]:
12+
polygons: List[BoxOrPolygonAnnoOrPred], min_area: float, max_area: float
13+
) -> List[BoxOrPolygonAnnoOrPred]:
1414
filter_fn = (
1515
lambda polygon: min_area
1616
<= polygon_annotation_to_geometry(polygon).signed_area
@@ -32,3 +32,9 @@ def confidence_filter(
3232
filter(filter_fn, predictions.__dict__[attr])
3333
)
3434
return predictions_copy
35+
36+
37+
def polygon_label_filter(
38+
polygons: List[BoxOrPolygonAnnoOrPred], label: str
39+
) -> List[BoxOrPolygonAnnoOrPred]:
40+
return list(filter(lambda polygon: polygon.label == label, polygons))

nucleus/metrics/metric_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
3+
4+
def compute_average_precision(recall, precision):
5+
"""Compute the average precision, given the recall and precision curves.
6+
Code originally from https://github.com/rbgirshick/py-faster-rcnn.
7+
# Arguments
8+
recall: The recall curve (list).
9+
precision: The precision curve (list).
10+
# Returns
11+
The average precision as computed in py-faster-rcnn.
12+
"""
13+
# correct AP calculation
14+
# first append sentinel values at the end
15+
mrec = np.concatenate(([0.0], recall, [1.0]))
16+
mpre = np.concatenate(([0.0], precision, [0.0]))
17+
18+
# compute the precision envelope
19+
for i in range(mpre.size - 1, 0, -1):
20+
mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])
21+
22+
# to calculate area under PR curve, look for points
23+
# where X axis (recall) changes value
24+
i = np.where(mrec[1:] != mrec[:-1])[0]
25+
26+
# and sum (\Delta recall) * prec
27+
ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
28+
return ap

0 commit comments

Comments
 (0)