class_docstr: Head of DDQDETR: Dense Distinct Query for
      End-to-End Object Detection.
  
  Code is modified from the `official github repo
      <https://github.com/jshilong/DDQ>`_.
  
  More details can be found in the `paper
      <https://arxiv.org/abs/2303.12776>`_ .
  
  Args:
      aux_num_pos (int): Number of positive targets assigned to a
          perdicted object. Defaults to 4.
docstr: Calculate the loss of distinct queries, that is, excluding denoising
  and dense queries. Only select the distinct queries in decoder for
  loss.
  
  Args:
      all_layers_cls_scores (Tensor): Classification scores of all
          decoder layers, has shape (num_decoder_layers, bs,
          num_queries, cls_out_channels).
      all_layers_bbox_preds (Tensor): Bbox coordinates of all decoder
          layers. It has shape (num_decoder_layers, bs,
          num_queries, 4) with the last dimension arranged as
          (cx, cy, w, h).
      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","of","distinct","queries",",","that","is",",","excluding","denoising","and","dense","queries",".","Only","select","the","distinct","queries","in","decoder","for","loss",".","Args",":","all_layers_cls_scores","(","Tensor",")",":","Classification","scores","of","all","decoder","layers",",","has","shape","(","num_decoder_layers",",","bs",",","num_queries",",","cls_out_channels",")",".","all_layers_bbox_preds","(","Tensor",")",":","Bbox","coordinates","of","all","decoder","layers",".","It","has","shape","(","num_decoder_layers",",","bs",",","num_queries",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")",".","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_for_distinct_queries(
      self,
      all_layers_cls_scores: Tensor,
      all_layers_bbox_preds: Tensor,
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      batch_gt_instances_ignore: OptInstanceList = None,
  ) -> Dict[str, Tensor]:
      """Calculate the loss of distinct queries, that is, excluding denoising
      and dense queries. Only select the distinct queries in decoder for
      loss.
  
      Args:
          all_layers_cls_scores (Tensor): Classification scores of all
              decoder layers, has shape (num_decoder_layers, bs,
              num_queries, cls_out_channels).
          all_layers_bbox_preds (Tensor): Bbox coordinates of all decoder
              layers. It has shape (num_decoder_layers, bs,
              num_queries, 4) with the last dimension arranged as
              (cx, cy, w, h).
          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.
      """
      assert batch_gt_instances_ignore is None, (
          f"{self.__class__.__name__} only supports "
          "for batch_gt_instances_ignore setting to None."
      )
  
      losses_cls, losses_bbox, losses_iou = multi_apply(
          self._loss_for_distinct_queries_single,
          all_layers_cls_scores,
          all_layers_bbox_preds,
          [i for i in range(len(all_layers_bbox_preds))],
          batch_gt_instances=batch_gt_instances,
          batch_img_metas=batch_img_metas,
      )
  
      loss_dict = dict()
      # loss from the last decoder layer
      loss_dict["loss_cls"] = losses_cls[-1]
      loss_dict["loss_bbox"] = losses_bbox[-1]
      loss_dict["loss_iou"] = losses_iou[-1]
      # loss from other decoder layers
      num_dec_layer = 0
      for loss_cls_i, loss_bbox_i, loss_iou_i in zip(
          losses_cls[:-1], losses_bbox[:-1], losses_iou[:-1]
      ):
          loss_dict[f"d{num_dec_layer}.loss_cls"] = loss_cls_i
          loss_dict[f"d{num_dec_layer}.loss_bbox"] = loss_bbox_i
          loss_dict[f"d{num_dec_layer}.loss_iou"] = loss_iou_i
          num_dec_layer += 1
      return loss_dict
  
imports: import copy
  from typing import Dict, List, Tuple
  import torch
  from mmengine.model import bias_init_with_prob, constant_init
  from torch import Tensor, nn
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox_cxcywh_to_xyxy
  from mmdet.utils import InstanceList, OptInstanceList, reduce_mean
  from ..layers import inverse_sigmoid
  from ..losses import DDQAuxLoss
  from ..utils import multi_apply
  from .dino_head import DINOHead
label_desc: Use image node_id 7 for calling the DDQDETRHead obj's underlying member method code with example usage: obj.loss_for_distinct_queries(all_layers_cls_scores, all_layers_bbox_preds, batch_gt_instances, batch_img_metas, batch_gt_instances_ignore) and returns: loss_dict