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: Denoising loss for outputs from a single decoder layer.
  
  Args:
      dn_cls_scores (Tensor): Classification scores of a single decoder
          layer in denoising part, has shape (bs, num_denoising_queries,
          cls_out_channels).
      dn_bbox_preds (Tensor): Regression outputs of a single decoder
          layer in denoising part. Each is a 4D-tensor with normalized
          coordinate format (cx, cy, w, h) and has shape
          (bs, num_denoising_queries, 4).
      batch_gt_instances (list[:obj:`InstanceData`]): Batch of
          gt_instance. It usually includes ``bboxes`` and ``labels``
          attributes.
      batch_img_metas (list[dict]): Meta information of each image, e.g.,
          image size, scaling factor, etc.
      dn_meta (Dict[str, int]): The dictionary saves information about
        group collation, including 'num_denoising_queries' and
        'num_denoising_groups'. It will be used for split outputs of
        denoising and matching parts and loss calculation.
  
  Returns:
      Tuple[Tensor]: A tuple including `loss_cls`, `loss_box` and
      `loss_iou`.
docstr_tok: ["Denoising","loss","for","outputs","from","a","single","decoder","layer",".","Args",":","dn_cls_scores","(","Tensor",")",":","Classification","scores","of","a","single","decoder","layer","in","denoising","part",",","has","shape","(","bs",",","num_denoising_queries",",","cls_out_channels",")",".","dn_bbox_preds","(","Tensor",")",":","Regression","outputs","of","a","single","decoder","layer","in","denoising","part",".","Each","is","a","4D-tensor","with","normalized","coordinate","format","(","cx",",","cy",",","w",",","h",")","and","has","shape","(","bs",",","num_denoising_queries",",","4",")",".","batch_gt_instances","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","Batch","of","gt_instance",".","It","usually","includes","``","bboxes","``","and","``","labels","``","attributes",".","batch_img_metas","(","list","[","dict","]",")",":","Meta","information","of","each","image",",","e.g.",",","image","size",",","scaling","factor",",","etc",".","dn_meta","(","Dict","[","str",",","int","]",")",":","The","dictionary","saves","information","about","group","collation",",","including","'num_denoising_queries","'","and","'num_denoising_groups","'",".","It","will","be","used","for","split","outputs","of","denoising","and","matching","parts","and","loss","calculation",".","Returns",":","Tuple","[","Tensor","]",":","A","tuple","including","`","loss_cls","`",",","`","loss_box","`","and","`","loss_iou","`","."]
code: def _loss_dn_single(
      self,
      dn_cls_scores: Tensor,
      dn_bbox_preds: Tensor,
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      dn_meta: Dict[str, int],
  ) -> Tuple[Tensor]:
      """Denoising loss for outputs from a single decoder layer.
  
      Args:
          dn_cls_scores (Tensor): Classification scores of a single decoder
              layer in denoising part, has shape (bs, num_denoising_queries,
              cls_out_channels).
          dn_bbox_preds (Tensor): Regression outputs of a single decoder
              layer in denoising part. Each is a 4D-tensor with normalized
              coordinate format (cx, cy, w, h) and has shape
              (bs, num_denoising_queries, 4).
          batch_gt_instances (list[:obj:`InstanceData`]): Batch of
              gt_instance. It usually includes ``bboxes`` and ``labels``
              attributes.
          batch_img_metas (list[dict]): Meta information of each image, e.g.,
              image size, scaling factor, etc.
          dn_meta (Dict[str, int]): The dictionary saves information about
            group collation, including 'num_denoising_queries' and
            'num_denoising_groups'. It will be used for split outputs of
            denoising and matching parts and loss calculation.
  
      Returns:
          Tuple[Tensor]: A tuple including `loss_cls`, `loss_box` and
          `loss_iou`.
      """
      cls_reg_targets = self.get_dn_targets(
          batch_gt_instances, batch_img_metas, dn_meta
      )
      (
          labels_list,
          label_weights_list,
          bbox_targets_list,
          bbox_weights_list,
          num_total_pos,
          num_total_neg,
      ) = cls_reg_targets
      labels = torch.stack(labels_list, 0)
      label_weights = torch.stack(label_weights_list, 0)
      bbox_targets = torch.cat(bbox_targets_list, 0)
      bbox_weights = torch.cat(bbox_weights_list, 0)
      # ===== this change =====
      # Loss is not computed for the padded regions of the text.
      assert self.text_masks.dim() == 2
      text_masks = self.text_masks.new_zeros(
          (self.text_masks.size(0), self.max_text_len)
      )
      text_masks[:, : self.text_masks.size(1)] = self.text_masks
      text_mask = (text_masks > 0).unsqueeze(1)
      text_mask = text_mask.repeat(1, dn_cls_scores.size(1), 1)
      cls_scores = torch.masked_select(
          dn_cls_scores, text_mask
      ).contiguous()
      labels = torch.masked_select(labels, text_mask)
      label_weights = label_weights[..., None].repeat(
          1, 1, text_mask.size(-1)
      )
      label_weights = torch.masked_select(label_weights, text_mask)
      # =======================
  
      # classification loss
      # construct weighted avg_factor to match with the official DETR repo
      cls_avg_factor = (
          num_total_pos * 1.0 + num_total_neg * self.bg_cls_weight
      )
      if self.sync_cls_avg_factor:
          cls_avg_factor = reduce_mean(
              cls_scores.new_tensor([cls_avg_factor])
          )
      cls_avg_factor = max(cls_avg_factor, 1)
  
      if len(cls_scores) > 0:
          if isinstance(self.loss_cls, QualityFocalLoss):
              raise NotImplementedError(
                  "QualityFocalLoss is not supported"
              )
          else:
              loss_cls = self.loss_cls(
                  cls_scores,
                  labels,
                  label_weights,
                  avg_factor=cls_avg_factor,
              )
      else:
          loss_cls = torch.zeros(
              1, dtype=cls_scores.dtype, device=cls_scores.device
          )
  
      # Compute the average number of gt boxes across all gpus, for
      # normalization purposes
      num_total_pos = loss_cls.new_tensor([num_total_pos])
      num_total_pos = torch.clamp(
          reduce_mean(num_total_pos), min=1
      ).item()
  
      # construct factors used for rescale bboxes
      factors = []
      for img_meta, bbox_pred in zip(batch_img_metas, dn_bbox_preds):
          img_h, img_w = img_meta["img_shape"]
          factor = (
              bbox_pred.new_tensor([img_w, img_h, img_w, img_h])
              .unsqueeze(0)
              .repeat(bbox_pred.size(0), 1)
          )
          factors.append(factor)
      factors = torch.cat(factors)
  
      # DETR regress the relative position of boxes (cxcywh) in the image,
      # thus the learning target is normalized by the image size. So here
      # we need to re-scale them for calculating IoU loss
      bbox_preds = dn_bbox_preds.reshape(-1, 4)
      bboxes = bbox_cxcywh_to_xyxy(bbox_preds) * factors
      bboxes_gt = bbox_cxcywh_to_xyxy(bbox_targets) * factors
  
      # regression IoU loss, defaultly GIoU loss
      loss_iou = self.loss_iou(
          bboxes, bboxes_gt, bbox_weights, avg_factor=num_total_pos
      )
  
      # regression L1 loss
      loss_bbox = self.loss_bbox(
          bbox_preds,
          bbox_targets,
          bbox_weights,
          avg_factor=num_total_pos,
      )
      return loss_cls, loss_bbox, loss_iou
  
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 11 for calling the GroundingDINOHead obj's underlying member method code with example usage: obj._loss_dn_single(dn_cls_scores, dn_bbox_preds, batch_gt_instances, batch_img_metas, dn_meta) and returns: loss_cls,  loss_bbox,  loss_iou