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 for outputs from a single decoder layer.
  
  Args:
      cls_scores (Tensor): Mask score logits from a single decoder layer
          for all images. Shape (batch_size, num_queries,
          cls_out_channels). Note `cls_out_channels` should includes
          background.
      mask_preds (Tensor): Mask logits for a pixel decoder for all
          images. Shape (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:
      tuple[Tensor]: Loss components for outputs from a single decoder                layer.
docstr_tok: ["Loss","function","for","outputs","from","a","single","decoder","layer",".","Args",":","cls_scores","(","Tensor",")",":","Mask","score","logits","from","a","single","decoder","layer","for","all","images",".","Shape","(","batch_size",",","num_queries",",","cls_out_channels",")",".","Note","`","cls_out_channels","`","should","includes","background",".","mask_preds","(","Tensor",")",":","Mask","logits","for","a","pixel","decoder","for","all","images",".","Shape","(","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",":","tuple","[","Tensor","]",":","Loss","components","for","outputs","from","a","single","decoder","layer","."]
code: def _loss_by_feat_single(
      self,
      cls_scores: Tensor,
      mask_preds: Tensor,
      batch_gt_instances: List[InstanceData],
      batch_img_metas: List[dict],
  ) -> Tuple[Tensor]:
      """Loss function for outputs from a single decoder layer.
  
          Args:
              cls_scores (Tensor): Mask score logits from a single decoder layer
                  for all images. Shape (batch_size, num_queries,
                  cls_out_channels). Note `cls_out_channels` should includes
                  background.
              mask_preds (Tensor): Mask logits for a pixel decoder for all
                  images. Shape (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:
              tuple[Tensor]: Loss components for outputs from a single decoder\
                  layer.
          """
      num_imgs = cls_scores.size(0)
      cls_scores_list = [cls_scores[i] for i in range(num_imgs)]
      mask_preds_list = [mask_preds[i] for i in range(num_imgs)]
  
      (
          labels_list,
          label_weights_list,
          mask_targets_list,
          mask_weights_list,
          avg_factor,
      ) = self.get_targets(
          cls_scores_list,
          mask_preds_list,
          batch_gt_instances,
          batch_img_metas,
      )
      # shape (batch_size, num_queries)
      labels = torch.stack(labels_list, dim=0)
      # shape (batch_size, num_queries)
      label_weights = torch.stack(label_weights_list, dim=0)
      # shape (num_total_gts, h, w)
      mask_targets = torch.cat(mask_targets_list, dim=0)
      # shape (batch_size, num_queries)
      mask_weights = torch.stack(mask_weights_list, dim=0)
  
      # classfication loss
      # shape (batch_size * num_queries, )
      cls_scores = cls_scores.flatten(0, 1)
      labels = labels.flatten(0, 1)
      label_weights = label_weights.flatten(0, 1)
  
      class_weight = cls_scores.new_tensor(self.class_weight)
      loss_cls = self.loss_cls(
          cls_scores,
          labels,
          label_weights,
          avg_factor=class_weight[labels].sum(),
      )
  
      num_total_masks = reduce_mean(cls_scores.new_tensor([avg_factor]))
      num_total_masks = max(num_total_masks, 1)
  
      # extract positive ones
      # shape (batch_size, num_queries, h, w) -> (num_total_gts, h, w)
      mask_preds = mask_preds[mask_weights > 0]
      target_shape = mask_targets.shape[-2:]
  
      if mask_targets.shape[0] == 0:
          # zero match
          loss_dice = mask_preds.sum()
          loss_mask = mask_preds.sum()
          return loss_cls, loss_mask, loss_dice
  
      # upsample to shape of target
      # shape (num_total_gts, h, w)
      mask_preds = F.interpolate(
          mask_preds.unsqueeze(1),
          target_shape,
          mode="bilinear",
          align_corners=False,
      ).squeeze(1)
  
      # dice loss
      loss_dice = self.loss_dice(
          mask_preds, mask_targets, avg_factor=num_total_masks
      )
  
      # mask loss
      # FocalLoss support input of shape (n, num_class)
      h, w = mask_preds.shape[-2:]
      # shape (num_total_gts, h, w) -> (num_total_gts * h * w, 1)
      mask_preds = mask_preds.reshape(-1, 1)
      # shape (num_total_gts, h, w) -> (num_total_gts * h * w)
      mask_targets = mask_targets.reshape(-1)
      # target is (1 - mask_targets) !!!
      loss_mask = self.loss_mask(
          mask_preds,
          1 - mask_targets,
          avg_factor=num_total_masks * h * w,
      )
  
      return loss_cls, loss_mask, loss_dice
  
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 7 for calling the MaskFormerHead obj's underlying member method code with example usage: obj._loss_by_feat_single(cls_scores, mask_preds, batch_gt_instances, batch_img_metas) and returns: loss_cls,  loss_mask,  loss_dice, loss_cls,  loss_mask,  loss_dice