class_docstr: AsymmetricUnifiedFocalLoss is a variant of Focal Loss.
  
  Actually, it's only supported for binary image segmentation now
  
  Reimplementation of the Asymmetric Unified Focal Tversky Loss described in:
  
  - "Unified Focal Loss: Generalising Dice and Cross Entropy-based Losses to Handle Class Imbalanced Medical Image Segmentation",
  Michael Yeung, Computerized Medical Imaging and Graphics
class_docstr_tok: ["AsymmetricUnifiedFocalLoss","is","a","variant","of","Focal","Loss",".","Actually",",","it","'s","only","supported","for","binary","image","segmentation","now","Reimplementation","of","the","Asymmetric","Unified","Focal","Tversky","Loss","described","in",":","-","``","Unified","Focal","Loss",":","Generalising","Dice","and","Cross","Entropy-based","Losses","to","Handle","Class","Imbalanced","Medical","Image","Segmentation","''",",","Michael","Yeung",",","Computerized","Medical","Imaging","and","Graphics"]
docstr: Args:
      y_pred : the shape should be BNH[WD], where N is the number of classes.
          It only supports binary segmentation.
          The input should be the original logits since it will be transformed by
              a sigmoid in the forward function.
      y_true : the shape should be BNH[WD], where N is the number of classes.
          It only supports binary segmentation.
  
  Raises:
      ValueError: When input and target are different shape
      ValueError: When len(y_pred.shape) != 4 and len(y_pred.shape) != 5
      ValueError: When num_classes
      ValueError: When the number of classes entered does not match the expected number
docstr_tok: ["Args",":","y_pred",":","the","shape","should","be","BNH","[","WD","]",",","where","N","is","the","number","of","classes",".","It","only","supports","binary","segmentation",".","The","input","should","be","the","original","logits","since","it","will","be","transformed","by","a","sigmoid","in","the","forward","function",".","y_true",":","the","shape","should","be","BNH","[","WD","]",",","where","N","is","the","number","of","classes",".","It","only","supports","binary","segmentation",".","Raises",":","ValueError",":","When","input","and","target","are","different","shape","ValueError",":","When","len","(","y_pred.shape",")","!","=","4","and","len","(","y_pred.shape",")","!","=","5","ValueError",":","When","num_classes","ValueError",":","When","the","number","of","classes","entered","does","not","match","the","expected","number"]
code: def forward(
      self, y_pred: torch.Tensor, y_true: torch.Tensor
  ) -> torch.Tensor:
      """
      Args:
          y_pred : the shape should be BNH[WD], where N is the number of classes.
              It only supports binary segmentation.
              The input should be the original logits since it will be transformed by
                  a sigmoid in the forward function.
          y_true : the shape should be BNH[WD], where N is the number of classes.
              It only supports binary segmentation.
  
      Raises:
          ValueError: When input and target are different shape
          ValueError: When len(y_pred.shape) != 4 and len(y_pred.shape) != 5
          ValueError: When num_classes
          ValueError: When the number of classes entered does not match the expected number
      """
      if y_pred.shape != y_true.shape:
          raise ValueError(
              f"ground truth has different shape ({y_true.shape}) from input ({y_pred.shape})"
          )
  
      if len(y_pred.shape) != 4 and len(y_pred.shape) != 5:
          raise ValueError(
              f"input shape must be 4 or 5, but got {y_pred.shape}"
          )
  
      if y_pred.shape[1] == 1:
          y_pred = one_hot(y_pred, num_classes=self.num_classes)
          y_true = one_hot(y_true, num_classes=self.num_classes)
  
      if torch.max(y_true) != self.num_classes - 1:
          raise ValueError(
              f"Please make sure the number of classes is {self.num_classes-1}"
          )
  
      n_pred_ch = y_pred.shape[1]
      if self.to_onehot_y:
          if n_pred_ch == 1:
              warnings.warn(
                  "single channel prediction, `to_onehot_y=True` ignored."
              )
          else:
              y_true = one_hot(y_true, num_classes=n_pred_ch)
  
      asy_focal_loss = self.asy_focal_loss(y_pred, y_true)
      asy_focal_tversky_loss = self.asy_focal_tversky_loss(
          y_pred, y_true
      )
  
      loss: torch.Tensor = (
          self.weight * asy_focal_loss
          + (1 - self.weight) * asy_focal_tversky_loss
      )
  
      if self.reduction == LossReduction.SUM.value:
          return torch.sum(loss)  # sum over the batch and channel dims
      if self.reduction == LossReduction.NONE.value:
          return loss  # returns [N, num_classes] losses
      if self.reduction == LossReduction.MEAN.value:
          return torch.mean(loss)
      raise ValueError(
          f'Unsupported reduction: {self.reduction}, available options are ["mean", "sum", "none"].'
      )
  
file_path: MONAI/monai/losses/unified_focal_loss.py
imports: from __future__ import annotations
  import warnings
  import torch
  from torch.nn.modules.loss import _Loss
  from monai.networks import one_hot
  from monai.utils import LossReduction
label_desc: Use image node_id 2 for calling the AsymmetricUnifiedFocalLoss obj's underlying member method code with example usage: obj.forward(y_pred, y_true) and returns: torch, loss, torch