class_docstr: Implements the Mask2Former head.
  
  See `Masked-attention Mask Transformer for Universal Image
  Segmentation <https://arxiv.org/pdf/2112.01527>`_ for details.
  
  Args:
      in_channels (list[int]): Number of channels in the input feature map.
      feat_channels (int): Number of channels for features.
      out_channels (int): Number of channels for output.
      num_things_classes (int): Number of things.
      num_stuff_classes (int): Number of stuff.
      num_queries (int): Number of query in Transformer decoder.
      pixel_decoder (:obj:`ConfigDict` or dict): Config for pixel
          decoder. Defaults to None.
      enforce_decoder_input_project (bool, optional): Whether to add
          a layer to change the embed_dim of tranformer encoder in
          pixel decoder to the embed_dim of transformer decoder.
          Defaults to False.
      transformer_decoder (:obj:`ConfigDict` or dict): Config for
          transformer decoder. Defaults to None.
      positional_encoding (:obj:`ConfigDict` or dict): Config for
          transformer decoder position encoding. Defaults to
          dict(num_feats=128, normalize=True).
      loss_cls (:obj:`ConfigDict` or dict): Config of the classification
          loss. Defaults to None.
      loss_mask (:obj:`ConfigDict` or dict): Config of the mask loss.
          Defaults to None.
      loss_dice (:obj:`ConfigDict` or dict): Config of the dice loss.
          Defaults to None.
      train_cfg (:obj:`ConfigDict` or dict, optional): Training config of
          Mask2Former head.
      test_cfg (:obj:`ConfigDict` or dict, optional): Testing config of
          Mask2Former head.
      init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or             dict], optional): Initialization config dict. Defaults to None.
code: def __init__(
      self,
      in_channels: List[int],
      feat_channels: int,
      out_channels: int,
      num_things_classes: int = 80,
      num_stuff_classes: int = 53,
      num_queries: int = 100,
      num_transformer_feat_level: int = 3,
      pixel_decoder: ConfigType = ...,
      enforce_decoder_input_project: bool = False,
      transformer_decoder: ConfigType = ...,
      positional_encoding: ConfigType = dict(
          num_feats=128, normalize=True
      ),
      loss_cls: ConfigType = dict(
          type="CrossEntropyLoss",
          use_sigmoid=False,
          loss_weight=2.0,
          reduction="mean",
          class_weight=[1.0] * 133 + [0.1],
      ),
      loss_mask: ConfigType = dict(
          type="CrossEntropyLoss",
          use_sigmoid=True,
          reduction="mean",
          loss_weight=5.0,
      ),
      loss_dice: ConfigType = dict(
          type="DiceLoss",
          use_sigmoid=True,
          activate=True,
          reduction="mean",
          naive_dice=True,
          eps=1.0,
          loss_weight=5.0,
      ),
      train_cfg: OptConfigType = None,
      test_cfg: OptConfigType = None,
      init_cfg: OptMultiConfig = None,
      **kwargs,
  ) -> None:
      super(AnchorFreeHead, self).__init__(init_cfg=init_cfg)
      self.num_things_classes = num_things_classes
      self.num_stuff_classes = num_stuff_classes
      self.num_classes = (
          self.num_things_classes + self.num_stuff_classes
      )
      self.num_queries = num_queries
      self.num_transformer_feat_level = num_transformer_feat_level
      self.num_heads = (
          transformer_decoder.layer_cfg.cross_attn_cfg.num_heads
      )
      self.num_transformer_decoder_layers = (
          transformer_decoder.num_layers
      )
      assert (
          pixel_decoder.encoder.layer_cfg.self_attn_cfg.num_levels
          == num_transformer_feat_level
      )
      pixel_decoder_ = copy.deepcopy(pixel_decoder)
      pixel_decoder_.update(
          in_channels=in_channels,
          feat_channels=feat_channels,
          out_channels=out_channels,
      )
      self.pixel_decoder = MODELS.build(pixel_decoder_)
      self.transformer_decoder = Mask2FormerTransformerDecoder(
          **transformer_decoder
      )
      self.decoder_embed_dims = self.transformer_decoder.embed_dims
  
      self.decoder_input_projs = ModuleList()
      # from low resolution to high resolution
      for _ in range(num_transformer_feat_level):
          if (
              self.decoder_embed_dims != feat_channels
              or enforce_decoder_input_project
          ):
              self.decoder_input_projs.append(
                  Conv2d(
                      feat_channels,
                      self.decoder_embed_dims,
                      kernel_size=1,
                  )
              )
          else:
              self.decoder_input_projs.append(nn.Identity())
      self.decoder_positional_encoding = SinePositionalEncoding(
          **positional_encoding
      )
      self.query_embed = nn.Embedding(self.num_queries, feat_channels)
      self.query_feat = nn.Embedding(self.num_queries, feat_channels)
      # from low resolution to high resolution
      self.level_embed = nn.Embedding(
          self.num_transformer_feat_level, feat_channels
      )
  
      self.cls_embed = nn.Linear(feat_channels, self.num_classes + 1)
      self.mask_embed = nn.Sequential(
          nn.Linear(feat_channels, feat_channels),
          nn.ReLU(inplace=True),
          nn.Linear(feat_channels, feat_channels),
          nn.ReLU(inplace=True),
          nn.Linear(feat_channels, out_channels),
      )
  
      self.test_cfg = test_cfg
      self.train_cfg = train_cfg
      if train_cfg:
          self.assigner = TASK_UTILS.build(self.train_cfg["assigner"])
          self.sampler = TASK_UTILS.build(
              self.train_cfg["sampler"], default_args=dict(context=self)
          )
          self.num_points = self.train_cfg.get("num_points", 12544)
          self.oversample_ratio = self.train_cfg.get(
              "oversample_ratio", 3.0
          )
          self.importance_sample_ratio = self.train_cfg.get(
              "importance_sample_ratio", 0.75
          )
  
      self.class_weight = loss_cls.class_weight
      self.loss_cls = MODELS.build(loss_cls)
      self.loss_mask = MODELS.build(loss_mask)
      self.loss_dice = MODELS.build(loss_dice)
  
imports: import copy
  from typing import List, Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import Conv2d
  from mmcv.ops import point_sample
  from mmengine.model import ModuleList, caffe2_xavier_init
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures import SampleList
  from mmdet.utils import ConfigType, OptConfigType, OptMultiConfig, reduce_mean
  from ..layers import Mask2FormerTransformerDecoder, SinePositionalEncoding
  from ..utils import get_uncertain_point_coords_with_randomness
  from .anchor_free_head import AnchorFreeHead
  from .maskformer_head import MaskFormerHead
label_desc: Use image node_id 1 to create a new Mask2FormerHead object from inherited base classes: MaskFormerHead with example: obj = Mask2FormerHead(in_channels, feat_channels, out_channels, num_things_classes, num_stuff_classes, num_queries, num_transformer_feat_level, pixel_decoder, enforce_decoder_input_project, transformer_decoder, positional_encoding, loss_cls, loss_mask, loss_dice, train_cfg, test_cfg, init_cfg)