Skip to content
data

Mask2FormerConfig

extendsModelConfig
Mask2FormerConfig(num_classes: int = 150, in_channels: int = 3, swin_embed_dim: int = 96, swin_depths: tuple[int, int, int, int] = (2, 2, 6, 2), swin_num_heads: tuple[int, int, int, int] = (3, 6, 12, 24), swin_window_size: int = 7, swin_mlp_ratio: float = 4.0, d_model: int = 256, mask_feature_size: int = 256, n_head: int = 8, num_encoder_layers: int = 6, encoder_feedforward_dim: int = 1024, num_decoder_layers: int = 10, dim_feedforward: int = 2048, dropout: float = 0.0, num_queries: int = 100, class_weight: float = 2.0, mask_weight: float = 5.0, dice_weight: float = 5.0, no_object_weight: float = 0.1, train_num_points: int = 12544, oversample_ratio: float = 3.0, importance_sample_ratio: float = 0.75, deep_supervision: bool = True, num_feature_levels: int = 3, feature_strides: tuple[int, int, int, int] = (4, 8, 16, 32), common_stride: int = 4)

Configuration for Mask2Former (Cheng et al., CVPR 2022).

The field set mirrors the reference framework's Mask2FormerConfig so the pretrained-weight converter is a near-identity key map. The pipeline is:

Image → Swin backbone → [stage1..4] feature maps → MSDeformAttn pixel decoder → 3 multi-scale memory levels + 1/4-scale mask features → 9-layer masked-attention transformer decoder (cycling levels) → class head (Linear → K+1) + mask head (MLP → dot mask features)

Args: num_classes: Number of semantic classes (foreground; the class head emits num_classes + 1). in_channels: Input image channels. swin_embed_dim: Swin patch-embedding dimension. swin_depths: Swin per-stage block counts. swin_num_heads: Swin per-stage head counts. swin_window_size: Swin attention window size. swin_mlp_ratio: Swin MLP expansion ratio. d_model: Transformer / pixel-decoder feature dim. mask_feature_size: Per-pixel mask-feature channel width. n_head: Number of attention heads. num_encoder_layers: Deformable pixel-decoder encoder depth. encoder_feedforward_dim: Pixel-decoder FFN inner dim. num_decoder_layers: Transformer decoder depth (the decoder uses num_decoder_layers - 1 masked layers; the extra slot is the pre-layer mask prediction). dim_feedforward: Transformer-decoder FFN inner dim. dropout: Dropout probability (0 at inference). num_queries: Number of learnable object queries N. num_feature_levels: Number of multi-scale memory levels (3). feature_strides: Backbone output strides. common_stride: Finest pixel-decoder stride.

-- Training objective (§3.2.2) -- class_weight: Weight of the classification term, and of the class cost inside the Hungarian matcher. mask_weight: Weight of the point-sampled mask BCE. dice_weight: Weight of the point-sampled dice term. no_object_weight: Class weight of the "no object" slot (0.1). train_num_points: K points per mask (12,544 = 112 x 112). oversample_ratio: Candidate multiplier for the importance sampler. importance_sample_ratio: Share of K taken from the most uncertain candidates; the rest are uniform. deep_supervision: Apply the criterion to every decoder layer, not just the last.

Note:

Training. Pass targets to forward for §3.2.2's objective: Hungarian matching on a class + mask-BCE + dice cost, the same three terms as the loss, all mask terms evaluated on train_num_points importance-sampled points, and — under deep_supervision — the whole criterion repeated on every decoder layer.

Examples

>>> from lucid.models.vision.mask2former import Mask2FormerConfig
>>> cfg = Mask2FormerConfig()
>>> cfg.num_classes, cfg.num_queries
(150, 100)
>>> cfg.model_type
'mask2former'

Used by 3