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: Compute reg loss of a single scale level based on the features
  extracted by the detection head.
  
  Args:
      anchors (Tensor): Box reference for each scale level with shape
          (N, num_total_anchors, 4).
      bbox_pred (Tensor): Box energies / deltas for each scale
          level with shape (N, num_base_priors * 4, H, W).
      iou_pred (Tensor): Iou for a single scale level, the
          channel number is (N, num_base_priors * 1, H, W).
      labels (Tensor): Labels of each anchors with shape
          (N, num_total_anchors).
      label_weights (Tensor): Label weights of each anchor with shape
          (N, num_total_anchors)
      bbox_targets (Tensor): BBox regression targets of each anchor with
          shape (N, num_total_anchors, 4).
      bbox_weights (Tensor): BBox weights of all anchors in the
          image with shape (N, 4)
      reweight_factor (List[float]): Reweight factor for cls and reg
          loss.
      avg_factor (float): 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 loss components.
docstr_tok: ["Compute","reg","loss","of","a","single","scale","level","based","on","the","features","extracted","by","the","detection","head",".","Args",":","anchors","(","Tensor",")",":","Box","reference","for","each","scale","level","with","shape","(","N",",","num_total_anchors",",","4",")",".","bbox_pred","(","Tensor",")",":","Box","energies","\/","deltas","for","each","scale","level","with","shape","(","N",",","num_base_priors","*","4",",","H",",","W",")",".","iou_pred","(","Tensor",")",":","Iou","for","a","single","scale","level",",","the","channel","number","is","(","N",",","num_base_priors","*","1",",","H",",","W",")",".","labels","(","Tensor",")",":","Labels","of","each","anchors","with","shape","(","N",",","num_total_anchors",")",".","label_weights","(","Tensor",")",":","Label","weights","of","each","anchor","with","shape","(","N",",","num_total_anchors",")","bbox_targets","(","Tensor",")",":","BBox","regression","targets","of","each","anchor","with","shape","(","N",",","num_total_anchors",",","4",")",".","bbox_weights","(","Tensor",")",":","BBox","weights","of","all","anchors","in","the","image","with","shape","(","N",",","4",")","reweight_factor","(","List","[","float","]",")",":","Reweight","factor","for","cls","and","reg","loss",".","avg_factor","(","float",")",":","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","loss","components","."]
code: def loss_reg_by_feat_single(
      self,
      anchors: Tensor,
      bbox_pred: Tensor,
      iou_pred: Tensor,
      labels,
      label_weights: Tensor,
      bbox_targets: Tensor,
      bbox_weights: Tensor,
      reweight_factor: List[float],
      avg_factor: float,
  ) -> Tuple[Tensor, Tensor]:
      """Compute reg loss of a single scale level based on the features
      extracted by the detection head.
  
      Args:
          anchors (Tensor): Box reference for each scale level with shape
              (N, num_total_anchors, 4).
          bbox_pred (Tensor): Box energies / deltas for each scale
              level with shape (N, num_base_priors * 4, H, W).
          iou_pred (Tensor): Iou for a single scale level, the
              channel number is (N, num_base_priors * 1, H, W).
          labels (Tensor): Labels of each anchors with shape
              (N, num_total_anchors).
          label_weights (Tensor): Label weights of each anchor with shape
              (N, num_total_anchors)
          bbox_targets (Tensor): BBox regression targets of each anchor with
              shape (N, num_total_anchors, 4).
          bbox_weights (Tensor): BBox weights of all anchors in the
              image with shape (N, 4)
          reweight_factor (List[float]): Reweight factor for cls and reg
              loss.
          avg_factor (float): 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 loss components.
      """
      anchors = anchors.reshape(-1, 4)
      bbox_pred = bbox_pred.permute(0, 2, 3, 1).reshape(-1, 4)
      iou_pred = iou_pred.permute(0, 2, 3, 1).reshape(
          -1,
      )
      bbox_targets = bbox_targets.reshape(-1, 4)
      bbox_weights = bbox_weights.reshape(-1, 4)
      labels = labels.reshape(-1)
      label_weights = label_weights.reshape(-1)
  
      iou_targets = label_weights.new_zeros(labels.shape)
      iou_weights = label_weights.new_zeros(labels.shape)
      iou_weights[
          (bbox_weights.sum(axis=1) > 0).nonzero(as_tuple=False)
      ] = 1.0
  
      # FG cat_id: [0, num_classes -1], BG cat_id: num_classes
      bg_class_ind = self.num_classes
      pos_inds = (
          ((labels >= 0) & (labels < bg_class_ind))
          .nonzero(as_tuple=False)
          .squeeze(1)
      )
  
      if len(pos_inds) > 0:
          pos_bbox_targets = bbox_targets[pos_inds]
          pos_bbox_pred = bbox_pred[pos_inds]
          pos_anchors = anchors[pos_inds]
  
          pos_decode_bbox_pred = self.bbox_coder.decode(
              pos_anchors, pos_bbox_pred
          )
          pos_decode_bbox_targets = self.bbox_coder.decode(
              pos_anchors, pos_bbox_targets
          )
  
          # regression loss
          loss_bbox = self.loss_bbox(
              pos_decode_bbox_pred,
              pos_decode_bbox_targets,
              avg_factor=avg_factor,
          )
  
          iou_targets[pos_inds] = bbox_overlaps(
              pos_decode_bbox_pred.detach(),
              pos_decode_bbox_targets,
              is_aligned=True,
          )
          loss_iou = self.loss_iou(
              iou_pred, iou_targets, iou_weights, avg_factor=avg_factor
          )
      else:
          loss_bbox = bbox_pred.sum() * 0
          loss_iou = iou_pred.sum() * 0
  
      return reweight_factor * loss_bbox, reweight_factor * loss_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 7 for calling the DDODHead obj's underlying member method code with example usage: obj.loss_reg_by_feat_single(anchors, bbox_pred, iou_pred, labels, label_weights, bbox_targets, bbox_weights, reweight_factor, avg_factor) and returns: unknown,  unknown