Repository navigation
Instance evaluation
These APIs were introduced in 0.0.1a6. exacta.instances evaluates
instance label maps: each case is a (B, H, W) or (B, D, H, W) array of nonnegative
integers in which 0 is background and every other value is one instance. Touching instances
such as nuclei, cells or glands stay apart, unlike the connected components of
lesion evaluation, and an
instance need not be connected. Values need not be contiguous; they are renumbered in
increasing order, which also fixes the order that breaks ties.
from exacta import instances
scores = instances.evaluate(pred_instances, target_instances) # IoU > 0.5
print(scores.pq, scores.aji, scores.aji_plus, scores.seg) # each (B,)
dsb = instances.dsb2018(pred_instances, target_instances) # (B,)With
| Field | Definition |
|---|---|
tp, fp, fn
|
Matched pairs, unmatched predictions, unmatched targets |
precision, recall
|
|
f1 |
|
accuracy |
accuracy
|
sq, pq
|
Mean IoU of matched pairs, and |
aji |
|
aji_plus |
The same sums over the one-to-one pairing that maximises summed IoU (Graham et al., Medical Image Analysis 2019) |
seg |
Mean over target instances of |
Pairs match one-to-one when their IoU exceeds threshold (default 0.5; strict=False uses
get_fast_pq, COCO panopticapi and the
Data Science Bowl. Below 0.5, matching="greedy" accepts pairs by decreasing IoU, while
"hungarian" maximises the summed IoU of accepted pairs, solved per group of overlapping
instances (SciPy). HoVer-Net assigns before thresholding and StarDist maximises the number of
matches first, so their low-threshold counts can differ. A sequence of thresholds adds a
trailing axis to the matching fields.
AJI follows HoVer-Net's get_fast_aji: a prediction taken by several targets enters each of
their unions, the over-penalisation AJI+ removes. Ties go to the larger intersection, then the
lower value, which is how HoVer-Net's 1e-6 term in the IoU denominator breaks exact ties. AJI+
pairings with equal summed IoU are resolved per overlapping group, so a tied optimum can
differ from a solve of the whole image. SEG ignores false positives.
A case without any instance gives both_empty (NaN by default) for every score. Precision
is NaN without predictions; recall and SEG are NaN without targets. With instances but no
match, F1, accuracy, SQ and PQ are 0, as in Panoptica, HoVer-Net and StarDist.
dsb2018 averages accuracy over the thresholds matching_dataset uses
InstanceMetrics accumulates statistics over batches and processes. With the default
aggregate="cases", it averages each case's score (skipping NaN), as HoVer-Net and PanNuke
report image means. With aggregate="pooled", it sums first:
tp, fp and fn are
dataset totals in both modes.
from exacta.instances import InstanceMetrics
pq_per_class = {c: InstanceMetrics(aggregate="pooled") for c in range(1, num_types)}
for pred_inst, pred_type, true_inst, true_type in loader:
for c, metric in pq_per_class.items():
metric.update(torch.where(pred_type == c, pred_inst, 0), torch.where(true_type == c, true_inst, 0))
mpq = torch.stack([m.compute().pq for m in pq_per_class.values()]).nanmean()For panoptic (per-class) scores, keep one accumulator per class with that class's instances,
as above. A class without instances is NaN here; CoNIC's mPQ+ adds compute() with identical settings, including ranks without updates, and samplers must not
repeat cases.
Instances are renumbered with a lookup table, or by sorting when values are sparse. A CPU (C++) or GPU (PyTorch) pass builds the sparse table of overlapping pairs. Matching, AJI and SEG then work only on those pairs, so the cost follows the number of overlaps rather than the product of instance counts. On a 1024 × 1024 image with 2000 touching nuclei, one evaluation took 16.9 ms on a Ryzen 9 5900XT (two threads) and 9.7 ms on an RTX 5070; see Performance. The values were checked against independent dense NumPy implementations of HoVer-Net's PQ, AJI and AJI+ procedures and of the SEG rule, in 2D and 3D.
Exacta documentation
- Home
- Installation
- Quickstart
- 2D medical segmentation
- Metrics
- Classification
- Regression
- Losses
- Advanced segmentation
- Conventions
- Lesion evaluation
- Instance evaluation
- Performance
- Development
Version: 0.0.1a6.