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: Loss function.
  
  Args:
      all_cls_scores (Tensor): Classification scores for all decoder
          layers 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 all decoder layers with
          shape (num_decoder, batch_size, num_queries, h, w).
      batch_gt_instances (list[obj:`InstanceData`]): each contains
          ``labels`` and ``masks``.
      batch_img_metas (list[dict]): List of image meta information.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Loss","function",".","Args",":","all_cls_scores","(","Tensor",")",":","Classification","scores","for","all","decoder","layers","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","all","decoder","layers","with","shape","(","num_decoder",",","batch_size",",","num_queries",",","h",",","w",")",".","batch_gt_instances","(","list","[","obj",":","`","InstanceData","`","]",")",":","each","contains","``","labels","``","and","``","masks","``",".","batch_img_metas","(","list","[","dict","]",")",":","List","of","image","meta","information",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss_by_feat(
      self,
      all_cls_scores: Tensor,
      all_mask_preds: Tensor,
      batch_gt_instances: List[InstanceData],
      batch_img_metas: List[dict],
  ) -> Dict[str, Tensor]:
      """Loss function.
  
      Args:
          all_cls_scores (Tensor): Classification scores for all decoder
              layers 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 all decoder layers with
              shape (num_decoder, batch_size, num_queries, h, w).
          batch_gt_instances (list[obj:`InstanceData`]): each contains
              ``labels`` and ``masks``.
          batch_img_metas (list[dict]): List of image meta information.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
      num_dec_layers = len(all_cls_scores)
      batch_gt_instances_list = [
          batch_gt_instances for _ in range(num_dec_layers)
      ]
      img_metas_list = [batch_img_metas for _ in range(num_dec_layers)]
      losses_cls, losses_mask, losses_dice = multi_apply(
          self._loss_by_feat_single,
          all_cls_scores,
          all_mask_preds,
          batch_gt_instances_list,
          img_metas_list,
      )
  
      loss_dict = dict()
      # loss from the last decoder layer
      loss_dict["loss_cls"] = losses_cls[-1]
      loss_dict["loss_mask"] = losses_mask[-1]
      loss_dict["loss_dice"] = losses_dice[-1]
      # loss from other decoder layers
      num_dec_layer = 0
      for loss_cls_i, loss_mask_i, loss_dice_i in zip(
          losses_cls[:-1], losses_mask[:-1], losses_dice[:-1]
      ):
          loss_dict[f"d{num_dec_layer}.loss_cls"] = loss_cls_i
          loss_dict[f"d{num_dec_layer}.loss_mask"] = loss_mask_i
          loss_dict[f"d{num_dec_layer}.loss_dice"] = loss_dice_i
          num_dec_layer += 1
      return loss_dict
  
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 6 for calling the MaskFormerHead obj's underlying member method code with example usage: obj.loss_by_feat(all_cls_scores, all_mask_preds, batch_gt_instances, batch_img_metas) and returns: loss_dict