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}