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:`mmcv.ConfigDict` | 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:`mmcv.ConfigDict` | dict): Config for
          transformer decoder. Defaults to None.
      positional_encoding (:obj:`mmcv.ConfigDict` | dict): Config for
          transformer decoder position encoding. Defaults to None.
      loss_cls (:obj:`mmcv.ConfigDict` | dict): Config of the classification
          loss. Defaults to None.
      loss_mask (:obj:`mmcv.ConfigDict` | dict): Config of the mask loss.
          Defaults to None.
      loss_dice (:obj:`mmcv.ConfigDict` | dict): Config of the dice loss.
          Defaults to None.
      train_cfg (:obj:`mmcv.ConfigDict` | dict): Training config of
          Mask2Former head.
      test_cfg (:obj:`mmcv.ConfigDict` | dict): Testing config of
          Mask2Former head.
      init_cfg (dict or list[dict], optional): Initialization config dict.
          Defaults to None.
class_docstr_tok: ["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",":","`","mmcv.ConfigDict","`","|","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",":","`","mmcv.ConfigDict","`","|","dict",")",":","Config","for","transformer","decoder",".","Defaults","to","None",".","positional_encoding","(",":","obj",":","`","mmcv.ConfigDict","`","|","dict",")",":","Config","for","transformer","decoder","position","encoding",".","Defaults","to","None",".","loss_cls","(",":","obj",":","`","mmcv.ConfigDict","`","|","dict",")",":","Config","of","the","classification","loss",".","Defaults","to","None",".","loss_mask","(",":","obj",":","`","mmcv.ConfigDict","`","|","dict",")",":","Config","of","the","mask","loss",".","Defaults","to","None",".","loss_dice","(",":","obj",":","`","mmcv.ConfigDict","`","|","dict",")",":","Config","of","the","dice","loss",".","Defaults","to","None",".","train_cfg","(",":","obj",":","`","mmcv.ConfigDict","`","|","dict",")",":","Training","config","of","Mask2Former","head",".","test_cfg","(",":","obj",":","`","mmcv.ConfigDict","`","|","dict",")",":","Testing","config","of","Mask2Former","head",".","init_cfg","(","dict","or","list","[","dict","]",",","optional",")",":","Initialization","config","dict",".","Defaults","to","None","."]
code: def __init__(
      self,
      in_channels,
      feat_channels,
      out_channels,
      num_things_classes=80,
      num_stuff_classes=53,
      num_queries=100,
      num_transformer_feat_level=3,
      pixel_decoder=None,
      enforce_decoder_input_project=False,
      transformer_decoder=None,
      positional_encoding=None,
      loss_cls=None,
      loss_mask=None,
      loss_dice=None,
      train_cfg=None,
      test_cfg=None,
      init_cfg=None,
      **kwargs,
  ):
      super(Mask2FormerHead, self).__init__(
          in_channels=in_channels,
          channels=feat_channels,
          num_classes=(num_things_classes + num_stuff_classes),
          init_cfg=init_cfg,
          input_transform="multiple_select",
          **kwargs,
      )
      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.transformerlayers.attn_cfgs.num_heads
      )
      self.num_transformer_decoder_layers = (
          transformer_decoder.num_layers
      )
      assert (
          pixel_decoder.encoder.transformerlayers.attn_cfgs.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 = build_plugin_layer(pixel_decoder_)[1]
      self.transformer_decoder = build_transformer_layer_sequence(
          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 = build_positional_encoding(
          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.conv_seg = None  # fix a bug here (conv_seg is not used)
  
      self.test_cfg = test_cfg
      self.train_cfg = train_cfg
      if train_cfg:
          self.assigner = build_assigner(self.train_cfg.assigner)
          self.sampler = build_sampler(
              self.train_cfg.sampler, 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 = build_loss(loss_cls)
      self.loss_mask = build_loss(loss_mask)
      self.loss_dice = build_loss(loss_dice)
  
imports: import copy
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import Conv2d, build_plugin_layer, caffe2_xavier_init
  from mmcv.cnn.bricks.transformer import build_positional_encoding, build_transformer_layer_sequence
  from mmcv.ops import point_sample
  from mmcv.runner import ModuleList, force_fp32
  from mmseg.models.builder import HEADS, build_loss
  from mmseg.models.decode_heads.decode_head import BaseDecodeHead
  from ...core import build_sampler, multi_apply, reduce_mean
  from ..builder import build_assigner
  from ..utils import get_uncertain_point_coords_with_randomness
label_desc: Use image node_id 1 to create a new Mask2FormerHead object from inherited base classes: BaseDecodeHead 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)