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: Preprocess the ground truth for all images.
  
  It aims to reorganize the `gt`. For example, in the
  `batch_data_sample.gt_instances.mask`, its shape is
  `(all_num_gts, h, w)`, but we don't know each gt belongs to which `img`
  (assume `num_frames` is 2). So, this func used to reshape the `gt_mask`
  to `(num_gts_per_img, num_frames, h, w)`. In addition, we can't
  guarantee that the number of instances in these two images is equal,
  so `-1` refers to nonexistent instances.
  
  Args:
      batch_gt_instances (list[:obj:`InstanceData`]): Batch of
          gt_instance. It usually includes ``labels``, each is
          ground truth labels of each bbox, with shape (num_gts, )
          and ``masks``, each is ground truth masks of each instances
          of an image, shape (num_gts, h, w).
  
  Returns:
      list[obj:`InstanceData`]: each contains the following keys
  
          - labels (Tensor): Ground truth class indices                    for an image, with shape (n, ), n is the sum of                    number of stuff type and number of instance in an image.
          - masks (Tensor): Ground truth mask for a                    image, with shape (n, t, h, w).
docstr_tok: ["Preprocess","the","ground","truth","for","all","images",".","It","aims","to","reorganize","the","`","gt","`",".","For","example",",","in","the","`","batch_data_sample.gt_instances.mask","`",",","its","shape","is","`","(","all_num_gts",",","h",",","w",")","`",",","but","we","do","n't","know","each","gt","belongs","to","which","`","img","`","(","assume","`","num_frames","`","is","2",")",".","So",",","this","func","used","to","reshape","the","`","gt_mask","`","to","`","(","num_gts_per_img",",","num_frames",",","h",",","w",")","`",".","In","addition",",","we","can't","guarantee","that","the","number","of","instances","in","these","two","images","is","equal",",","so","`","-1","`","refers","to","nonexistent","instances",".","Args",":","batch_gt_instances","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","Batch","of","gt_instance",".","It","usually","includes","``","labels","``",",","each","is","ground","truth","labels","of","each","bbox",",","with","shape","(","num_gts",",",")","and","``","masks","``",",","each","is","ground","truth","masks","of","each","instances","of","an","image",",","shape","(","num_gts",",","h",",","w",")",".","Returns",":","list","[","obj",":","`","InstanceData","`","]",":","each","contains","the","following","keys","-","labels","(","Tensor",")",":","Ground","truth","class","indices","for","an","image",",","with","shape","(","n",",",")",",","n","is","the","sum","of","number","of","stuff","type","and","number","of","instance","in","an","image",".","-","masks","(","Tensor",")",":","Ground","truth","mask","for","a","image",",","with","shape","(","n",",","t",",","h",",","w",")","."]
code: def preprocess_gt(
      self, batch_gt_instances: InstanceList
  ) -> InstanceList:
      """Preprocess the ground truth for all images.
  
          It aims to reorganize the `gt`. For example, in the
          `batch_data_sample.gt_instances.mask`, its shape is
          `(all_num_gts, h, w)`, but we don't know each gt belongs to which `img`
          (assume `num_frames` is 2). So, this func used to reshape the `gt_mask`
          to `(num_gts_per_img, num_frames, h, w)`. In addition, we can't
          guarantee that the number of instances in these two images is equal,
          so `-1` refers to nonexistent instances.
  
          Args:
              batch_gt_instances (list[:obj:`InstanceData`]): Batch of
                  gt_instance. It usually includes ``labels``, each is
                  ground truth labels of each bbox, with shape (num_gts, )
                  and ``masks``, each is ground truth masks of each instances
                  of an image, shape (num_gts, h, w).
  
          Returns:
              list[obj:`InstanceData`]: each contains the following keys
  
                  - labels (Tensor): Ground truth class indices\
                      for an image, with shape (n, ), n is the sum of\
                      number of stuff type and number of instance in an image.
                  - masks (Tensor): Ground truth mask for a\
                      image, with shape (n, t, h, w).
          """
      final_batch_gt_instances = []
      batch_size = len(batch_gt_instances) // self.num_frames
      for batch_idx in range(batch_size):
          pair_gt_insatences = batch_gt_instances[
              batch_idx * self.num_frames : batch_idx * self.num_frames
              + self.num_frames
          ]
  
          assert (
              len(pair_gt_insatences) > 1
          ), f"mask2former for vis need multi frames to train, \
                  but you only use {len(pair_gt_insatences)} frames"
  
          _device = pair_gt_insatences[0].labels.device
  
          for gt_instances in pair_gt_insatences:
              gt_instances.masks = gt_instances.masks.to_tensor(
                  dtype=torch.bool, device=_device
              )
          all_ins_id = torch.cat(
              [
                  gt_instances.instances_ids
                  for gt_instances in pair_gt_insatences
              ]
          )
          all_ins_id = all_ins_id.unique().tolist()
          map_ins_id = dict()
          for i, ins_id in enumerate(all_ins_id):
              map_ins_id[ins_id] = i
  
          num_instances = len(all_ins_id)
          mask_shape = [
              num_instances,
              self.num_frames,
              pair_gt_insatences[0].masks.shape[1],
              pair_gt_insatences[0].masks.shape[2],
          ]
          gt_masks_per_video = torch.zeros(
              mask_shape, dtype=torch.bool, device=_device
          )
          gt_ids_per_video = torch.full(
              (num_instances, self.num_frames),
              -1,
              dtype=torch.long,
              device=_device,
          )
          gt_labels_per_video = torch.full(
              (num_instances,), -1, dtype=torch.long, device=_device
          )
  
          for frame_id in range(self.num_frames):
              cur_frame_gts = pair_gt_insatences[frame_id]
              ins_ids = cur_frame_gts.instances_ids.tolist()
              for i, id in enumerate(ins_ids):
                  gt_masks_per_video[
                      map_ins_id[id], frame_id, :, :
                  ] = cur_frame_gts.masks[i]
                  gt_ids_per_video[
                      map_ins_id[id], frame_id
                  ] = cur_frame_gts.instances_ids[i]
                  gt_labels_per_video[
                      map_ins_id[id]
                  ] = cur_frame_gts.labels[i]
  
          tmp_instances = InstanceData(
              labels=gt_labels_per_video,
              masks=gt_masks_per_video.long(),
              instances_id=gt_ids_per_video,
          )
          final_batch_gt_instances.append(tmp_instances)
  
      return final_batch_gt_instances
  
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 3 for calling the Mask2FormerTrackHead obj's underlying member method code with example usage: obj.preprocess_gt(batch_gt_instances) and returns: final_batch_gt_instances