class_docstr: Implements the DETR transformer head.
  
  See `paper: End-to-End Object Detection with Transformers
  <https://arxiv.org/pdf/2005.12872>`_ for details.
  
  Args:
      num_classes (int): Number of categories excluding the background.
      in_channels (int): Number of channels in the input feature map.
      num_fcs (int, optional): Number of fully-connected layers used in
          `FFN`, which is then used for the regression head. Default 2.
      transformer (dict, optional): Config for transformer.
      positional_encoding (dict, optional): Config for position encoding.
      loss_cls (dict, optional): Config of the classification loss.
          Default `CrossEntropyLoss`.
      loss_bbox (dict, optional): Config of the regression loss.
          Default `L1Loss`.
      loss_iou (dict, optional): Config of the regression iou loss.
          Default `GIoULoss`.
      tran_cfg (dict, optional): Training config of transformer head.
      test_cfg (dict, optional): Testing config of transformer head.
  
  Example:
      >>> import torch
      >>> self = TransformerHead(80, 2048)
      >>> x = torch.rand(1, 2048, 32, 32)
      >>> mask = torch.ones(1, 32, 32).to(x.dtype)
      >>> mask[:, :16, :15] = 0
      >>> all_cls_scores, all_bbox_preds = self(x, mask)
docstr: "Forward function for a single feature level.
  
  Args:
      x (Tensor): Input feature from backbone's single stage, shape
          [bs, c, h, w].
      img_metas (list[dict]): List of image information.
  
  Returns:
      all_cls_scores (Tensor): Outputs from the classification head,
          shape [nb_dec, bs, num_query, cls_out_channels]. Note
          cls_out_channels should includes background.
      all_bbox_preds (Tensor): Sigmoid outputs from the regression
          head with normalized coordinate format (cx, cy, w, h).
          Shape [nb_dec, bs, num_query, 4].
docstr_tok: ["``","Forward","function","for","a","single","feature","level",".","Args",":","x","(","Tensor",")",":","Input","feature","from","backbone","'s","single","stage",",","shape","[","bs",",","c",",","h",",","w","]",".","img_metas","(","list","[","dict","]",")",":","List","of","image","information",".","Returns",":","all_cls_scores","(","Tensor",")",":","Outputs","from","the","classification","head",",","shape","[","nb_dec",",","bs",",","num_query",",","cls_out_channels","]",".","Note","cls_out_channels","should","includes","background",".","all_bbox_preds","(","Tensor",")",":","Sigmoid","outputs","from","the","regression","head","with","normalized","coordinate","format","(","cx",",","cy",",","w",",","h",")",".","Shape","[","nb_dec",",","bs",",","num_query",",","4","]","."]
code: def forward_single(self, x, img_metas):
      """ "Forward function for a single feature level.
  
      Args:
          x (Tensor): Input feature from backbone's single stage, shape
              [bs, c, h, w].
          img_metas (list[dict]): List of image information.
  
      Returns:
          all_cls_scores (Tensor): Outputs from the classification head,
              shape [nb_dec, bs, num_query, cls_out_channels]. Note
              cls_out_channels should includes background.
          all_bbox_preds (Tensor): Sigmoid outputs from the regression
              head with normalized coordinate format (cx, cy, w, h).
              Shape [nb_dec, bs, num_query, 4].
      """
      # construct binary masks which used for the transformer.
      # NOTE following the official DETR repo, non-zero values representing
      # ignored positions, while zero values means valid positions.
      batch_size = x.size(0)
      input_img_h, input_img_w = img_metas[0]["batch_input_shape"]
      masks = x.new_ones((batch_size, input_img_h, input_img_w))
      for img_id in range(batch_size):
          img_h, img_w, _ = img_metas[img_id]["img_shape"]
          masks[img_id, :img_h, :img_w] = 0
  
      x = self.input_proj(x)
      # interpolate masks to have the same spatial shape with x
      masks = (
          F.interpolate(masks.unsqueeze(1), size=x.shape[-2:])
          .to(torch.bool)
          .squeeze(1)
      )
      # position encoding
      pos_embed = self.positional_encoding(
          masks
      )  # [bs, embed_dim, h, w]
      # outs_dec: [nb_dec, bs, num_query, embed_dim]
      outs_dec, _ = self.transformer(
          x, masks, self.query_embedding.weight, pos_embed
      )
  
      all_cls_scores = self.fc_cls(outs_dec)
      all_bbox_preds = self.fc_reg(
          self.activate(self.reg_ffn(outs_dec))
      ).sigmoid()
      return all_cls_scores, all_bbox_preds
  
imports: import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import Conv2d, Linear, build_activation_layer
  from mmcv.runner import force_fp32
  from mmdet.core import bbox_cxcywh_to_xyxy, bbox_xyxy_to_cxcywh, build_assigner, build_sampler, multi_apply, reduce_mean
  from mmdet.models.utils import FFN, build_positional_encoding, build_transformer
  from ..builder import HEADS, build_loss
  from .anchor_free_head import AnchorFreeHead
label_desc: Use image node_id 6 for calling the TransformerHead obj's underlying member method code with example usage: obj.forward_single(x, img_metas) and returns: all_cls_scores,  all_bbox_preds