class_docstr: Anchor-free head used in `FSAF <https://arxiv.org/abs/1903.00621>`_.
  
  The head contains two subnetworks. The first classifies anchor boxes and
  the second regresses deltas for the anchors (num_anchors is 1 for anchor-
  free methods)
  
  Args:
      *args: Same as its base class in :class:`RetinaHead`
      score_threshold (float, optional): The score_threshold to calculate
          positive recall. If given, prediction scores lower than this value
          is counted as incorrect prediction. Default to None.
      **kwargs: Same as its base class in :class:`RetinaHead`
  
  Example:
      >>> import torch
      >>> self = FSAFHead(11, 7)
      >>> x = torch.rand(1, 7, 32, 32)
      >>> cls_score, bbox_pred = self.forward_single(x)
      >>> # Each anchor predicts a score for each class except background
      >>> cls_per_anchor = cls_score.shape[1] / self.num_anchors
      >>> box_per_anchor = bbox_pred.shape[1] / self.num_anchors
      >>> assert cls_per_anchor == self.num_classes
      >>> assert box_per_anchor == 4
class_docstr_tok: ["Anchor-free","head","used","in","`","FSAF","<","https",":","\/\/arxiv.org\/abs\/1903.00621",">","`","_",".","The","head","contains","two","subnetworks",".","The","first","classifies","anchor","boxes","and","the","second","regresses","deltas","for","the","anchors","(","num_anchors","is","1","for","anchor-","free","methods",")","Args",":","*","args",":","Same","as","its","base","class","in",":","class",":","`","RetinaHead","`","score_threshold","(","float",",","optional",")",":","The","score_threshold","to","calculate","positive","recall",".","If","given",",","prediction","scores","lower","than","this","value","is","counted","as","incorrect","prediction",".","Default","to","None",".","*","*","kwargs",":","Same","as","its","base","class","in",":","class",":","`","RetinaHead","`","Example",":",">",">",">","import","torch",">",">",">","self","=","FSAFHead","(","11",",","7",")",">",">",">","x","=","torch.rand","(","1",",","7",",","32",",","32",")",">",">",">","cls_score",",","bbox_pred","=","self.forward_single","(","x",")",">",">",">","#","Each","anchor","predicts","a","score","for","each","class","except","background",">",">",">","cls_per_anchor","=","cls_score.shape","[","1","]","\/","self.num_anchors",">",">",">","box_per_anchor","=","bbox_pred.shape","[","1","]","\/","self.num_anchors",">",">",">","assert","cls_per_anchor","==","self.num_classes",">",">",">","assert","box_per_anchor","==","4"]
docstr: Reweight loss values at each level.
  
  Reassign loss values at each level by masking those where the
  pre-calculated loss is too large. Then return the reduced losses.
  
  Args:
      cls_loss (Tensor): Element-wise classification loss.
        Shape: (num_anchors, num_classes)
      reg_loss (Tensor): Element-wise regression loss.
        Shape: (num_anchors, 4)
      assigned_gt_inds (Tensor): The gt indices that each anchor bbox
        is assigned to. -1 denotes a negative anchor, otherwise it is the
        gt index (0-based). Shape: (num_anchors, ),
      labels (Tensor): Label assigned to anchors. Shape: (num_anchors, ).
      level (int): The current level index in the pyramid
        (0-4 for RetinaNet)
      min_levels (Tensor): The best-matching level for each gt.
        Shape: (num_gts, ),
  
  Returns:
      tuple:
          - cls_loss: Reduced corrected classification loss. Scalar.
          - reg_loss: Reduced corrected regression loss. Scalar.
          - pos_flags (Tensor): Corrected bool tensor indicating the
            final postive anchors. Shape: (num_anchors, ).
docstr_tok: ["Reweight","loss","values","at","each","level",".","Reassign","loss","values","at","each","level","by","masking","those","where","the","pre-calculated","loss","is","too","large",".","Then","return","the","reduced","losses",".","Args",":","cls_loss","(","Tensor",")",":","Element-wise","classification","loss",".","Shape",":","(","num_anchors",",","num_classes",")","reg_loss","(","Tensor",")",":","Element-wise","regression","loss",".","Shape",":","(","num_anchors",",","4",")","assigned_gt_inds","(","Tensor",")",":","The","gt","indices","that","each","anchor","bbox","is","assigned","to",".","-1","denotes","a","negative","anchor",",","otherwise","it","is","the","gt","index","(","0-based",")",".","Shape",":","(","num_anchors",",",")",",","labels","(","Tensor",")",":","Label","assigned","to","anchors",".","Shape",":","(","num_anchors",",",")",".","level","(","int",")",":","The","current","level","index","in","the","pyramid","(","0-4","for","RetinaNet",")","min_levels","(","Tensor",")",":","The","best-matching","level","for","each","gt",".","Shape",":","(","num_gts",",",")",",","Returns",":","tuple",":","-","cls_loss",":","Reduced","corrected","classification","loss",".","Scalar",".","-","reg_loss",":","Reduced","corrected","regression","loss",".","Scalar",".","-","pos_flags","(","Tensor",")",":","Corrected","bool","tensor","indicating","the","final","postive","anchors",".","Shape",":","(","num_anchors",",",")","."]
code: def reweight_loss_single(
      self,
      cls_loss,
      reg_loss,
      assigned_gt_inds,
      labels,
      level,
      min_levels,
  ):
      """Reweight loss values at each level.
  
      Reassign loss values at each level by masking those where the
      pre-calculated loss is too large. Then return the reduced losses.
  
      Args:
          cls_loss (Tensor): Element-wise classification loss.
            Shape: (num_anchors, num_classes)
          reg_loss (Tensor): Element-wise regression loss.
            Shape: (num_anchors, 4)
          assigned_gt_inds (Tensor): The gt indices that each anchor bbox
            is assigned to. -1 denotes a negative anchor, otherwise it is the
            gt index (0-based). Shape: (num_anchors, ),
          labels (Tensor): Label assigned to anchors. Shape: (num_anchors, ).
          level (int): The current level index in the pyramid
            (0-4 for RetinaNet)
          min_levels (Tensor): The best-matching level for each gt.
            Shape: (num_gts, ),
  
      Returns:
          tuple:
              - cls_loss: Reduced corrected classification loss. Scalar.
              - reg_loss: Reduced corrected regression loss. Scalar.
              - pos_flags (Tensor): Corrected bool tensor indicating the
                final postive anchors. Shape: (num_anchors, ).
      """
      loc_weight = torch.ones_like(reg_loss)
      cls_weight = torch.ones_like(cls_loss)
      pos_flags = assigned_gt_inds >= 0  # positive pixel flag
      pos_indices = torch.nonzero(pos_flags, as_tuple=False).flatten()
  
      if pos_flags.any():  # pos pixels exist
          pos_assigned_gt_inds = assigned_gt_inds[pos_flags]
          zeroing_indices = min_levels[pos_assigned_gt_inds] != level
          neg_indices = pos_indices[zeroing_indices]
  
          if neg_indices.numel():
              pos_flags[neg_indices] = 0
              loc_weight[neg_indices] = 0
              # Only the weight corresponding to the label is
              #  zeroed out if not selected
              zeroing_labels = labels[neg_indices]
              assert (zeroing_labels >= 0).all()
              cls_weight[neg_indices, zeroing_labels] = 0
  
      # Weighted loss for both cls and reg loss
      cls_loss = weight_reduce_loss(
          cls_loss, cls_weight, reduction="sum"
      )
      reg_loss = weight_reduce_loss(
          reg_loss, loc_weight, reduction="sum"
      )
  
      return cls_loss, reg_loss, pos_flags
  
file_path: insightface/detection/scrfd/mmdet/models/dense_heads/fsaf_head.py
imports: import numpy
  import torch
  from mmcv.cnn import normal_init
  from mmcv.runner import force_fp32
  from mmdet.core import anchor_inside_flags, images_to_levels, multi_apply, unmap
  from ..builder import HEADS
  from ..losses.accuracy import accuracy
  from ..losses.utils import weight_reduce_loss
  from .retina_head import RetinaHead
label_desc: Use image node_id 8 for calling the FSAFHead obj's underlying member method code with example usage: obj.reweight_loss_single(cls_loss, reg_loss, assigned_gt_inds, labels, level, min_levels) and returns: cls_loss,  reg_loss,  pos_flags