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: Forward function.
  
  Args:
      hidden_states (Tensor): Hidden states output from each decoder
          layer, has shape (num_decoder_layers, bs, num_queries, dim).
      references (list[Tensor]): List of the reference from the decoder.
          The first reference is the `init_reference` (initial) and the
          other num_decoder_layers(6) references are `inter_references`
          (intermediate). The `init_reference` has shape (bs,
          num_queries, 4) when `as_two_stage` of the detector is `True`,
          otherwise (bs, num_queries, 2). Each `inter_reference` has
          shape (bs, num_queries, 4) when `with_box_refine` of the
          detector is `True`, otherwise (bs, num_queries, 2). The
          coordinates are arranged as (cx, cy) when the last dimension is
          2, and (cx, cy, w, h) when it is 4.
  
  Returns:
      tuple[Tensor]: results of head containing the following tensor.
  
      - all_layers_outputs_classes (Tensor): Outputs from the
        classification head, has shape (num_decoder_layers, bs,
        num_queries, cls_out_channels).
      - all_layers_outputs_coords (Tensor): Sigmoid outputs from the
        regression head with normalized coordinate format (cx, cy, w,
        h), has shape (num_decoder_layers, bs, num_queries, 4) with the
        last dimension arranged as (cx, cy, w, h).
docstr_tok: ["Forward","function",".","Args",":","hidden_states","(","Tensor",")",":","Hidden","states","output","from","each","decoder","layer",",","has","shape","(","num_decoder_layers",",","bs",",","num_queries",",","dim",")",".","references","(","list","[","Tensor","]",")",":","List","of","the","reference","from","the","decoder",".","The","first","reference","is","the","`","init_reference","`","(","initial",")","and","the","other","num_decoder_layers","(","6",")","references","are","`","inter_references","`","(","intermediate",")",".","The","`","init_reference","`","has","shape","(","bs",",","num_queries",",","4",")","when","`","as_two_stage","`","of","the","detector","is","`","True","`",",","otherwise","(","bs",",","num_queries",",","2",")",".","Each","`","inter_reference","`","has","shape","(","bs",",","num_queries",",","4",")","when","`","with_box_refine","`","of","the","detector","is","`","True","`",",","otherwise","(","bs",",","num_queries",",","2",")",".","The","coordinates","are","arranged","as","(","cx",",","cy",")","when","the","last","dimension","is","2",",","and","(","cx",",","cy",",","w",",","h",")","when","it","is","4",".","Returns",":","tuple","[","Tensor","]",":","results","of","head","containing","the","following","tensor",".","-","all_layers_outputs_classes","(","Tensor",")",":","Outputs","from","the","classification","head",",","has","shape","(","num_decoder_layers",",","bs",",","num_queries",",","cls_out_channels",")",".","-","all_layers_outputs_coords","(","Tensor",")",":","Sigmoid","outputs","from","the","regression","head","with","normalized","coordinate","format","(","cx",",","cy",",","w",",","h",")",",","has","shape","(","num_decoder_layers",",","bs",",","num_queries",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")","."]
code: def forward(
      self, hidden_states: Tensor, references: List[Tensor]
  ) -> Tuple[Tensor, Tensor]:
      """Forward function.
  
      Args:
          hidden_states (Tensor): Hidden states output from each decoder
              layer, has shape (num_decoder_layers, bs, num_queries, dim).
          references (list[Tensor]): List of the reference from the decoder.
              The first reference is the `init_reference` (initial) and the
              other num_decoder_layers(6) references are `inter_references`
              (intermediate). The `init_reference` has shape (bs,
              num_queries, 4) when `as_two_stage` of the detector is `True`,
              otherwise (bs, num_queries, 2). Each `inter_reference` has
              shape (bs, num_queries, 4) when `with_box_refine` of the
              detector is `True`, otherwise (bs, num_queries, 2). The
              coordinates are arranged as (cx, cy) when the last dimension is
              2, and (cx, cy, w, h) when it is 4.
  
      Returns:
          tuple[Tensor]: results of head containing the following tensor.
  
          - all_layers_outputs_classes (Tensor): Outputs from the
            classification head, has shape (num_decoder_layers, bs,
            num_queries, cls_out_channels).
          - all_layers_outputs_coords (Tensor): Sigmoid outputs from the
            regression head with normalized coordinate format (cx, cy, w,
            h), has shape (num_decoder_layers, bs, num_queries, 4) with the
            last dimension arranged as (cx, cy, w, h).
      """
      all_layers_outputs_classes = []
      all_layers_outputs_coords = []
  
      for layer_id in range(hidden_states.shape[0]):
          reference = inverse_sigmoid(references[layer_id])
          # NOTE The last reference will not be used.
          hidden_state = hidden_states[layer_id]
          outputs_class = self.cls_branches[layer_id](hidden_state)
          tmp_reg_preds = self.reg_branches[layer_id](hidden_state)
          if reference.shape[-1] == 4:
              # When `layer` is 0 and `as_two_stage` of the detector
              # is `True`, or when `layer` is greater than 0 and
              # `with_box_refine` of the detector is `True`.
              tmp_reg_preds += reference
          else:
              # When `layer` is 0 and `as_two_stage` of the detector
              # is `False`, or when `layer` is greater than 0 and
              # `with_box_refine` of the detector is `False`.
              assert reference.shape[-1] == 2
              tmp_reg_preds[..., :2] += reference
          outputs_coord = tmp_reg_preds.sigmoid()
          all_layers_outputs_classes.append(outputs_class)
          all_layers_outputs_coords.append(outputs_coord)
  
      all_layers_outputs_classes = torch.stack(
          all_layers_outputs_classes
      )
      all_layers_outputs_coords = torch.stack(all_layers_outputs_coords)
  
      return all_layers_outputs_classes, all_layers_outputs_coords
  
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 4 for calling the DeformableDETRHead obj's underlying member method code with example usage: obj.forward(hidden_states, references) and returns: all_layers_outputs_classes,  all_layers_outputs_coords