class_docstr: Anchor-based head (RPN, RetinaNet, SSD, etc.).
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (int): Number of channels in the input feature map.
      feat_channels (int): Number of hidden channels. Used in child classes.
      anchor_generator (dict): Config dict for anchor generator
      bbox_coder (dict): Config of bounding box coder.
      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. Default False. It should be `True` when
          using `IoULoss`, `GIoULoss`, or `DIoULoss` in the bbox head.
      loss_cls (dict): Config of classification loss.
      loss_bbox (dict): Config of localization loss.
      train_cfg (dict): Training config of anchor head.
      test_cfg (dict): Testing config of anchor head.
      init_cfg (dict or list[dict], optional): Initialization config dict.
class_docstr_tok: ["Anchor-based","head","(","RPN",",","RetinaNet",",","SSD",",","etc.",")",".","Args",":","num_classes","(","int",")",":","Number","of","categories","excluding","the","background","category",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","feat_channels","(","int",")",":","Number","of","hidden","channels",".","Used","in","child","classes",".","anchor_generator","(","dict",")",":","Config","dict","for","anchor","generator","bbox_coder","(","dict",")",":","Config","of","bounding","box","coder",".","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",".","Default","False",".","It","should","be","`","True","`","when","using","`","IoULoss","`",",","`","GIoULoss","`",",","or","`","DIoULoss","`","in","the","bbox","head",".","loss_cls","(","dict",")",":","Config","of","classification","loss",".","loss_bbox","(","dict",")",":","Config","of","localization","loss",".","train_cfg","(","dict",")",":","Training","config","of","anchor","head",".","test_cfg","(","dict",")",":","Testing","config","of","anchor","head",".","init_cfg","(","dict","or","list","[","dict","]",",","optional",")",":","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
          weight shape (N, num_total_anchors, 4).
      bbox_weights (Tensor): BBox regression loss weights of each anchor
          with shape (N, num_total_anchors, 4).
      avg_factor (int): Average factor that is used to average the loss.
  
  Returns:
      tuple: 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","weight","shape","(","N",",","num_total_anchors",",","4",")",".","bbox_weights","(","Tensor",")",":","BBox","regression","loss","weights","of","each","anchor","with","shape","(","N",",","num_total_anchors",",","4",")",".","avg_factor","(","int",")",":","Average","factor","that","is","used","to","average","the","loss",".","Returns",":","tuple",":","loss","components","."]
code: def loss_by_feat_single(
      self,
      cls_score: Tensor,
      bbox_pred: Tensor,
      anchors: Tensor,
      labels: Tensor,
      label_weights: Tensor,
      bbox_targets: Tensor,
      bbox_weights: Tensor,
      avg_factor: int,
  ) -> tuple:
      """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
              weight shape (N, num_total_anchors, 4).
          bbox_weights (Tensor): BBox regression loss weights of each anchor
              with shape (N, num_total_anchors, 4).
          avg_factor (int): Average factor that is used to average the loss.
  
      Returns:
          tuple: loss components.
      """
      # classification loss
      labels = labels.reshape(-1)
      label_weights = label_weights.reshape(-1)
      cls_score = cls_score.permute(0, 2, 3, 1).reshape(
          -1, self.cls_out_channels
      )
      loss_cls = self.loss_cls(
          cls_score, labels, label_weights, avg_factor=avg_factor
      )
      # regression loss
      target_dim = bbox_targets.size(-1)
      bbox_targets = bbox_targets.reshape(-1, target_dim)
      bbox_weights = bbox_weights.reshape(-1, target_dim)
      bbox_pred = bbox_pred.permute(0, 2, 3, 1).reshape(
          -1, self.bbox_coder.encode_size
      )
      if self.reg_decoded_bbox:
          # When the regression loss (e.g. `IouLoss`, `GIouLoss`)
          # is applied directly on the decoded bounding boxes, it
          # decodes the already encoded coordinates to absolute format.
          anchors = anchors.reshape(-1, anchors.size(-1))
          bbox_pred = self.bbox_coder.decode(anchors, bbox_pred)
          bbox_pred = get_box_tensor(bbox_pred)
      loss_bbox = self.loss_bbox(
          bbox_pred, bbox_targets, bbox_weights, avg_factor=avg_factor
      )
      return loss_cls, loss_bbox
  
imports: import warnings
  from typing import List, Optional, Tuple, Union
  import torch
  import torch.nn
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures.bbox import BaseBoxes, cat_boxes, get_box_tensor
  from mmdet.utils import ConfigType, InstanceList, OptConfigType, OptInstanceList, OptMultiConfig
  from ..task_modules.prior_generators import AnchorGenerator, anchor_inside_flags
  from ..task_modules.samplers import PseudoSampler
  from ..utils import images_to_levels, multi_apply, unmap
  from .base_dense_head import BaseDenseHead
label_desc: Use image node_id 10 for calling the AnchorHead obj's underlying member method code with example usage: obj.loss_by_feat_single(cls_score, bbox_pred, anchors, labels, label_weights, bbox_targets, bbox_weights, avg_factor) and returns: loss_cls,  loss_bbox