class_docstr: Implementation of `GLIP <https://arxiv.org/abs/2112.03857>`_
  Args:
      backbone (:obj:`ConfigDict` or dict): The backbone config.
      neck (:obj:`ConfigDict` or dict): The neck config.
      bbox_head (:obj:`ConfigDict` or dict): The bbox head config.
      language_model (:obj:`ConfigDict` or dict): The language model config.
      train_cfg (:obj:`ConfigDict` or dict, optional): The training config
          of GLIP. Defaults to None.
      test_cfg (:obj:`ConfigDict` or dict, optional): The testing config
          of GLIP. Defaults to None.
      data_preprocessor (:obj:`ConfigDict` or dict, optional): Config of
          :class:`DetDataPreprocessor` to process the input data.
          Defaults to None.
      init_cfg (:obj:`ConfigDict` or list[:obj:`ConfigDict`] or dict or
          list[dict], optional): Initialization config dict.
          Defaults to None.
class_docstr_tok: ["Implementation","of","`","GLIP","<","https",":","\/\/arxiv.org\/abs\/2112.03857",">","`","_","Args",":","backbone","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","The","backbone","config",".","neck","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","The","neck","config",".","bbox_head","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","The","bbox","head","config",".","language_model","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","The","language","model","config",".","train_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","The","training","config","of","GLIP",".","Defaults","to","None",".","test_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","The","testing","config","of","GLIP",".","Defaults","to","None",".","data_preprocessor","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Config","of",":","class",":","`","DetDataPreprocessor","`","to","process","the","input","data",".","Defaults","to","None",".","init_cfg","(",":","obj",":","`","ConfigDict","`","or","list","[",":","obj",":","`","ConfigDict","`","]","or","dict","or","list","[","dict","]",",","optional",")",":","Initialization","config","dict",".","Defaults","to","None","."]
docstr: Predict results from a batch of inputs and data samples with post-
  processing.
  
  Args:
      batch_inputs (Tensor): Inputs with shape (N, C, H, W).
      batch_data_samples (List[:obj:`DetDataSample`]): The Data
          Samples. It usually includes information such as
          `gt_instance`, `gt_panoptic_seg` and `gt_sem_seg`.
      rescale (bool): Whether to rescale the results.
          Defaults to True.
  
  Returns:
      list[:obj:`DetDataSample`]: Detection results of the
      input images. Each DetDataSample usually contain
      'pred_instances'. And the ``pred_instances`` usually
      contains following keys.
  
          - scores (Tensor): Classification scores, has a shape
              (num_instance, )
          - labels (Tensor): Labels of bboxes, has a shape
              (num_instances, ).
          - label_names (List[str]): Label names of bboxes.
          - bboxes (Tensor): Has a shape (num_instances, 4),
              the last dimension 4 arrange as (x1, y1, x2, y2).
docstr_tok: ["Predict","results","from","a","batch","of","inputs","and","data","samples","with","post-","processing",".","Args",":","batch_inputs","(","Tensor",")",":","Inputs","with","shape","(","N",",","C",",","H",",","W",")",".","batch_data_samples","(","List","[",":","obj",":","`","DetDataSample","`","]",")",":","The","Data","Samples",".","It","usually","includes","information","such","as","`","gt_instance","`",",","`","gt_panoptic_seg","`","and","`","gt_sem_seg","`",".","rescale","(","bool",")",":","Whether","to","rescale","the","results",".","Defaults","to","True",".","Returns",":","list","[",":","obj",":","`","DetDataSample","`","]",":","Detection","results","of","the","input","images",".","Each","DetDataSample","usually","contain","'pred_instances","'",".","And","the","``","pred_instances","``","usually","contains","following","keys",".","-","scores","(","Tensor",")",":","Classification","scores",",","has","a","shape","(","num_instance",",",")","-","labels","(","Tensor",")",":","Labels","of","bboxes",",","has","a","shape","(","num_instances",",",")",".","-","label_names","(","List","[","str","]",")",":","Label","names","of","bboxes",".","-","bboxes","(","Tensor",")",":","Has","a","shape","(","num_instances",",","4",")",",","the","last","dimension","4","arrange","as","(","x1",",","y1",",","x2",",","y2",")","."]
code: def predict(
      self,
      batch_inputs: Tensor,
      batch_data_samples: SampleList,
      rescale: bool = True,
  ) -> SampleList:
      """Predict results from a batch of inputs and data samples with post-
      processing.
  
      Args:
          batch_inputs (Tensor): Inputs with shape (N, C, H, W).
          batch_data_samples (List[:obj:`DetDataSample`]): The Data
              Samples. It usually includes information such as
              `gt_instance`, `gt_panoptic_seg` and `gt_sem_seg`.
          rescale (bool): Whether to rescale the results.
              Defaults to True.
  
      Returns:
          list[:obj:`DetDataSample`]: Detection results of the
          input images. Each DetDataSample usually contain
          'pred_instances'. And the ``pred_instances`` usually
          contains following keys.
  
              - scores (Tensor): Classification scores, has a shape
                  (num_instance, )
              - labels (Tensor): Labels of bboxes, has a shape
                  (num_instances, ).
              - label_names (List[str]): Label names of bboxes.
              - bboxes (Tensor): Has a shape (num_instances, 4),
                  the last dimension 4 arrange as (x1, y1, x2, y2).
      """
      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(set(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
      )
  
      visual_features = 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]
              language_dict_features = self.language_model(
                  text_prompts_once
              )
              batch_data_samples[
                  0
              ].token_positive_map = token_positive_maps_once
  
              pred_instances = self.bbox_head.predict(
                  copy.deepcopy(visual_features),
                  language_dict_features,
                  batch_data_samples,
                  rescale=rescale,
              )[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)]
      else:
          language_dict_features = self.language_model(
              list(text_prompts)
          )
  
          for i, data_samples in enumerate(batch_data_samples):
              data_samples.token_positive_map = token_positive_maps[i]
  
          results_list = self.bbox_head.predict(
              visual_features,
              language_dict_features,
              batch_data_samples,
              rescale=rescale,
          )
  
      for data_sample, pred_instances, entity in zip(
          batch_data_samples, results_list, entities
      ):
          if len(pred_instances) > 0:
              label_names = []
              for labels in pred_instances.labels:
                  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 Optional, Tuple, Union
  import torch
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.utils import ConfigType, OptConfigType, OptMultiConfig
  from .single_stage import SingleStageDetector
label_desc: Use image node_id 9 for calling the GLIP obj's underlying member method code with example usage: obj.predict(batch_inputs, batch_data_samples, rescale) and returns: batch_data_samples