class_docstr: Implement query generator of the Contrastive denoising (CDN) proposed in
  `DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object
  Detection <https://arxiv.org/abs/2203.03605>`_
  
  Code is modified from the `official github repo
  <https://github.com/IDEA-Research/DINO>`_.
  
  Args:
      num_classes (int): Number of object classes.
      embed_dims (int): The embedding dimensions of the generated queries.
      num_matching_queries (int): The queries number of the matching part.
          Used for generating dn_mask.
      label_noise_scale (float): The scale of label noise, defaults to 0.5.
      box_noise_scale (float): The scale of box noise, defaults to 1.0.
      group_cfg (:obj:`ConfigDict` or dict, optional): The config of the
          denoising queries grouping, includes `dynamic`, `num_dn_queries`,
          and `num_groups`. Two grouping strategies, 'static dn groups' and
          'dynamic dn groups', are supported. When `dynamic` is `False`,
          the `num_groups` should be set, and the number of denoising query
          groups will always be `num_groups`. When `dynamic` is `True`, the
          `num_dn_queries` should be set, and the group number will be
          dynamic to ensure that the denoising queries number will not exceed
          `num_dn_queries` to prevent large fluctuations of memory. Defaults
          to `None`.
docstr: Generate noisy labels and their query embeddings.
  
  The strategy for generating noisy labels is: Randomly choose labels of
  `self.label_noise_scale * 0.5` proportion and override each of them
  with a random object category label.
  
  NOTE Not add noise to all labels. Besides, the `self.label_noise_scale
  * 0.5` arg is the ratio of the chosen positions, which is higher than
  the actual proportion of noisy labels, because the labels to override
  may be correct. And the gap becomes larger as the number of target
  categories decreases. The users should notice this and modify the scale
  arg or the corresponding logic according to specific dataset.
  
  Args:
      gt_labels (Tensor): The concatenated gt labels of all samples
          in the batch, has shape (num_target_total, ) where
          `num_target_total = sum(num_target_list)`.
      num_groups (int): The number of denoising query groups.
  
  Returns:
      Tensor: The query embeddings of noisy labels, has shape
      (num_noisy_targets, embed_dims), where `num_noisy_targets =
      num_target_total * num_groups * 2`.
docstr_tok: ["Generate","noisy","labels","and","their","query","embeddings",".","The","strategy","for","generating","noisy","labels","is",":","Randomly","choose","labels","of","`","self.label_noise_scale","*","0.5","`","proportion","and","override","each","of","them","with","a","random","object","category","label",".","NOTE","Not","add","noise","to","all","labels",".","Besides",",","the","`","self.label_noise_scale","*","0.5","`","arg","is","the","ratio","of","the","chosen","positions",",","which","is","higher","than","the","actual","proportion","of","noisy","labels",",","because","the","labels","to","override","may","be","correct",".","And","the","gap","becomes","larger","as","the","number","of","target","categories","decreases",".","The","users","should","notice","this","and","modify","the","scale","arg","or","the","corresponding","logic","according","to","specific","dataset",".","Args",":","gt_labels","(","Tensor",")",":","The","concatenated","gt","labels","of","all","samples","in","the","batch",",","has","shape","(","num_target_total",",",")","where","`","num_target_total","=","sum","(","num_target_list",")","`",".","num_groups","(","int",")",":","The","number","of","denoising","query","groups",".","Returns",":","Tensor",":","The","query","embeddings","of","noisy","labels",",","has","shape","(","num_noisy_targets",",","embed_dims",")",",","where","`","num_noisy_targets","=","num_target_total","*","num_groups","*","2","`","."]
code: def generate_dn_label_query(
      self, gt_labels: Tensor, num_groups: int
  ) -> Tensor:
      """Generate noisy labels and their query embeddings.
  
      The strategy for generating noisy labels is: Randomly choose labels of
      `self.label_noise_scale * 0.5` proportion and override each of them
      with a random object category label.
  
      NOTE Not add noise to all labels. Besides, the `self.label_noise_scale
      * 0.5` arg is the ratio of the chosen positions, which is higher than
      the actual proportion of noisy labels, because the labels to override
      may be correct. And the gap becomes larger as the number of target
      categories decreases. The users should notice this and modify the scale
      arg or the corresponding logic according to specific dataset.
  
      Args:
          gt_labels (Tensor): The concatenated gt labels of all samples
              in the batch, has shape (num_target_total, ) where
              `num_target_total = sum(num_target_list)`.
          num_groups (int): The number of denoising query groups.
  
      Returns:
          Tensor: The query embeddings of noisy labels, has shape
          (num_noisy_targets, embed_dims), where `num_noisy_targets =
          num_target_total * num_groups * 2`.
      """
      assert self.label_noise_scale > 0
      gt_labels_expand = gt_labels.repeat(2 * num_groups, 1).view(
          -1
      )  # Note `* 2`  # noqa
      p = torch.rand_like(gt_labels_expand.float())
      chosen_indice = torch.nonzero(
          p < (self.label_noise_scale * 0.5)
      ).view(
          -1
      )  # Note `* 0.5`
      new_labels = torch.randint_like(
          chosen_indice, 0, self.num_classes
      )
      noisy_labels_expand = gt_labels_expand.scatter(
          0, chosen_indice, new_labels
      )
      dn_label_query = self.label_embedding(noisy_labels_expand)
      return dn_label_query
  
imports: import warnings
  from typing import Tuple, Union
  import torch
  from mmengine.model import BaseModule
  from torch import Tensor, nn
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox_xyxy_to_cxcywh
  from mmdet.utils import OptConfigType
  from .deformable_detr_layers import DeformableDetrTransformerDecoder
  from .utils import MLP, coordinate_to_encoding, inverse_sigmoid
label_desc: Use image node_id 4 for calling the CdnQueryGenerator obj's underlying member method code with example usage: obj.generate_dn_label_query(gt_labels, num_groups) and returns: dn_label_query