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: Compute classification and mask targets for one image.
  
  Args:
      cls_score (Tensor): Mask score logits from a single decoder layer
          for one image. Shape (num_queries, cls_out_channels).
      mask_pred (Tensor): Mask logits for a single decoder layer for one
          image. Shape (num_queries, h, w).
      gt_instances (:obj:`InstanceData`): It contains ``labels`` and
          ``masks``.
      img_meta (dict): Image informtation.
  
  Returns:
      tuple: a tuple containing the following for one image.
  
          - labels (Tensor): Labels of each image.
              shape (num_queries, ).
          - label_weights (Tensor): Label weights of each image.
              shape (num_queries, ).
          - mask_targets (Tensor): Mask targets of each image.
              shape (num_queries, h, w).
          - mask_weights (Tensor): Mask weights of each image.
              shape (num_queries, ).
          - pos_inds (Tensor): Sampled positive indices for each image.
          - neg_inds (Tensor): Sampled negative indices for each image.
          - sampling_result (:obj:`SamplingResult`): Sampling results.
docstr_tok: ["Compute","classification","and","mask","targets","for","one","image",".","Args",":","cls_score","(","Tensor",")",":","Mask","score","logits","from","a","single","decoder","layer","for","one","image",".","Shape","(","num_queries",",","cls_out_channels",")",".","mask_pred","(","Tensor",")",":","Mask","logits","for","a","single","decoder","layer","for","one","image",".","Shape","(","num_queries",",","h",",","w",")",".","gt_instances","(",":","obj",":","`","InstanceData","`",")",":","It","contains","``","labels","``","and","``","masks","``",".","img_meta","(","dict",")",":","Image","informtation",".","Returns",":","tuple",":","a","tuple","containing","the","following","for","one","image",".","-","labels","(","Tensor",")",":","Labels","of","each","image",".","shape","(","num_queries",",",")",".","-","label_weights","(","Tensor",")",":","Label","weights","of","each","image",".","shape","(","num_queries",",",")",".","-","mask_targets","(","Tensor",")",":","Mask","targets","of","each","image",".","shape","(","num_queries",",","h",",","w",")",".","-","mask_weights","(","Tensor",")",":","Mask","weights","of","each","image",".","shape","(","num_queries",",",")",".","-","pos_inds","(","Tensor",")",":","Sampled","positive","indices","for","each","image",".","-","neg_inds","(","Tensor",")",":","Sampled","negative","indices","for","each","image",".","-","sampling_result","(",":","obj",":","`","SamplingResult","`",")",":","Sampling","results","."]
code: def _get_targets_single(
      self,
      cls_score: Tensor,
      mask_pred: Tensor,
      gt_instances: InstanceData,
      img_meta: dict,
  ) -> Tuple[Tensor]:
      """Compute classification and mask targets for one image.
  
      Args:
          cls_score (Tensor): Mask score logits from a single decoder layer
              for one image. Shape (num_queries, cls_out_channels).
          mask_pred (Tensor): Mask logits for a single decoder layer for one
              image. Shape (num_queries, h, w).
          gt_instances (:obj:`InstanceData`): It contains ``labels`` and
              ``masks``.
          img_meta (dict): Image informtation.
  
      Returns:
          tuple: a tuple containing the following for one image.
  
              - labels (Tensor): Labels of each image.
                  shape (num_queries, ).
              - label_weights (Tensor): Label weights of each image.
                  shape (num_queries, ).
              - mask_targets (Tensor): Mask targets of each image.
                  shape (num_queries, h, w).
              - mask_weights (Tensor): Mask weights of each image.
                  shape (num_queries, ).
              - pos_inds (Tensor): Sampled positive indices for each image.
              - neg_inds (Tensor): Sampled negative indices for each image.
              - sampling_result (:obj:`SamplingResult`): Sampling results.
      """
      gt_masks = gt_instances.masks
      gt_labels = gt_instances.labels
  
      target_shape = mask_pred.shape[-2:]
      if gt_masks.shape[0] > 0:
          gt_masks_downsampled = (
              F.interpolate(
                  gt_masks.unsqueeze(1).float(),
                  target_shape,
                  mode="nearest",
              )
              .squeeze(1)
              .long()
          )
      else:
          gt_masks_downsampled = gt_masks
  
      pred_instances = InstanceData(scores=cls_score, masks=mask_pred)
      downsampled_gt_instances = InstanceData(
          labels=gt_labels, masks=gt_masks_downsampled
      )
      # assign and sample
      assign_result = self.assigner.assign(
          pred_instances=pred_instances,
          gt_instances=downsampled_gt_instances,
          img_meta=img_meta,
      )
      sampling_result = self.sampler.sample(
          assign_result=assign_result,
          pred_instances=pred_instances,
          gt_instances=gt_instances,
      )
      pos_inds = sampling_result.pos_inds
      neg_inds = sampling_result.neg_inds
  
      # label target
      labels = gt_labels.new_full(
          (self.num_queries,), self.num_classes, dtype=torch.long
      )
      labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds]
      label_weights = gt_labels.new_ones(self.num_queries)
  
      # mask target
      mask_targets = gt_masks[sampling_result.pos_assigned_gt_inds]
      mask_weights = mask_pred.new_zeros((self.num_queries,))
      mask_weights[pos_inds] = 1.0
  
      return (
          labels,
          label_weights,
          mask_targets,
          mask_weights,
          pos_inds,
          neg_inds,
          sampling_result,
      )
  
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 5 for calling the MaskFormerHead obj's underlying member method code with example usage: obj._get_targets_single(cls_score, mask_pred, gt_instances, img_meta) and returns: labels,  label_weights,  mask_targets,  mask_weights,  pos_inds,  neg_inds,  sampling_result