class_docstr: Implementation of Jaccard loss for image segmentation task.
  It supports binary, multi-class and multi-label cases.
class_docstr_tok: ["Implementation","of","Jaccard","loss","for","image","segmentation","task",".","It","supports","binary",",","multi-class","and","multi-label","cases","."]
docstr: :param y_pred: NxCxHxW
  :param y_true: NxHxW
  :return: scalar
docstr_tok: [":","param","y_pred",":","NxCxHxW",":","param","y_true",":","NxHxW",":","return",":","scalar"]
code: def forward(self, y_pred: Tensor, y_true: Tensor) -> Tensor:
      """
  
      :param y_pred: NxCxHxW
      :param y_true: NxHxW
      :return: scalar
      """
      assert y_true.size(0) == y_pred.size(0)
  
      if self.from_logits:
          # Apply activations to get [0..1] class probabilities
          # Using Log-Exp as this gives more numerically stable result and does not cause vanishing gradient on
          # extreme values 0 and 1
          if self.mode == MULTICLASS_MODE:
              y_pred = y_pred.log_softmax(dim=1).exp()
          else:
              y_pred = F.logsigmoid(y_pred).exp()
  
      bs = y_true.size(0)
      num_classes = y_pred.size(1)
      dims = (0, 2)
  
      if self.mode == BINARY_MODE:
          y_true = y_true.view(bs, 1, -1)
          y_pred = y_pred.view(bs, 1, -1)
  
      if self.mode == MULTICLASS_MODE:
          y_true = y_true.view(bs, -1)
          y_pred = y_pred.view(bs, num_classes, -1)
  
          y_true = F.one_hot(y_true, num_classes)  # N,H*W -> N,H*W, C
          y_true = y_true.permute(0, 2, 1)  # H, C, H*W
  
      if self.mode == MULTILABEL_MODE:
          y_true = y_true.view(bs, num_classes, -1)
          y_pred = y_pred.view(bs, num_classes, -1)
  
      scores = soft_jaccard_score(
          y_pred,
          y_true.type(y_pred.dtype),
          smooth=self.smooth,
          eps=self.eps,
          dims=dims,
      )
  
      if self.log_loss:
          loss = -torch.log(scores.clamp_min(self.eps))
      else:
          loss = 1.0 - scores
  
      # IoU loss is defined for non-empty classes
      # So we zero contribution of channel that does not have true pixels
      # NOTE: A better workaround would be to use loss term `mean(y_pred)`
      # for this case, however it will be a modified jaccard loss
  
      mask = y_true.sum(dims) > 0
      loss *= mask.float()
  
      if self.classes is not None:
          loss = loss[self.classes]
  
      return loss.mean()
  
file_path: pytorch-toolbelt/pytorch_toolbelt/losses/jaccard.py
imports: from typing import List
  import torch
  import torch.nn.functional
  from pytorch_toolbelt.utils.torch_utils import to_tensor
  from torch import Tensor
  from torch.nn.modules.loss import _Loss
  from .functional import soft_jaccard_score
label_desc: Use image node_id 2 for calling the JaccardLoss obj's underlying member method code with example usage: obj.forward(y_pred, y_true) and returns: loss