Source code for catalyst.dl.utils.criterion.iou
import torch
from catalyst.utils import get_activation_fn
[docs]def iou(
outputs: torch.Tensor,
targets: torch.Tensor,
eps: float = 1e-7,
threshold: float = None,
activation: str = "Sigmoid"
):
"""
Args:
outputs (torch.Tensor): A list of predicted elements
targets (torch.Tensor): A list of elements that are to be predicted
eps (float): epsilon to avoid zero division
threshold (float): threshold for outputs binarization
activation (str): An torch.nn activation applied to the outputs.
Must be one of ["none", "Sigmoid", "Softmax2d"]
Returns:
float: IoU (Jaccard) score
"""
activation_fn = get_activation_fn(activation)
outputs = activation_fn(outputs)
if threshold is not None:
outputs = (outputs > threshold).float()
intersection = torch.sum(targets * outputs)
union = torch.sum(targets) + torch.sum(outputs)
iou = intersection / (union - intersection + eps)
return iou
jaccard = iou
__all__ = ["iou", "jaccard"]