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 for head part which is called after every decoder layer.
  
  Args:
      decoder_out (Tensor): in shape (num_queries, batch_size, c).
      mask_feature (Tensor): in shape (batch_size, t, c, h, w).
      attn_mask_target_size (tuple[int, int]): target attention
          mask size.
  
  Returns:
      tuple: A tuple contain three elements.
  
          - cls_pred (Tensor): Classification scores in shape                     (batch_size, num_queries, cls_out_channels).                     Note `cls_out_channels` should include background.
          - mask_pred (Tensor): Mask scores in shape                     (batch_size, num_queries,h, w).
          - attn_mask (Tensor): Attention mask in shape                     (batch_size * num_heads, num_queries, h, w).
docstr_tok: ["Forward","for","head","part","which","is","called","after","every","decoder","layer",".","Args",":","decoder_out","(","Tensor",")",":","in","shape","(","num_queries",",","batch_size",",","c",")",".","mask_feature","(","Tensor",")",":","in","shape","(","batch_size",",","t",",","c",",","h",",","w",")",".","attn_mask_target_size","(","tuple","[","int",",","int","]",")",":","target","attention","mask","size",".","Returns",":","tuple",":","A","tuple","contain","three","elements",".","-","cls_pred","(","Tensor",")",":","Classification","scores","in","shape","(","batch_size",",","num_queries",",","cls_out_channels",")",".","Note","`","cls_out_channels","`","should","include","background",".","-","mask_pred","(","Tensor",")",":","Mask","scores","in","shape","(","batch_size",",","num_queries",",","h",",","w",")",".","-","attn_mask","(","Tensor",")",":","Attention","mask","in","shape","(","batch_size","*","num_heads",",","num_queries",",","h",",","w",")","."]
code: def _forward_head(
      self,
      decoder_out: Tensor,
      mask_feature: Tensor,
      attn_mask_target_size: Tuple[int, int],
  ) -> Tuple[Tensor, Tensor, Tensor]:
      """Forward for head part which is called after every decoder layer.
  
          Args:
              decoder_out (Tensor): in shape (num_queries, batch_size, c).
              mask_feature (Tensor): in shape (batch_size, t, c, h, w).
              attn_mask_target_size (tuple[int, int]): target attention
                  mask size.
  
          Returns:
              tuple: A tuple contain three elements.
  
                  - cls_pred (Tensor): Classification scores in shape \
                      (batch_size, num_queries, cls_out_channels). \
                      Note `cls_out_channels` should include background.
                  - mask_pred (Tensor): Mask scores in shape \
                      (batch_size, num_queries,h, w).
                  - attn_mask (Tensor): Attention mask in shape \
                      (batch_size * num_heads, num_queries, h, w).
          """
      decoder_out = self.transformer_decoder.post_norm(decoder_out)
      cls_pred = self.cls_embed(decoder_out)
      mask_embed = self.mask_embed(decoder_out)
  
      # shape (batch_size, num_queries, t, h, w)
      mask_pred = torch.einsum(
          "bqc,btchw->bqthw", mask_embed, mask_feature
      )
      b, q, t, _, _ = mask_pred.shape
  
      attn_mask = F.interpolate(
          mask_pred.flatten(0, 1),
          attn_mask_target_size,
          mode="bilinear",
          align_corners=False,
      ).view(
          b, q, t, attn_mask_target_size[0], attn_mask_target_size[1]
      )
  
      # shape (batch_size, num_queries, t, h, w) ->
      # (batch_size, num_queries, t*h*w) ->
      # (batch_size, num_head, num_queries, t*h*w) ->
      # (batch_size*num_head, num_queries, t*h*w)
      attn_mask = (
          attn_mask.flatten(2)
          .unsqueeze(1)
          .repeat((1, self.num_heads, 1, 1))
          .flatten(0, 1)
      )
      attn_mask = attn_mask.sigmoid() < 0.5
      attn_mask = attn_mask.detach()
  
      return cls_pred, mask_pred, attn_mask
  
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 6 for calling the Mask2FormerTrackHead obj's underlying member method code with example usage: obj._forward_head(decoder_out, mask_feature, attn_mask_target_size) and returns: cls_pred,  mask_pred,  attn_mask