class_docstr: Bridging the Gap Between Anchor-based and Anchor-free Detection via
  Adaptive Training Sample Selection.
  
  ATSS head structure is similar with FCOS, however ATSS use anchor boxes
  and assign label by Adaptive Training Sample Selection instead max-iou.
  
  https://arxiv.org/abs/1912.02424
class_docstr_tok: ["Bridging","the","Gap","Between","Anchor-based","and","Anchor-free","Detection","via","Adaptive","Training","Sample","Selection",".","ATSS","head","structure","is","similar","with","FCOS",",","however","ATSS","use","anchor","boxes","and","assign","label","by","Adaptive","Training","Sample","Selection","instead","max-iou",".","https",":","\/\/arxiv.org\/abs\/1912.02424"]
docstr: Compute losses of the head.
  
  Args:
      cls_scores (list[Tensor]): Box scores for each scale level
          Has shape (N, num_anchors * num_classes, H, W)
      bbox_preds (list[Tensor]): Box energies / deltas for each scale
          level with shape (N, num_anchors * 4, H, W)
      centernesses (list[Tensor]): Centerness for each scale
          level with shape (N, num_anchors * 1, H, W)
      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","]",")",":","Box","scores","for","each","scale","level","Has","shape","(","N",",","num_anchors","*","num_classes",",","H",",","W",")","bbox_preds","(","list","[","Tensor","]",")",":","Box","energies","\/","deltas","for","each","scale","level","with","shape","(","N",",","num_anchors","*","4",",","H",",","W",")","centernesses","(","list","[","Tensor","]",")",":","Centerness","for","each","scale","level","with","shape","(","N",",","num_anchors","*","1",",","H",",","W",")","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,
      centernesses,
      gt_bboxes,
      gt_labels,
      img_metas,
      gt_bboxes_ignore=None,
  ):
      """Compute losses of the head.
  
      Args:
          cls_scores (list[Tensor]): Box scores for each scale level
              Has shape (N, num_anchors * num_classes, H, W)
          bbox_preds (list[Tensor]): Box energies / deltas for each scale
              level with shape (N, num_anchors * 4, H, W)
          centernesses (list[Tensor]): Centerness for each scale
              level with shape (N, num_anchors * 1, H, W)
          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,
          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,
          num_total_pos,
          num_total_neg,
      ) = cls_reg_targets
  
      num_total_samples = reduce_mean(
          torch.tensor(num_total_pos).cuda()
      ).item()
      num_total_samples = max(num_total_samples, 1.0)
  
      (
          losses_cls,
          losses_bbox,
          loss_centerness,
          bbox_avg_factor,
      ) = multi_apply(
          self.loss_single,
          anchor_list,
          cls_scores,
          bbox_preds,
          centernesses,
          labels_list,
          label_weights_list,
          bbox_targets_list,
          num_total_samples=num_total_samples,
      )
  
      bbox_avg_factor = sum(bbox_avg_factor)
      bbox_avg_factor = reduce_mean(bbox_avg_factor).item()
      if bbox_avg_factor < EPS:
          bbox_avg_factor = 1
      losses_bbox = list(
          map(lambda x: x / bbox_avg_factor, losses_bbox)
      )
      return dict(
          loss_cls=losses_cls,
          loss_bbox=losses_bbox,
          loss_centerness=loss_centerness,
      )
  
imports: import torch
  import torch.nn
  from mmcv.cnn import ConvModule, Scale, bias_init_with_prob, normal_init
  from mmcv.runner import force_fp32
  from mmdet.core import anchor_inside_flags, build_assigner, build_sampler, images_to_levels, multi_apply, multiclass_nms, reduce_mean, unmap
  from ..builder import HEADS, build_loss
  from .anchor_head import AnchorHead
label_desc: Use image node_id 7 for calling the ATSSHead obj's underlying member method code with example usage: obj.loss(cls_scores, bbox_preds, centernesses, gt_bboxes, gt_labels, img_metas, gt_bboxes_ignore) and returns: dict, None