class_docstr: Generalized Focal Loss: Learning Qualified and Distributed Bounding
  Boxes for Dense Object Detection.
  
  GFL head structure is similar with ATSS, however GFL uses
  1) joint representation for classification and localization quality, and
  2) flexible General distribution for bounding box locations,
  which are supervised by
  Quality Focal Loss (QFL) and Distribution Focal Loss (DFL), respectively
  
  https://arxiv.org/abs/2006.04388
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (int): Number of channels in the input feature map.
      stacked_convs (int): Number of conv layers in cls and reg tower.
          Default: 4.
      conv_cfg (dict): dictionary to construct and config conv layer.
          Default: None.
      norm_cfg (dict): dictionary to construct and config norm layer.
          Default: dict(type='GN', num_groups=32, requires_grad=True).
      loss_qfl (dict): Config of Quality Focal Loss (QFL).
      reg_max (int): Max value of integral set :math: `{0, ..., reg_max}`
          in QFL setting. Default: 16.
  Example:
      >>> self = GFLHead(11, 7)
      >>> feats = [torch.rand(1, 7, s, s) for s in [4, 8, 16, 32, 64]]
      >>> cls_quality_score, bbox_pred = self.forward(feats)
      >>> assert len(cls_quality_score) == len(self.scales)
class_docstr_tok: ["Generalized","Focal","Loss",":","Learning","Qualified","and","Distributed","Bounding","Boxes","for","Dense","Object","Detection",".","GFL","head","structure","is","similar","with","ATSS",",","however","GFL","uses","1",")","joint","representation","for","classification","and","localization","quality",",","and","2",")","flexible","General","distribution","for","bounding","box","locations",",","which","are","supervised","by","Quality","Focal","Loss","(","QFL",")","and","Distribution","Focal","Loss","(","DFL",")",",","respectively","https",":","\/\/arxiv.org\/abs\/2006.04388","Args",":","num_classes","(","int",")",":","Number","of","categories","excluding","the","background","category",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","stacked_convs","(","int",")",":","Number","of","conv","layers","in","cls","and","reg","tower",".","Default",":","4.","conv_cfg","(","dict",")",":","dictionary","to","construct","and","config","conv","layer",".","Default",":","None",".","norm_cfg","(","dict",")",":","dictionary","to","construct","and","config","norm","layer",".","Default",":","dict","(","type='GN","'",",","num_groups=32",",","requires_grad=True",")",".","loss_qfl","(","dict",")",":","Config","of","Quality","Focal","Loss","(","QFL",")",".","reg_max","(","int",")",":","Max","value","of","integral","set",":","math",":","`","{","0",",","...",",","reg_max","}","`","in","QFL","setting",".","Default",":","16",".","Example",":",">",">",">","self","=","GFLHead","(","11",",","7",")",">",">",">","feats","=","[","torch.rand","(","1",",","7",",","s",",","s",")","for","s","in","[","4",",","8",",","16",",","32",",","64","]","]",">",">",">","cls_quality_score",",","bbox_pred","=","self.forward","(","feats",")",">",">",">","assert","len","(","cls_quality_score",")","==","len","(","self.scales",")"]
docstr: Compute regression, classification targets for anchors in a single
  image.
  
  Args:
      flat_anchors (Tensor): Multi-level anchors of the image, which are
          concatenated into a single tensor of shape (num_anchors, 4)
      valid_flags (Tensor): Multi level valid flags of the image,
          which are concatenated into a single tensor of
              shape (num_anchors,).
      num_level_anchors Tensor): Number of anchors of each scale level.
      gt_bboxes (Tensor): Ground truth bboxes of the image,
          shape (num_gts, 4).
      gt_bboxes_ignore (Tensor): Ground truth bboxes to be
          ignored, shape (num_ignored_gts, 4).
      gt_labels (Tensor): Ground truth labels of each box,
          shape (num_gts,).
      img_meta (dict): Meta info of the image.
      label_channels (int): Channel of label.
      unmap_outputs (bool): Whether to map outputs back to the original
          set of anchors.
  
  Returns:
      tuple: N is the number of total anchors in the image.
          anchors (Tensor): All anchors in the image with shape (N, 4).
          labels (Tensor): Labels of all anchors in the image with shape
              (N,).
          label_weights (Tensor): Label weights of all anchor in the
              image with shape (N,).
          bbox_targets (Tensor): BBox targets of all anchors in the
              image with shape (N, 4).
          bbox_weights (Tensor): BBox weights of all anchors in the
              image with shape (N, 4).
          pos_inds (Tensor): Indices of postive anchor with shape
              (num_pos,).
          neg_inds (Tensor): Indices of negative anchor with shape
              (num_neg,).
docstr_tok: ["Compute","regression",",","classification","targets","for","anchors","in","a","single","image",".","Args",":","flat_anchors","(","Tensor",")",":","Multi-level","anchors","of","the","image",",","which","are","concatenated","into","a","single","tensor","of","shape","(","num_anchors",",","4",")","valid_flags","(","Tensor",")",":","Multi","level","valid","flags","of","the","image",",","which","are","concatenated","into","a","single","tensor","of","shape","(","num_anchors",",",")",".","num_level_anchors","Tensor",")",":","Number","of","anchors","of","each","scale","level",".","gt_bboxes","(","Tensor",")",":","Ground","truth","bboxes","of","the","image",",","shape","(","num_gts",",","4",")",".","gt_bboxes_ignore","(","Tensor",")",":","Ground","truth","bboxes","to","be","ignored",",","shape","(","num_ignored_gts",",","4",")",".","gt_labels","(","Tensor",")",":","Ground","truth","labels","of","each","box",",","shape","(","num_gts",",",")",".","img_meta","(","dict",")",":","Meta","info","of","the","image",".","label_channels","(","int",")",":","Channel","of","label",".","unmap_outputs","(","bool",")",":","Whether","to","map","outputs","back","to","the","original","set","of","anchors",".","Returns",":","tuple",":","N","is","the","number","of","total","anchors","in","the","image",".","anchors","(","Tensor",")",":","All","anchors","in","the","image","with","shape","(","N",",","4",")",".","labels","(","Tensor",")",":","Labels","of","all","anchors","in","the","image","with","shape","(","N",",",")",".","label_weights","(","Tensor",")",":","Label","weights","of","all","anchor","in","the","image","with","shape","(","N",",",")",".","bbox_targets","(","Tensor",")",":","BBox","targets","of","all","anchors","in","the","image","with","shape","(","N",",","4",")",".","bbox_weights","(","Tensor",")",":","BBox","weights","of","all","anchors","in","the","image","with","shape","(","N",",","4",")",".","pos_inds","(","Tensor",")",":","Indices","of","postive","anchor","with","shape","(","num_pos",",",")",".","neg_inds","(","Tensor",")",":","Indices","of","negative","anchor","with","shape","(","num_neg",",",")","."]
code: def _get_target_single(
      self,
      flat_anchors,
      valid_flags,
      num_level_anchors,
      gt_bboxes,
      gt_bboxes_ignore,
      gt_labels,
      gt_keypointss,
      img_meta,
      label_channels=1,
      unmap_outputs=True,
  ):
      """Compute regression, classification targets for anchors in a single
      image.
  
      Args:
          flat_anchors (Tensor): Multi-level anchors of the image, which are
              concatenated into a single tensor of shape (num_anchors, 4)
          valid_flags (Tensor): Multi level valid flags of the image,
              which are concatenated into a single tensor of
                  shape (num_anchors,).
          num_level_anchors Tensor): Number of anchors of each scale level.
          gt_bboxes (Tensor): Ground truth bboxes of the image,
              shape (num_gts, 4).
          gt_bboxes_ignore (Tensor): Ground truth bboxes to be
              ignored, shape (num_ignored_gts, 4).
          gt_labels (Tensor): Ground truth labels of each box,
              shape (num_gts,).
          img_meta (dict): Meta info of the image.
          label_channels (int): Channel of label.
          unmap_outputs (bool): Whether to map outputs back to the original
              set of anchors.
  
      Returns:
          tuple: N is the number of total anchors in the image.
              anchors (Tensor): All anchors in the image with shape (N, 4).
              labels (Tensor): Labels of all anchors in the image with shape
                  (N,).
              label_weights (Tensor): Label weights of all anchor in the
                  image with shape (N,).
              bbox_targets (Tensor): BBox targets of all anchors in the
                  image with shape (N, 4).
              bbox_weights (Tensor): BBox weights of all anchors in the
                  image with shape (N, 4).
              pos_inds (Tensor): Indices of postive anchor with shape
                  (num_pos,).
              neg_inds (Tensor): Indices of negative anchor with shape
                  (num_neg,).
      """
      inside_flags = anchor_inside_flags(
          flat_anchors,
          valid_flags,
          img_meta["img_shape"][:2],
          self.train_cfg.allowed_border,
      )
      if not inside_flags.any():
          return (None,) * 7
      # assign gt and sample anchors
      anchors = flat_anchors[inside_flags, :]
  
      num_level_anchors_inside = self.get_num_level_anchors_inside(
          num_level_anchors, inside_flags
      )
      if self.assigner.__class__.__name__ == "ATSSAssigner":
          assign_result = self.assigner.assign(
              anchors,
              num_level_anchors_inside,
              gt_bboxes,
              gt_bboxes_ignore,
              gt_labels,
          )
      else:
          assign_result = self.assigner.assign(
              anchors, gt_bboxes, gt_bboxes_ignore, gt_labels
          )
  
      sampling_result = self.sampler.sample(
          assign_result, anchors, gt_bboxes
      )
  
      num_valid_anchors = anchors.shape[0]
      bbox_targets = torch.zeros_like(anchors)
      bbox_weights = torch.zeros_like(anchors)
      kps_targets = anchors.new_zeros(
          size=(anchors.shape[0], self.NK * 2)
      )
      kps_weights = anchors.new_zeros(
          size=(anchors.shape[0], self.NK * 2)
      )
      labels = anchors.new_full(
          (num_valid_anchors,), self.num_classes, dtype=torch.long
      )
      label_weights = anchors.new_zeros(
          num_valid_anchors, dtype=torch.float
      )
  
      pos_inds = sampling_result.pos_inds
      neg_inds = sampling_result.neg_inds
      if len(pos_inds) > 0:
          pos_bbox_targets = sampling_result.pos_gt_bboxes
          bbox_targets[pos_inds, :] = pos_bbox_targets
          bbox_weights[pos_inds, :] = 1.0
          if self.use_kps:
              pos_assigned_gt_inds = (
                  sampling_result.pos_assigned_gt_inds
              )
              # print('BBB', anchors.shape, gt_bboxes.shape, gt_keypointss.shape, pos_inds.shape, bbox_targets.shape, pos_bbox_targets.shape)
              kps_targets[pos_inds, :] = gt_keypointss[
                  pos_assigned_gt_inds, :, :2
              ].reshape((-1, self.NK * 2))
              kps_weights[pos_inds, :] = torch.mean(
                  gt_keypointss[pos_assigned_gt_inds, :, 2],
                  dim=1,
                  keepdims=True,
              )
          # kps_weights[pos_inds, :] = 1.0
          if gt_labels is None:
              # Only rpn gives gt_labels as None
              # Foreground is the first class
              labels[pos_inds] = 0
          else:
              labels[pos_inds] = gt_labels[
                  sampling_result.pos_assigned_gt_inds
              ]
          if self.train_cfg.pos_weight <= 0:
              label_weights[pos_inds] = 1.0
          else:
              label_weights[pos_inds] = self.train_cfg.pos_weight
      if len(neg_inds) > 0:
          label_weights[neg_inds] = 1.0
  
      # map up to original set of anchors
      if unmap_outputs:
          num_total_anchors = flat_anchors.size(0)
          anchors = unmap(anchors, num_total_anchors, inside_flags)
          labels = unmap(
              labels,
              num_total_anchors,
              inside_flags,
              fill=self.num_classes,
          )
          label_weights = unmap(
              label_weights, num_total_anchors, inside_flags
          )
          bbox_targets = unmap(
              bbox_targets, num_total_anchors, inside_flags
          )
          bbox_weights = unmap(
              bbox_weights, num_total_anchors, inside_flags
          )
          if self.use_kps:
              kps_targets = unmap(
                  kps_targets, num_total_anchors, inside_flags
              )
              kps_weights = unmap(
                  kps_weights, num_total_anchors, inside_flags
              )
  
      return (
          anchors,
          labels,
          label_weights,
          bbox_targets,
          bbox_weights,
          kps_targets,
          kps_weights,
          pos_inds,
          neg_inds,
      )
  
imports: import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import ConvModule, Scale, bias_init_with_prob, normal_init, DepthwiseSeparableConvModule
  from mmcv.cnn import constant_init, kaiming_init
  from mmcv.runner import force_fp32
  from mmdet.core import anchor_inside_flags, bbox2distance, bbox_overlaps, build_assigner, build_sampler, distance2bbox, distance2kps, kps2distance, images_to_levels, multi_apply, multiclass_nms, reduce_mean, unmap
  from ..builder import HEADS, build_loss
  from .anchor_head import AnchorHead
  from .base_dense_head import BaseDenseHead
  from .dense_test_mixins import BBoxTestMixin
label_desc: Use image node_id 15 for calling the SCRFDHead obj's underlying member method code with example usage: obj._get_target_single(flat_anchors, valid_flags, num_level_anchors, gt_bboxes, gt_bboxes_ignore, gt_labels, gt_keypointss, img_meta, label_channels, unmap_outputs) and returns: anchors,  labels,  label_weights,  bbox_targets,  bbox_weights,  kps_targets,  kps_weights,  pos_inds,  neg_inds, unknown