codingutf8 2022 meta platforms inc and the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license mask2former model configuration from typing import dict list optional from configurationutils import pretrainedconfig from utils import logging from auto import configmapping mask2formerpretrainedconfigarchivemap facebookmask2formerswinsmallcocoinstance https huggingface cofacebookmask2formerswinsmallcocoinstanceblobmainconfig json see all mask2former models at https huggingface comodels filtermask2former logger logging getloggername class mask2formerconfigpretrainedconfig r this is the configuration class to store the configuration of a mask2formermodel it is used to instantiate a mask2former model according to the specified arguments defining the model architecture instantiating a configuration with the defaults will yield a similar configuration to that of the mask2former facebookmask2formerswinsmallcocoinstancehttps huggingface cofacebookmask2formerswinsmallcocoinstance architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information currently mask2former only supports the swin transformerswin as backbone args backboneconfig pretrainedconfig or dict optional defaults to swinconfig the configuration of the backbone model if unset the configuration corresponding to swinbasepatch4window12384 will be used featuresize int optional defaults to 256 the features channels of the resulting feature maps maskfeaturesize int optional defaults to 256 the masks features size this value will also be used to specify the feature pyramid network features size hiddendim int optional defaults to 256 dimensionality of the encoder layers encoderfeedforwarddim int optional defaults to 1024 dimension of feedforward network for deformable detr encoder used as part of pixel decoder encoderlayers int optional defaults to 6 number of layers in the deformable detr encoder used as part of pixel decoder decoderlayers int optional defaults to 10 number of layers in the transformer decoder numattentionheads int optional defaults to 8 number of attention heads for each attention layer dropout float optional defaults to 0 1 the dropout probability for all fully connected layers in the embeddings encoder dimfeedforward int optional defaults to 2048 feature dimension in feedforward network for transformer decoder prenorm bool optional defaults to false whether to use prelayernorm or not for transformer decoder enforceinputprojection bool optional defaults to false whether to add an input projection 1x1 convolution even if the input channels and hidden dim are identical in the transformer decoder commonstride int optional defaults to 4 parameter used for determining number of fpn levels used as part of pixel decoder ignorevalue int optional defaults to 255 category id to be ignored during training numqueries int optional defaults to 100 number of queries for the decoder noobjectweight int optional defaults to 0 1 the weight to apply to the null no object class classweight int optional defaults to 2 0 the weight for the cross entropy loss maskweight int optional defaults to 5 0 the weight for the mask loss diceweight int optional defaults to 5 0 the weight for the dice loss trainnumpoints str or function optional defaults to 12544 number of points used for sampling during loss calculation oversampleratio float optional defaults to 3 0 oversampling parameter used for calculating no of sampled points importancesampleratio float optional defaults to 0 75 ratio of points that are sampled via importance sampling initstd float optional defaults to 0 02 the standard deviation of the truncatednormalinitializer for initializing all weight matrices initxavierstd float optional defaults to 1 0 the scaling factor used for the xavier initialization gain in the hm attention map module useauxiliaryloss boolean optional defaults to true if true mask2formerforuniversalsegmentationoutput will contain the auxiliary losses computed using the logits from each decoder s stage featurestrides listint optional defaults to 4 8 16 32 feature strides corresponding to features generated from backbone network outputauxiliarylogits bool optional should the model output its auxiliarylogits or not examples python from transformers import mask2formerconfig mask2formermodel initializing a mask2former facebookmask2formerswinsmallcocoinstance configuration configuration mask2formerconfig initializing a model with random weights from the facebookmask2formerswinsmallcocoinstance style configuration model mask2formermodelconfiguration accessing the model configuration configuration model config verify that the backbone is supported instantiate a mask2formerconfig or a derived class from a pretrained backbone model configuration args backboneconfig pretrainedconfig the backbone configuration returns mask2formerconfig an instance of a configuration object coding utf 8 2022 meta platforms inc and the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license mask2former model configuration see all mask2former models at https huggingface co models filter mask2former this is the configuration class to store the configuration of a mask2formermodel it is used to instantiate a mask2former model according to the specified arguments defining the model architecture instantiating a configuration with the defaults will yield a similar configuration to that of the mask2former facebook mask2former swin small coco instance https huggingface co facebook mask2former swin small coco instance architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information currently mask2former only supports the swin transformer swin as backbone args backbone_config pretrainedconfig or dict optional defaults to swinconfig the configuration of the backbone model if unset the configuration corresponding to swin base patch4 window12 384 will be used feature_size int optional defaults to 256 the features channels of the resulting feature maps mask_feature_size int optional defaults to 256 the masks features size this value will also be used to specify the feature pyramid network features size hidden_dim int optional defaults to 256 dimensionality of the encoder layers encoder_feedforward_dim int optional defaults to 1024 dimension of feedforward network for deformable detr encoder used as part of pixel decoder encoder_layers int optional defaults to 6 number of layers in the deformable detr encoder used as part of pixel decoder decoder_layers int optional defaults to 10 number of layers in the transformer decoder num_attention_heads int optional defaults to 8 number of attention heads for each attention layer dropout float optional defaults to 0 1 the dropout probability for all fully connected layers in the embeddings encoder dim_feedforward int optional defaults to 2048 feature dimension in feedforward network for transformer decoder pre_norm bool optional defaults to false whether to use pre layernorm or not for transformer decoder enforce_input_projection bool optional defaults to false whether to add an input projection 1x1 convolution even if the input channels and hidden dim are identical in the transformer decoder common_stride int optional defaults to 4 parameter used for determining number of fpn levels used as part of pixel decoder ignore_value int optional defaults to 255 category id to be ignored during training num_queries int optional defaults to 100 number of queries for the decoder no_object_weight int optional defaults to 0 1 the weight to apply to the null no object class class_weight int optional defaults to 2 0 the weight for the cross entropy loss mask_weight int optional defaults to 5 0 the weight for the mask loss dice_weight int optional defaults to 5 0 the weight for the dice loss train_num_points str or function optional defaults to 12544 number of points used for sampling during loss calculation oversample_ratio float optional defaults to 3 0 oversampling parameter used for calculating no of sampled points importance_sample_ratio float optional defaults to 0 75 ratio of points that are sampled via importance sampling init_std float optional defaults to 0 02 the standard deviation of the truncated_normal_initializer for initializing all weight matrices init_xavier_std float optional defaults to 1 0 the scaling factor used for the xavier initialization gain in the hm attention map module use_auxiliary_loss boolean optional defaults to true if true mask2formerforuniversalsegmentationoutput will contain the auxiliary losses computed using the logits from each decoder s stage feature_strides list int optional defaults to 4 8 16 32 feature strides corresponding to features generated from backbone network output_auxiliary_logits bool optional should the model output its auxiliary_logits or not examples python from transformers import mask2formerconfig mask2formermodel initializing a mask2former facebook mask2former swin small coco instance configuration configuration mask2formerconfig initializing a model with random weights from the facebook mask2former swin small coco instance style configuration model mask2formermodel configuration accessing the model configuration configuration model config verify that the backbone is supported instantiate a mask2formerconfig or a derived class from a pre trained backbone model configuration args backbone_config pretrainedconfig the backbone configuration returns mask2formerconfig an instance of a configuration object
from typing import Dict, List, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "facebook/mask2former-swin-small-coco-instance": (
        "https://huggingface.co/facebook/mask2former-swin-small-coco-instance/blob/main/config.json"
    )
    
}

logger = logging.get_logger(__name__)


class Mask2FormerConfig(PretrainedConfig):
    r

    model_type = "mask2former"
    backbones_supported = ["swin"]
    attribute_map = {"hidden_size": "hidden_dim"}

    def __init__(
        self,
        backbone_config: Optional[Dict] = None,
        feature_size: int = 256,
        mask_feature_size: int = 256,
        hidden_dim: int = 256,
        encoder_feedforward_dim: int = 1024,
        activation_function: str = "relu",
        encoder_layers: int = 6,
        decoder_layers: int = 10,
        num_attention_heads: int = 8,
        dropout: float = 0.0,
        dim_feedforward: int = 2048,
        pre_norm: bool = False,
        enforce_input_projection: bool = False,
        common_stride: int = 4,
        ignore_value: int = 255,
        num_queries: int = 100,
        no_object_weight: float = 0.1,
        class_weight: float = 2.0,
        mask_weight: float = 5.0,
        dice_weight: float = 5.0,
        train_num_points: int = 12544,
        oversample_ratio: float = 3.0,
        importance_sample_ratio: float = 0.75,
        init_std: float = 0.02,
        init_xavier_std: float = 1.0,
        use_auxiliary_loss: bool = True,
        feature_strides: List[int] = [4, 8, 16, 32],
        output_auxiliary_logits: bool = None,
        **kwargs,
    ):
        if backbone_config is None:
            logger.info("`backbone_config` is `None`. Initializing the config with the default `Swin` backbone.")
            backbone_config = CONFIG_MAPPING["swin"](
                image_size=224,
                in_channels=3,
                patch_size=4,
                embed_dim=96,
                depths=[2, 2, 18, 2],
                num_heads=[3, 6, 12, 24],
                window_size=7,
                drop_path_rate=0.3,
                use_absolute_embeddings=False,
                out_features=["stage1", "stage2", "stage3", "stage4"],
            )

        if isinstance(backbone_config, dict):
            backbone_model_type = backbone_config.pop("model_type")
            config_class = CONFIG_MAPPING[backbone_model_type]
            backbone_config = config_class.from_dict(backbone_config)

        
        if backbone_config.model_type not in self.backbones_supported:
            logger.warning_once(
                f"Backbone {backbone_config.model_type} is not a supported model and may not be compatible with Mask2Former. "
                f"Supported model types: {','.join(self.backbones_supported)}"
            )

        self.backbone_config = backbone_config
        self.feature_size = feature_size
        self.mask_feature_size = mask_feature_size
        self.hidden_dim = hidden_dim
        self.encoder_feedforward_dim = encoder_feedforward_dim
        self.activation_function = activation_function
        self.encoder_layers = encoder_layers
        self.decoder_layers = decoder_layers
        self.num_attention_heads = num_attention_heads
        self.dropout = dropout
        self.dim_feedforward = dim_feedforward
        self.pre_norm = pre_norm
        self.enforce_input_projection = enforce_input_projection
        self.common_stride = common_stride
        self.ignore_value = ignore_value
        self.num_queries = num_queries
        self.no_object_weight = no_object_weight
        self.class_weight = class_weight
        self.mask_weight = mask_weight
        self.dice_weight = dice_weight
        self.train_num_points = train_num_points
        self.oversample_ratio = oversample_ratio
        self.importance_sample_ratio = importance_sample_ratio
        self.init_std = init_std
        self.init_xavier_std = init_xavier_std
        self.use_auxiliary_loss = use_auxiliary_loss
        self.feature_strides = feature_strides
        self.output_auxiliary_logits = output_auxiliary_logits
        self.num_hidden_layers = decoder_layers

        super().__init__(**kwargs)

    @classmethod
    def from_backbone_config(cls, backbone_config: PretrainedConfig, **kwargs):
        
        return cls(
            backbone_config=backbone_config,
            **kwargs,
        )