class_docstr: Head of DeformDETR: Deformable DETR: Deformable Transformers for
  End-to-End Object Detection.
  
  Code is modified from the `official github repo
  <https://github.com/fundamentalvision/Deformable-DETR>`_.
  
  More details can be found in the `paper
  <https://arxiv.org/abs/2010.04159>`_ .
  
  Args:
      share_pred_layer (bool): Whether to share parameters for all the
          prediction layers. Defaults to `False`.
      num_pred_layer (int): The number of the prediction layers.
          Defaults to 6.
      as_two_stage (bool, optional): Whether to generate the proposal
          from the outputs of encoder. Defaults to `False`.
docstr: Loss function.
  
  Args:
      all_layers_cls_scores (Tensor): Classification scores of all
          decoder layers, has shape (num_decoder_layers, bs, num_queries,
          cls_out_channels).
      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, 4) with the last dimension arranged as
          (cx, cy, w, h).
      enc_cls_scores (Tensor): The score of each point on encode
          feature map, has shape (bs, num_feat_points, cls_out_channels).
          Only when `as_two_stage` is `True` it would be passes in,
          otherwise, it would be `None`.
      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). Only when `as_two_stage`
          is `True` it would be passed in, otherwise it would be `None`.
      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",".","Args",":","all_layers_cls_scores","(","Tensor",")",":","Classification","scores","of","all","decoder","layers",",","has","shape","(","num_decoder_layers",",","bs",",","num_queries",",","cls_out_channels",")",".","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",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")",".","enc_cls_scores","(","Tensor",")",":","The","score","of","each","point","on","encode","feature","map",",","has","shape","(","bs",",","num_feat_points",",","cls_out_channels",")",".","Only","when","`","as_two_stage","`","is","`","True","`","it","would","be","passes","in",",","otherwise",",","it","would","be","`","None","`",".","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",")",".","Only","when","`","as_two_stage","`","is","`","True","`","it","would","be","passed","in",",","otherwise","it","would","be","`","None","`",".","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,
      enc_cls_scores: Tensor,
      enc_bbox_preds: Tensor,
      batch_gt_instances: InstanceList,
      batch_img_metas: List[dict],
      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,
              cls_out_channels).
          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, 4) with the last dimension arranged as
              (cx, cy, w, h).
          enc_cls_scores (Tensor): The score of each point on encode
              feature map, has shape (bs, num_feat_points, cls_out_channels).
              Only when `as_two_stage` is `True` it would be passes in,
              otherwise, it would be `None`.
          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). Only when `as_two_stage`
              is `True` it would be passed in, otherwise it would be `None`.
          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.
      """
      loss_dict = super().loss_by_feat(
          all_layers_cls_scores,
          all_layers_bbox_preds,
          batch_gt_instances,
          batch_img_metas,
          batch_gt_instances_ignore,
      )
  
      # loss of proposal generated from encode feature map.
      if enc_cls_scores is not None:
          proposal_gt_instances = copy.deepcopy(batch_gt_instances)
          for i in range(len(proposal_gt_instances)):
              proposal_gt_instances[i].labels = torch.zeros_like(
                  proposal_gt_instances[i].labels
              )
          (
              enc_loss_cls,
              enc_losses_bbox,
              enc_losses_iou,
          ) = self.loss_by_feat_single(
              enc_cls_scores,
              enc_bbox_preds,
              batch_gt_instances=proposal_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
      return loss_dict
  
imports: import copy
  from typing import Dict, List, Tuple
  import torch
  import torch.nn
  from mmcv.cnn import Linear
  from mmengine.model import bias_init_with_prob, constant_init
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.utils import InstanceList, OptInstanceList
  from ..layers import inverse_sigmoid
  from .detr_head import DETRHead
label_desc: Use image node_id 6 for calling the DeformableDETRHead 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, batch_gt_instances_ignore) and returns: loss_dict