class_docstr: GHM Classification Loss.
  
  Details of the theorem can be viewed in the paper
  `Gradient Harmonized Single-stage Detector
  <https://arxiv.org/abs/1811.05181>`_.
  
  Args:
      bins (int): Number of the unit regions for distribution calculation.
      momentum (float): The parameter for moving average.
      use_sigmoid (bool): Can only be true for BCE based loss now.
      loss_weight (float): The weight of the total GHM-C loss.
      reduction (str): Options are "none", "mean" and "sum".
          Defaults to "mean"
class_docstr_tok: ["GHM","Classification","Loss",".","Details","of","the","theorem","can","be","viewed","in","the","paper","`","Gradient","Harmonized","Single-stage","Detector","<","https",":","\/\/arxiv.org\/abs\/1811.05181",">","`","_",".","Args",":","bins","(","int",")",":","Number","of","the","unit","regions","for","distribution","calculation",".","momentum","(","float",")",":","The","parameter","for","moving","average",".","use_sigmoid","(","bool",")",":","Can","only","be","true","for","BCE","based","loss","now",".","loss_weight","(","float",")",":","The","weight","of","the","total","GHM-C","loss",".","reduction","(","str",")",":","Options","are","``","none","''",",","``","mean","''","and","``","sum","''",".","Defaults","to","``","mean","''"]
docstr: Calculate the GHM-C loss.
  
  Args:
      pred (float tensor of size [batch_num, class_num]):
          The direct prediction of classification fc layer.
      target (float tensor of size [batch_num, class_num]):
          Binary class target for each sample.
      label_weight (float tensor of size [batch_num, class_num]):
          the value is 1 if the sample is valid and 0 if ignored.
      reduction_override (str, optional): The reduction method used to
          override the original reduction method of the loss.
          Defaults to None.
  Returns:
      The gradient harmonized loss.
docstr_tok: ["Calculate","the","GHM-C","loss",".","Args",":","pred","(","float","tensor","of","size","[","batch_num",",","class_num","]",")",":","The","direct","prediction","of","classification","fc","layer",".","target","(","float","tensor","of","size","[","batch_num",",","class_num","]",")",":","Binary","class","target","for","each","sample",".","label_weight","(","float","tensor","of","size","[","batch_num",",","class_num","]",")",":","the","value","is","1","if","the","sample","is","valid","and","0","if","ignored",".","reduction_override","(","str",",","optional",")",":","The","reduction","method","used","to","override","the","original","reduction","method","of","the","loss",".","Defaults","to","None",".","Returns",":","The","gradient","harmonized","loss","."]
code: def forward(
      self,
      pred,
      target,
      label_weight,
      reduction_override=None,
      **kwargs,
  ):
      """Calculate the GHM-C loss.
  
      Args:
          pred (float tensor of size [batch_num, class_num]):
              The direct prediction of classification fc layer.
          target (float tensor of size [batch_num, class_num]):
              Binary class target for each sample.
          label_weight (float tensor of size [batch_num, class_num]):
              the value is 1 if the sample is valid and 0 if ignored.
          reduction_override (str, optional): The reduction method used to
              override the original reduction method of the loss.
              Defaults to None.
      Returns:
          The gradient harmonized loss.
      """
      assert reduction_override in (None, "none", "mean", "sum")
      reduction = (
          reduction_override if reduction_override else self.reduction
      )
      # the target should be binary class label
      if pred.dim() != target.dim():
          target, label_weight = _expand_onehot_labels(
              target, label_weight, pred.size(-1)
          )
      target, label_weight = target.float(), label_weight.float()
      edges = self.edges
      mmt = self.momentum
      weights = torch.zeros_like(pred)
  
      # gradient length
      g = torch.abs(pred.sigmoid().detach() - target)
  
      valid = label_weight > 0
      tot = max(valid.float().sum().item(), 1.0)
      n = 0  # n valid bins
      for i in range(self.bins):
          inds = (g >= edges[i]) & (g < edges[i + 1]) & valid
          num_in_bin = inds.sum().item()
          if num_in_bin > 0:
              if mmt > 0:
                  self.acc_sum[i] = (
                      mmt * self.acc_sum[i] + (1 - mmt) * num_in_bin
                  )
                  weights[inds] = tot / self.acc_sum[i]
              else:
                  weights[inds] = tot / num_in_bin
              n += 1
      if n > 0:
          weights = weights / n
  
      loss = F.binary_cross_entropy_with_logits(
          pred, target, reduction="none"
      )
      loss = weight_reduce_loss(
          loss, weights, reduction=reduction, avg_factor=tot
      )
      return loss * self.loss_weight
  
file_path: mmdetection/mmdet/models/losses/ghm_loss.py
imports: import torch
  import torch.nn
  import torch.nn.functional
  from mmdet.registry import MODELS
  from .utils import weight_reduce_loss
label_desc: Use image node_id 2 for calling the GHMC obj's underlying member method code with example usage: obj.forward(pred, target, label_weight, reduction_override) and returns: unknown