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)
code: def __init__(
      self,
      num_classes,
      in_channels,
      num_fcs=2,
      transformer=dict(
          type="Transformer",
          embed_dims=256,
          num_heads=8,
          num_encoder_layers=6,
          num_decoder_layers=6,
          feedforward_channels=2048,
          dropout=0.1,
          act_cfg=dict(type="ReLU", inplace=True),
          norm_cfg=dict(type="LN"),
          num_fcs=2,
          pre_norm=False,
          return_intermediate_dec=True,
      ),
      positional_encoding=dict(
          type="SinePositionalEncoding", num_feats=128, normalize=True
      ),
      loss_cls=dict(
          type="CrossEntropyLoss",
          bg_cls_weight=0.1,
          use_sigmoid=False,
          loss_weight=1.0,
          class_weight=1.0,
      ),
      loss_bbox=dict(type="L1Loss", loss_weight=5.0),
      loss_iou=dict(type="GIoULoss", loss_weight=2.0),
      train_cfg=dict(
          assigner=dict(
              type="HungarianAssigner",
              cls_weight=1.0,
              bbox_weight=5.0,
              iou_weight=2.0,
              iou_calculator=dict(type="BboxOverlaps2D"),
              iou_mode="giou",
          )
      ),
      test_cfg=dict(max_per_img=100),
      **kwargs,
  ):
      # NOTE here use `AnchorFreeHead` instead of `TransformerHead`,
      # since it brings inconvenience when the initialization of
      # `AnchorFreeHead` is called.
      super(AnchorFreeHead, self).__init__()
      use_sigmoid_cls = loss_cls.get("use_sigmoid", False)
      assert not use_sigmoid_cls, (
          "setting use_sigmoid_cls as True is "
          "not supported in DETR, since background is needed for the "
          "matching process."
      )
      assert (
          "embed_dims" in transformer
          and "num_feats" in positional_encoding
      )
      num_feats = positional_encoding["num_feats"]
      embed_dims = transformer["embed_dims"]
      assert num_feats * 2 == embed_dims, (
          "embed_dims should"
          f" be exactly 2 times of num_feats. Found {embed_dims}"
          f" and {num_feats}."
      )
      assert test_cfg is not None and "max_per_img" in test_cfg
  
      class_weight = loss_cls.get("class_weight", None)
      if class_weight is not None:
          assert isinstance(class_weight, float), (
              "Expected "
              "class_weight to have type float. Found "
              f"{type(class_weight)}."
          )
          # NOTE following the official DETR rep0, bg_cls_weight means
          # relative classification weight of the no-object class.
          bg_cls_weight = loss_cls.get("bg_cls_weight", class_weight)
          assert isinstance(bg_cls_weight, float), (
              "Expected "
              "bg_cls_weight to have type float. Found "
              f"{type(bg_cls_weight)}."
          )
          class_weight = torch.ones(num_classes + 1) * class_weight
          # set background class as the last indice
          class_weight[num_classes] = bg_cls_weight
          loss_cls.update({"class_weight": class_weight})
          if "bg_cls_weight" in loss_cls:
              loss_cls.pop("bg_cls_weight")
          self.bg_cls_weight = bg_cls_weight
  
      if train_cfg:
          assert "assigner" in train_cfg, (
              "assigner should be provided " "when train_cfg is set."
          )
          assigner = train_cfg["assigner"]
          assert loss_cls["loss_weight"] == assigner["cls_weight"], (
              "The classification weight for loss and matcher should be"
              "exactly the same."
          )
          assert loss_bbox["loss_weight"] == assigner["bbox_weight"], (
              "The regression L1 weight for loss and matcher should be"
              "exactly the same."
          )
          assert loss_iou["loss_weight"] == assigner["iou_weight"], (
              "The regression iou weight for loss and matcher should be"
              "exactly the same."
          )
          self.assigner = build_assigner(assigner)
          # DETR sampling=False, so use PseudoSampler
          sampler_cfg = dict(type="PseudoSampler")
          self.sampler = build_sampler(sampler_cfg, context=self)
      self.num_classes = num_classes
      self.cls_out_channels = num_classes + 1
      self.in_channels = in_channels
      self.num_fcs = num_fcs
      self.train_cfg = train_cfg
      self.test_cfg = test_cfg
      self.use_sigmoid_cls = use_sigmoid_cls
      self.embed_dims = embed_dims
      self.num_query = test_cfg["max_per_img"]
      self.fp16_enabled = False
      self.loss_cls = build_loss(loss_cls)
      self.loss_bbox = build_loss(loss_bbox)
      self.loss_iou = build_loss(loss_iou)
      self.act_cfg = transformer.get(
          "act_cfg", dict(type="ReLU", inplace=True)
      )
      self.activate = build_activation_layer(self.act_cfg)
      self.positional_encoding = build_positional_encoding(
          positional_encoding
      )
      self.transformer = build_transformer(transformer)
      self._init_layers()
  
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 1 to create a new TransformerHead object from inherited base classes: AnchorFreeHead with example: obj = TransformerHead(num_classes, in_channels, num_fcs, transformer, positional_encoding, loss_cls, loss_bbox, loss_iou, train_cfg, test_cfg)