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 losses of the head.
  
  Args:
      cls_scores (list[Tensor]): Cls and quality scores for each scale
          level has shape (N, num_classes, H, W).
      bbox_preds (list[Tensor]): Box distribution logits for each scale
          level with shape (N, 4*(n+1), H, W), n is max value of integral
          set.
      gt_bboxes (list[Tensor]): Ground truth bboxes for each image with
          shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
      gt_labels (list[Tensor]): class indices corresponding to each box
      img_metas (list[dict]): Meta information of each image, e.g.,
          image size, scaling factor, etc.
      gt_bboxes_ignore (list[Tensor] | None): specify which bounding
          boxes can be ignored when computing the loss.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Compute","losses","of","the","head",".","Args",":","cls_scores","(","list","[","Tensor","]",")",":","Cls","and","quality","scores","for","each","scale","level","has","shape","(","N",",","num_classes",",","H",",","W",")",".","bbox_preds","(","list","[","Tensor","]",")",":","Box","distribution","logits","for","each","scale","level","with","shape","(","N",",","4","*","(","n+1",")",",","H",",","W",")",",","n","is","max","value","of","integral","set",".","gt_bboxes","(","list","[","Tensor","]",")",":","Ground","truth","bboxes","for","each","image","with","shape","(","num_gts",",","4",")","in","[","tl_x",",","tl_y",",","br_x",",","br_y","]","format",".","gt_labels","(","list","[","Tensor","]",")",":","class","indices","corresponding","to","each","box","img_metas","(","list","[","dict","]",")",":","Meta","information","of","each","image",",","e.g.",",","image","size",",","scaling","factor",",","etc",".","gt_bboxes_ignore","(","list","[","Tensor","]","|","None",")",":","specify","which","bounding","boxes","can","be","ignored","when","computing","the","loss",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss(
      self,
      cls_scores,
      bbox_preds,
      kps_preds,
      gt_bboxes,
      gt_labels,
      gt_keypointss,
      img_metas,
      gt_bboxes_ignore=None,
  ):
      """Compute losses of the head.
  
      Args:
          cls_scores (list[Tensor]): Cls and quality scores for each scale
              level has shape (N, num_classes, H, W).
          bbox_preds (list[Tensor]): Box distribution logits for each scale
              level with shape (N, 4*(n+1), H, W), n is max value of integral
              set.
          gt_bboxes (list[Tensor]): Ground truth bboxes for each image with
              shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
          gt_labels (list[Tensor]): class indices corresponding to each box
          img_metas (list[dict]): Meta information of each image, e.g.,
              image size, scaling factor, etc.
          gt_bboxes_ignore (list[Tensor] | None): specify which bounding
              boxes can be ignored when computing the loss.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
  
      featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
      assert len(featmap_sizes) == self.anchor_generator.num_levels
  
      device = cls_scores[0].device
      anchor_list, valid_flag_list = self.get_anchors(
          featmap_sizes, img_metas, device=device
      )
      label_channels = (
          self.cls_out_channels if self.use_sigmoid_cls else 1
      )
  
      cls_reg_targets = self.get_targets(
          anchor_list,
          valid_flag_list,
          gt_bboxes,
          gt_keypointss,
          img_metas,
          gt_bboxes_ignore_list=gt_bboxes_ignore,
          gt_labels_list=gt_labels,
          label_channels=label_channels,
      )
      if cls_reg_targets is None:
          return None
  
      (
          anchor_list,
          labels_list,
          label_weights_list,
          bbox_targets_list,
          bbox_weights_list,
          keypoints_targets_list,
          keypoints_weights_list,
          num_total_pos,
          num_total_neg,
      ) = cls_reg_targets
  
      num_total_samples = reduce_mean(
          torch.tensor(num_total_pos, dtype=torch.float, device=device)
      ).item()
      num_total_samples = max(num_total_samples, 1.0)
  
      (
          losses_cls,
          losses_bbox,
          losses_dfl,
          losses_kps,
          avg_factor,
      ) = multi_apply(
          self.loss_single,
          anchor_list,
          cls_scores,
          bbox_preds,
          kps_preds,
          labels_list,
          label_weights_list,
          bbox_targets_list,
          keypoints_targets_list,
          keypoints_weights_list,
          self.anchor_generator.strides,
          num_total_samples=num_total_samples,
      )
  
      # if self.train_step%100==0:
      #    print('loss_cls:', losses_cls)
      #    print('avg_factor:', avg_factor)
  
      avg_factor = sum(avg_factor)
      avg_factor = reduce_mean(avg_factor).item()
      losses_bbox = list(map(lambda x: x / avg_factor, losses_bbox))
      losses = dict(loss_cls=losses_cls, loss_bbox=losses_bbox)
      if self.use_kps:
          losses_kps = list(map(lambda x: x / avg_factor, losses_kps))
          losses["loss_kps"] = losses_kps
      if self.use_dfl:
          losses_dfl = list(map(lambda x: x / avg_factor, losses_dfl))
          losses["loss_dfl"] = losses_dfl
      return losses
  
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 11 for calling the SCRFDHead obj's underlying member method code with example usage: obj.loss(cls_scores, bbox_preds, kps_preds, gt_bboxes, gt_labels, gt_keypointss, img_metas, gt_bboxes_ignore) and returns: losses, None