class_docstr: Implementation of `Grounding DINO: Marrying DINO with Grounded Pre-
  Training for Open-Set Object Detection.
  
  <https://arxiv.org/abs/2303.05499>`_
  
  Code is modified from the `official github repo
  <https://github.com/IDEA-Research/GroundingDINO>`_.
class_docstr_tok: ["Implementation","of","`","Grounding","DINO",":","Marrying","DINO","with","Grounded","Pre-","Training","for","Open-Set","Object","Detection",".","<","https",":","\/\/arxiv.org\/abs\/2303.05499",">","`","_","Code","is","modified","from","the","`","official","github","repo","<","https",":","\/\/github.com\/IDEA-Research\/GroundingDINO",">","`","_","."]
code: def pre_decoder(
      self,
      memory: Tensor,
      memory_mask: Tensor,
      spatial_shapes: Tensor,
      memory_text: Tensor,
      text_token_mask: Tensor,
      batch_data_samples: OptSampleList = None,
  ) -> Tuple[Dict]:
      bs, _, c = memory.shape
  
      (
          output_memory,
          output_proposals,
      ) = self.gen_encoder_output_proposals(
          memory, memory_mask, spatial_shapes
      )
  
      enc_outputs_class = self.bbox_head.cls_branches[
          self.decoder.num_layers
      ](output_memory, memory_text, text_token_mask)
      cls_out_features = self.bbox_head.cls_branches[
          self.decoder.num_layers
      ].max_text_len
      enc_outputs_coord_unact = (
          self.bbox_head.reg_branches[self.decoder.num_layers](
              output_memory
          )
          + output_proposals
      )
  
      # NOTE The DINO selects top-k proposals according to scores of
      # multi-class classification, while DeformDETR, where the input
      # is `enc_outputs_class[..., 0]` selects according to scores of
      # binary classification.
      topk_indices = torch.topk(
          enc_outputs_class.max(-1)[0], k=self.num_queries, dim=1
      )[1]
  
      topk_score = torch.gather(
          enc_outputs_class,
          1,
          topk_indices.unsqueeze(-1).repeat(1, 1, cls_out_features),
      )
      topk_coords_unact = torch.gather(
          enc_outputs_coord_unact,
          1,
          topk_indices.unsqueeze(-1).repeat(1, 1, 4),
      )
      topk_coords = topk_coords_unact.sigmoid()
      topk_coords_unact = topk_coords_unact.detach()
  
      query = self.query_embedding.weight[:, None, :]
      query = query.repeat(1, bs, 1).transpose(0, 1)
      if self.training:
          (
              dn_label_query,
              dn_bbox_query,
              dn_mask,
              dn_meta,
          ) = self.dn_query_generator(batch_data_samples)
          query = torch.cat([dn_label_query, query], dim=1)
          reference_points = torch.cat(
              [dn_bbox_query, topk_coords_unact], dim=1
          )
      else:
          reference_points = topk_coords_unact
          dn_mask, dn_meta = None, None
      reference_points = reference_points.sigmoid()
  
      decoder_inputs_dict = dict(
          query=query,
          memory=memory,
          reference_points=reference_points,
          dn_mask=dn_mask,
          memory_text=memory_text,
          text_attention_mask=~text_token_mask,
      )
      # NOTE DINO calculates encoder losses on scores and coordinates
      # of selected top-k encoder queries, while DeformDETR is of all
      # encoder queries.
      head_inputs_dict = (
          dict(
              enc_outputs_class=topk_score,
              enc_outputs_coord=topk_coords,
              dn_meta=dn_meta,
          )
          if self.training
          else dict()
      )
      # append text_feats to head_inputs_dict
      head_inputs_dict["memory_text"] = memory_text
      head_inputs_dict["text_token_mask"] = text_token_mask
      return decoder_inputs_dict, head_inputs_dict
  
imports: import copy
  import re
  import warnings
  from typing import Dict, Optional, Tuple, Union
  import torch
  import torch.nn
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures import OptSampleList, SampleList
  from mmdet.utils import ConfigType
  from ..layers import SinePositionalEncoding
  from ..layers.transformer.grounding_dino_layers import GroundingDinoTransformerDecoder, GroundingDinoTransformerEncoder
  from .dino import DINO
  from .glip import create_positive_map, create_positive_map_label_to_token, run_ner
label_desc: Use image node_id 12 for calling the GroundingDINO obj's underlying member method code with example usage: obj.pre_decoder(memory, memory_mask, spatial_shapes, memory_text, text_token_mask, batch_data_samples) and returns: decoder_inputs_dict,  head_inputs_dict