class_docstr: Implements the MaskFormer head.
  
  See `Per-Pixel Classification is Not All You Need for Semantic
  Segmentation <https://arxiv.org/pdf/2107.06278>`_ for details.
  
  Args:
      in_channels (list[int]): Number of channels in the input feature map.
      feat_channels (int): Number of channels for feature.
      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.
      pixel_decoder (:obj:`ConfigDict` or dict): Config for pixel
          decoder.
      enforce_decoder_input_project (bool): Whether to add a layer
          to change the embed_dim of transformer encoder in pixel decoder to
          the embed_dim of transformer decoder. Defaults to False.
      transformer_decoder (:obj:`ConfigDict` or dict): Config for
          transformer decoder.
      positional_encoding (:obj:`ConfigDict` or dict): Config for
          transformer decoder position encoding.
      loss_cls (:obj:`ConfigDict` or dict): Config of the classification
          loss. Defaults to `CrossEntropyLoss`.
      loss_mask (:obj:`ConfigDict` or dict): Config of the mask loss.
          Defaults to `FocalLoss`.
      loss_dice (:obj:`ConfigDict` or dict): Config of the dice loss.
          Defaults to `DiceLoss`.
      train_cfg (:obj:`ConfigDict` or dict, optional): Training config of
          MaskFormer head.
      test_cfg (:obj:`ConfigDict` or dict, optional): Testing config of
          MaskFormer head.
      init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or             dict], optional): Initialization config dict. Defaults to None.
class_docstr_tok: ["Implements","the","MaskFormer","head",".","See","`","Per-Pixel","Classification","is","Not","All","You","Need","for","Semantic","Segmentation","<","https",":","\/\/arxiv.org\/pdf\/2107.06278",">","`","_","for","details",".","Args",":","in_channels","(","list","[","int","]",")",":","Number","of","channels","in","the","input","feature","map",".","feat_channels","(","int",")",":","Number","of","channels","for","feature",".","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",".","pixel_decoder","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","for","pixel","decoder",".","enforce_decoder_input_project","(","bool",")",":","Whether","to","add","a","layer","to","change","the","embed_dim","of","transformer","encoder","in","pixel","decoder","to","the","embed_dim","of","transformer","decoder",".","Defaults","to","False",".","transformer_decoder","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","for","transformer","decoder",".","positional_encoding","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","for","transformer","decoder","position","encoding",".","loss_cls","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","the","classification","loss",".","Defaults","to","`","CrossEntropyLoss","`",".","loss_mask","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","the","mask","loss",".","Defaults","to","`","FocalLoss","`",".","loss_dice","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","the","dice","loss",".","Defaults","to","`","DiceLoss","`",".","train_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Training","config","of","MaskFormer","head",".","test_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Testing","config","of","MaskFormer","head",".","init_cfg","(",":","obj",":","`","ConfigDict","`","or","dict","or","list","[",":","obj",":","`","ConfigDict","`","or","dict","]",",","optional",")",":","Initialization","config","dict",".","Defaults","to","None","."]
docstr: Forward function.
  
  Args:
      x (tuple[Tensor]): Features from the upstream network, each
          is a 4D-tensor.
      batch_data_samples (List[:obj:`DetDataSample`]): The Data
          Samples. It usually includes information such as
          `gt_instance`, `gt_panoptic_seg` and `gt_sem_seg`.
  
  Returns:
      tuple[Tensor]: a tuple contains two elements.
  
          - all_cls_scores (Tensor): Classification scores for each                    scale level. Each is a 4D-tensor with shape                    (num_decoder, batch_size, num_queries, cls_out_channels).                    Note `cls_out_channels` should includes background.
          - all_mask_preds (Tensor): Mask scores for each decoder                    layer. Each with shape (num_decoder, batch_size,                    num_queries, h, w).
docstr_tok: ["Forward","function",".","Args",":","x","(","tuple","[","Tensor","]",")",":","Features","from","the","upstream","network",",","each","is","a","4D-tensor",".","batch_data_samples","(","List","[",":","obj",":","`","DetDataSample","`","]",")",":","The","Data","Samples",".","It","usually","includes","information","such","as","`","gt_instance","`",",","`","gt_panoptic_seg","`","and","`","gt_sem_seg","`",".","Returns",":","tuple","[","Tensor","]",":","a","tuple","contains","two","elements",".","-","all_cls_scores","(","Tensor",")",":","Classification","scores","for","each","scale","level",".","Each","is","a","4D-tensor","with","shape","(","num_decoder",",","batch_size",",","num_queries",",","cls_out_channels",")",".","Note","`","cls_out_channels","`","should","includes","background",".","-","all_mask_preds","(","Tensor",")",":","Mask","scores","for","each","decoder","layer",".","Each","with","shape","(","num_decoder",",","batch_size",",","num_queries",",","h",",","w",")","."]
code: def forward(
      self, x: Tuple[Tensor], batch_data_samples: SampleList
  ) -> Tuple[Tensor]:
      """Forward function.
  
          Args:
              x (tuple[Tensor]): Features from the upstream network, each
                  is a 4D-tensor.
              batch_data_samples (List[:obj:`DetDataSample`]): The Data
                  Samples. It usually includes information such as
                  `gt_instance`, `gt_panoptic_seg` and `gt_sem_seg`.
  
          Returns:
              tuple[Tensor]: a tuple contains two elements.
  
                  - all_cls_scores (Tensor): Classification scores for each\
                      scale level. Each is a 4D-tensor with shape\
                      (num_decoder, batch_size, num_queries, cls_out_channels).\
                      Note `cls_out_channels` should includes background.
                  - all_mask_preds (Tensor): Mask scores for each decoder\
                      layer. Each with shape (num_decoder, batch_size,\
                      num_queries, h, w).
          """
      batch_img_metas = [
          data_sample.metainfo for data_sample in batch_data_samples
      ]
      batch_size = x[0].shape[0]
      input_img_h, input_img_w = batch_img_metas[0]["batch_input_shape"]
      padding_mask = x[-1].new_ones(
          (batch_size, input_img_h, input_img_w), dtype=torch.float32
      )
      for i in range(batch_size):
          img_h, img_w = batch_img_metas[i]["img_shape"]
          padding_mask[i, :img_h, :img_w] = 0
      padding_mask = (
          F.interpolate(
              padding_mask.unsqueeze(1),
              size=x[-1].shape[-2:],
              mode="nearest",
          )
          .to(torch.bool)
          .squeeze(1)
      )
      # when backbone is swin, memory is output of last stage of swin.
      # when backbone is r50, memory is output of tranformer encoder.
      mask_features, memory = self.pixel_decoder(x, batch_img_metas)
      pos_embed = self.decoder_pe(padding_mask)
      memory = self.decoder_input_proj(memory)
      # shape (batch_size, c, h, w) -> (batch_size, h*w, c)
      memory = memory.flatten(2).permute(0, 2, 1)
      pos_embed = pos_embed.flatten(2).permute(0, 2, 1)
      # shape (batch_size, h * w)
      padding_mask = padding_mask.flatten(1)
      # shape = (num_queries, embed_dims)
      query_embed = self.query_embed.weight
      # shape = (batch_size, num_queries, embed_dims)
      query_embed = query_embed.unsqueeze(0).repeat(batch_size, 1, 1)
      target = torch.zeros_like(query_embed)
      # shape (num_decoder, num_queries, batch_size, embed_dims)
      out_dec = self.transformer_decoder(
          query=target,
          key=memory,
          value=memory,
          query_pos=query_embed,
          key_pos=pos_embed,
          key_padding_mask=padding_mask,
      )
  
      # cls_scores
      all_cls_scores = self.cls_embed(out_dec)
  
      # mask_preds
      mask_embed = self.mask_embed(out_dec)
      all_mask_preds = torch.einsum(
          "lbqc,bchw->lbqhw", mask_embed, mask_features
      )
  
      return all_cls_scores, all_mask_preds
  
imports: from typing import Dict, List, Optional, Tuple, Union
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import Conv2d
  from mmengine.model import caffe2_xavier_init
  from mmengine.structures import InstanceData, PixelData
  from torch import Tensor
  from mmdet.models.layers.pixel_decoder import PixelDecoder
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures import SampleList
  from mmdet.utils import ConfigType, InstanceList, OptConfigType, OptMultiConfig, reduce_mean
  from ..layers import DetrTransformerDecoder, SinePositionalEncoding
  from ..utils import multi_apply, preprocess_panoptic_gt
  from .anchor_free_head import AnchorFreeHead
label_desc: Use image node_id 8 for calling the MaskFormerHead obj's underlying member method code with example usage: obj.forward(x, batch_data_samples) and returns: all_cls_scores,  all_mask_preds