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: "Loss function.
  
  Only outputs from the last feature level are used for computing
  losses by default.
  
  Args:
      all_cls_scores_list (list[Tensor]): Classification outputs
          for each feature level. Each is a 4D-tensor with shape
          [nb_dec, bs, num_query, cls_out_channels].
      all_bbox_preds_list (list[Tensor]): Sigmoid regression
          outputs for each feature level. Each is a 4D-tensor with
          normalized coordinate format (cx, cy, w, h) and shape
          [nb_dec, bs, num_query, 4].
      gt_bboxes_list (list[Tensor]): Ground truth bboxes for each image
          with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
      gt_labels_list (list[Tensor]): Ground truth class indices for each
          image with shape (num_gts, ).
      img_metas (list[dict]): List of image meta information.
      gt_bboxes_ignore (list[Tensor], optional): Bounding boxes
          which can be ignored for each image. Default 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_cls_scores_list","(","list","[","Tensor","]",")",":","Classification","outputs","for","each","feature","level",".","Each","is","a","4D-tensor","with","shape","[","nb_dec",",","bs",",","num_query",",","cls_out_channels","]",".","all_bbox_preds_list","(","list","[","Tensor","]",")",":","Sigmoid","regression","outputs","for","each","feature","level",".","Each","is","a","4D-tensor","with","normalized","coordinate","format","(","cx",",","cy",",","w",",","h",")","and","shape","[","nb_dec",",","bs",",","num_query",",","4","]",".","gt_bboxes_list","(","list","[","Tensor","]",")",":","Ground","truth","bboxes","for","each","image","with","shape","(","num_gts",",","4",")","in","[","tl_x",",","tl_y",",","br_x",",","br_y","]","format",".","gt_labels_list","(","list","[","Tensor","]",")",":","Ground","truth","class","indices","for","each","image","with","shape","(","num_gts",",",")",".","img_metas","(","list","[","dict","]",")",":","List","of","image","meta","information",".","gt_bboxes_ignore","(","list","[","Tensor","]",",","optional",")",":","Bounding","boxes","which","can","be","ignored","for","each","image",".","Default","None",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss(
      self,
      all_cls_scores_list,
      all_bbox_preds_list,
      gt_bboxes_list,
      gt_labels_list,
      img_metas,
      gt_bboxes_ignore=None,
  ):
      """ "Loss function.
  
      Only outputs from the last feature level are used for computing
      losses by default.
  
      Args:
          all_cls_scores_list (list[Tensor]): Classification outputs
              for each feature level. Each is a 4D-tensor with shape
              [nb_dec, bs, num_query, cls_out_channels].
          all_bbox_preds_list (list[Tensor]): Sigmoid regression
              outputs for each feature level. Each is a 4D-tensor with
              normalized coordinate format (cx, cy, w, h) and shape
              [nb_dec, bs, num_query, 4].
          gt_bboxes_list (list[Tensor]): Ground truth bboxes for each image
              with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
          gt_labels_list (list[Tensor]): Ground truth class indices for each
              image with shape (num_gts, ).
          img_metas (list[dict]): List of image meta information.
          gt_bboxes_ignore (list[Tensor], optional): Bounding boxes
              which can be ignored for each image. Default None.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
      # NOTE defaultly only the outputs from the last feature scale is used.
      all_cls_scores = all_cls_scores_list[-1]
      all_bbox_preds = all_bbox_preds_list[-1]
      assert (
          gt_bboxes_ignore is None
      ), "Only supports for gt_bboxes_ignore setting to None."
  
      num_dec_layers = len(all_cls_scores)
      all_gt_bboxes_list = [
          gt_bboxes_list for _ in range(num_dec_layers)
      ]
      all_gt_labels_list = [
          gt_labels_list for _ in range(num_dec_layers)
      ]
      all_gt_bboxes_ignore_list = [
          gt_bboxes_ignore for _ in range(num_dec_layers)
      ]
      img_metas_list = [img_metas for _ in range(num_dec_layers)]
  
      losses_cls, losses_bbox, losses_iou = multi_apply(
          self.loss_single,
          all_cls_scores,
          all_bbox_preds,
          all_gt_bboxes_list,
          all_gt_labels_list,
          img_metas_list,
          all_gt_bboxes_ignore_list,
      )
  
      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: 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 7 for calling the TransformerHead obj's underlying member method code with example usage: obj.loss(all_cls_scores_list, all_bbox_preds_list, gt_bboxes_list, gt_labels_list, img_metas, gt_bboxes_ignore) and returns: loss_dict