class_docstr: GHM Regression Loss.
  
  Details of the theorem can be viewed in the paper
  `Gradient Harmonized Single-stage Detector
  <https://arxiv.org/abs/1811.05181>`_.
  
  Args:
      mu (float): The parameter for the Authentic Smooth L1 loss.
      bins (int): Number of the unit regions for distribution calculation.
      momentum (float): The parameter for moving average.
      loss_weight (float): The weight of the total GHM-R loss.
      reduction (str): Options are "none", "mean" and "sum".
          Defaults to "mean"
class_docstr_tok: ["GHM","Regression","Loss",".","Details","of","the","theorem","can","be","viewed","in","the","paper","`","Gradient","Harmonized","Single-stage","Detector","<","https",":","\/\/arxiv.org\/abs\/1811.05181",">","`","_",".","Args",":","mu","(","float",")",":","The","parameter","for","the","Authentic","Smooth","L1","loss",".","bins","(","int",")",":","Number","of","the","unit","regions","for","distribution","calculation",".","momentum","(","float",")",":","The","parameter","for","moving","average",".","loss_weight","(","float",")",":","The","weight","of","the","total","GHM-R","loss",".","reduction","(","str",")",":","Options","are","``","none","''",",","``","mean","''","and","``","sum","''",".","Defaults","to","``","mean","''"]
docstr: Calculate the GHM-R loss.
  
  Args:
      pred (float tensor of size [batch_num, 4 (* class_num)]):
          The prediction of box regression layer. Channel number can be 4
          or 4 * class_num depending on whether it is class-agnostic.
      target (float tensor of size [batch_num, 4 (* class_num)]):
          The target regression values with the same size of pred.
      label_weight (float tensor of size [batch_num, 4 (* class_num)]):
          The weight of each sample, 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-R","loss",".","Args",":","pred","(","float","tensor","of","size","[","batch_num",",","4","(","*","class_num",")","]",")",":","The","prediction","of","box","regression","layer",".","Channel","number","can","be","4","or","4","*","class_num","depending","on","whether","it","is","class-agnostic",".","target","(","float","tensor","of","size","[","batch_num",",","4","(","*","class_num",")","]",")",":","The","target","regression","values","with","the","same","size","of","pred",".","label_weight","(","float","tensor","of","size","[","batch_num",",","4","(","*","class_num",")","]",")",":","The","weight","of","each","sample",",","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,
      avg_factor=None,
      reduction_override=None,
  ):
      """Calculate the GHM-R loss.
  
      Args:
          pred (float tensor of size [batch_num, 4 (* class_num)]):
              The prediction of box regression layer. Channel number can be 4
              or 4 * class_num depending on whether it is class-agnostic.
          target (float tensor of size [batch_num, 4 (* class_num)]):
              The target regression values with the same size of pred.
          label_weight (float tensor of size [batch_num, 4 (* class_num)]):
              The weight of each sample, 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
      )
      mu = self.mu
      edges = self.edges
      mmt = self.momentum
  
      # ASL1 loss
      diff = pred - target
      loss = torch.sqrt(diff * diff + mu * mu) - mu
  
      # gradient length
      g = torch.abs(diff / torch.sqrt(mu * mu + diff * diff)).detach()
      weights = torch.zeros_like(g)
  
      valid = label_weight > 0
      tot = max(label_weight.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:
              n += 1
              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
      if n > 0:
          weights /= n
      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 GHMR obj's underlying member method code with example usage: obj.forward(pred, target, label_weight, avg_factor, reduction_override) and returns: unknown