segmentation
Semantic- and part-segmentation metrics: intersection over union of a confusion matrix, or per sample.
Functions:
-
compute_intersection_union–Compute per-class intersection and union counts.
-
intersection_over_union–Intersection over Union (IoU, the Jaccard index) of a confusion matrix.
-
part_intersection_over_union–Per-shape IoU averaged over the parts of the shape's category (the ShapeNetPart protocol).
-
part_mean_intersection_over_union–Mean IoU of per-shape IoUs (the ShapeNetPart instance and class mIoU).
compute_intersection_union
[source]
¶
compute_intersection_union(
preds: Tensor,
target: Tensor,
num_classes: int,
batch: Optional[Tensor] = None,
ignore_index: Optional[int] = None,
) -> tuple[Tensor, Tensor]
Compute per-class intersection and union counts.
Parameters:
-
preds(Tensor) –Predicted class indices, shape \((N,)\).
-
target(Tensor) –Ground truth class indices, shape \((N,)\).
-
num_classes(int) –Total number of classes.
-
batch(Optional[Tensor], default:None) –Optional per-point batch index for per-sample counts. One row is emitted per sample (even for samples whose points are all ignored, which count as zero).
-
ignore_index(Optional[int], default:None) –Class index to exclude. Points where
target == ignore_indexare dropped, and the returned intersection/union at this index are \(0\).
Returns:
-
Tensor–Tuple \((\text{intersection}, \text{union})\), each of shape \((\text{num\_classes},)\)
-
Tensor–or \((\text{batch\_size}, \text{num\_classes})\) if
batchis provided.
intersection_over_union
[source]
¶
intersection_over_union(
cm: Tensor,
*,
average: Literal["macro"] = ...,
ignore_index: Union[int, Sequence[int], None] = ...,
zero_division: float = ...,
class_names: Optional[Sequence[str]] = ...,
) -> float
intersection_over_union(
cm: Tensor,
*,
average: Literal["macro", "none"] = "macro",
ignore_index: Union[int, Sequence[int], None] = None,
zero_division: float = 0.0,
class_names: Optional[Sequence[str]] = None,
) -> Union[float, Tensor, Dict[str, float]]
Intersection over Union (IoU, the Jaccard index) of a confusion matrix.
Confusion matrices add up, so the matrix may describe one batch or the sum of confusion_matrix over a
whole split. With average="macro" a class absent from the whole matrix (zero union) counts as
zero_division, matching sklearn's jaccard_score(zero_division=0); toolboxes that average only over
present classes report a higher value on splits missing a class, so compare published numbers accordingly.
Parameters:
-
cm(Tensor) –Confusion matrix with true classes as rows, shape \((C, C)\) (see
confusion_matrix). -
average(Literal['macro', 'none'], default:'macro') –"macro"returns the mean IoU (mIoU) over the classes;"none"returns the per-class IoU. -
ignore_index(Union[int, Sequence[int], None], default:None) –Class index, or indices, to ignore: points whose true class is ignored are dropped, and the ignored classes are left out of the mean. Indices outside \([0, C)\) have no effect.
-
zero_division(float, default:0.0) –IoU given to a class with zero union (and to the ignored classes with
average="none"). -
class_names(Optional[Sequence[str]], default:None) –Name of each class index; with
average="none"the per-class IoU comes back as a{name: iou}dict instead of a tensor.
Returns:
-
Union[float, Tensor, Dict[str, float]]–The mean IoU as a float with
average="macro", or the per-class IoU, shape \((C,)\), withaverage="none" -
Union[float, Tensor, Dict[str, float]]–(a
{name: iou}dict whenclass_namesis given).
Shape
- cm: \((C, C)\)
- output: scalar, or \((C,)\) with
average="none"
part_intersection_over_union
[source]
¶
part_intersection_over_union(
preds: Tensor,
target: Tensor,
part_ids: Sequence[Sequence[int]],
category: Tensor,
batch: Optional[Tensor] = None,
) -> Tensor
Per-shape IoU averaged over the parts of the shape's category (the ShapeNetPart protocol).
Each shape is scored only over the part labels its category owns (e.g. ShapeNetPart's Airplane
owns parts \([0, 1, 2, 3]\)); a part absent from both the prediction and the target counts as IoU \(1\).
Parameters:
-
preds(Tensor) –Predicted part indices, shape \((N,)\).
-
target(Tensor) –Ground truth part indices, shape \((N,)\).
-
part_ids(Sequence[Sequence[int]]) –Part labels owned by each category, e.g.
ShapeNetPart.seg_ids.values(). -
category(Tensor) –Per-shape category index into
part_ids, shape \((B,)\). -
batch(Optional[Tensor], default:None) –Optional per-point shape index, shape \((N,)\); when omitted, all the points belong to one shape.
Returns:
-
Tensor–Per-shape IoU tensor of shape \((B,)\).
Shape
- preds, target, batch: \((N,)\)
- category: \((B,)\)
- output: \((B,)\)
part_mean_intersection_over_union
[source]
¶
part_mean_intersection_over_union(
ious: Tensor,
category: Tensor,
*,
average: Literal["micro", "macro"] = ...,
num_classes: Optional[int] = ...,
class_names: Optional[Sequence[str]] = ...,
) -> float
part_mean_intersection_over_union(
ious: Tensor,
category: Tensor,
*,
average: Literal["micro", "macro", "none"] = "micro",
num_classes: Optional[int] = None,
class_names: Optional[Sequence[str]] = None,
) -> Union[float, Tensor, Dict[str, float]]
Mean IoU of per-shape IoUs (the ShapeNetPart instance and class mIoU).
A shape's IoU does not depend on the other shapes of its batch, so the IoUs may come from one batch or be the
part_intersection_over_union of every batch of a split, concatenated.
Parameters:
-
ious(Tensor) –Per-shape IoUs (see
part_intersection_over_union), shape \((B,)\). -
category(Tensor) –Per-shape category index, shape \((B,)\).
-
average(Literal['micro', 'macro', 'none'], default:'micro') –"micro"returns the instance mIoU (the mean over all shapes);"macro"returns the class mIoU (the mean, over the categories that have a shape, of their per-category means);"none"returns the per-category mean IoU. -
num_classes(Optional[int], default:None) –Number of categories, i.e. the length of the
average="none"output; defaults to the number ofclass_names, else to the largest category index met plus one. -
class_names(Optional[Sequence[str]], default:None) –Name of each category index; with
average="none"the per-category mean IoU comes back as a{name: iou}dict instead of a tensor.
Returns:
-
Union[float, Tensor, Dict[str, float]]–The mIoU as a float with
average="micro"or"macro"(NaN without any shape), or the per-category mean -
Union[float, Tensor, Dict[str, float]]–IoU, shape \((C,)\), with
average="none"(a{name: iou}dict whenclass_namesis given), holding NaN -
Union[float, Tensor, Dict[str, float]]–for the categories without any shape.
Shape
- ious: \((B,)\)
- category: \((B,)\)
- output: scalar, or \((C,)\) with
average="none"
Example
>>> ious, category = torch.tensor([1.0, 0.5, 0.0]), torch.tensor([0, 0, 1])
>>> part_mean_intersection_over_union(ious, category)
0.5
>>> part_mean_intersection_over_union(ious, category, average="macro")
0.375
>>> part_mean_intersection_over_union(ious, category, average="none", class_names=["Airplane", "Bag"])
{'Airplane': 0.75, 'Bag': 0.0}