Skip to content

Commit 4d26de6

Browse files
authored
[Validate] Added all segmentation to poly metrics (#292)
* Added all segmentation to poly metrics * Clean up init * Add segmentation configs
1 parent 05173ab commit 4d26de6

File tree

10 files changed

+1158
-15
lines changed

10 files changed

+1158
-15
lines changed

nucleus/metrics/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@
1515
PolygonPrecision,
1616
PolygonRecall,
1717
)
18+
from .segmentation_metrics import (
19+
SegmentationMaskToPolyMetric,
20+
SegmentationToPolyAveragePrecision,
21+
SegmentationToPolyIOU,
22+
SegmentationToPolyMAP,
23+
SegmentationToPolyPrecision,
24+
SegmentationToPolyRecall,
25+
)

nucleus/metrics/cuboid_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
try:
77
from shapely.geometry import Polygon
88
except ModuleNotFoundError:
9-
from .shapely_not_installed import ShapelyNotInstalled
9+
from ..shapely_not_installed import ShapelyNotInstalled
1010

1111
Polygon = ShapelyNotInstalled
1212

nucleus/metrics/polygon_metrics.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,11 @@ def eval(
541541
) = get_true_false_positives_confidences(
542542
annotations_filtered, predictions_filtered, self.iou_threshold
543543
)
544-
idxes = np.argsort(-confidences)
545-
true_false_positives_sorted = true_false_positives[idxes]
544+
if np.all(confidences):
545+
idxes = np.argsort(-confidences)
546+
true_false_positives_sorted = true_false_positives[idxes]
547+
else:
548+
true_false_positives_sorted = true_false_positives
546549
cumulative_true_positives = np.cumsum(true_false_positives_sorted)
547550
total_predictions = np.arange(1, len(true_false_positives) + 1)
548551
precisions = cumulative_true_positives / total_predictions

nucleus/metrics/polygon_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
try:
1414
from shapely.geometry import Polygon
1515
except ModuleNotFoundError:
16-
from .shapely_not_installed import ShapelyNotInstalled
16+
from ..shapely_not_installed import ShapelyNotInstalled
1717

1818
Polygon = ShapelyNotInstalled
1919

nucleus/metrics/segmentation_metrics.py

Lines changed: 524 additions & 0 deletions
Large diffs are not rendered by default.

nucleus/metrics/segmentation_utils.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import numpy as np
2+
from rasterio import features
3+
4+
from nucleus import Point, PolygonPrediction
5+
6+
try:
7+
from shapely import geometry
8+
except ModuleNotFoundError:
9+
from nucleus.shapely_not_installed import ( # pylint: disable=ungrouped-imports
10+
ShapelyNotInstalled,
11+
)
12+
13+
geometry = ShapelyNotInstalled
14+
15+
16+
def instance_mask_to_polys(instance_mask: np.ndarray, background_code=None):
17+
mask_values = []
18+
all_polygons = []
19+
not_background_mask = (
20+
(instance_mask != background_code) if background_code else None
21+
)
22+
for shape, value in features.shapes(
23+
instance_mask.astype(np.int16),
24+
mask=not_background_mask,
25+
):
26+
poly = geometry.shape(shape)
27+
all_polygons.append(poly)
28+
mask_values.append(int(value))
29+
30+
return mask_values, all_polygons
31+
32+
33+
def transform_poly_codes_to_poly_preds(
34+
dataset_item_id: str, pred_value, pred_polys, code_to_label
35+
):
36+
polygon_predictions = []
37+
for code, poly in zip(pred_value, pred_polys):
38+
if poly.type != "Polygon":
39+
continue
40+
label = code_to_label[code]
41+
x_stack, y_stack = poly.exterior.coords.xy
42+
pred = PolygonPrediction(
43+
label,
44+
vertices=[Point(x, y) for x, y in zip(x_stack, y_stack)],
45+
reference_id=dataset_item_id,
46+
)
47+
polygon_predictions.append(pred)
48+
return polygon_predictions

nucleus/validate/eval_functions/available_eval_functions.py

Lines changed: 279 additions & 3 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)