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","."]
docstr: Forward function.
  
  Args:
      feats (list[Tensor]): Multi scale Features from the
          upstream network, each is a 4D-tensor.
      img_metas (list[dict]): List of image information.
  
  Returns:
      tuple: A tuple contains two elements.
  
      - cls_pred_list (list[Tensor)]: Classification logits                 for each decoder layer. Each is a 3D-tensor with shape                 (batch_size, num_queries, cls_out_channels).                 Note `cls_out_channels` should includes background.
      - mask_pred_list (list[Tensor]): Mask logits for each                 decoder layer. Each with shape (batch_size, num_queries,                  h, w).
docstr_tok: ["Forward","function",".","Args",":","feats","(","list","[","Tensor","]",")",":","Multi","scale","Features","from","the","upstream","network",",","each","is","a","4D-tensor",".","img_metas","(","list","[","dict","]",")",":","List","of","image","information",".","Returns",":","tuple",":","A","tuple","contains","two","elements",".","-","cls_pred_list","(","list","[","Tensor",")","]",":","Classification","logits","for","each","decoder","layer",".","Each","is","a","3D-tensor","with","shape","(","batch_size",",","num_queries",",","cls_out_channels",")",".","Note","`","cls_out_channels","`","should","includes","background",".","-","mask_pred_list","(","list","[","Tensor","]",")",":","Mask","logits","for","each","decoder","layer",".","Each","with","shape","(","batch_size",",","num_queries",",","h",",","w",")","."]
code: def forward(self, feats, img_metas):
      """Forward function.
  
          Args:
              feats (list[Tensor]): Multi scale Features from the
                  upstream network, each is a 4D-tensor.
              img_metas (list[dict]): List of image information.
  
          Returns:
              tuple: A tuple contains two elements.
  
              - cls_pred_list (list[Tensor)]: Classification logits \
                  for each decoder layer. Each is a 3D-tensor with shape \
                  (batch_size, num_queries, cls_out_channels). \
                  Note `cls_out_channels` should includes background.
              - mask_pred_list (list[Tensor]): Mask logits for each \
                  decoder layer. Each with shape (batch_size, num_queries, \
                   h, w).
          """
      batch_size = len(img_metas)
      mask_features, multi_scale_memorys = self.pixel_decoder(feats)
      # multi_scale_memorys (from low resolution to high resolution)
      decoder_inputs = []
      decoder_positional_encodings = []
      for i in range(self.num_transformer_feat_level):
          decoder_input = self.decoder_input_projs[i](
              multi_scale_memorys[i]
          )
          # shape (batch_size, c, h, w) -> (h*w, batch_size, c)
          decoder_input = decoder_input.flatten(2).permute(2, 0, 1)
          level_embed = self.level_embed.weight[i].view(1, 1, -1)
          decoder_input = decoder_input + level_embed
          # shape (batch_size, c, h, w) -> (h*w, batch_size, c)
          mask = decoder_input.new_zeros(
              (batch_size,) + multi_scale_memorys[i].shape[-2:],
              dtype=torch.bool,
          )
          decoder_positional_encoding = (
              self.decoder_positional_encoding(mask)
          )
          decoder_positional_encoding = (
              decoder_positional_encoding.flatten(2).permute(2, 0, 1)
          )
          decoder_inputs.append(decoder_input)
          decoder_positional_encodings.append(
              decoder_positional_encoding
          )
      # shape (num_queries, c) -> (num_queries, batch_size, c)
      query_feat = self.query_feat.weight.unsqueeze(1).repeat(
          (1, batch_size, 1)
      )
      query_embed = self.query_embed.weight.unsqueeze(1).repeat(
          (1, batch_size, 1)
      )
  
      cls_pred_list = []
      mask_pred_list = []
      cls_pred, mask_pred, attn_mask = self.forward_head(
          query_feat, mask_features, multi_scale_memorys[0].shape[-2:]
      )
      cls_pred_list.append(cls_pred)
      mask_pred_list.append(mask_pred)
  
      for i in range(self.num_transformer_decoder_layers):
          level_idx = i % self.num_transformer_feat_level
          # if a mask is all True(all background), then set it all False.
          attn_mask[
              torch.where(attn_mask.sum(-1) == attn_mask.shape[-1])
          ] = False
  
          # cross_attn + self_attn
          layer = self.transformer_decoder.layers[i]
          attn_masks = [attn_mask, None]
          query_feat = layer(
              query=query_feat,
              key=decoder_inputs[level_idx],
              value=decoder_inputs[level_idx],
              query_pos=query_embed,
              key_pos=decoder_positional_encodings[level_idx],
              attn_masks=attn_masks,
              query_key_padding_mask=None,
              # here we do not apply masking on padded region
              key_padding_mask=None,
          )
          cls_pred, mask_pred, attn_mask = self.forward_head(
              query_feat,
              mask_features,
              multi_scale_memorys[
                  (i + 1) % self.num_transformer_feat_level
              ].shape[-2:],
          )
  
          cls_pred_list.append(cls_pred)
          mask_pred_list.append(mask_pred)
  
      return cls_pred_list, mask_pred_list
  
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 8 for calling the Mask2FormerHead obj's underlying member method code with example usage: obj.forward(feats, img_metas) and returns: cls_pred_list,  mask_pred_list