class_docstr: Head of the Align-DETR: Improving DETR with Simple IoU-aware BCE loss
  
  Code is modified from the `official github repo
  <https://github.com/FelixCaae/AlignDETR>`_.
  
  More details can be found in the `paper
  <https://arxiv.org/abs/2304.07527>`_ .
  
  Args:
      all_layers_num_gt_repeat List[int]: Number to repeat gt for 1-to-k
          matching between ground truth and predictions of each decoder
          layer. Only used for matching queries, not for denoising queries.
          Element count is `num_pred_layer`. If `as_two_stage` is True, then
          the last element is for encoder output, and the others for
          decoder layers. Otherwise, all elements are for decoder layers.
          Defaults to a list of `1` for the last decoder layer and `2` for
          the others.
      alpha (float): Hyper-parameter of classification loss that controls
          the proportion of each item to calculate `t`, the weighted
          geometric average of the confident score and the IoU score, to
          align classification and regression scores. Defaults to `0.25`.
      gamma (float): Hyper-parameter of classification loss to do the hard
          negative mining. Defaults to `2.0`.
      tau (float): Hyper-parameter of classification and regression losses,
          it is the temperature controlling the sharpness of the function
          to calculate positive sample weight. Defaults to `1.5`.
class_docstr_tok: ["Head","of","the","Align-DETR",":","Improving","DETR","with","Simple","IoU-aware","BCE","loss","Code","is","modified","from","the","`","official","github","repo","<","https",":","\/\/github.com\/FelixCaae\/AlignDETR",">","`","_",".","More","details","can","be","found","in","the","`","paper","<","https",":","\/\/arxiv.org\/abs\/2304.07527",">","`","_",".","Args",":","all_layers_num_gt_repeat","List","[","int","]",":","Number","to","repeat","gt","for","1-to-k","matching","between","ground","truth","and","predictions","of","each","decoder","layer",".","Only","used","for","matching","queries",",","not","for","denoising","queries",".","Element","count","is","`","num_pred_layer","`",".","If","`","as_two_stage","`","is","True",",","then","the","last","element","is","for","encoder","output",",","and","the","others","for","decoder","layers",".","Otherwise",",","all","elements","are","for","decoder","layers",".","Defaults","to","a","list","of","`","1","`","for","the","last","decoder","layer","and","`","2","`","for","the","others",".","alpha","(","float",")",":","Hyper-parameter","of","classification","loss","that","controls","the","proportion","of","each","item","to","calculate","`","t","`",",","the","weighted","geometric","average","of","the","confident","score","and","the","IoU","score",",","to","align","classification","and","regression","scores",".","Defaults","to","`","0.25","`",".","gamma","(","float",")",":","Hyper-parameter","of","classification","loss","to","do","the","hard","negative","mining",".","Defaults","to","`","2.0","`",".","tau","(","float",")",":","Hyper-parameter","of","classification","and","regression","losses",",","it","is","the","temperature","controlling","the","sharpness","of","the","function","to","calculate","positive","sample","weight",".","Defaults","to","`","1.5","`","."]
docstr: Denoising loss for outputs from a single decoder layer.
      AlignDETR: This method is based on `DINOHead._loss_dn_single`.
  
  Args:
      dn_cls_scores (KeysRecorder): 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",".","AlignDETR",":","This","method","is","based","on","`","DINOHead._loss_dn_single","`",".","Args",":","dn_cls_scores","(","KeysRecorder",")",":","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: KeysRecorder,
      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.
          AlignDETR: This method is based on `DINOHead._loss_dn_single`.
  
      Args:
          dn_cls_scores (KeysRecorder): 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`.
      """
      # AlignDETR: Get dn_cls_scores tensor.
      dn_cls_scores = dn_cls_scores.obj
  
      # AlignDETR: Add layer outputs to meta info because they are not
      #   variables of method `_get_dn_targets_single`.
      for image_index, img_meta in enumerate(batch_img_metas):
          img_meta["dn_cls_score"] = dn_cls_scores[image_index]
          img_meta["dn_bbox_pred"] = dn_bbox_preds[image_index]
  
      results = super()._loss_dn_single(
          dn_cls_scores,
          dn_bbox_preds,
          batch_gt_instances,
          batch_img_metas,
          dn_meta,
      )
      return results
  
file_path: mmdetection/projects/AlignDETR/align_detr/align_detr_head.py
imports: from typing import Any, Dict, List, Tuple, Union
  import torch
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.models.dense_heads import DINOHead
  from mmdet.registry import MODELS
  from mmdet.structures.bbox import bbox_cxcywh_to_xyxy, bbox_overlaps, bbox_xyxy_to_cxcywh
  from mmdet.utils import InstanceList
  from .utils import KeysRecorder
label_desc: Use image node_id 6 for calling the AlignDETRHead 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: results