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: Perform forward propagation and loss calculation of the detection
  head on the queries of the upstream network.
  
  Args:
      hidden_states (Tensor): Hidden states output from each decoder
          layer, has shape (num_decoder_layers, bs, num_queries_total,
          dim), where `num_queries_total` is the sum of
          `num_denoising_queries`, `num_queries` and `num_dense_queries`
          when `self.training` is `True`, else `num_queries`.
      references (list[Tensor]): List of the reference from the decoder.
          The first reference is the `init_reference` (initial) and the
          other num_decoder_layers(6) references are `inter_references`
          (intermediate). Each reference has shape (bs,
          num_queries_total, 4) with the last dimension arranged as
          (cx, cy, w, h).
      enc_outputs_class (Tensor): The top k classification score of
          each point on encoder feature map, has shape (bs, num_queries,
          cls_out_channels).
      enc_outputs_coord (Tensor): The proposal generated from points
          with top k score, has shape (bs, num_queries, 4) with the
          last dimension arranged as (cx, cy, w, h).
      batch_data_samples (list[:obj:`DetDataSample`]): The Data
          Samples. It usually includes information such as
          `gt_instance`, `gt_panoptic_seg` and `gt_sem_seg`.
      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.
      aux_enc_outputs_class (Tensor): The `dense_topk` classification
          score of each point on encoder feature map, has shape (bs,
          num_dense_queries, cls_out_channels).
          It is `None` when `self.training` is `False`.
      aux_enc_outputs_coord (Tensor): The proposal generated from points
          with `dense_topk` score, has shape (bs, num_dense_queries, 4)
          with the last dimension arranged as (cx, cy, w, h).
          It is `None` when `self.training` is `False`.
  
  Returns:
      dict: A dictionary of loss components.
docstr_tok: ["Perform","forward","propagation","and","loss","calculation","of","the","detection","head","on","the","queries","of","the","upstream","network",".","Args",":","hidden_states","(","Tensor",")",":","Hidden","states","output","from","each","decoder","layer",",","has","shape","(","num_decoder_layers",",","bs",",","num_queries_total",",","dim",")",",","where","`","num_queries_total","`","is","the","sum","of","`","num_denoising_queries","`",",","`","num_queries","`","and","`","num_dense_queries","`","when","`","self.training","`","is","`","True","`",",","else","`","num_queries","`",".","references","(","list","[","Tensor","]",")",":","List","of","the","reference","from","the","decoder",".","The","first","reference","is","the","`","init_reference","`","(","initial",")","and","the","other","num_decoder_layers","(","6",")","references","are","`","inter_references","`","(","intermediate",")",".","Each","reference","has","shape","(","bs",",","num_queries_total",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")",".","enc_outputs_class","(","Tensor",")",":","The","top","k","classification","score","of","each","point","on","encoder","feature","map",",","has","shape","(","bs",",","num_queries",",","cls_out_channels",")",".","enc_outputs_coord","(","Tensor",")",":","The","proposal","generated","from","points","with","top","k","score",",","has","shape","(","bs",",","num_queries",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")",".","batch_data_samples","(","list","[",":","obj",":","`","DetDataSample","`","]",")",":","The","Data","Samples",".","It","usually","includes","information","such","as","`","gt_instance","`",",","`","gt_panoptic_seg","`","and","`","gt_sem_seg","`",".","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",".","aux_enc_outputs_class","(","Tensor",")",":","The","`","dense_topk","`","classification","score","of","each","point","on","encoder","feature","map",",","has","shape","(","bs",",","num_dense_queries",",","cls_out_channels",")",".","It","is","`","None","`","when","`","self.training","`","is","`","False","`",".","aux_enc_outputs_coord","(","Tensor",")",":","The","proposal","generated","from","points","with","`","dense_topk","`","score",",","has","shape","(","bs",",","num_dense_queries",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")",".","It","is","`","None","`","when","`","self.training","`","is","`","False","`",".","Returns",":","dict",":","A","dictionary","of","loss","components","."]
code: def loss(
      self,
      hidden_states: Tensor,
      references: List[Tensor],
      enc_outputs_class: Tensor,
      enc_outputs_coord: Tensor,
      batch_data_samples: SampleList,
      dn_meta: Dict[str, int],
      aux_enc_outputs_class=None,
      aux_enc_outputs_coord=None,
  ) -> dict:
      """Perform forward propagation and loss calculation of the detection
      head on the queries of the upstream network.
  
      Args:
          hidden_states (Tensor): Hidden states output from each decoder
              layer, has shape (num_decoder_layers, bs, num_queries_total,
              dim), where `num_queries_total` is the sum of
              `num_denoising_queries`, `num_queries` and `num_dense_queries`
              when `self.training` is `True`, else `num_queries`.
          references (list[Tensor]): List of the reference from the decoder.
              The first reference is the `init_reference` (initial) and the
              other num_decoder_layers(6) references are `inter_references`
              (intermediate). Each reference has shape (bs,
              num_queries_total, 4) with the last dimension arranged as
              (cx, cy, w, h).
          enc_outputs_class (Tensor): The top k classification score of
              each point on encoder feature map, has shape (bs, num_queries,
              cls_out_channels).
          enc_outputs_coord (Tensor): The proposal generated from points
              with top k score, has shape (bs, num_queries, 4) with the
              last dimension arranged as (cx, cy, w, h).
          batch_data_samples (list[:obj:`DetDataSample`]): The Data
              Samples. It usually includes information such as
              `gt_instance`, `gt_panoptic_seg` and `gt_sem_seg`.
          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.
          aux_enc_outputs_class (Tensor): The `dense_topk` classification
              score of each point on encoder feature map, has shape (bs,
              num_dense_queries, cls_out_channels).
              It is `None` when `self.training` is `False`.
          aux_enc_outputs_coord (Tensor): The proposal generated from points
              with `dense_topk` score, has shape (bs, num_dense_queries, 4)
              with the last dimension arranged as (cx, cy, w, h).
              It is `None` when `self.training` is `False`.
  
      Returns:
          dict: A dictionary of loss components.
      """
      batch_gt_instances = []
      batch_img_metas = []
      for data_sample in batch_data_samples:
          batch_img_metas.append(data_sample.metainfo)
          batch_gt_instances.append(data_sample.gt_instances)
  
      outs = self(hidden_states, references)
      loss_inputs = outs + (
          enc_outputs_class,
          enc_outputs_coord,
          batch_gt_instances,
          batch_img_metas,
          dn_meta,
      )
      losses = self.loss_by_feat(*loss_inputs)
  
      aux_enc_outputs_coord = bbox_cxcywh_to_xyxy(aux_enc_outputs_coord)
      aux_enc_outputs_coord_list = []
      for img_id in range(len(aux_enc_outputs_coord)):
          det_bboxes = aux_enc_outputs_coord[img_id]
          img_shape = batch_img_metas[img_id]["img_shape"]
          det_bboxes[:, 0::2] = det_bboxes[:, 0::2] * img_shape[1]
          det_bboxes[:, 1::2] = det_bboxes[:, 1::2] * img_shape[0]
          aux_enc_outputs_coord_list.append(det_bboxes)
      aux_enc_outputs_coord = torch.stack(aux_enc_outputs_coord_list)
      aux_loss = self.aux_loss_for_dense.loss(
          aux_enc_outputs_class.sigmoid(),
          aux_enc_outputs_coord,
          [item.bboxes for item in batch_gt_instances],
          [item.labels for item in batch_gt_instances],
          batch_img_metas,
      )
      for k, v in aux_loss.items():
          losses[f"aux_enc_{k}"] = v
  
      return losses
  
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 5 for calling the DDQDETRHead obj's underlying member method code with example usage: obj.loss(hidden_states, references, enc_outputs_class, enc_outputs_coord, batch_data_samples, dn_meta, aux_enc_outputs_class, aux_enc_outputs_coord) and returns: losses