class_docstr: Head of the DINO: DETR with Improved DeNoising Anchor Boxes
  for End-to-End Object Detection
  
  Code is modified from the `official github repo
  <https://github.com/IDEA-Research/DINO>`_.
  
  More details can be found in the `paper
  <https://arxiv.org/abs/2203.03605>`_ .
class_docstr_tok: ["Head","of","the","DINO",":","DETR","with","Improved","DeNoising","Anchor","Boxes","for","End-to-End","Object","Detection","Code","is","modified","from","the","`","official","github","repo","<","https",":","\/\/github.com\/IDEA-Research\/DINO",">","`","_",".","More","details","can","be","found","in","the","`","paper","<","https",":","\/\/arxiv.org\/abs\/2203.03605",">","`","_","."]
docstr: Loss function.
  
  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`.
      all_layers_bbox_preds (Tensor): Regression outputs of all decoder
          layers. Each is a 4D-tensor with normalized coordinate format
          (cx, cy, w, h) and has shape (num_decoder_layers, bs,
          num_queries_total, 4).
      enc_cls_scores (Tensor): The score of each point on encode
          feature map, has shape (bs, num_feat_points, cls_out_channels).
      enc_bbox_preds (Tensor): The proposal generate from the encode
          feature map, has shape (bs, num_feat_points, 4) with the last
          dimension arranged as (cx, cy, w, h).
      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.
      batch_gt_instances_ignore (list[:obj:`InstanceData`], optional):
          Batch of gt_instances_ignore. It includes ``bboxes`` attribute
          data that is ignored during training and testing.
          Defaults to None.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Loss","function",".","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","`",".","all_layers_bbox_preds","(","Tensor",")",":","Regression","outputs","of","all","decoder","layers",".","Each","is","a","4D-tensor","with","normalized","coordinate","format","(","cx",",","cy",",","w",",","h",")","and","has","shape","(","num_decoder_layers",",","bs",",","num_queries_total",",","4",")",".","enc_cls_scores","(","Tensor",")",":","The","score","of","each","point","on","encode","feature","map",",","has","shape","(","bs",",","num_feat_points",",","cls_out_channels",")",".","enc_bbox_preds","(","Tensor",")",":","The","proposal","generate","from","the","encode","feature","map",",","has","shape","(","bs",",","num_feat_points",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")",".","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",".","batch_gt_instances_ignore","(","list","[",":","obj",":","`","InstanceData","`","]",",","optional",")",":","Batch","of","gt_instances_ignore",".","It","includes","``","bboxes","``","attribute","data","that","is","ignored","during","training","and","testing",".","Defaults","to","None",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss_by_feat(
      self,
      all_layers_cls_scores: Tensor,
      all_layers_bbox_preds: Tensor,
      enc_cls_scores: Tensor,
      enc_bbox_preds: Tensor,
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      dn_meta: Dict[str, int],
      batch_gt_instances_ignore: OptInstanceList = None,
  ) -> Dict[str, Tensor]:
      """Loss function.
  
      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`.
          all_layers_bbox_preds (Tensor): Regression outputs of all decoder
              layers. Each is a 4D-tensor with normalized coordinate format
              (cx, cy, w, h) and has shape (num_decoder_layers, bs,
              num_queries_total, 4).
          enc_cls_scores (Tensor): The score of each point on encode
              feature map, has shape (bs, num_feat_points, cls_out_channels).
          enc_bbox_preds (Tensor): The proposal generate from the encode
              feature map, has shape (bs, num_feat_points, 4) with the last
              dimension arranged as (cx, cy, w, h).
          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.
          batch_gt_instances_ignore (list[:obj:`InstanceData`], optional):
              Batch of gt_instances_ignore. It includes ``bboxes`` attribute
              data that is ignored during training and testing.
              Defaults to None.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
      # extract denoising and matching part of outputs
      (
          all_layers_matching_cls_scores,
          all_layers_matching_bbox_preds,
          all_layers_denoising_cls_scores,
          all_layers_denoising_bbox_preds,
      ) = self.split_outputs(
          all_layers_cls_scores, all_layers_bbox_preds, dn_meta
      )
  
      loss_dict = super(DeformableDETRHead, self).loss_by_feat(
          all_layers_matching_cls_scores,
          all_layers_matching_bbox_preds,
          batch_gt_instances,
          batch_img_metas,
          batch_gt_instances_ignore,
      )
      # NOTE DETRHead.loss_by_feat but not DeformableDETRHead.loss_by_feat
      # is called, because the encoder loss calculations are different
      # between DINO and DeformableDETR.
  
      # loss of proposal generated from encode feature map.
      if enc_cls_scores is not None:
          # NOTE The enc_loss calculation of the DINO is
          # different from that of Deformable DETR.
          (
              enc_loss_cls,
              enc_losses_bbox,
              enc_losses_iou,
          ) = self.loss_by_feat_single(
              enc_cls_scores,
              enc_bbox_preds,
              batch_gt_instances=batch_gt_instances,
              batch_img_metas=batch_img_metas,
          )
          loss_dict["enc_loss_cls"] = enc_loss_cls
          loss_dict["enc_loss_bbox"] = enc_losses_bbox
          loss_dict["enc_loss_iou"] = enc_losses_iou
  
      if all_layers_denoising_cls_scores is not None:
          # calculate denoising loss from all decoder layers
          dn_losses_cls, dn_losses_bbox, dn_losses_iou = self.loss_dn(
              all_layers_denoising_cls_scores,
              all_layers_denoising_bbox_preds,
              batch_gt_instances=batch_gt_instances,
              batch_img_metas=batch_img_metas,
              dn_meta=dn_meta,
          )
          # collate denoising loss
          loss_dict["dn_loss_cls"] = dn_losses_cls[-1]
          loss_dict["dn_loss_bbox"] = dn_losses_bbox[-1]
          loss_dict["dn_loss_iou"] = dn_losses_iou[-1]
          for num_dec_layer, (
              loss_cls_i,
              loss_bbox_i,
              loss_iou_i,
          ) in enumerate(
              zip(
                  dn_losses_cls[:-1],
                  dn_losses_bbox[:-1],
                  dn_losses_iou[:-1],
              )
          ):
              loss_dict[f"d{num_dec_layer}.dn_loss_cls"] = loss_cls_i
              loss_dict[f"d{num_dec_layer}.dn_loss_bbox"] = loss_bbox_i
              loss_dict[f"d{num_dec_layer}.dn_loss_iou"] = loss_iou_i
      return loss_dict
  
imports: from typing import Dict, List, Tuple
  import torch
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox_cxcywh_to_xyxy, bbox_overlaps, bbox_xyxy_to_cxcywh
  from mmdet.utils import InstanceList, OptInstanceList, reduce_mean
  from ..losses import QualityFocalLoss
  from ..utils import multi_apply
  from .deformable_detr_head import DeformableDETRHead
label_desc: Use image node_id 2 for calling the DINOHead obj's underlying member method code with example usage: obj.loss_by_feat(all_layers_cls_scores, all_layers_bbox_preds, enc_cls_scores, enc_bbox_preds, batch_gt_instances, batch_img_metas, dn_meta, batch_gt_instances_ignore) and returns: loss_dict