class_docstr: Detection Head of `DDOD <https://arxiv.org/abs/2107.02963>`_.
  
  DDOD head decomposes conjunctions lying in most current one-stage
  detectors via label assignment disentanglement, spatial feature
  disentanglement, and pyramid supervision disentanglement.
  
  Args:
      num_classes (int): Number of categories excluding the
          background category.
      in_channels (int): Number of channels in the input feature map.
      stacked_convs (int): The number of stacked Conv. Defaults to 4.
      conv_cfg (:obj:`ConfigDict` or dict, optional): Config dict for
          convolution layer. Defaults to None.
      use_dcn (bool): Use dcn, Same as ATSS when False. Defaults to True.
      norm_cfg (:obj:`ConfigDict` or dict): Normal config of ddod head.
          Defaults to dict(type='GN', num_groups=32, requires_grad=True).
      loss_iou (:obj:`ConfigDict` or dict): Config of IoU loss. Defaults to
          dict(type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0).
docstr: Calculate the loss based on the features extracted by the detection
  head.
  
  Args:
      cls_scores (list[Tensor]): Box scores for each scale level
          Has shape (N, num_base_priors * num_classes, H, W)
      bbox_preds (list[Tensor]): Box energies / deltas for each scale
          level with shape (N, num_base_priors * 4, H, W)
      iou_preds (list[Tensor]): Score factor for all scale level,
          each is a 4D-tensor, has shape (batch_size, 1, 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, Tensor]: A dictionary of loss components.
docstr_tok: ["Calculate","the","loss","based","on","the","features","extracted","by","the","detection","head",".","Args",":","cls_scores","(","list","[","Tensor","]",")",":","Box","scores","for","each","scale","level","Has","shape","(","N",",","num_base_priors","*","num_classes",",","H",",","W",")","bbox_preds","(","list","[","Tensor","]",")",":","Box","energies","\/","deltas","for","each","scale","level","with","shape","(","N",",","num_base_priors","*","4",",","H",",","W",")","iou_preds","(","list","[","Tensor","]",")",":","Score","factor","for","all","scale","level",",","each","is","a","4D-tensor",",","has","shape","(","batch_size",",","1",",","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",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss_by_feat(
      self,
      cls_scores: List[Tensor],
      bbox_preds: List[Tensor],
      iou_preds: List[Tensor],
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      batch_gt_instances_ignore: OptInstanceList = None,
  ) -> dict:
      """Calculate the loss based on the features extracted by the detection
      head.
  
      Args:
          cls_scores (list[Tensor]): Box scores for each scale level
              Has shape (N, num_base_priors * num_classes, H, W)
          bbox_preds (list[Tensor]): Box energies / deltas for each scale
              level with shape (N, num_base_priors * 4, H, W)
          iou_preds (list[Tensor]): Score factor for all scale level,
              each is a 4D-tensor, has shape (batch_size, 1, 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, Tensor]: A dictionary of loss components.
      """
      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
      )
  
      # calculate common vars for cls and reg assigners at once
      targets_com = self.process_predictions_and_anchors(
          anchor_list,
          valid_flag_list,
          cls_scores,
          bbox_preds,
          batch_img_metas,
          batch_gt_instances_ignore,
      )
      (
          anchor_list,
          valid_flag_list,
          num_level_anchors_list,
          cls_score_list,
          bbox_pred_list,
          batch_gt_instances_ignore,
      ) = targets_com
  
      # classification branch assigner
      cls_targets = self.get_cls_targets(
          anchor_list,
          valid_flag_list,
          num_level_anchors_list,
          cls_score_list,
          bbox_pred_list,
          batch_gt_instances,
          batch_img_metas,
          batch_gt_instances_ignore=batch_gt_instances_ignore,
      )
  
      (
          cls_anchor_list,
          labels_list,
          label_weights_list,
          bbox_targets_list,
          bbox_weights_list,
          avg_factor,
      ) = cls_targets
  
      avg_factor = reduce_mean(
          torch.tensor(avg_factor, dtype=torch.float, device=device)
      ).item()
      avg_factor = max(avg_factor, 1.0)
  
      reweight_factor_per_level = self.calc_reweight_factor(labels_list)
  
      (cls_losses_cls,) = multi_apply(
          self.loss_cls_by_feat_single,
          cls_scores,
          labels_list,
          label_weights_list,
          reweight_factor_per_level,
          avg_factor=avg_factor,
      )
  
      # regression branch assigner
      reg_targets = self.get_reg_targets(
          anchor_list,
          valid_flag_list,
          num_level_anchors_list,
          cls_score_list,
          bbox_pred_list,
          batch_gt_instances,
          batch_img_metas,
          batch_gt_instances_ignore=batch_gt_instances_ignore,
      )
  
      (
          reg_anchor_list,
          labels_list,
          label_weights_list,
          bbox_targets_list,
          bbox_weights_list,
          avg_factor,
      ) = reg_targets
  
      avg_factor = reduce_mean(
          torch.tensor(avg_factor, dtype=torch.float, device=device)
      ).item()
      avg_factor = max(avg_factor, 1.0)
  
      reweight_factor_per_level = self.calc_reweight_factor(labels_list)
  
      reg_losses_bbox, reg_losses_iou = multi_apply(
          self.loss_reg_by_feat_single,
          reg_anchor_list,
          bbox_preds,
          iou_preds,
          labels_list,
          label_weights_list,
          bbox_targets_list,
          bbox_weights_list,
          reweight_factor_per_level,
          avg_factor=avg_factor,
      )
  
      return dict(
          loss_cls=cls_losses_cls,
          loss_bbox=reg_losses_bbox,
          loss_iou=reg_losses_iou,
      )
  
imports: from typing import List, Optional, Sequence, Tuple
  import torch
  import torch.nn
  from mmcv.cnn import ConvModule, Scale
  from mmengine.model import bias_init_with_prob, normal_init
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures.bbox import bbox_overlaps
  from mmdet.utils import ConfigType, InstanceList, OptConfigType, OptInstanceList, reduce_mean
  from ..task_modules.prior_generators import anchor_inside_flags
  from ..utils import images_to_levels, multi_apply, unmap
  from .anchor_head import AnchorHead
label_desc: Use image node_id 9 for calling the DDODHead obj's underlying member method code with example usage: obj.loss_by_feat(cls_scores, bbox_preds, iou_preds, batch_gt_instances, batch_img_metas, batch_gt_instances_ignore) and returns: dict