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","."]
code: def __init__(
      self,
      in_channels: List[int],
      feat_channels: int,
      out_channels: int,
      num_classes: int,
      num_frames: int = 2,
      num_queries: int = 100,
      num_transformer_feat_level: int = 3,
      pixel_decoder: ConfigType = ...,
      enforce_decoder_input_project: bool = False,
      transformer_decoder: ConfigType = ...,
      positional_encoding: ConfigType = dict(
          num_feats=128, normalize=True
      ),
      loss_cls: ConfigType = dict(
          type="CrossEntropyLoss",
          use_sigmoid=False,
          loss_weight=2.0,
          reduction="mean",
          class_weight=[1.0] * 133 + [0.1],
      ),
      loss_mask: ConfigType = dict(
          type="CrossEntropyLoss",
          use_sigmoid=True,
          reduction="mean",
          loss_weight=5.0,
      ),
      loss_dice: ConfigType = dict(
          type="DiceLoss",
          use_sigmoid=True,
          activate=True,
          reduction="mean",
          naive_dice=True,
          eps=1.0,
          loss_weight=5.0,
      ),
      train_cfg: OptConfigType = None,
      test_cfg: OptConfigType = None,
      init_cfg: OptMultiConfig = None,
      **kwargs,
  ) -> None:
      super(AnchorFreeHead, self).__init__(init_cfg=init_cfg)
      self.num_classes = num_classes
      self.num_frames = num_frames
      self.num_queries = num_queries
      self.num_transformer_feat_level = num_transformer_feat_level
      self.num_transformer_feat_level = num_transformer_feat_level
      self.num_heads = (
          transformer_decoder.layer_cfg.cross_attn_cfg.num_heads
      )
      self.num_transformer_decoder_layers = (
          transformer_decoder.num_layers
      )
      assert (
          pixel_decoder.encoder.layer_cfg.self_attn_cfg.num_levels
          == num_transformer_feat_level
      )
      pixel_decoder_ = copy.deepcopy(pixel_decoder)
      pixel_decoder_.update(
          in_channels=in_channels,
          feat_channels=feat_channels,
          out_channels=out_channels,
      )
      self.pixel_decoder = MODELS.build(pixel_decoder_)
      self.transformer_decoder = Mask2FormerTransformerDecoder(
          **transformer_decoder
      )
      self.decoder_embed_dims = self.transformer_decoder.embed_dims
  
      self.decoder_input_projs = ModuleList()
      # from low resolution to high resolution
      for _ in range(num_transformer_feat_level):
          if (
              self.decoder_embed_dims != feat_channels
              or enforce_decoder_input_project
          ):
              self.decoder_input_projs.append(
                  Conv2d(
                      feat_channels,
                      self.decoder_embed_dims,
                      kernel_size=1,
                  )
              )
          else:
              self.decoder_input_projs.append(nn.Identity())
      self.decoder_positional_encoding = MODELS.build(
          positional_encoding
      )
      self.query_embed = nn.Embedding(self.num_queries, feat_channels)
      self.query_feat = nn.Embedding(self.num_queries, feat_channels)
      # from low resolution to high resolution
      self.level_embed = nn.Embedding(
          self.num_transformer_feat_level, feat_channels
      )
  
      self.cls_embed = nn.Linear(feat_channels, self.num_classes + 1)
      self.mask_embed = nn.Sequential(
          nn.Linear(feat_channels, feat_channels),
          nn.ReLU(inplace=True),
          nn.Linear(feat_channels, feat_channels),
          nn.ReLU(inplace=True),
          nn.Linear(feat_channels, out_channels),
      )
  
      self.test_cfg = test_cfg
      self.train_cfg = train_cfg
      if train_cfg:
          self.assigner = TASK_UTILS.build(self.train_cfg.assigner)
          self.sampler = TASK_UTILS.build(
              # self.train_cfg.sampler, default_args=dict(context=self))
              self.train_cfg["sampler"],
              default_args=dict(context=self),
          )
          self.num_points = self.train_cfg.get("num_points", 12544)
          self.oversample_ratio = self.train_cfg.get(
              "oversample_ratio", 3.0
          )
          self.importance_sample_ratio = self.train_cfg.get(
              "importance_sample_ratio", 0.75
          )
  
      self.class_weight = loss_cls.class_weight
      self.loss_cls = MODELS.build(loss_cls)
      self.loss_mask = MODELS.build(loss_mask)
      self.loss_dice = MODELS.build(loss_dice)
  
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 1 to create a new Mask2FormerTrackHead object from inherited base classes: MaskFormerHead with example: obj = Mask2FormerTrackHead(in_channels, feat_channels, out_channels, num_classes, num_frames, num_queries, num_transformer_feat_level, pixel_decoder, enforce_decoder_input_project, transformer_decoder, positional_encoding, loss_cls, loss_mask, loss_dice, train_cfg, test_cfg, init_cfg)