class_docstr: Implementation of `SSD head <https://arxiv.org/abs/1512.02325>`_
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (Sequence[int]): Number of channels in the input feature
          map.
      stacked_convs (int): Number of conv layers in cls and reg tower.
          Defaults to 0.
      feat_channels (int): Number of hidden channels when stacked_convs
          > 0. Defaults to 256.
      use_depthwise (bool): Whether to use DepthwiseSeparableConv.
          Defaults to False.
      conv_cfg (:obj:`ConfigDict` or dict, Optional): Dictionary to construct
          and config conv layer. Defaults to None.
      norm_cfg (:obj:`ConfigDict` or dict, Optional): Dictionary to construct
          and config norm layer. Defaults to None.
      act_cfg (:obj:`ConfigDict` or dict, Optional): Dictionary to construct
          and config activation layer. Defaults to None.
      anchor_generator (:obj:`ConfigDict` or dict): Config dict for anchor
          generator.
      bbox_coder (:obj:`ConfigDict` or 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. Defaults to False. It should be `True` when
          using `IoULoss`, `GIoULoss`, or `DIoULoss` in the bbox head.
      train_cfg (:obj:`ConfigDict` or dict, Optional): Training config of
          anchor head.
      test_cfg (:obj:`ConfigDict` or dict, Optional): Testing config of
          anchor head.
      init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or             dict], Optional): Initialization config dict.
class_docstr_tok: ["Implementation","of","`","SSD","head","<","https",":","\/\/arxiv.org\/abs\/1512.02325",">","`","_","Args",":","num_classes","(","int",")",":","Number","of","categories","excluding","the","background","category",".","in_channels","(","Sequence","[","int","]",")",":","Number","of","channels","in","the","input","feature","map",".","stacked_convs","(","int",")",":","Number","of","conv","layers","in","cls","and","reg","tower",".","Defaults","to","0.","feat_channels","(","int",")",":","Number","of","hidden","channels","when","stacked_convs",">","0",".","Defaults","to","256.","use_depthwise","(","bool",")",":","Whether","to","use","DepthwiseSeparableConv",".","Defaults","to","False",".","conv_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","Optional",")",":","Dictionary","to","construct","and","config","conv","layer",".","Defaults","to","None",".","norm_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","Optional",")",":","Dictionary","to","construct","and","config","norm","layer",".","Defaults","to","None",".","act_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","Optional",")",":","Dictionary","to","construct","and","config","activation","layer",".","Defaults","to","None",".","anchor_generator","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","anchor","generator",".","bbox_coder","(",":","obj",":","`","ConfigDict","`","or","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",".","Defaults","to","False",".","It","should","be","`","True","`","when","using","`","IoULoss","`",",","`","GIoULoss","`",",","or","`","DIoULoss","`","in","the","bbox","head",".","train_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","Optional",")",":","Training","config","of","anchor","head",".","test_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","Optional",")",":","Testing","config","of","anchor","head",".","init_cfg","(",":","obj",":","`","ConfigDict","`","or","dict","or","list","[",":","obj",":","`","ConfigDict","`","or","dict","]",",","Optional",")",":","Initialization","config","dict","."]
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)
      batch_gt_instances (list[:obj:`InstanceData`]): Batch of
          gt_instance.  It usually includes ``bboxes`` and ``labels``
          attributes.
      batch_img_metas (list[dict]): Meta information of each image, e.g.,
          image size, scaling factor, etc.
      batch_gt_instances_ignore (list[:obj:`InstanceData`], Optional):
          Batch of gt_instances_ignore. It includes ``bboxes`` attribute
          data that is ignored during training and testing.
          Defaults to None.
  
  Returns:
      dict[str, list[Tensor]]: A dictionary of loss components. the dict
      has components below:
  
      - loss_cls (list[Tensor]): A list containing each feature map             classification loss.
      - loss_bbox (list[Tensor]): A list containing each feature map             regression loss.
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",")","batch_gt_instances","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","Batch","of","gt_instance",".","It","usually","includes","``","bboxes","``","and","``","labels","``","attributes",".","batch_img_metas","(","list","[","dict","]",")",":","Meta","information","of","each","image",",","e.g.",",","image","size",",","scaling","factor",",","etc",".","batch_gt_instances_ignore","(","list","[",":","obj",":","`","InstanceData","`","]",",","Optional",")",":","Batch","of","gt_instances_ignore",".","It","includes","``","bboxes","``","attribute","data","that","is","ignored","during","training","and","testing",".","Defaults","to","None",".","Returns",":","dict","[","str",",","list","[","Tensor","]","]",":","A","dictionary","of","loss","components",".","the","dict","has","components","below",":","-","loss_cls","(","list","[","Tensor","]",")",":","A","list","containing","each","feature","map","classification","loss",".","-","loss_bbox","(","list","[","Tensor","]",")",":","A","list","containing","each","feature","map","regression","loss","."]
code: def loss_by_feat(
      self,
      cls_scores: List[Tensor],
      bbox_preds: List[Tensor],
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      batch_gt_instances_ignore: OptInstanceList = None,
  ) -> Dict[str, List[Tensor]]:
      """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)
              batch_gt_instances (list[:obj:`InstanceData`]): Batch of
                  gt_instance.  It usually includes ``bboxes`` and ``labels``
                  attributes.
              batch_img_metas (list[dict]): Meta information of each image, e.g.,
                  image size, scaling factor, etc.
              batch_gt_instances_ignore (list[:obj:`InstanceData`], Optional):
                  Batch of gt_instances_ignore. It includes ``bboxes`` attribute
                  data that is ignored during training and testing.
                  Defaults to None.
  
          Returns:
              dict[str, list[Tensor]]: A dictionary of loss components. the dict
              has components below:
  
              - loss_cls (list[Tensor]): A list containing each feature map \
              classification loss.
              - loss_bbox (list[Tensor]): A list containing each feature map \
              regression loss.
          """
      featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
      assert len(featmap_sizes) == self.prior_generator.num_levels
  
      device = cls_scores[0].device
  
      anchor_list, valid_flag_list = self.get_anchors(
          featmap_sizes, batch_img_metas, device=device
      )
      cls_reg_targets = self.get_targets(
          anchor_list,
          valid_flag_list,
          batch_gt_instances,
          batch_img_metas,
          batch_gt_instances_ignore=batch_gt_instances_ignore,
          unmap_outputs=True,
      )
      (
          labels_list,
          label_weights_list,
          bbox_targets_list,
          bbox_weights_list,
          avg_factor,
      ) = cls_reg_targets
  
      num_images = len(batch_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]))
  
      losses_cls, losses_bbox = multi_apply(
          self.loss_by_feat_single,
          all_cls_scores,
          all_bbox_preds,
          all_anchors,
          all_labels,
          all_label_weights,
          all_bbox_targets,
          all_bbox_weights,
          avg_factor=avg_factor,
      )
      return dict(loss_cls=losses_cls, loss_bbox=losses_bbox)
  
file_path: mmdetection/mmdet/models/dense_heads/ssd_head.py
imports: from typing import Dict, List, Optional, Sequence, Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import ConvModule, DepthwiseSeparableConvModule
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.utils import ConfigType, InstanceList, MultiConfig, OptInstanceList
  from ..losses import smooth_l1_loss
  from ..task_modules.samplers import PseudoSampler
  from ..utils import multi_apply
  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_by_feat(cls_scores, bbox_preds, batch_gt_instances, batch_img_metas, batch_gt_instances_ignore) and returns: dict