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_classes (int): Number of VIS classes.
      num_queries (int): Number of query in Transformer decoder.
          Defaults to 100.
      num_transformer_feat_level (int): Number of feats levels.
          Defaults to 3.
      pixel_decoder (:obj:`ConfigDict` or dict): Config for pixel
          decoder.
      enforce_decoder_input_project (bool, optional): 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.
          Defaults to `SinePositionalEncoding3D`.
      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 'CrossEntropyLoss'.
      loss_dice (:obj:`ConfigDict` or dict): Config of the dice loss.
          Defaults to 'DiceLoss'.
      train_cfg (:obj:`ConfigDict` or dict, optional): Training config of
          Mask2Former head. Defaults to None.
      test_cfg (:obj:`ConfigDict` or dict, optional): Testing config of
          Mask2Former head. Defaults to None.
      init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or             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_classes","(","int",")",":","Number","of","VIS","classes",".","num_queries","(","int",")",":","Number","of","query","in","Transformer","decoder",".","Defaults","to","100.","num_transformer_feat_level","(","int",")",":","Number","of","feats","levels",".","Defaults","to","3.","pixel_decoder","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","for","pixel","decoder",".","enforce_decoder_input_project","(","bool",",","optional",")",":","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",".","Defaults","to","`","SinePositionalEncoding3D","`",".","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","'CrossEntropyLoss","'",".","loss_dice","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","the","dice","loss",".","Defaults","to","'DiceLoss","'",".","train_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Training","config","of","Mask2Former","head",".","Defaults","to","None",".","test_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","Testing","config","of","Mask2Former","head",".","Defaults","to","None",".","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, num_frames, h, w).
      gt_instances (:obj:`InstanceData`): It contains ``labels`` and
          ``masks``.
      img_meta (dict): Image informtation.
  
  Returns:
      tuple[Tensor]: 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, num_frames, 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",",","num_frames",",","h",",","w",")",".","gt_instances","(",":","obj",":","`","InstanceData","`",")",":","It","contains","``","labels","``","and","``","masks","``",".","img_meta","(","dict",")",":","Image","informtation",".","Returns",":","tuple","[","Tensor","]",":","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",",","num_frames",",","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, num_frames, h, w).
              gt_instances (:obj:`InstanceData`): It contains ``labels`` and
                  ``masks``.
              img_meta (dict): Image informtation.
  
          Returns:
              tuple[Tensor]: 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, num_frames, 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.
          """
      # (num_gts, )
      gt_labels = gt_instances.labels
      # (num_gts, num_frames, h, w)
      gt_masks = gt_instances.masks
      # sample points
      num_queries = cls_score.shape[0]
      num_gts = gt_labels.shape[0]
  
      point_coords = torch.rand(
          (1, self.num_points, 2), device=cls_score.device
      )
  
      # shape (num_queries, num_points)
      mask_points_pred = point_sample(
          mask_pred, point_coords.repeat(num_queries, 1, 1)
      ).flatten(1)
      # shape (num_gts, num_points)
      gt_points_masks = point_sample(
          gt_masks.float(), point_coords.repeat(num_gts, 1, 1)
      ).flatten(1)
  
      sampled_gt_instances = InstanceData(
          labels=gt_labels, masks=gt_points_masks
      )
      sampled_pred_instances = InstanceData(
          scores=cls_score, masks=mask_points_pred
      )
      # assign and sample
      assign_result = self.assigner.assign(
          pred_instances=sampled_pred_instances,
          gt_instances=sampled_gt_instances,
          img_meta=img_meta,
      )
      pred_instances = InstanceData(scores=cls_score, masks=mask_pred)
      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: import copy
  from collections import defaultdict
  from typing import Dict, List, Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import Conv2d
  from mmcv.ops import point_sample
  from mmengine.model import ModuleList
  from mmengine.model.weight_init import caffe2_xavier_init
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.models.dense_heads import AnchorFreeHead, MaskFormerHead
  from mmdet.models.utils import get_uncertain_point_coords_with_randomness
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures import TrackDataSample, TrackSampleList
  from mmdet.structures.mask import mask2bbox
  from mmdet.utils import ConfigType, InstanceList, OptConfigType, OptMultiConfig, reduce_mean
  from ..layers import Mask2FormerTransformerDecoder
label_desc: Use image node_id 4 for calling the Mask2FormerTrackHead 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