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: Loss function.
      AlignDETR: This method is based on `DINOHead.loss_by_feat`.
  
  Args:
      all_layers_cls_scores (Tensor): Classification scores of all
          decoder layers, has shape (num_decoder_layers, bs,
          num_queries_total, cls_out_channels), where
          `num_queries_total` is the sum of `num_denoising_queries`
          and `num_matching_queries`.
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Loss","function",".","AlignDETR",":","This","method","is","based","on","`","DINOHead.loss_by_feat","`",".","Args",":","all_layers_cls_scores","(","Tensor",")",":","Classification","scores","of","all","decoder","layers",",","has","shape","(","num_decoder_layers",",","bs",",","num_queries_total",",","cls_out_channels",")",",","where","`","num_queries_total","`","is","the","sum","of","`","num_denoising_queries","`","and","`","num_matching_queries","`",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss_by_feat(
      self, all_layers_cls_scores: Tensor, *args, **kwargs
  ) -> Any:
      """Loss function.
          AlignDETR: This method is based on `DINOHead.loss_by_feat`.
  
      Args:
          all_layers_cls_scores (Tensor): Classification scores of all
              decoder layers, has shape (num_decoder_layers, bs,
              num_queries_total, cls_out_channels), where
              `num_queries_total` is the sum of `num_denoising_queries`
              and `num_matching_queries`.
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
      # Wrap `all_layers_cls_scores` with KeysRecorder to record its
      #   `__getitem__` keys and get decoder layer index.
      all_layers_cls_scores = KeysRecorder(all_layers_cls_scores)
      result = super(AlignDETRHead, self).loss_by_feat(
          all_layers_cls_scores, *args, **kwargs
      )
      return result
  
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 2 for calling the AlignDETRHead obj's underlying member method code with example usage: obj.loss_by_feat(all_layers_cls_scores) and returns: result