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 predict(
      self, batch_inputs, batch_data_samples, rescale: bool = True
  ):
      text_prompts = []
      enhanced_text_prompts = []
      tokens_positives = []
      for data_samples in batch_data_samples:
          text_prompts.append(data_samples.text)
          if "caption_prompt" in data_samples:
              enhanced_text_prompts.append(data_samples.caption_prompt)
          else:
              enhanced_text_prompts.append(None)
          tokens_positives.append(
              data_samples.get("tokens_positive", None)
          )
  
      if "custom_entities" in batch_data_samples[0]:
          # Assuming that the `custom_entities` flag
          # inside a batch is always the same. For single image inference
          custom_entities = batch_data_samples[0].custom_entities
      else:
          custom_entities = False
      if len(text_prompts) == 1:
          # All the text prompts are the same,
          # so there is no need to calculate them multiple times.
          _positive_maps_and_prompts = [
              self.get_tokens_positive_and_prompts(
                  text_prompts[0],
                  custom_entities,
                  enhanced_text_prompts[0],
                  tokens_positives[0],
              )
          ] * len(batch_inputs)
      else:
          _positive_maps_and_prompts = [
              self.get_tokens_positive_and_prompts(
                  text_prompt,
                  custom_entities,
                  enhanced_text_prompt,
                  tokens_positive,
              )
              for text_prompt, enhanced_text_prompt, tokens_positive in zip(
                  text_prompts, enhanced_text_prompts, tokens_positives
              )
          ]
      token_positive_maps, text_prompts, _, entities = zip(
          *_positive_maps_and_prompts
      )
  
      # image feature extraction
      visual_feats = self.extract_feat(batch_inputs)
  
      if isinstance(text_prompts[0], list):
          # chunked text prompts, only bs=1 is supported
          assert len(batch_inputs) == 1
          count = 0
          results_list = []
  
          entities = [[item for lst in entities[0] for item in lst]]
  
          for b in range(len(text_prompts[0])):
              text_prompts_once = [text_prompts[0][b]]
              token_positive_maps_once = token_positive_maps[0][b]
              text_dict = self.language_model(text_prompts_once)
              # text feature map layer
              if self.text_feat_map is not None:
                  text_dict["embedded"] = self.text_feat_map(
                      text_dict["embedded"]
                  )
  
              batch_data_samples[
                  0
              ].token_positive_map = token_positive_maps_once
  
              head_inputs_dict = self.forward_transformer(
                  copy.deepcopy(visual_feats),
                  text_dict,
                  batch_data_samples,
              )
              pred_instances = self.bbox_head.predict(
                  **head_inputs_dict,
                  rescale=rescale,
                  batch_data_samples=batch_data_samples,
              )[0]
  
              if len(pred_instances) > 0:
                  pred_instances.labels += count
              count += len(token_positive_maps_once)
              results_list.append(pred_instances)
          results_list = [results_list[0].cat(results_list)]
          is_rec_tasks = [False] * len(results_list)
      else:
          # extract text feats
          text_dict = self.language_model(list(text_prompts))
          # text feature map layer
          if self.text_feat_map is not None:
              text_dict["embedded"] = self.text_feat_map(
                  text_dict["embedded"]
              )
  
          is_rec_tasks = []
          for i, data_samples in enumerate(batch_data_samples):
              if token_positive_maps[i] is not None:
                  is_rec_tasks.append(False)
              else:
                  is_rec_tasks.append(True)
              data_samples.token_positive_map = token_positive_maps[i]
  
          head_inputs_dict = self.forward_transformer(
              visual_feats, text_dict, batch_data_samples
          )
          results_list = self.bbox_head.predict(
              **head_inputs_dict,
              rescale=rescale,
              batch_data_samples=batch_data_samples,
          )
  
      for data_sample, pred_instances, entity, is_rec_task in zip(
          batch_data_samples, results_list, entities, is_rec_tasks
      ):
          if len(pred_instances) > 0:
              label_names = []
              for labels in pred_instances.labels:
                  if is_rec_task:
                      label_names.append(entity)
                      continue
                  if labels >= len(entity):
                      warnings.warn(
                          "The unexpected output indicates an issue with "
                          "named entity recognition. You can try "
                          "setting custom_entities=True and running "
                          "again to see if it helps."
                      )
                      label_names.append("unobject")
                  else:
                      label_names.append(entity[labels])
              # for visualization
              pred_instances.label_names = label_names
          data_sample.pred_instances = pred_instances
      return batch_data_samples
  
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 14 for calling the GroundingDINO obj's underlying member method code with example usage: obj.predict(batch_inputs, batch_data_samples, rescale) and returns: batch_data_samples