code: def py_focal_loss_with_prob(
      pred,
      target,
      weight=None,
      gamma=2.0,
      alpha=0.25,
      reduction="mean",
      avg_factor=None,
  ):
      """PyTorch version of `Focal Loss <https://arxiv.org/abs/1708.02002>`_.
      Different from `py_sigmoid_focal_loss`, this function accepts probability
      as input.
  
      Args:
          pred (torch.Tensor): The prediction probability with shape (N, C),
              C is the number of classes.
          target (torch.Tensor): The learning label of the prediction.
              The target shape support (N,C) or (N,), (N,C) means one-hot form.
          weight (torch.Tensor, optional): Sample-wise loss weight.
          gamma (float, optional): The gamma for calculating the modulating
              factor. Defaults to 2.0.
          alpha (float, optional): A balanced form for Focal Loss.
              Defaults to 0.25.
          reduction (str, optional): The method used to reduce the loss into
              a scalar. Defaults to 'mean'.
          avg_factor (int, optional): Average factor that is used to average
              the loss. Defaults to None.
      """
      if pred.dim() != target.dim():
          num_classes = pred.size(1)
          target = F.one_hot(target, num_classes=num_classes + 1)
          target = target[:, :num_classes]
  
      target = target.type_as(pred)
      pt = (1 - pred) * target + pred * (1 - target)
      focal_weight = (
          alpha * target + (1 - alpha) * (1 - target)
      ) * pt.pow(gamma)
      loss = (
          F.binary_cross_entropy(pred, target, reduction="none")
          * focal_weight
      )
      if weight is not None:
          if weight.shape != loss.shape:
              if weight.size(0) == loss.size(0):
                  # For most cases, weight is of shape (num_priors, ),
                  #  which means it does not have the second axis num_class
                  weight = weight.view(-1, 1)
              else:
                  # Sometimes, weight per anchor per class is also needed. e.g.
                  #  in FSAF. But it may be flattened of shape
                  #  (num_priors x num_class, ), while loss is still of shape
                  #  (num_priors, num_class).
                  assert weight.numel() == loss.numel()
                  weight = weight.view(loss.size(0), -1)
          assert weight.ndim == loss.ndim
      loss = weight_reduce_loss(loss, weight, reduction, avg_factor)
      return loss
  
imports: import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.ops import sigmoid_focal_loss
  from mmdet.registry import MODELS
  from .accuracy import accuracy
  from .utils import weight_reduce_loss
label_desc: Use image node_id 2 for calling a global function with example usage: py_focal_loss_with_prob(pred, target, weight, gamma, alpha, reduction, avg_factor) and returns: loss