class_docstr: Head of DETR. DETR:End-to-End Object Detection with Transformers.
  
  More details can be found in the `paper
  <https://arxiv.org/pdf/2005.12872>`_ .
  
  Args:
      num_classes (int): Number of categories excluding the background.
      embed_dims (int): The dims of Transformer embedding.
      num_reg_fcs (int): Number of fully-connected layers used in `FFN`,
          which is then used for the regression head. Defaults to 2.
      sync_cls_avg_factor (bool): Whether to sync the `avg_factor` of
          all ranks. Default to `False`.
      loss_cls (:obj:`ConfigDict` or dict): Config of the classification
          loss. Defaults to `CrossEntropyLoss`.
      loss_bbox (:obj:`ConfigDict` or dict): Config of the regression bbox
          loss. Defaults to `L1Loss`.
      loss_iou (:obj:`ConfigDict` or dict): Config of the regression iou
          loss. Defaults to `GIoULoss`.
      train_cfg (:obj:`ConfigDict` or dict): Training config of transformer
          head.
      test_cfg (:obj:`ConfigDict` or dict): Testing config of transformer
          head.
      init_cfg (:obj:`ConfigDict` or dict, optional): the config to control
          the initialization. Defaults to None.
docstr: "Loss function.
  
  Only outputs from the last feature level are used for computing
  losses by default.
  
  Args:
      all_layers_cls_scores (Tensor): Classification outputs
          of each decoder layers. Each is a 4D-tensor, has shape
          (num_decoder_layers, bs, num_queries, cls_out_channels).
      all_layers_bbox_preds (Tensor): Sigmoid regression
          outputs of each decoder layers. Each is a 4D-tensor with
          normalized coordinate format (cx, cy, w, h) and shape
          (num_decoder_layers, bs, num_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.
      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",".","Only","outputs","from","the","last","feature","level","are","used","for","computing","losses","by","default",".","Args",":","all_layers_cls_scores","(","Tensor",")",":","Classification","outputs","of","each","decoder","layers",".","Each","is","a","4D-tensor",",","has","shape","(","num_decoder_layers",",","bs",",","num_queries",",","cls_out_channels",")",".","all_layers_bbox_preds","(","Tensor",")",":","Sigmoid","regression","outputs","of","each","decoder","layers",".","Each","is","a","4D-tensor","with","normalized","coordinate","format","(","cx",",","cy",",","w",",","h",")","and","shape","(","num_decoder_layers",",","bs",",","num_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",".","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,
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      batch_gt_instances_ignore: OptInstanceList = None,
  ) -> Dict[str, Tensor]:
      """ "Loss function.
  
      Only outputs from the last feature level are used for computing
      losses by default.
  
      Args:
          all_layers_cls_scores (Tensor): Classification outputs
              of each decoder layers. Each is a 4D-tensor, has shape
              (num_decoder_layers, bs, num_queries, cls_out_channels).
          all_layers_bbox_preds (Tensor): Sigmoid regression
              outputs of each decoder layers. Each is a 4D-tensor with
              normalized coordinate format (cx, cy, w, h) and shape
              (num_decoder_layers, bs, num_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.
          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.
      """
      assert batch_gt_instances_ignore is None, (
          f"{self.__class__.__name__} only supports "
          "for batch_gt_instances_ignore setting to None."
      )
  
      losses_cls, losses_bbox, losses_iou = multi_apply(
          self.loss_by_feat_single,
          all_layers_cls_scores,
          all_layers_bbox_preds,
          batch_gt_instances=batch_gt_instances,
          batch_img_metas=batch_img_metas,
      )
  
      loss_dict = dict()
      # loss from the last decoder layer
      loss_dict["loss_cls"] = losses_cls[-1]
      loss_dict["loss_bbox"] = losses_bbox[-1]
      loss_dict["loss_iou"] = losses_iou[-1]
      # loss from other decoder layers
      num_dec_layer = 0
      for loss_cls_i, loss_bbox_i, loss_iou_i in zip(
          losses_cls[:-1], losses_bbox[:-1], losses_iou[:-1]
      ):
          loss_dict[f"d{num_dec_layer}.loss_cls"] = loss_cls_i
          loss_dict[f"d{num_dec_layer}.loss_bbox"] = loss_bbox_i
          loss_dict[f"d{num_dec_layer}.loss_iou"] = loss_iou_i
          num_dec_layer += 1
      return loss_dict
  
imports: from typing import Dict, List, Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import Linear
  from mmcv.cnn.bricks.transformer import FFN
  from mmengine.model import BaseModule
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox_cxcywh_to_xyxy, bbox_overlaps, bbox_xyxy_to_cxcywh
  from mmdet.utils import ConfigType, InstanceList, OptInstanceList, OptMultiConfig, reduce_mean
  from ..losses import QualityFocalLoss
  from ..utils import multi_apply
label_desc: Use image node_id 5 for calling the DETRHead obj's underlying member method code with example usage: obj.loss_by_feat(all_layers_cls_scores, all_layers_bbox_preds, batch_gt_instances, batch_img_metas, batch_gt_instances_ignore) and returns: loss_dict