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 loss of a single scale level.
  
  Args:
      cls_score (Tensor): Box scores for each scale level
          Has shape (N, num_anchors * num_classes, H, W).
      bbox_pred (Tensor): Box energies / deltas for each scale
          level with shape (N, num_anchors * 4, H, W).
      anchors (Tensor): Box reference for each scale level with shape
          (N, num_total_anchors, 4).
      labels (Tensor): Labels of each anchors with shape
          (N, num_total_anchors).
      label_weights (Tensor): Label weights of each anchor with shape
          (N, num_total_anchors)
      bbox_targets (Tensor): BBox regression targets of each anchor wight
          shape (N, num_total_anchors, 4).
      num_total_samples (int): Number os positive samples that is
          reduced over all GPUs.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Compute","loss","of","a","single","scale","level",".","Args",":","cls_score","(","Tensor",")",":","Box","scores","for","each","scale","level","Has","shape","(","N",",","num_anchors","*","num_classes",",","H",",","W",")",".","bbox_pred","(","Tensor",")",":","Box","energies","\/","deltas","for","each","scale","level","with","shape","(","N",",","num_anchors","*","4",",","H",",","W",")",".","anchors","(","Tensor",")",":","Box","reference","for","each","scale","level","with","shape","(","N",",","num_total_anchors",",","4",")",".","labels","(","Tensor",")",":","Labels","of","each","anchors","with","shape","(","N",",","num_total_anchors",")",".","label_weights","(","Tensor",")",":","Label","weights","of","each","anchor","with","shape","(","N",",","num_total_anchors",")","bbox_targets","(","Tensor",")",":","BBox","regression","targets","of","each","anchor","wight","shape","(","N",",","num_total_anchors",",","4",")",".","num_total_samples","(","int",")",":","Number","os","positive","samples","that","is","reduced","over","all","GPUs",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss_single(
      self,
      anchors,
      cls_score,
      bbox_pred,
      centerness,
      labels,
      label_weights,
      bbox_targets,
      num_total_samples,
  ):
      """Compute loss of a single scale level.
  
      Args:
          cls_score (Tensor): Box scores for each scale level
              Has shape (N, num_anchors * num_classes, H, W).
          bbox_pred (Tensor): Box energies / deltas for each scale
              level with shape (N, num_anchors * 4, H, W).
          anchors (Tensor): Box reference for each scale level with shape
              (N, num_total_anchors, 4).
          labels (Tensor): Labels of each anchors with shape
              (N, num_total_anchors).
          label_weights (Tensor): Label weights of each anchor with shape
              (N, num_total_anchors)
          bbox_targets (Tensor): BBox regression targets of each anchor wight
              shape (N, num_total_anchors, 4).
          num_total_samples (int): Number os positive samples that is
              reduced over all GPUs.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
  
      anchors = anchors.reshape(-1, 4)
      cls_score = (
          cls_score.permute(0, 2, 3, 1)
          .reshape(-1, self.cls_out_channels)
          .contiguous()
      )
      bbox_pred = bbox_pred.permute(0, 2, 3, 1).reshape(-1, 4)
      centerness = centerness.permute(0, 2, 3, 1).reshape(-1)
      bbox_targets = bbox_targets.reshape(-1, 4)
      labels = labels.reshape(-1)
      label_weights = label_weights.reshape(-1)
  
      # classification loss
      loss_cls = self.loss_cls(
          cls_score, labels, label_weights, avg_factor=num_total_samples
      )
  
      # FG cat_id: [0, num_classes -1], BG cat_id: num_classes
      bg_class_ind = self.num_classes
      pos_inds = (
          ((labels >= 0) & (labels < bg_class_ind)).nonzero().squeeze(1)
      )
  
      if len(pos_inds) > 0:
          pos_bbox_targets = bbox_targets[pos_inds]
          pos_bbox_pred = bbox_pred[pos_inds]
          pos_anchors = anchors[pos_inds]
          pos_centerness = centerness[pos_inds]
  
          centerness_targets = self.centerness_target(
              pos_anchors, pos_bbox_targets
          )
          pos_decode_bbox_pred = self.bbox_coder.decode(
              pos_anchors, pos_bbox_pred
          )
          pos_decode_bbox_targets = self.bbox_coder.decode(
              pos_anchors, pos_bbox_targets
          )
  
          # regression loss
          loss_bbox = self.loss_bbox(
              pos_decode_bbox_pred,
              pos_decode_bbox_targets,
              weight=centerness_targets,
              avg_factor=1.0,
          )
  
          # centerness loss
          loss_centerness = self.loss_centerness(
              pos_centerness,
              centerness_targets,
              avg_factor=num_total_samples,
          )
  
      else:
          loss_bbox = bbox_pred.sum() * 0
          loss_centerness = centerness.sum() * 0
          centerness_targets = bbox_targets.new_tensor(0.0)
  
      return (
          loss_cls,
          loss_bbox,
          loss_centerness,
          centerness_targets.sum(),
      )
  
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 6 for calling the ATSSHead obj's underlying member method code with example usage: obj.loss_single(anchors, cls_score, bbox_pred, centerness, labels, label_weights, bbox_targets, num_total_samples) and returns: loss_cls,  loss_bbox,  loss_centerness,  centerness_targets