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 loss(
      self, batch_inputs: Tensor, batch_data_samples: SampleList
  ) -> Union[dict, list]:
      text_prompts = [
          data_samples.text for data_samples in batch_data_samples
      ]
  
      gt_labels = [
          data_samples.gt_instances.labels
          for data_samples in batch_data_samples
      ]
  
      if "tokens_positive" in batch_data_samples[0]:
          tokens_positive = [
              data_samples.tokens_positive
              for data_samples in batch_data_samples
          ]
          positive_maps = []
          for token_positive, text_prompt, gt_label in zip(
              tokens_positive, text_prompts, gt_labels
          ):
              tokenized = self.language_model.tokenizer(
                  [text_prompt],
                  padding="max_length"
                  if self.language_model.pad_to_max
                  else "longest",
                  return_tensors="pt",
              )
              new_tokens_positive = [
                  token_positive[label.item()] for label in gt_label
              ]
              _, positive_map = self.get_positive_map(
                  tokenized, new_tokens_positive
              )
              positive_maps.append(positive_map)
          new_text_prompts = text_prompts
      else:
          new_text_prompts = []
          positive_maps = []
          if len(set(text_prompts)) == 1:
              # All the text prompts are the same,
              # so there is no need to calculate them multiple times.
              (
                  tokenized,
                  caption_string,
                  tokens_positive,
                  _,
              ) = self.get_tokens_and_prompts(text_prompts[0], True)
              new_text_prompts = [caption_string] * len(batch_inputs)
              for gt_label in gt_labels:
                  new_tokens_positive = [
                      tokens_positive[label] for label in gt_label
                  ]
                  _, positive_map = self.get_positive_map(
                      tokenized, new_tokens_positive
                  )
                  positive_maps.append(positive_map)
          else:
              for text_prompt, gt_label in zip(text_prompts, gt_labels):
                  (
                      tokenized,
                      caption_string,
                      tokens_positive,
                      _,
                  ) = self.get_tokens_and_prompts(text_prompt, True)
                  new_tokens_positive = [
                      tokens_positive[label] for label in gt_label
                  ]
                  _, positive_map = self.get_positive_map(
                      tokenized, new_tokens_positive
                  )
                  positive_maps.append(positive_map)
                  new_text_prompts.append(caption_string)
  
      text_dict = self.language_model(new_text_prompts)
      if self.text_feat_map is not None:
          text_dict["embedded"] = self.text_feat_map(
              text_dict["embedded"]
          )
  
      for i, data_samples in enumerate(batch_data_samples):
          positive_map = (
              positive_maps[i].to(batch_inputs.device).bool().float()
          )
          text_token_mask = text_dict["text_token_mask"][i]
          data_samples.gt_instances.positive_maps = positive_map
          data_samples.gt_instances.text_token_mask = (
              text_token_mask.unsqueeze(0).repeat(len(positive_map), 1)
          )
  
      visual_features = self.extract_feat(batch_inputs)
      head_inputs_dict = self.forward_transformer(
          visual_features, text_dict, batch_data_samples
      )
  
      losses = self.bbox_head.loss(
          **head_inputs_dict, batch_data_samples=batch_data_samples
      )
      return losses
  
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 13 for calling the GroundingDINO obj's underlying member method code with example usage: obj.loss(batch_inputs, batch_data_samples) and returns: losses