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: Forward function.
  
  Args:
      x (list[Tensor]): Multi scale Features from the
          upstream network, each is a 4D-tensor.
      data_samples (List[:obj:`TrackDataSample`]): The Data
          Samples. It usually includes information such as `gt_instance`.
  
  Returns:
      tuple[list[Tensor]]: A tuple contains two elements.
  
          - cls_pred_list (list[Tensor)]: Classification logits                     for each decoder layer. Each is a 3D-tensor with shape                     (batch_size, num_queries, cls_out_channels).                     Note `cls_out_channels` should include background.
          - mask_pred_list (list[Tensor]): Mask logits for each                     decoder layer. Each with shape (batch_size, num_queries,                     h, w).
docstr_tok: ["Forward","function",".","Args",":","x","(","list","[","Tensor","]",")",":","Multi","scale","Features","from","the","upstream","network",",","each","is","a","4D-tensor",".","data_samples","(","List","[",":","obj",":","`","TrackDataSample","`","]",")",":","The","Data","Samples",".","It","usually","includes","information","such","as","`","gt_instance","`",".","Returns",":","tuple","[","list","[","Tensor","]","]",":","A","tuple","contains","two","elements",".","-","cls_pred_list","(","list","[","Tensor",")","]",":","Classification","logits","for","each","decoder","layer",".","Each","is","a","3D-tensor","with","shape","(","batch_size",",","num_queries",",","cls_out_channels",")",".","Note","`","cls_out_channels","`","should","include","background",".","-","mask_pred_list","(","list","[","Tensor","]",")",":","Mask","logits","for","each","decoder","layer",".","Each","with","shape","(","batch_size",",","num_queries",",","h",",","w",")","."]
code: def forward(
      self, x: List[Tensor], data_samples: TrackDataSample
  ) -> Tuple[List[Tensor], List[Tensor]]:
      """Forward function.
  
          Args:
              x (list[Tensor]): Multi scale Features from the
                  upstream network, each is a 4D-tensor.
              data_samples (List[:obj:`TrackDataSample`]): The Data
                  Samples. It usually includes information such as `gt_instance`.
  
          Returns:
              tuple[list[Tensor]]: A tuple contains two elements.
  
                  - cls_pred_list (list[Tensor)]: Classification logits \
                      for each decoder layer. Each is a 3D-tensor with shape \
                      (batch_size, num_queries, cls_out_channels). \
                      Note `cls_out_channels` should include background.
                  - mask_pred_list (list[Tensor]): Mask logits for each \
                      decoder layer. Each with shape (batch_size, num_queries, \
                      h, w).
          """
      mask_features, multi_scale_memorys = self.pixel_decoder(x)
      bt, c_m, h_m, w_m = mask_features.shape
      batch_size = bt // self.num_frames if self.training else 1
      t = bt // batch_size
      mask_features = mask_features.view(batch_size, t, c_m, h_m, w_m)
      # multi_scale_memorys (from low resolution to high resolution)
      decoder_inputs = []
      decoder_positional_encodings = []
      for i in range(self.num_transformer_feat_level):
          decoder_input = self.decoder_input_projs[i](
              multi_scale_memorys[i]
          )
          decoder_input = decoder_input.flatten(2)
          level_embed = self.level_embed.weight[i][None, :, None]
          decoder_input = decoder_input + level_embed
          _, c, hw = decoder_input.shape
          # shape (batch_size*t, c, h, w) ->
          # (batch_size, t, c, hw) ->
          # (batch_size, t*h*w, c)
          decoder_input = (
              decoder_input.view(batch_size, t, c, hw)
              .permute(0, 1, 3, 2)
              .flatten(1, 2)
          )
          # shape (batch_size, c, h, w) -> (h*w, batch_size, c)
          mask = decoder_input.new_zeros(
              (batch_size, t) + multi_scale_memorys[i].shape[-2:],
              dtype=torch.bool,
          )
          decoder_positional_encoding = (
              self.decoder_positional_encoding(mask)
          )
          decoder_positional_encoding = (
              decoder_positional_encoding.flatten(3)
              .permute(0, 1, 3, 2)
              .flatten(1, 2)
          )
          decoder_inputs.append(decoder_input)
          decoder_positional_encodings.append(
              decoder_positional_encoding
          )
      # shape (num_queries, c) -> (batch_size, num_queries, c)
      query_feat = self.query_feat.weight.unsqueeze(0).repeat(
          (batch_size, 1, 1)
      )
      query_embed = self.query_embed.weight.unsqueeze(0).repeat(
          (batch_size, 1, 1)
      )
  
      cls_pred_list = []
      mask_pred_list = []
      cls_pred, mask_pred, attn_mask = self._forward_head(
          query_feat, mask_features, multi_scale_memorys[0].shape[-2:]
      )
      cls_pred_list.append(cls_pred)
      mask_pred_list.append(mask_pred)
  
      for i in range(self.num_transformer_decoder_layers):
          level_idx = i % self.num_transformer_feat_level
          # if a mask is all True(all background), then set it all False.
          attn_mask[
              torch.where(attn_mask.sum(-1) == attn_mask.shape[-1])
          ] = False
  
          # cross_attn + self_attn
          layer = self.transformer_decoder.layers[i]
          query_feat = layer(
              query=query_feat,
              key=decoder_inputs[level_idx],
              value=decoder_inputs[level_idx],
              query_pos=query_embed,
              key_pos=decoder_positional_encodings[level_idx],
              cross_attn_mask=attn_mask,
              query_key_padding_mask=None,
              # here we do not apply masking on padded region
              key_padding_mask=None,
          )
          cls_pred, mask_pred, attn_mask = self._forward_head(
              query_feat,
              mask_features,
              multi_scale_memorys[
                  (i + 1) % self.num_transformer_feat_level
              ].shape[-2:],
          )
  
          cls_pred_list.append(cls_pred)
          mask_pred_list.append(mask_pred)
  
      return cls_pred_list, mask_pred_list
  
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 7 for calling the Mask2FormerTrackHead obj's underlying member method code with example usage: obj.forward(x, data_samples) and returns: cls_pred_list,  mask_pred_list