docstr: Balanced Binary-CrossEntropy Loss.
  
  Args:
      pred (Tensor): Shape of :math:`(1, H, W)`.
      gt (Tensor): Shape of :math:`(1, H, W)`.
      mask (Tensor): Shape of :math:`(1, H, W)`.
  
  Returns:
      Tensor: Balanced bce loss.
docstr_tok: ["Balanced","Binary-CrossEntropy","Loss",".","Args",":","pred","(","Tensor",")",":","Shape","of",":","math",":","`","(","1",",","H",",","W",")","`",".","gt","(","Tensor",")",":","Shape","of",":","math",":","`","(","1",",","H",",","W",")","`",".","mask","(","Tensor",")",":","Shape","of",":","math",":","`","(","1",",","H",",","W",")","`",".","Returns",":","Tensor",":","Balanced","bce","loss","."]
code: def balance_bce_loss(self, pred, gt, mask):
      """Balanced Binary-CrossEntropy Loss.
  
      Args:
          pred (Tensor): Shape of :math:`(1, H, W)`.
          gt (Tensor): Shape of :math:`(1, H, W)`.
          mask (Tensor): Shape of :math:`(1, H, W)`.
  
      Returns:
          Tensor: Balanced bce loss.
      """
      assert pred.shape == gt.shape == mask.shape
      assert paddle.all(pred >= 0) and paddle.all(pred <= 1)
      assert paddle.all(gt >= 0) and paddle.all(gt <= 1)
      positive = gt * mask
      negative = (1 - gt) * mask
      positive_count = int(positive.sum())
  
      if positive_count > 0:
          loss = F.binary_cross_entropy(pred, gt, reduction="none")
          positive_loss = paddle.sum(loss * positive)
          negative_loss = loss * negative
          negative_count = min(
              int(negative.sum()), int(positive_count * self.ohem_ratio)
          )
      else:
          positive_loss = paddle.to_tensor(0.0)
          loss = F.binary_cross_entropy(pred, gt, reduction="none")
          negative_loss = loss * negative
          negative_count = 100
      negative_loss, _ = paddle.topk(
          negative_loss.reshape([-1]), negative_count
      )
  
      balance_loss = (positive_loss + paddle.sum(negative_loss)) / (
          float(positive_count + negative_count) + 1e-5
      )
  
      return balance_loss
  
file_path: PaddleOCR/ppocr/losses/det_drrg_loss.py
imports: import paddle
  import paddle.nn.functional
  from paddle import nn
label_desc: Use image node_id 2 for calling the DRRGLoss obj's underlying member method code with example usage: obj.balance_bce_loss(pred, gt, mask) and returns: balance_loss