class_docstr: Head of the DINO: DETR with Improved DeNoising Anchor Boxes
  for End-to-End Object Detection
  
  Code is modified from the `official github repo
  <https://github.com/IDEA-Research/DINO>`_.
  
  More details can be found in the `paper
  <https://arxiv.org/abs/2203.03605>`_ .
class_docstr_tok: ["Head","of","the","DINO",":","DETR","with","Improved","DeNoising","Anchor","Boxes","for","End-to-End","Object","Detection","Code","is","modified","from","the","`","official","github","repo","<","https",":","\/\/github.com\/IDEA-Research\/DINO",">","`","_",".","More","details","can","be","found","in","the","`","paper","<","https",":","\/\/arxiv.org\/abs\/2203.03605",">","`","_","."]
docstr: Get targets in denoising part for one image.
  
  Args:
      gt_instances (:obj:`InstanceData`): Ground truth of instance
          annotations. It should includes ``bboxes`` and ``labels``
          attributes.
      img_meta (dict): Meta information for one image.
      dn_meta (Dict[str, int]): The dictionary saves information about
        group collation, including 'num_denoising_queries' and
        'num_denoising_groups'. It will be used for split outputs of
        denoising and matching parts and loss calculation.
  
  Returns:
      tuple[Tensor]: a tuple containing the following for one image.
  
      - labels (Tensor): Labels of each image.
      - label_weights (Tensor]): Label weights of each image.
      - bbox_targets (Tensor): BBox targets of each image.
      - bbox_weights (Tensor): BBox weights of each image.
      - pos_inds (Tensor): Sampled positive indices for each image.
      - neg_inds (Tensor): Sampled negative indices for each image.
docstr_tok: ["Get","targets","in","denoising","part","for","one","image",".","Args",":","gt_instances","(",":","obj",":","`","InstanceData","`",")",":","Ground","truth","of","instance","annotations",".","It","should","includes","``","bboxes","``","and","``","labels","``","attributes",".","img_meta","(","dict",")",":","Meta","information","for","one","image",".","dn_meta","(","Dict","[","str",",","int","]",")",":","The","dictionary","saves","information","about","group","collation",",","including","'num_denoising_queries","'","and","'num_denoising_groups","'",".","It","will","be","used","for","split","outputs","of","denoising","and","matching","parts","and","loss","calculation",".","Returns",":","tuple","[","Tensor","]",":","a","tuple","containing","the","following","for","one","image",".","-","labels","(","Tensor",")",":","Labels","of","each","image",".","-","label_weights","(","Tensor","]",")",":","Label","weights","of","each","image",".","-","bbox_targets","(","Tensor",")",":","BBox","targets","of","each","image",".","-","bbox_weights","(","Tensor",")",":","BBox","weights","of","each","image",".","-","pos_inds","(","Tensor",")",":","Sampled","positive","indices","for","each","image",".","-","neg_inds","(","Tensor",")",":","Sampled","negative","indices","for","each","image","."]
code: def _get_dn_targets_single(
      self,
      gt_instances: InstanceData,
      img_meta: dict,
      dn_meta: Dict[str, int],
  ) -> tuple:
      """Get targets in denoising part for one image.
  
      Args:
          gt_instances (:obj:`InstanceData`): Ground truth of instance
              annotations. It should includes ``bboxes`` and ``labels``
              attributes.
          img_meta (dict): Meta information for one image.
          dn_meta (Dict[str, int]): The dictionary saves information about
            group collation, including 'num_denoising_queries' and
            'num_denoising_groups'. It will be used for split outputs of
            denoising and matching parts and loss calculation.
  
      Returns:
          tuple[Tensor]: a tuple containing the following for one image.
  
          - labels (Tensor): Labels of each image.
          - label_weights (Tensor]): Label weights of each image.
          - bbox_targets (Tensor): BBox targets of each image.
          - bbox_weights (Tensor): BBox weights of each image.
          - pos_inds (Tensor): Sampled positive indices for each image.
          - neg_inds (Tensor): Sampled negative indices for each image.
      """
      gt_bboxes = gt_instances.bboxes
      gt_labels = gt_instances.labels
      num_groups = dn_meta["num_denoising_groups"]
      num_denoising_queries = dn_meta["num_denoising_queries"]
      num_queries_each_group = int(num_denoising_queries / num_groups)
      device = gt_bboxes.device
  
      if len(gt_labels) > 0:
          t = torch.arange(
              len(gt_labels), dtype=torch.long, device=device
          )
          t = t.unsqueeze(0).repeat(num_groups, 1)
          pos_assigned_gt_inds = t.flatten()
          pos_inds = torch.arange(
              num_groups, dtype=torch.long, device=device
          )
          pos_inds = pos_inds.unsqueeze(1) * num_queries_each_group + t
          pos_inds = pos_inds.flatten()
      else:
          pos_inds = pos_assigned_gt_inds = gt_bboxes.new_tensor(
              [], dtype=torch.long
          )
  
      neg_inds = pos_inds + num_queries_each_group // 2
  
      # label targets
      labels = gt_bboxes.new_full(
          (num_denoising_queries,), self.num_classes, dtype=torch.long
      )
      labels[pos_inds] = gt_labels[pos_assigned_gt_inds]
      label_weights = gt_bboxes.new_ones(num_denoising_queries)
  
      # bbox targets
      bbox_targets = torch.zeros(
          num_denoising_queries, 4, device=device
      )
      bbox_weights = torch.zeros(
          num_denoising_queries, 4, device=device
      )
      bbox_weights[pos_inds] = 1.0
      img_h, img_w = img_meta["img_shape"]
  
      # DETR regress the relative position of boxes (cxcywh) in the image.
      # Thus the learning target should be normalized by the image size, also
      # the box format should be converted from defaultly x1y1x2y2 to cxcywh.
      factor = gt_bboxes.new_tensor(
          [img_w, img_h, img_w, img_h]
      ).unsqueeze(0)
      gt_bboxes_normalized = gt_bboxes / factor
      gt_bboxes_targets = bbox_xyxy_to_cxcywh(gt_bboxes_normalized)
      bbox_targets[pos_inds] = gt_bboxes_targets.repeat([num_groups, 1])
  
      return (
          labels,
          label_weights,
          bbox_targets,
          bbox_weights,
          pos_inds,
          neg_inds,
      )
  
imports: from typing import Dict, List, Tuple
  import torch
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox_cxcywh_to_xyxy, bbox_overlaps, bbox_xyxy_to_cxcywh
  from mmdet.utils import InstanceList, OptInstanceList, reduce_mean
  from ..losses import QualityFocalLoss
  from ..utils import multi_apply
  from .deformable_detr_head import DeformableDETRHead
label_desc: Use image node_id 6 for calling the DINOHead obj's underlying member method code with example usage: obj._get_dn_targets_single(gt_instances, img_meta, dn_meta) and returns: labels,  label_weights,  bbox_targets,  bbox_weights,  pos_inds,  neg_inds