class_docstr: Head of the Grounding DINO: Marrying DINO with Grounded Pre-Training for
  Open-Set Object Detection.
  
  Args:
      contrastive_cfg (dict, optional): Contrastive config that contains
        keys like ``max_text_len``. Defaults to dict(max_text_len=256).
class_docstr_tok: ["Head","of","the","Grounding","DINO",":","Marrying","DINO","with","Grounded","Pre-Training","for","Open-Set","Object","Detection",".","Args",":","contrastive_cfg","(","dict",",","optional",")",":","Contrastive","config","that","contains","keys","like","``","max_text_len","``",".","Defaults","to","dict","(","max_text_len=256",")","."]
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` and `num_matching_queries` when
          `self.training` is `True`, else `num_matching_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). The `init_reference` has shape (bs,
          num_queries_total, 4) and each `inter_reference` has shape
          (bs, num_queries, 4) with the last dimension arranged as
          (cx, cy, w, h).
      memory_text (Tensor): Memory text. It has shape (bs, len_text,
          text_embed_dims).
      enc_outputs_class (Tensor): The score of each point on encode
          feature map, has shape (bs, num_feat_points, cls_out_channels).
      enc_outputs_coord (Tensor): The proposal generate from the
          encode feature map, has shape (bs, num_feat_points, 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.
  
  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","`","and","`","num_matching_queries","`","when","`","self.training","`","is","`","True","`",",","else","`","num_matching_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",")",".","The","`","init_reference","`","has","shape","(","bs",",","num_queries_total",",","4",")","and","each","`","inter_reference","`","has","shape","(","bs",",","num_queries",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")",".","memory_text","(","Tensor",")",":","Memory","text",".","It","has","shape","(","bs",",","len_text",",","text_embed_dims",")",".","enc_outputs_class","(","Tensor",")",":","The","score","of","each","point","on","encode","feature","map",",","has","shape","(","bs",",","num_feat_points",",","cls_out_channels",")",".","enc_outputs_coord","(","Tensor",")",":","The","proposal","generate","from","the","encode","feature","map",",","has","shape","(","bs",",","num_feat_points",",","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",".","Returns",":","dict",":","A","dictionary","of","loss","components","."]
code: def loss(
      self,
      hidden_states: Tensor,
      references: List[Tensor],
      memory_text: Tensor,
      text_token_mask: Tensor,
      enc_outputs_class: Tensor,
      enc_outputs_coord: Tensor,
      batch_data_samples: SampleList,
      dn_meta: Dict[str, int],
  ) -> 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` and `num_matching_queries` when
              `self.training` is `True`, else `num_matching_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). The `init_reference` has shape (bs,
              num_queries_total, 4) and each `inter_reference` has shape
              (bs, num_queries, 4) with the last dimension arranged as
              (cx, cy, w, h).
          memory_text (Tensor): Memory text. It has shape (bs, len_text,
              text_embed_dims).
          enc_outputs_class (Tensor): The score of each point on encode
              feature map, has shape (bs, num_feat_points, cls_out_channels).
          enc_outputs_coord (Tensor): The proposal generate from the
              encode feature map, has shape (bs, num_feat_points, 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.
  
      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, memory_text, text_token_mask
      )
      self.text_masks = text_token_mask
      loss_inputs = outs + (
          enc_outputs_class,
          enc_outputs_coord,
          batch_gt_instances,
          batch_img_metas,
          dn_meta,
      )
      losses = self.loss_by_feat(*loss_inputs)
      return losses
  
imports: import copy
  import math
  from typing import Dict, List, Optional, Tuple, Union
  import torch
  import torch.nn
  from mmcv.cnn import Linear
  from mmengine.model import constant_init
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.models.losses import QualityFocalLoss
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox_cxcywh_to_xyxy, bbox_xyxy_to_cxcywh
  from mmdet.utils import InstanceList, reduce_mean
  from ..layers import inverse_sigmoid
  from .atss_vlfusion_head import convert_grounding_to_cls_scores
  from .dino_head import DINOHead
label_desc: Use image node_id 9 for calling the GroundingDINOHead obj's underlying member method code with example usage: obj.loss(hidden_states, references, memory_text, text_token_mask, enc_outputs_class, enc_outputs_coord, batch_data_samples, dn_meta) and returns: losses