class_docstr: Detection Head of `ATSS <https://arxiv.org/abs/1912.02424>`_.
  
  ATSS head structure is similar with FCOS, however ATSS use anchor boxes
  and assign label by Adaptive Training Sample Selection instead max-iou.
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (int): Number of channels in the input feature map.
      pred_kernel_size (int): Kernel size of ``nn.Conv2d``
      stacked_convs (int): Number of stacking convs of the head.
      conv_cfg (:obj:`ConfigDict` or dict, optional): Config dict for
          convolution layer. Defaults to None.
      norm_cfg (:obj:`ConfigDict` or dict): Config dict for normalization
          layer. Defaults to ``dict(type='GN', num_groups=32,
          requires_grad=True)``.
      reg_decoded_bbox (bool): If true, the regression loss would be
          applied directly on decoded bounding boxes, converting both
          the predicted boxes and regression targets to absolute
          coordinates format. Defaults to False. It should be `True` when
          using `IoULoss`, `GIoULoss`, or `DIoULoss` in the bbox head.
      loss_centerness (:obj:`ConfigDict` or dict): Config of centerness loss.
          Defaults to ``dict(type='CrossEntropyLoss', use_sigmoid=True,
          loss_weight=1.0)``.
      init_cfg (:obj:`ConfigDict` or dict or list[dict] or
          list[:obj:`ConfigDict`]): Initialization config dict.
docstr: Calculate the loss of a single scale level based on the features
  extracted by the detection head.
  
  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 with
          shape (N, num_total_anchors, 4).
      avg_factor (float): Average factor that is used to average
          the loss. When using sampling method, avg_factor is usually
          the sum of positive and negative priors. When using
          `PseudoSampler`, `avg_factor` is usually equal to the number
          of positive priors.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Calculate","the","loss","of","a","single","scale","level","based","on","the","features","extracted","by","the","detection","head",".","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","with","shape","(","N",",","num_total_anchors",",","4",")",".","avg_factor","(","float",")",":","Average","factor","that","is","used","to","average","the","loss",".","When","using","sampling","method",",","avg_factor","is","usually","the","sum","of","positive","and","negative","priors",".","When","using","`","PseudoSampler","`",",","`","avg_factor","`","is","usually","equal","to","the","number","of","positive","priors",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss_by_feat_single(
      self,
      anchors: Tensor,
      cls_score: Tensor,
      bbox_pred: Tensor,
      centerness: Tensor,
      labels: Tensor,
      label_weights: Tensor,
      bbox_targets: Tensor,
      avg_factor: float,
  ) -> dict:
      """Calculate the loss of a single scale level based on the features
      extracted by the detection head.
  
      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 with
              shape (N, num_total_anchors, 4).
          avg_factor (float): Average factor that is used to average
              the loss. When using sampling method, avg_factor is usually
              the sum of positive and negative priors. When using
              `PseudoSampler`, `avg_factor` is usually equal to the number
              of positive priors.
  
      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=avg_factor
      )
  
      # 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
          )
  
          # regression loss
          loss_bbox = self.loss_bbox(
              pos_decode_bbox_pred,
              pos_bbox_targets,
              weight=centerness_targets,
              avg_factor=1.0,
          )
  
          # centerness loss
          loss_centerness = self.loss_centerness(
              pos_centerness, centerness_targets, avg_factor=avg_factor
          )
  
      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: from typing import List, Optional, Sequence, Tuple
  import torch
  import torch.nn
  from mmcv.cnn import ConvModule, Scale
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.utils import ConfigType, InstanceList, MultiConfig, OptConfigType, OptInstanceList, reduce_mean
  from ..task_modules.prior_generators import anchor_inside_flags
  from ..utils import images_to_levels, multi_apply, unmap
  from .anchor_head import AnchorHead
label_desc: Use image node_id 5 for calling the ATSSHead obj's underlying member method code with example usage: obj.loss_by_feat_single(anchors, cls_score, bbox_pred, centerness, labels, label_weights, bbox_targets, avg_factor) and returns: loss_cls,  loss_bbox,  loss_centerness,  centerness_targets