codingutf8 2022 shi labs 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 oneformer model configuration from typing import dict optional from configurationutils import pretrainedconfig from utils import logging from auto import configmapping logger logging getloggername oneformerpretrainedconfigarchivemap shilabsoneformerade20kswintiny https huggingface coshilabsoneformerade20kswintinyblobmainconfig json see all oneformer models at https huggingface comodels filteroneformer class oneformerconfigpretrainedconfig r this is the configuration class to store the configuration of a oneformermodel it is used to instantiate a oneformer 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 oneformer shilabsoneformerade20kswintinyhttps huggingface coshilabsoneformerade20kswintiny architecture trained on ade20k150https huggingface codatasetssceneparse150 configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args backboneconfig pretrainedconfig optional defaults to swinconfig the configuration of the backbone model ignorevalue int optional defaults to 255 values to be ignored in gt label while calculating loss numqueries int optional defaults to 150 number of object queries noobjectweight float optional defaults to 0 1 weight for noobject class predictions classweight float optional defaults to 2 0 weight for classification ce loss maskweight float optional defaults to 5 0 weight for binary ce loss diceweight float optional defaults to 5 0 weight for dice loss contrastiveweight float optional defaults to 0 5 weight for contrastive loss contrastivetemperature float optional defaults to 0 07 initial value for scaling the contrastive logits trainnumpoints int optional defaults to 12544 number of points to sample while calculating losses on mask predictions oversampleratio float optional defaults to 3 0 ratio to decide how many points to oversample importancesampleratio float optional defaults to 0 75 ratio of points that are sampled via importance sampling initstd float optional defaults to 0 02 standard deviation for normal intialization initxavierstd float optional defaults to 1 0 standard deviation for xavier uniform initialization layernormeps float optional defaults to 1e05 epsilon for layer normalization istraining bool optional defaults to false whether to run in training or inference mode useauxiliaryloss bool optional defaults to true whether to calculate loss using intermediate predictions from transformer decoder outputauxiliarylogits bool optional defaults to true whether to return intermediate predictions from transformer decoder strides list optional defaults to 4 8 16 32 list containing the strides for feature maps in the encoder taskseqlen int optional defaults to 77 sequence length for tokenizing text list input textencoderwidth int optional defaults to 256 hidden size for text encoder textencodercontextlength int optional defaults to 77 input sequence length for text encoder textencodernumlayers int optional defaults to 6 number of layers for transformer in text encoder textencodervocabsize int optional defaults to 49408 vocabulary size for tokenizer textencoderprojlayers int optional defaults to 2 number of layers in mlp for project text queries textencodernctx int optional defaults to 16 number of learnable text context queries convdim int optional defaults to 256 feature map dimension to map outputs from the backbone maskdim int optional defaults to 256 dimension for feature maps in pixel decoder hiddendim int optional defaults to 256 dimension for hidden states in transformer decoder encoderfeedforwarddim int optional defaults to 1024 dimension for ffn layer in pixel decoder norm str optional defaults to gn type of normalization encoderlayers int optional defaults to 6 number of layers in pixel decoder decoderlayers int optional defaults to 10 number of layers in transformer decoder usetasknorm bool optional defaults to true whether to normalize the task token numattentionheads int optional defaults to 8 number of attention heads in transformer layers in the pixel and transformer decoders dropout float optional defaults to 0 1 dropout probability for pixel and transformer decoders dimfeedforward int optional defaults to 2048 dimension for ffn layer in transformer decoder prenorm bool optional defaults to false whether to normalize hidden states before attention layers in transformer decoder enforceinputproj bool optional defaults to false whether to project hidden states in transformer decoder querydeclayers int optional defaults to 2 number of layers in query transformer commonstride int optional defaults to 4 common stride used for features in pixel decoder examples python from transformers import oneformerconfig oneformermodel initializing a oneformer shilabsoneformerade20kswintiny configuration configuration oneformerconfig initializing a model with random weights from the shilabsoneformerade20kswintiny style configuration model oneformermodelconfiguration accessing the model configuration configuration model config coding utf 8 2022 shi labs 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 oneformer model configuration see all oneformer models at https huggingface co models filter oneformer this is the configuration class to store the configuration of a oneformermodel it is used to instantiate a oneformer 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 oneformer shi labs oneformer_ade20k_swin_tiny https huggingface co shi labs oneformer_ade20k_swin_tiny architecture trained on ade20k 150 https huggingface co datasets scene_parse_150 configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args backbone_config pretrainedconfig optional defaults to swinconfig the configuration of the backbone model ignore_value int optional defaults to 255 values to be ignored in gt label while calculating loss num_queries int optional defaults to 150 number of object queries no_object_weight float optional defaults to 0 1 weight for no object class predictions class_weight float optional defaults to 2 0 weight for classification ce loss mask_weight float optional defaults to 5 0 weight for binary ce loss dice_weight float optional defaults to 5 0 weight for dice loss contrastive_weight float optional defaults to 0 5 weight for contrastive loss contrastive_temperature float optional defaults to 0 07 initial value for scaling the contrastive logits train_num_points int optional defaults to 12544 number of points to sample while calculating losses on mask predictions oversample_ratio float optional defaults to 3 0 ratio to decide how many points to oversample 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 standard deviation for normal intialization init_xavier_std float optional defaults to 1 0 standard deviation for xavier uniform initialization layer_norm_eps float optional defaults to 1e 05 epsilon for layer normalization is_training bool optional defaults to false whether to run in training or inference mode use_auxiliary_loss bool optional defaults to true whether to calculate loss using intermediate predictions from transformer decoder output_auxiliary_logits bool optional defaults to true whether to return intermediate predictions from transformer decoder strides list optional defaults to 4 8 16 32 list containing the strides for feature maps in the encoder task_seq_len int optional defaults to 77 sequence length for tokenizing text list input text_encoder_width int optional defaults to 256 hidden size for text encoder text_encoder_context_length int optional defaults to 77 input sequence length for text encoder text_encoder_num_layers int optional defaults to 6 number of layers for transformer in text encoder text_encoder_vocab_size int optional defaults to 49408 vocabulary size for tokenizer text_encoder_proj_layers int optional defaults to 2 number of layers in mlp for project text queries text_encoder_n_ctx int optional defaults to 16 number of learnable text context queries conv_dim int optional defaults to 256 feature map dimension to map outputs from the backbone mask_dim int optional defaults to 256 dimension for feature maps in pixel decoder hidden_dim int optional defaults to 256 dimension for hidden states in transformer decoder encoder_feedforward_dim int optional defaults to 1024 dimension for ffn layer in pixel decoder norm str optional defaults to gn type of normalization encoder_layers int optional defaults to 6 number of layers in pixel decoder decoder_layers int optional defaults to 10 number of layers in transformer decoder use_task_norm bool optional defaults to true whether to normalize the task token num_attention_heads int optional defaults to 8 number of attention heads in transformer layers in the pixel and transformer decoders dropout float optional defaults to 0 1 dropout probability for pixel and transformer decoders dim_feedforward int optional defaults to 2048 dimension for ffn layer in transformer decoder pre_norm bool optional defaults to false whether to normalize hidden states before attention layers in transformer decoder enforce_input_proj bool optional defaults to false whether to project hidden states in transformer decoder query_dec_layers int optional defaults to 2 number of layers in query transformer common_stride int optional defaults to 4 common stride used for features in pixel decoder examples python from transformers import oneformerconfig oneformermodel initializing a oneformer shi labs oneformer_ade20k_swin_tiny configuration configuration oneformerconfig initializing a model with random weights from the shi labs oneformer_ade20k_swin_tiny style configuration model oneformermodel configuration accessing the model configuration configuration model config
from typing import Dict, Optional

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


logger = logging.get_logger(__name__)

ONEFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "shi-labs/oneformer_ade20k_swin_tiny": (
        "https://huggingface.co/shi-labs/oneformer_ade20k_swin_tiny/blob/main/config.json"
    ),
    
}


class OneFormerConfig(PretrainedConfig):
    r

    model_type = "oneformer"
    attribute_map = {"hidden_size": "hidden_dim"}

    def __init__(
        self,
        backbone_config: Optional[Dict] = None,
        ignore_value: int = 255,
        num_queries: int = 150,
        no_object_weight: int = 0.1,
        class_weight: float = 2.0,
        mask_weight: float = 5.0,
        dice_weight: float = 5.0,
        contrastive_weight: float = 0.5,
        contrastive_temperature: float = 0.07,
        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,
        layer_norm_eps: float = 1e-05,
        is_training: bool = False,
        use_auxiliary_loss: bool = True,
        output_auxiliary_logits: bool = True,
        strides: Optional[list] = [4, 8, 16, 32],
        task_seq_len: int = 77,
        text_encoder_width: int = 256,
        text_encoder_context_length: int = 77,
        text_encoder_num_layers: int = 6,
        text_encoder_vocab_size: int = 49408,
        text_encoder_proj_layers: int = 2,
        text_encoder_n_ctx: int = 16,
        conv_dim: int = 256,
        mask_dim: int = 256,
        hidden_dim: int = 256,
        encoder_feedforward_dim: int = 1024,
        norm: str = "GN",
        encoder_layers: int = 6,
        decoder_layers: int = 10,
        use_task_norm: bool = True,
        num_attention_heads: int = 8,
        dropout: float = 0.1,
        dim_feedforward: int = 2048,
        pre_norm: bool = False,
        enforce_input_proj: bool = False,
        query_dec_layers: int = 2,
        common_stride: int = 4,
        **kwargs,
    ):
        if backbone_config is None:
            logger.info("`backbone_config` is unset. 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, 6, 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"],
            )
        elif isinstance(backbone_config, dict):
            backbone_model_type = backbone_config.get("model_type")
            config_class = CONFIG_MAPPING[backbone_model_type]
            backbone_config = config_class.from_dict(backbone_config)

        self.backbone_config = backbone_config

        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.contrastive_weight = contrastive_weight
        self.contrastive_temperature = contrastive_temperature
        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.layer_norm_eps = layer_norm_eps
        self.is_training = is_training
        self.use_auxiliary_loss = use_auxiliary_loss
        self.output_auxiliary_logits = output_auxiliary_logits
        self.strides = strides
        self.task_seq_len = task_seq_len
        self.text_encoder_width = text_encoder_width
        self.text_encoder_context_length = text_encoder_context_length
        self.text_encoder_num_layers = text_encoder_num_layers
        self.text_encoder_vocab_size = text_encoder_vocab_size
        self.text_encoder_proj_layers = text_encoder_proj_layers
        self.text_encoder_n_ctx = text_encoder_n_ctx
        self.conv_dim = conv_dim
        self.mask_dim = mask_dim
        self.hidden_dim = hidden_dim
        self.encoder_feedforward_dim = encoder_feedforward_dim
        self.norm = norm
        self.encoder_layers = encoder_layers
        self.decoder_layers = decoder_layers
        self.use_task_norm = use_task_norm
        self.num_attention_heads = num_attention_heads
        self.dropout = dropout
        self.dim_feedforward = dim_feedforward
        self.pre_norm = pre_norm
        self.enforce_input_proj = enforce_input_proj
        self.query_dec_layers = query_dec_layers
        self.common_stride = common_stride
        self.num_hidden_layers = decoder_layers

        super().__init__(**kwargs)