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 regression, classification targets for anchors in a single
  image.
  
  Args:
      flat_anchors (Tensor): Multi-level anchors of the image,
          which are concatenated into a single tensor of shape
          (num_base_priors, 4).
      valid_flags (Tensor): Multi level valid flags of the image,
          which are concatenated into a single tensor of
          shape (num_base_priors,).
      cls_scores (Tensor): Classification scores for all scale
          levels of the image.
      bbox_preds (Tensor): Box energies / deltas for all scale
          levels of the image.
      num_level_anchors (List[int]): Number of anchors of each
          scale level.
      gt_instances (:obj:`InstanceData`): Ground truth of instance
          annotations. It usually includes ``bboxes`` and ``labels``
          attributes.
      img_meta (dict): Meta information for current image.
      gt_instances_ignore (:obj:`InstanceData`, optional): Instances
          to be ignored during training. It includes ``bboxes`` attribute
          data that is ignored during training and testing.
          Defaults to None.
      unmap_outputs (bool): Whether to map outputs back to the original
          set of anchors. Defaults to True.
      is_cls_assigner (bool): Classification or regression.
          Defaults to True.
  
  Returns:
      tuple: N is the number of total anchors in the image.
      - anchors (Tensor): all anchors in the image with shape (N, 4).
      - labels (Tensor): Labels of all anchors in the image with             shape (N, ).
      - label_weights (Tensor): Label weights of all anchor in the             image with shape (N, ).
      - bbox_targets (Tensor): BBox targets of all anchors in the             image with shape (N, 4).
      - bbox_weights (Tensor): BBox weights of all anchors in the             image with shape (N, 4)
      - pos_inds (Tensor): Indices of positive anchor with shape             (num_pos, ).
      - neg_inds (Tensor): Indices of negative anchor with shape             (num_neg, ).
      - sampling_result (:obj:`SamplingResult`): Sampling results.
docstr_tok: ["Compute","regression",",","classification","targets","for","anchors","in","a","single","image",".","Args",":","flat_anchors","(","Tensor",")",":","Multi-level","anchors","of","the","image",",","which","are","concatenated","into","a","single","tensor","of","shape","(","num_base_priors",",","4",")",".","valid_flags","(","Tensor",")",":","Multi","level","valid","flags","of","the","image",",","which","are","concatenated","into","a","single","tensor","of","shape","(","num_base_priors",",",")",".","cls_scores","(","Tensor",")",":","Classification","scores","for","all","scale","levels","of","the","image",".","bbox_preds","(","Tensor",")",":","Box","energies","\/","deltas","for","all","scale","levels","of","the","image",".","num_level_anchors","(","List","[","int","]",")",":","Number","of","anchors","of","each","scale","level",".","gt_instances","(",":","obj",":","`","InstanceData","`",")",":","Ground","truth","of","instance","annotations",".","It","usually","includes","``","bboxes","``","and","``","labels","``","attributes",".","img_meta","(","dict",")",":","Meta","information","for","current","image",".","gt_instances_ignore","(",":","obj",":","`","InstanceData","`",",","optional",")",":","Instances","to","be","ignored","during","training",".","It","includes","``","bboxes","``","attribute","data","that","is","ignored","during","training","and","testing",".","Defaults","to","None",".","unmap_outputs","(","bool",")",":","Whether","to","map","outputs","back","to","the","original","set","of","anchors",".","Defaults","to","True",".","is_cls_assigner","(","bool",")",":","Classification","or","regression",".","Defaults","to","True",".","Returns",":","tuple",":","N","is","the","number","of","total","anchors","in","the","image",".","-","anchors","(","Tensor",")",":","all","anchors","in","the","image","with","shape","(","N",",","4",")",".","-","labels","(","Tensor",")",":","Labels","of","all","anchors","in","the","image","with","shape","(","N",",",")",".","-","label_weights","(","Tensor",")",":","Label","weights","of","all","anchor","in","the","image","with","shape","(","N",",",")",".","-","bbox_targets","(","Tensor",")",":","BBox","targets","of","all","anchors","in","the","image","with","shape","(","N",",","4",")",".","-","bbox_weights","(","Tensor",")",":","BBox","weights","of","all","anchors","in","the","image","with","shape","(","N",",","4",")","-","pos_inds","(","Tensor",")",":","Indices","of","positive","anchor","with","shape","(","num_pos",",",")",".","-","neg_inds","(","Tensor",")",":","Indices","of","negative","anchor","with","shape","(","num_neg",",",")",".","-","sampling_result","(",":","obj",":","`","SamplingResult","`",")",":","Sampling","results","."]
code: def _get_targets_single(
      self,
      flat_anchors: Tensor,
      valid_flags: Tensor,
      cls_scores: Tensor,
      bbox_preds: Tensor,
      num_level_anchors: List[int],
      gt_instances: InstanceData,
      img_meta: dict,
      gt_instances_ignore: Optional[InstanceData] = None,
      unmap_outputs: bool = True,
      is_cls_assigner: bool = True,
  ) -> tuple:
      """Compute regression, classification targets for anchors in a single
          image.
  
          Args:
              flat_anchors (Tensor): Multi-level anchors of the image,
                  which are concatenated into a single tensor of shape
                  (num_base_priors, 4).
              valid_flags (Tensor): Multi level valid flags of the image,
                  which are concatenated into a single tensor of
                  shape (num_base_priors,).
              cls_scores (Tensor): Classification scores for all scale
                  levels of the image.
              bbox_preds (Tensor): Box energies / deltas for all scale
                  levels of the image.
              num_level_anchors (List[int]): Number of anchors of each
                  scale level.
              gt_instances (:obj:`InstanceData`): Ground truth of instance
                  annotations. It usually includes ``bboxes`` and ``labels``
                  attributes.
              img_meta (dict): Meta information for current image.
              gt_instances_ignore (:obj:`InstanceData`, optional): Instances
                  to be ignored during training. It includes ``bboxes`` attribute
                  data that is ignored during training and testing.
                  Defaults to None.
              unmap_outputs (bool): Whether to map outputs back to the original
                  set of anchors. Defaults to True.
              is_cls_assigner (bool): Classification or regression.
                  Defaults to True.
  
          Returns:
              tuple: N is the number of total anchors in the image.
              - anchors (Tensor): all anchors in the image with shape (N, 4).
              - labels (Tensor): Labels of all anchors in the image with \
              shape (N, ).
              - label_weights (Tensor): Label weights of all anchor in the \
              image with shape (N, ).
              - bbox_targets (Tensor): BBox targets of all anchors in the \
              image with shape (N, 4).
              - bbox_weights (Tensor): BBox weights of all anchors in the \
              image with shape (N, 4)
              - pos_inds (Tensor): Indices of positive anchor with shape \
              (num_pos, ).
              - neg_inds (Tensor): Indices of negative anchor with shape \
              (num_neg, ).
              - sampling_result (:obj:`SamplingResult`): Sampling results.
          """
      inside_flags = anchor_inside_flags(
          flat_anchors,
          valid_flags,
          img_meta["img_shape"][:2],
          self.train_cfg["allowed_border"],
      )
      if not inside_flags.any():
          raise ValueError(
              "There is no valid anchor inside the image boundary. Please "
              "check the image size and anchor sizes, or set "
              "``allowed_border`` to -1 to skip the condition."
          )
      # assign gt and sample anchors
      anchors = flat_anchors[inside_flags, :]
  
      num_level_anchors_inside = self.get_num_level_anchors_inside(
          num_level_anchors, inside_flags
      )
      bbox_preds_valid = bbox_preds[inside_flags, :]
      cls_scores_valid = cls_scores[inside_flags, :]
  
      assigner = (
          self.cls_assigner if is_cls_assigner else self.reg_assigner
      )
  
      # decode prediction out of assigner
      bbox_preds_valid = self.bbox_coder.decode(
          anchors, bbox_preds_valid
      )
      pred_instances = InstanceData(
          priors=anchors,
          bboxes=bbox_preds_valid,
          scores=cls_scores_valid,
      )
  
      assign_result = assigner.assign(
          pred_instances=pred_instances,
          num_level_priors=num_level_anchors_inside,
          gt_instances=gt_instances,
          gt_instances_ignore=gt_instances_ignore,
      )
      sampling_result = self.sampler.sample(
          assign_result=assign_result,
          pred_instances=pred_instances,
          gt_instances=gt_instances,
      )
  
      num_valid_anchors = anchors.shape[0]
      bbox_targets = torch.zeros_like(anchors)
      bbox_weights = torch.zeros_like(anchors)
      labels = anchors.new_full(
          (num_valid_anchors,), self.num_classes, dtype=torch.long
      )
      label_weights = anchors.new_zeros(
          num_valid_anchors, dtype=torch.float
      )
  
      pos_inds = sampling_result.pos_inds
      neg_inds = sampling_result.neg_inds
      if len(pos_inds) > 0:
          pos_bbox_targets = self.bbox_coder.encode(
              sampling_result.pos_bboxes, sampling_result.pos_gt_bboxes
          )
          bbox_targets[pos_inds, :] = pos_bbox_targets
          bbox_weights[pos_inds, :] = 1.0
  
          labels[pos_inds] = sampling_result.pos_gt_labels
          if self.train_cfg["pos_weight"] <= 0:
              label_weights[pos_inds] = 1.0
          else:
              label_weights[pos_inds] = self.train_cfg["pos_weight"]
      if len(neg_inds) > 0:
          label_weights[neg_inds] = 1.0
  
      # map up to original set of anchors
      if unmap_outputs:
          num_total_anchors = flat_anchors.size(0)
          anchors = unmap(anchors, num_total_anchors, inside_flags)
          labels = unmap(
              labels,
              num_total_anchors,
              inside_flags,
              fill=self.num_classes,
          )
          label_weights = unmap(
              label_weights, num_total_anchors, inside_flags
          )
          bbox_targets = unmap(
              bbox_targets, num_total_anchors, inside_flags
          )
          bbox_weights = unmap(
              bbox_weights, num_total_anchors, inside_flags
          )
  
      return (
          anchors,
          labels,
          label_weights,
          bbox_targets,
          bbox_weights,
          pos_inds,
          neg_inds,
          sampling_result,
      )
  
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 13 for calling the DDODHead obj's underlying member method code with example usage: obj._get_targets_single(flat_anchors, valid_flags, cls_scores, bbox_preds, num_level_anchors, gt_instances, img_meta, gt_instances_ignore, unmap_outputs, is_cls_assigner) and returns: anchors,  labels,  label_weights,  bbox_targets,  bbox_weights,  pos_inds,  neg_inds,  sampling_result