class_docstr: Balanced cross entropy loss.
  Shape:
      - Input: :math:`(N, 1, H, W)`
      - GT: :math:`(N, 1, H, W)`, same shape as the input
      - Mask: :math:`(N, H, W)`, same spatial shape as the input
      - Output: scalar.
class_docstr_tok: ["Balanced","cross","entropy","loss",".","Shape",":","-","Input",":",":","math",":","`","(","N",",","1",",","H",",","W",")","`","-","GT",":",":","math",":","`","(","N",",","1",",","H",",","W",")","`",",","same","shape","as","the","input","-","Mask",":",":","math",":","`","(","N",",","H",",","W",")","`",",","same","spatial","shape","as","the","input","-","Output",":","scalar","."]
docstr: Args:
      pred: shape :math:`(N, 1, H, W)`, the prediction of network
      gt: shape :math:`(N, 1, H, W)`, the target
      mask: shape :math:`(N, H, W)`, the mask indicates positive regions
docstr_tok: ["Args",":","pred",":","shape",":","math",":","`","(","N",",","1",",","H",",","W",")","`",",","the","prediction","of","network","gt",":","shape",":","math",":","`","(","N",",","1",",","H",",","W",")","`",",","the","target","mask",":","shape",":","math",":","`","(","N",",","H",",","W",")","`",",","the","mask","indicates","positive","regions"]
code: def forward(
      self,
      pred: paddle.Tensor,
      gt: paddle.Tensor,
      mask: paddle.Tensor,
      return_origin=False,
  ):
      """
      Args:
          pred: shape :math:`(N, 1, H, W)`, the prediction of network
          gt: shape :math:`(N, 1, H, W)`, the target
          mask: shape :math:`(N, H, W)`, the mask indicates positive regions
      """
      positive = gt * mask
      negative = (1 - gt) * mask
      positive_count = int(positive.sum())
      negative_count = min(
          int(negative.sum()), int(positive_count * self.negative_ratio)
      )
      loss = nn.functional.binary_cross_entropy(
          pred, gt, reduction="none"
      )
      positive_loss = loss * positive
      negative_loss = loss * negative
      negative_loss, _ = negative_loss.reshape([-1]).topk(
          negative_count
      )
  
      balance_loss = (positive_loss.sum() + negative_loss.sum()) / (
          positive_count + negative_count + self.eps
      )
  
      if return_origin:
          return balance_loss, loss
      return balance_loss
  
file_path: PaddleOCR/benchmark/PaddleOCR_DBNet/models/losses/basic_loss.py
imports: import paddle
  import paddle.nn
label_desc: Use image node_id 2 for calling the BalanceCrossEntropyLoss obj's underlying member method code with example usage: obj.forward(pred, gt, mask, return_origin) and returns: balance_loss, balance_loss,  loss