class_docstr: TOODHead used in `TOOD: Task-aligned One-stage Object Detection.
  
  <https://arxiv.org/abs/2108.07755>`_.
  
  TOOD uses Task-aligned head (T-head) and is optimized by Task Alignment
  Learning (TAL).
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (int): Number of channels in the input feature map.
      num_dcn (int): Number of deformable convolution in the head.
          Defaults to 0.
      anchor_type (str): If set to ``anchor_free``, the head will use centers
          to regress bboxes. If set to ``anchor_based``, the head will
          regress bboxes based on anchors. Defaults to ``anchor_free``.
      initial_loss_cls (:obj:`ConfigDict` or dict): Config of initial loss.
  
  Example:
      >>> self = TOODHead(11, 7)
      >>> feats = [torch.rand(1, 7, s, s) for s in [4, 8, 16, 32, 64]]
      >>> cls_score, bbox_pred = self.forward(feats)
      >>> assert len(cls_score) == len(self.scales)
class_docstr_tok: ["TOODHead","used","in","`","TOOD",":","Task-aligned","One-stage","Object","Detection",".","<","https",":","\/\/arxiv.org\/abs\/2108.07755",">","`","_",".","TOOD","uses","Task-aligned","head","(","T-head",")","and","is","optimized","by","Task","Alignment","Learning","(","TAL",")",".","Args",":","num_classes","(","int",")",":","Number","of","categories","excluding","the","background","category",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","num_dcn","(","int",")",":","Number","of","deformable","convolution","in","the","head",".","Defaults","to","0.","anchor_type","(","str",")",":","If","set","to","``","anchor_free","``",",","the","head","will","use","centers","to","regress","bboxes",".","If","set","to","``","anchor_based","``",",","the","head","will","regress","bboxes","based","on","anchors",".","Defaults","to","``","anchor_free","``",".","initial_loss_cls","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","initial","loss",".","Example",":",">",">",">","self","=","TOODHead","(","11",",","7",")",">",">",">","feats","=","[","torch.rand","(","1",",","7",",","s",",","s",")","for","s","in","[","4",",","8",",","16",",","32",",","64","]","]",">",">",">","cls_score",",","bbox_pred","=","self.forward","(","feats",")",">",">",">","assert","len","(","cls_score",")","==","len","(","self.scales",")"]
docstr: Compute regression, classification targets for anchors in a single
  image.
  
  Args:
      cls_scores (Tensor): Box scores for each image.
      bbox_preds (Tensor): Box energies / deltas for each image.
      flat_anchors (Tensor): Multi-level anchors of the image, which are
          concatenated into a single tensor of shape (num_anchors ,4)
      valid_flags (Tensor): Multi level valid flags of the image,
          which are concatenated into a single tensor of
              shape (num_anchors,).
      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.
  
  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).
          norm_alignment_metrics (Tensor): Normalized alignment metrics
              of all priors in the image with shape (N,).
docstr_tok: ["Compute","regression",",","classification","targets","for","anchors","in","a","single","image",".","Args",":","cls_scores","(","Tensor",")",":","Box","scores","for","each","image",".","bbox_preds","(","Tensor",")",":","Box","energies","\/","deltas","for","each","image",".","flat_anchors","(","Tensor",")",":","Multi-level","anchors","of","the","image",",","which","are","concatenated","into","a","single","tensor","of","shape","(","num_anchors",",4",")","valid_flags","(","Tensor",")",":","Multi","level","valid","flags","of","the","image",",","which","are","concatenated","into","a","single","tensor","of","shape","(","num_anchors",",",")",".","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",".","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",")",".","norm_alignment_metrics","(","Tensor",")",":","Normalized","alignment","metrics","of","all","priors","in","the","image","with","shape","(","N",",",")","."]
code: def _get_targets_single(
      self,
      cls_scores: Tensor,
      bbox_preds: Tensor,
      flat_anchors: Tensor,
      valid_flags: Tensor,
      gt_instances: InstanceData,
      img_meta: dict,
      gt_instances_ignore: Optional[InstanceData] = None,
      unmap_outputs: bool = True,
  ) -> tuple:
      """Compute regression, classification targets for anchors in a single
      image.
  
      Args:
          cls_scores (Tensor): Box scores for each image.
          bbox_preds (Tensor): Box energies / deltas for each image.
          flat_anchors (Tensor): Multi-level anchors of the image, which are
              concatenated into a single tensor of shape (num_anchors ,4)
          valid_flags (Tensor): Multi level valid flags of the image,
              which are concatenated into a single tensor of
                  shape (num_anchors,).
          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.
  
      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).
              norm_alignment_metrics (Tensor): Normalized alignment metrics
                  of all priors in the image with shape (N,).
      """
      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, :]
      pred_instances = InstanceData(
          priors=anchors,
          scores=cls_scores[inside_flags, :],
          bboxes=bbox_preds[inside_flags, :],
      )
      assign_result = self.alignment_assigner.assign(
          pred_instances,
          gt_instances,
          gt_instances_ignore,
          self.alpha,
          self.beta,
      )
      assign_ious = assign_result.max_overlaps
      assign_metrics = assign_result.assign_metrics
  
      sampling_result = self.sampler.sample(
          assign_result, pred_instances, gt_instances
      )
  
      num_valid_anchors = anchors.shape[0]
      bbox_targets = 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
      )
      norm_alignment_metrics = 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:
          # point-based
          pos_bbox_targets = sampling_result.pos_gt_bboxes
          bbox_targets[pos_inds, :] = pos_bbox_targets
  
          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
  
      class_assigned_gt_inds = torch.unique(
          sampling_result.pos_assigned_gt_inds
      )
      for gt_inds in class_assigned_gt_inds:
          gt_class_inds = pos_inds[
              sampling_result.pos_assigned_gt_inds == gt_inds
          ]
          pos_alignment_metrics = assign_metrics[gt_class_inds]
          pos_ious = assign_ious[gt_class_inds]
          pos_norm_alignment_metrics = (
              pos_alignment_metrics
              / (pos_alignment_metrics.max() + 10e-8)
              * pos_ious.max()
          )
          norm_alignment_metrics[
              gt_class_inds
          ] = pos_norm_alignment_metrics
  
      # 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
          )
          norm_alignment_metrics = unmap(
              norm_alignment_metrics, num_total_anchors, inside_flags
          )
      return (
          anchors,
          labels,
          label_weights,
          bbox_targets,
          norm_alignment_metrics,
      )
  
imports: from typing import List, Optional, Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import ConvModule, Scale
  from mmcv.ops import deform_conv2d
  from mmengine import MessageHub
  from mmengine.config import ConfigDict
  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 distance2bbox
  from mmdet.utils import ConfigType, InstanceList, OptConfigType, OptInstanceList, reduce_mean
  from ..task_modules.prior_generators import anchor_inside_flags
  from ..utils import filter_scores_and_topk, images_to_levels, multi_apply, sigmoid_geometric_mean, unmap
  from .atss_head import ATSSHead
label_desc: Use image node_id 11 for calling the TOODHead obj's underlying member method code with example usage: obj._get_targets_single(cls_scores, bbox_preds, flat_anchors, valid_flags, gt_instances, img_meta, gt_instances_ignore, unmap_outputs) and returns: anchors,  labels,  label_weights,  bbox_targets,  norm_alignment_metrics