class_docstr: Detection Head of RTMDet.
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (int): Number of channels in the input feature map.
      with_objectness (bool): Whether to add an objectness branch.
          Defaults to True.
      act_cfg (:obj:`ConfigDict` or dict): Config dict for activation layer.
          Default: dict(type='ReLU')
class_docstr_tok: ["Detection","Head","of","RTMDet",".","Args",":","num_classes","(","int",")",":","Number","of","categories","excluding","the","background","category",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","with_objectness","(","bool",")",":","Whether","to","add","an","objectness","branch",".","Defaults","to","True",".","act_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","activation","layer",".","Default",":","dict","(","type='ReLU","'",")"]
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): Decoded bboxes for each scale
          level with shape (N, num_anchors * 4, H, W).
      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).
      assign_metrics (Tensor): Assign metrics with shape
          (N, num_total_anchors).
      stride (List[int]): Downsample stride of the feature map.
  
  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",")",":","Decoded","bboxes","for","each","scale","level","with","shape","(","N",",","num_anchors","*","4",",","H",",","W",")",".","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",")",".","assign_metrics","(","Tensor",")",":","Assign","metrics","with","shape","(","N",",","num_total_anchors",")",".","stride","(","List","[","int","]",")",":","Downsample","stride","of","the","feature","map",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss_by_feat_single(
      self,
      cls_score: Tensor,
      bbox_pred: Tensor,
      labels: Tensor,
      label_weights: Tensor,
      bbox_targets: Tensor,
      assign_metrics: Tensor,
      stride: List[int],
  ):
      """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): Decoded bboxes for each scale
              level with shape (N, num_anchors * 4, H, W).
          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).
          assign_metrics (Tensor): Assign metrics with shape
              (N, num_total_anchors).
          stride (List[int]): Downsample stride of the feature map.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
      assert (
          stride[0] == stride[1]
      ), "h stride is not equal to w stride!"
      cls_score = (
          cls_score.permute(0, 2, 3, 1)
          .reshape(-1, self.cls_out_channels)
          .contiguous()
      )
      bbox_pred = bbox_pred.reshape(-1, 4)
      bbox_targets = bbox_targets.reshape(-1, 4)
      labels = labels.reshape(-1)
      assign_metrics = assign_metrics.reshape(-1)
      label_weights = label_weights.reshape(-1)
      targets = (labels, assign_metrics)
  
      loss_cls = self.loss_cls(
          cls_score, targets, label_weights, avg_factor=1.0
      )
  
      # 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_decode_bbox_pred = pos_bbox_pred
          pos_decode_bbox_targets = pos_bbox_targets
  
          # regression loss
          pos_bbox_weight = assign_metrics[pos_inds]
  
          loss_bbox = self.loss_bbox(
              pos_decode_bbox_pred,
              pos_decode_bbox_targets,
              weight=pos_bbox_weight,
              avg_factor=1.0,
          )
      else:
          loss_bbox = bbox_pred.sum() * 0
          pos_bbox_weight = bbox_targets.new_tensor(0.0)
  
      return (
          loss_cls,
          loss_bbox,
          assign_metrics.sum(),
          pos_bbox_weight.sum(),
      )
  
imports: from typing import List, Optional, Tuple, Union
  import torch
  import torch.nn
  from mmcv.cnn import ConvModule, DepthwiseSeparableConvModule, Scale, is_norm
  from mmengine.model import bias_init_with_prob, constant_init, normal_init
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures.bbox import distance2bbox
  from mmdet.utils import ConfigType, InstanceList, OptInstanceList, reduce_mean
  from ..layers.transformer import inverse_sigmoid
  from ..task_modules import anchor_inside_flags
  from ..utils import images_to_levels, multi_apply, sigmoid_geometric_mean, unmap
  from .atss_head import ATSSHead
label_desc: Use image node_id 5 for calling the RTMDetHead obj's underlying member method code with example usage: obj.loss_by_feat_single(cls_score, bbox_pred, labels, label_weights, bbox_targets, assign_metrics, stride) and returns: loss_cls,  loss_bbox,  assign_metrics,  pos_bbox_weight