class_docstr: SSD head used in https://arxiv.org/abs/1512.02325.
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (int): Number of channels in the input feature map.
      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 on decoded bounding boxes. Default: False
      train_cfg (dict): Training config of anchor head.
      test_cfg (dict): Testing config of anchor head.
docstr: Compute loss of a single image.
  
  Args:
      cls_score (Tensor): Box scores for eachimage
          Has shape (num_total_anchors, num_classes).
      bbox_pred (Tensor): Box energies / deltas for each image
          level with shape (num_total_anchors, 4).
      anchors (Tensor): Box reference for each scale level with shape
          (num_total_anchors, 4).
      labels (Tensor): Labels of each anchors with shape
          (num_total_anchors,).
      label_weights (Tensor): Label weights of each anchor with shape
          (num_total_anchors,)
      bbox_targets (Tensor): BBox regression targets of each anchor wight
          shape (num_total_anchors, 4).
      bbox_weights (Tensor): BBox regression loss weights of each anchor
          with shape (num_total_anchors, 4).
      num_total_samples (int): If sampling, num total samples equal to
          the number of total anchors; Otherwise, it is the number of
          positive anchors.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Compute","loss","of","a","single","image",".","Args",":","cls_score","(","Tensor",")",":","Box","scores","for","eachimage","Has","shape","(","num_total_anchors",",","num_classes",")",".","bbox_pred","(","Tensor",")",":","Box","energies","\/","deltas","for","each","image","level","with","shape","(","num_total_anchors",",","4",")",".","anchors","(","Tensor",")",":","Box","reference","for","each","scale","level","with","shape","(","num_total_anchors",",","4",")",".","labels","(","Tensor",")",":","Labels","of","each","anchors","with","shape","(","num_total_anchors",",",")",".","label_weights","(","Tensor",")",":","Label","weights","of","each","anchor","with","shape","(","num_total_anchors",",",")","bbox_targets","(","Tensor",")",":","BBox","regression","targets","of","each","anchor","wight","shape","(","num_total_anchors",",","4",")",".","bbox_weights","(","Tensor",")",":","BBox","regression","loss","weights","of","each","anchor","with","shape","(","num_total_anchors",",","4",")",".","num_total_samples","(","int",")",":","If","sampling",",","num","total","samples","equal","to","the","number","of","total","anchors",";","Otherwise",",","it","is","the","number","of","positive","anchors",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss_single(
      self,
      cls_score,
      bbox_pred,
      anchor,
      labels,
      label_weights,
      bbox_targets,
      bbox_weights,
      num_total_samples,
  ):
      """Compute loss of a single image.
  
      Args:
          cls_score (Tensor): Box scores for eachimage
              Has shape (num_total_anchors, num_classes).
          bbox_pred (Tensor): Box energies / deltas for each image
              level with shape (num_total_anchors, 4).
          anchors (Tensor): Box reference for each scale level with shape
              (num_total_anchors, 4).
          labels (Tensor): Labels of each anchors with shape
              (num_total_anchors,).
          label_weights (Tensor): Label weights of each anchor with shape
              (num_total_anchors,)
          bbox_targets (Tensor): BBox regression targets of each anchor wight
              shape (num_total_anchors, 4).
          bbox_weights (Tensor): BBox regression loss weights of each anchor
              with shape (num_total_anchors, 4).
          num_total_samples (int): If sampling, num total samples equal to
              the number of total anchors; Otherwise, it is the number of
              positive anchors.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
  
      loss_cls_all = (
          F.cross_entropy(cls_score, labels, reduction="none")
          * label_weights
      )
      # FG cat_id: [0, num_classes -1], BG cat_id: num_classes
      pos_inds = (
          ((labels >= 0) & (labels < self.num_classes))
          .nonzero()
          .reshape(-1)
      )
      neg_inds = (labels == self.num_classes).nonzero().view(-1)
  
      num_pos_samples = pos_inds.size(0)
      num_neg_samples = self.train_cfg.neg_pos_ratio * num_pos_samples
      if num_neg_samples > neg_inds.size(0):
          num_neg_samples = neg_inds.size(0)
      topk_loss_cls_neg, _ = loss_cls_all[neg_inds].topk(
          num_neg_samples
      )
      loss_cls_pos = loss_cls_all[pos_inds].sum()
      loss_cls_neg = topk_loss_cls_neg.sum()
      loss_cls = (loss_cls_pos + loss_cls_neg) / num_total_samples
  
      if self.reg_decoded_bbox:
          bbox_pred = self.bbox_coder.decode(anchor, bbox_pred)
  
      loss_bbox = smooth_l1_loss(
          bbox_pred,
          bbox_targets,
          bbox_weights,
          beta=self.train_cfg.smoothl1_beta,
          avg_factor=num_total_samples,
      )
      return loss_cls[None], loss_bbox
  
file_path: insightface/detection/scrfd/mmdet/models/dense_heads/ssd_head.py
imports: import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import xavier_init
  from mmcv.runner import force_fp32
  from mmdet.core import build_anchor_generator, build_assigner, build_bbox_coder, build_sampler, multi_apply
  from ..builder import HEADS
  from ..losses import smooth_l1_loss
  from .anchor_head import AnchorHead
label_desc: Use image node_id 4 for calling the SSDHead obj's underlying member method code with example usage: obj.loss_single(cls_score, bbox_pred, anchor, labels, label_weights, bbox_targets, bbox_weights, num_total_samples) and returns: loss_cls,  loss_bbox