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: "Compute regression and classification targets for one image.
  
  Outputs from a single decoder layer of a single feature level are used.
  
  Args:
      cls_score (Tensor): Box score logits from a single decoder layer
          for one image. Shape [num_query, cls_out_channels].
      bbox_pred (Tensor): Sigmoid outputs from a single decoder layer
          for one image, with normalized coordinate (cx, cy, w, h) and
          shape [num_query, 4].
      gt_bboxes (Tensor): Ground truth bboxes for one image with
          shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
      gt_labels (Tensor): Ground truth class indices for one image
          with shape (num_gts, ).
      img_meta (dict): Meta information for one image.
      gt_bboxes_ignore (Tensor, optional): Bounding boxes
          which can be ignored. Default None.
  
  Returns:
      tuple[Tensor]: a tuple containing the following for one image.
  
          - labels (Tensor): Labels of each image.
          - label_weights (Tensor]): Label weights of each image.
          - bbox_targets (Tensor): BBox targets of each image.
          - bbox_weights (Tensor): BBox weights of each image.
          - pos_inds (Tensor): Sampled positive indices for each image.
          - neg_inds (Tensor): Sampled negative indices for each image.
docstr_tok: ["``","Compute","regression","and","classification","targets","for","one","image",".","Outputs","from","a","single","decoder","layer","of","a","single","feature","level","are","used",".","Args",":","cls_score","(","Tensor",")",":","Box","score","logits","from","a","single","decoder","layer","for","one","image",".","Shape","[","num_query",",","cls_out_channels","]",".","bbox_pred","(","Tensor",")",":","Sigmoid","outputs","from","a","single","decoder","layer","for","one","image",",","with","normalized","coordinate","(","cx",",","cy",",","w",",","h",")","and","shape","[","num_query",",","4","]",".","gt_bboxes","(","Tensor",")",":","Ground","truth","bboxes","for","one","image","with","shape","(","num_gts",",","4",")","in","[","tl_x",",","tl_y",",","br_x",",","br_y","]","format",".","gt_labels","(","Tensor",")",":","Ground","truth","class","indices","for","one","image","with","shape","(","num_gts",",",")",".","img_meta","(","dict",")",":","Meta","information","for","one","image",".","gt_bboxes_ignore","(","Tensor",",","optional",")",":","Bounding","boxes","which","can","be","ignored",".","Default","None",".","Returns",":","tuple","[","Tensor","]",":","a","tuple","containing","the","following","for","one","image",".","-","labels","(","Tensor",")",":","Labels","of","each","image",".","-","label_weights","(","Tensor","]",")",":","Label","weights","of","each","image",".","-","bbox_targets","(","Tensor",")",":","BBox","targets","of","each","image",".","-","bbox_weights","(","Tensor",")",":","BBox","weights","of","each","image",".","-","pos_inds","(","Tensor",")",":","Sampled","positive","indices","for","each","image",".","-","neg_inds","(","Tensor",")",":","Sampled","negative","indices","for","each","image","."]
code: def _get_target_single(
      self,
      cls_score,
      bbox_pred,
      gt_bboxes,
      gt_labels,
      img_meta,
      gt_bboxes_ignore=None,
  ):
      """ "Compute regression and classification targets for one image.
  
      Outputs from a single decoder layer of a single feature level are used.
  
      Args:
          cls_score (Tensor): Box score logits from a single decoder layer
              for one image. Shape [num_query, cls_out_channels].
          bbox_pred (Tensor): Sigmoid outputs from a single decoder layer
              for one image, with normalized coordinate (cx, cy, w, h) and
              shape [num_query, 4].
          gt_bboxes (Tensor): Ground truth bboxes for one image with
              shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
          gt_labels (Tensor): Ground truth class indices for one image
              with shape (num_gts, ).
          img_meta (dict): Meta information for one image.
          gt_bboxes_ignore (Tensor, optional): Bounding boxes
              which can be ignored. Default None.
  
      Returns:
          tuple[Tensor]: a tuple containing the following for one image.
  
              - labels (Tensor): Labels of each image.
              - label_weights (Tensor]): Label weights of each image.
              - bbox_targets (Tensor): BBox targets of each image.
              - bbox_weights (Tensor): BBox weights of each image.
              - pos_inds (Tensor): Sampled positive indices for each image.
              - neg_inds (Tensor): Sampled negative indices for each image.
      """
  
      num_bboxes = bbox_pred.size(0)
      # assigner and sampler
      assign_result = self.assigner.assign(
          bbox_pred,
          cls_score,
          gt_bboxes,
          gt_labels,
          img_meta,
          gt_bboxes_ignore,
      )
      sampling_result = self.sampler.sample(
          assign_result, bbox_pred, gt_bboxes
      )
      pos_inds = sampling_result.pos_inds
      neg_inds = sampling_result.neg_inds
  
      # label targets
      labels = gt_bboxes.new_full(
          (num_bboxes,), self.num_classes, dtype=torch.long
      )
      labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds]
      label_weights = gt_bboxes.new_ones(num_bboxes)
  
      # bbox targets
      bbox_targets = torch.zeros_like(bbox_pred)
      bbox_weights = torch.zeros_like(bbox_pred)
      bbox_weights[pos_inds] = 1.0
      img_h, img_w, _ = img_meta["img_shape"]
  
      # DETR regress the relative position of boxes (cxcywh) in the image.
      # Thus the learning target should be normalized by the image size, also
      # the box format should be converted from defaultly x1y1x2y2 to cxcywh.
      factor = bbox_pred.new_tensor(
          [img_w, img_h, img_w, img_h]
      ).unsqueeze(0)
      pos_gt_bboxes_normalized = sampling_result.pos_gt_bboxes / factor
      pos_gt_bboxes_targets = bbox_xyxy_to_cxcywh(
          pos_gt_bboxes_normalized
      )
      bbox_targets[pos_inds] = pos_gt_bboxes_targets
      return (
          labels,
          label_weights,
          bbox_targets,
          bbox_weights,
          pos_inds,
          neg_inds,
      )
  
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 10 for calling the TransformerHead obj's underlying member method code with example usage: obj._get_target_single(cls_score, bbox_pred, gt_bboxes, gt_labels, img_meta, gt_bboxes_ignore) and returns: labels,  label_weights,  bbox_targets,  bbox_weights,  pos_inds,  neg_inds