def update(self, preds: Tensor, target: Tensor): """ Update state with predictions and targets. Args: preds: Predictions from model target: Ground truth values """ confmat = _confusion_matrix_update(preds, target, self.num_classes, self.threshold, self.multilabel) self.confmat += confmat
def jaccard_index( preds: Tensor, target: Tensor, num_classes: int, ignore_index: Optional[int] = None, absent_score: float = 0.0, threshold: float = 0.5, reduction: Literal["elementwise_mean", "sum", "none", None] = "elementwise_mean", ) -> Tensor: r"""Computes `Jaccard index`_ .. math:: J(A,B) = \frac{|A\cap B|}{|A\cup B|} Where: :math:`A` and :math:`B` are both tensors of the same size, containing integer class values. They may be subject to conversion from input data (see description below). Note that it is different from box IoU. If preds and target are the same shape and preds is a float tensor, we use the ``self.threshold`` argument to convert into integer labels. This is the case for binary and multi-label probabilities. If pred has an extra dimension as in the case of multi-class scores we perform an argmax on ``dim=1``. Args: preds: tensor containing predictions from model (probabilities, or labels) with shape ``[N, d1, d2, ...]`` target: tensor containing ground truth labels with shape ``[N, d1, d2, ...]`` num_classes: Specify the number of classes ignore_index: optional int specifying a target class to ignore. If given, this class index does not contribute to the returned score, regardless of reduction method. Has no effect if given an int that is not in the range ``[0, num_classes-1]``, where num_classes is either given or derived from pred and target. By default, no index is ignored, and all classes are used. absent_score: score to use for an individual class, if no instances of the class index were present in ``preds`` AND no instances of the class index were present in ``target``. For example, if we have 3 classes, [0, 0] for ``preds``, and [0, 2] for ``target``, then class 1 would be assigned the `absent_score`. threshold: Threshold value for binary or multi-label probabilities. reduction: a method to reduce metric score over labels. - ``'elementwise_mean'``: takes the mean (default) - ``'sum'``: takes the sum - ``'none'`` or ``None``: no reduction will be applied Return: IoU score: Tensor containing single value if reduction is 'elementwise_mean', or number of classes if reduction is 'none' Example: >>> from torchmetrics.functional import jaccard_index >>> target = torch.randint(0, 2, (10, 25, 25)) >>> pred = torch.tensor(target) >>> pred[2:5, 7:13, 9:15] = 1 - pred[2:5, 7:13, 9:15] >>> jaccard_index(pred, target, num_classes=2) tensor(0.9660) """ confmat = _confusion_matrix_update(preds, target, num_classes, threshold) return _jaccard_from_confmat(confmat, num_classes, ignore_index, absent_score, reduction)