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 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 with
          shape (num_total_anchors, 4).
      bbox_weights (Tensor): BBox regression loss weights of each anchor
          with shape (num_total_anchors, 4).
      avg_factor (int): Average factor that is used to average
          the loss. When using sampling method, avg_factor is usually
          the sum of positive and negative priors. When using
          `PseudoSampler`, `avg_factor` is usually equal to the number
          of positive priors.
  
  Returns:
      Tuple[Tensor, Tensor]: A tuple of cls loss and bbox loss of one
      feature map.
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","with","shape","(","num_total_anchors",",","4",")",".","bbox_weights","(","Tensor",")",":","BBox","regression","loss","weights","of","each","anchor","with","shape","(","num_total_anchors",",","4",")",".","avg_factor","(","int",")",":","Average","factor","that","is","used","to","average","the","loss",".","When","using","sampling","method",",","avg_factor","is","usually","the","sum","of","positive","and","negative","priors",".","When","using","`","PseudoSampler","`",",","`","avg_factor","`","is","usually","equal","to","the","number","of","positive","priors",".","Returns",":","Tuple","[","Tensor",",","Tensor","]",":","A","tuple","of","cls","loss","and","bbox","loss","of","one","feature","map","."]
code: def loss_by_feat_single(
      self,
      cls_score: Tensor,
      bbox_pred: Tensor,
      anchor: Tensor,
      labels: Tensor,
      label_weights: Tensor,
      bbox_targets: Tensor,
      bbox_weights: Tensor,
      avg_factor: int,
  ) -> Tuple[Tensor, Tensor]:
      """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 with
              shape (num_total_anchors, 4).
          bbox_weights (Tensor): BBox regression loss weights of each anchor
              with shape (num_total_anchors, 4).
          avg_factor (int): Average factor that is used to average
              the loss. When using sampling method, avg_factor is usually
              the sum of positive and negative priors. When using
              `PseudoSampler`, `avg_factor` is usually equal to the number
              of positive priors.
  
      Returns:
          Tuple[Tensor, Tensor]: A tuple of cls loss and bbox loss of one
          feature map.
      """
  
      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(as_tuple=False)
          .reshape(-1)
      )
      neg_inds = (
          (labels == self.num_classes).nonzero(as_tuple=False).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) / avg_factor
  
      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.
          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=avg_factor,
      )
      return loss_cls[None], loss_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 4 for calling the SSDHead obj's underlying member method code with example usage: obj.loss_by_feat_single(cls_score, bbox_pred, anchor, labels, label_weights, bbox_targets, bbox_weights, avg_factor) and returns: loss_cls,  loss_bbox