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: Forward function.
  
  Args:
      hidden_states (Tensor): Hidden states output from each decoder
          layer, has shape (num_decoder_layers, bs, num_queries, dim).
      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, 4) when `as_two_stage` of the detector is `True`,
          otherwise (bs, num_queries, 2). Each `inter_reference` has
          shape (bs, num_queries, 4) when `with_box_refine` of the
          detector is `True`, otherwise (bs, num_queries, 2). The
          coordinates are arranged as (cx, cy) when the last dimension is
          2, and (cx, cy, w, h) when it is 4.
      memory_text (Tensor): Memory text. It has shape (bs, len_text,
          text_embed_dims).
      text_token_mask (Tensor): Text token mask. It has shape (bs,
          len_text).
  
  Returns:
      tuple[Tensor]: results of head containing the following tensor.
  
      - all_layers_outputs_classes (Tensor): Outputs from the
        classification head, has shape (num_decoder_layers, bs,
        num_queries, cls_out_channels).
      - all_layers_outputs_coords (Tensor): Sigmoid outputs from the
        regression head with normalized coordinate format (cx, cy, w,
        h), has shape (num_decoder_layers, bs, num_queries, 4) with the
        last dimension arranged as (cx, cy, w, h).
docstr_tok: ["Forward","function",".","Args",":","hidden_states","(","Tensor",")",":","Hidden","states","output","from","each","decoder","layer",",","has","shape","(","num_decoder_layers",",","bs",",","num_queries",",","dim",")",".","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",",","4",")","when","`","as_two_stage","`","of","the","detector","is","`","True","`",",","otherwise","(","bs",",","num_queries",",","2",")",".","Each","`","inter_reference","`","has","shape","(","bs",",","num_queries",",","4",")","when","`","with_box_refine","`","of","the","detector","is","`","True","`",",","otherwise","(","bs",",","num_queries",",","2",")",".","The","coordinates","are","arranged","as","(","cx",",","cy",")","when","the","last","dimension","is","2",",","and","(","cx",",","cy",",","w",",","h",")","when","it","is","4.","memory_text","(","Tensor",")",":","Memory","text",".","It","has","shape","(","bs",",","len_text",",","text_embed_dims",")",".","text_token_mask","(","Tensor",")",":","Text","token","mask",".","It","has","shape","(","bs",",","len_text",")",".","Returns",":","tuple","[","Tensor","]",":","results","of","head","containing","the","following","tensor",".","-","all_layers_outputs_classes","(","Tensor",")",":","Outputs","from","the","classification","head",",","has","shape","(","num_decoder_layers",",","bs",",","num_queries",",","cls_out_channels",")",".","-","all_layers_outputs_coords","(","Tensor",")",":","Sigmoid","outputs","from","the","regression","head","with","normalized","coordinate","format","(","cx",",","cy",",","w",",","h",")",",","has","shape","(","num_decoder_layers",",","bs",",","num_queries",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")","."]
code: def forward(
      self,
      hidden_states: Tensor,
      references: List[Tensor],
      memory_text: Tensor,
      text_token_mask: Tensor,
  ) -> Tuple[Tensor]:
      """Forward function.
  
      Args:
          hidden_states (Tensor): Hidden states output from each decoder
              layer, has shape (num_decoder_layers, bs, num_queries, dim).
          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, 4) when `as_two_stage` of the detector is `True`,
              otherwise (bs, num_queries, 2). Each `inter_reference` has
              shape (bs, num_queries, 4) when `with_box_refine` of the
              detector is `True`, otherwise (bs, num_queries, 2). The
              coordinates are arranged as (cx, cy) when the last dimension is
              2, and (cx, cy, w, h) when it is 4.
          memory_text (Tensor): Memory text. It has shape (bs, len_text,
              text_embed_dims).
          text_token_mask (Tensor): Text token mask. It has shape (bs,
              len_text).
  
      Returns:
          tuple[Tensor]: results of head containing the following tensor.
  
          - all_layers_outputs_classes (Tensor): Outputs from the
            classification head, has shape (num_decoder_layers, bs,
            num_queries, cls_out_channels).
          - all_layers_outputs_coords (Tensor): Sigmoid outputs from the
            regression head with normalized coordinate format (cx, cy, w,
            h), has shape (num_decoder_layers, bs, num_queries, 4) with the
            last dimension arranged as (cx, cy, w, h).
      """
      all_layers_outputs_classes = []
      all_layers_outputs_coords = []
  
      for layer_id in range(hidden_states.shape[0]):
          reference = inverse_sigmoid(references[layer_id])
          # NOTE The last reference will not be used.
          hidden_state = hidden_states[layer_id]
          outputs_class = self.cls_branches[layer_id](
              hidden_state, memory_text, text_token_mask
          )
          tmp_reg_preds = self.reg_branches[layer_id](hidden_state)
          if reference.shape[-1] == 4:
              # When `layer` is 0 and `as_two_stage` of the detector
              # is `True`, or when `layer` is greater than 0 and
              # `with_box_refine` of the detector is `True`.
              tmp_reg_preds += reference
          else:
              # When `layer` is 0 and `as_two_stage` of the detector
              # is `False`, or when `layer` is greater than 0 and
              # `with_box_refine` of the detector is `False`.
              assert reference.shape[-1] == 2
              tmp_reg_preds[..., :2] += reference
          outputs_coord = tmp_reg_preds.sigmoid()
          all_layers_outputs_classes.append(outputs_class)
          all_layers_outputs_coords.append(outputs_coord)
  
      all_layers_outputs_classes = torch.stack(
          all_layers_outputs_classes
      )
      all_layers_outputs_coords = torch.stack(all_layers_outputs_coords)
  
      return all_layers_outputs_classes, all_layers_outputs_coords
  
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 5 for calling the GroundingDINOHead obj's underlying member method code with example usage: obj.forward(hidden_states, references, memory_text, text_token_mask) and returns: all_layers_outputs_classes,  all_layers_outputs_coords