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 losses of the head.
  
  Args:
      cls_scores (list[Tensor]): Box scores for each scale level
          Has shape (N, num_anchors * num_classes, H, W)
      bbox_preds (list[Tensor]): Box energies / deltas for each scale
          level with shape (N, num_anchors * 4, H, W)
      gt_bboxes (list[Tensor]): each item are the truth boxes for each
          image in [tl_x, tl_y, br_x, br_y] format.
      gt_labels (list[Tensor]): class indices corresponding to each box
      img_metas (list[dict]): Meta information of each image, e.g.,
          image size, scaling factor, etc.
      gt_bboxes_ignore (None | list[Tensor]): specify which bounding
          boxes can be ignored when computing the loss.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Compute","losses","of","the","head",".","Args",":","cls_scores","(","list","[","Tensor","]",")",":","Box","scores","for","each","scale","level","Has","shape","(","N",",","num_anchors","*","num_classes",",","H",",","W",")","bbox_preds","(","list","[","Tensor","]",")",":","Box","energies","\/","deltas","for","each","scale","level","with","shape","(","N",",","num_anchors","*","4",",","H",",","W",")","gt_bboxes","(","list","[","Tensor","]",")",":","each","item","are","the","truth","boxes","for","each","image","in","[","tl_x",",","tl_y",",","br_x",",","br_y","]","format",".","gt_labels","(","list","[","Tensor","]",")",":","class","indices","corresponding","to","each","box","img_metas","(","list","[","dict","]",")",":","Meta","information","of","each","image",",","e.g.",",","image","size",",","scaling","factor",",","etc",".","gt_bboxes_ignore","(","None","|","list","[","Tensor","]",")",":","specify","which","bounding","boxes","can","be","ignored","when","computing","the","loss",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss(
      self,
      cls_scores,
      bbox_preds,
      gt_bboxes,
      gt_labels,
      img_metas,
      gt_bboxes_ignore=None,
  ):
      """Compute losses of the head.
  
      Args:
          cls_scores (list[Tensor]): Box scores for each scale level
              Has shape (N, num_anchors * num_classes, H, W)
          bbox_preds (list[Tensor]): Box energies / deltas for each scale
              level with shape (N, num_anchors * 4, H, W)
          gt_bboxes (list[Tensor]): each item are the truth boxes for each
              image in [tl_x, tl_y, br_x, br_y] format.
          gt_labels (list[Tensor]): class indices corresponding to each box
          img_metas (list[dict]): Meta information of each image, e.g.,
              image size, scaling factor, etc.
          gt_bboxes_ignore (None | list[Tensor]): specify which bounding
              boxes can be ignored when computing the loss.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
      featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
      assert len(featmap_sizes) == self.anchor_generator.num_levels
  
      device = cls_scores[0].device
  
      anchor_list, valid_flag_list = self.get_anchors(
          featmap_sizes, img_metas, device=device
      )
      cls_reg_targets = self.get_targets(
          anchor_list,
          valid_flag_list,
          gt_bboxes,
          img_metas,
          gt_bboxes_ignore_list=gt_bboxes_ignore,
          gt_labels_list=gt_labels,
          label_channels=1,
          unmap_outputs=False,
      )
      if cls_reg_targets is None:
          return None
      (
          labels_list,
          label_weights_list,
          bbox_targets_list,
          bbox_weights_list,
          num_total_pos,
          num_total_neg,
      ) = cls_reg_targets
  
      num_images = len(img_metas)
      all_cls_scores = torch.cat(
          [
              s.permute(0, 2, 3, 1).reshape(
                  num_images, -1, self.cls_out_channels
              )
              for s in cls_scores
          ],
          1,
      )
      all_labels = torch.cat(labels_list, -1).view(num_images, -1)
      all_label_weights = torch.cat(label_weights_list, -1).view(
          num_images, -1
      )
      all_bbox_preds = torch.cat(
          [
              b.permute(0, 2, 3, 1).reshape(num_images, -1, 4)
              for b in bbox_preds
          ],
          -2,
      )
      all_bbox_targets = torch.cat(bbox_targets_list, -2).view(
          num_images, -1, 4
      )
      all_bbox_weights = torch.cat(bbox_weights_list, -2).view(
          num_images, -1, 4
      )
  
      # concat all level anchors to a single tensor
      all_anchors = []
      for i in range(num_images):
          all_anchors.append(torch.cat(anchor_list[i]))
  
      # check NaN and Inf
      assert (
          torch.isfinite(all_cls_scores).all().item()
      ), "classification scores become infinite or NaN!"
      assert (
          torch.isfinite(all_bbox_preds).all().item()
      ), "bbox predications become infinite or NaN!"
  
      losses_cls, losses_bbox = multi_apply(
          self.loss_single,
          all_cls_scores,
          all_bbox_preds,
          all_anchors,
          all_labels,
          all_label_weights,
          all_bbox_targets,
          all_bbox_weights,
          num_total_samples=num_total_pos,
      )
      return dict(loss_cls=losses_cls, loss_bbox=losses_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 5 for calling the SSDHead obj's underlying member method code with example usage: obj.loss(cls_scores, bbox_preds, gt_bboxes, gt_labels, img_metas, gt_bboxes_ignore) and returns: dict, None