codingutf8 2022 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 swinv2 transformer model configuration from configurationutils import pretrainedconfig from utils import logging logger logging getloggername swinv2pretrainedconfigarchivemap microsoftswinv2tinypatch4window8256 https huggingface comicrosoftswinv2tinypatch4window8256resolvemainconfig json class swinv2configpretrainedconfig r this is the configuration class to store the configuration of a swinv2model it is used to instantiate a swin transformer v2 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 swin transformer v2 microsoftswinv2tinypatch4window8256https huggingface comicrosoftswinv2tinypatch4window8256 architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args imagesize int optional defaults to 224 the size resolution of each image patchsize int optional defaults to 4 the size resolution of each patch numchannels int optional defaults to 3 the number of input channels embeddim int optional defaults to 96 dimensionality of patch embedding depths listint optional defaults to 2 2 6 2 depth of each layer in the transformer encoder numheads listint optional defaults to 3 6 12 24 number of attention heads in each layer of the transformer encoder windowsize int optional defaults to 7 size of windows mlpratio float optional defaults to 4 0 ratio of mlp hidden dimensionality to embedding dimensionality qkvbias bool optional defaults to true whether or not a learnable bias should be added to the queries keys and values hiddendropoutprob float optional defaults to 0 0 the dropout probability for all fully connected layers in the embeddings and encoder attentionprobsdropoutprob float optional defaults to 0 0 the dropout ratio for the attention probabilities droppathrate float optional defaults to 0 1 stochastic depth rate hiddenact str or function optional defaults to gelu the nonlinear activation function function or string in the encoder if string gelu relu selu and gelunew are supported useabsoluteembeddings bool optional defaults to false whether or not to add absolute position embeddings to the patch embeddings initializerrange float optional defaults to 0 02 the standard deviation of the truncatednormalinitializer for initializing all weight matrices layernormeps float optional defaults to 1e05 the epsilon used by the layer normalization layers encoderstride int optional defaults to 32 factor to increase the spatial resolution by in the decoder head for masked image modeling example python from transformers import swinv2config swinv2model initializing a swinv2 microsoftswinv2tinypatch4window8256 style configuration configuration swinv2config initializing a model with random weights from the microsoftswinv2tinypatch4window8256 style configuration model swinv2modelconfiguration accessing the model configuration configuration model config modeltype swinv2 attributemap numattentionheads numheads numhiddenlayers numlayers def init self imagesize224 patchsize4 numchannels3 embeddim96 depths2 2 6 2 numheads3 6 12 24 windowsize7 mlpratio4 0 qkvbiastrue hiddendropoutprob0 0 attentionprobsdropoutprob0 0 droppathrate0 1 hiddenactgelu useabsoluteembeddingsfalse initializerrange0 02 layernormeps1e5 encoderstride32 kwargs super initkwargs self imagesize imagesize self patchsize patchsize self numchannels numchannels self embeddim embeddim self depths depths self numlayers lendepths self numheads numheads self windowsize windowsize self mlpratio mlpratio self qkvbias qkvbias self hiddendropoutprob hiddendropoutprob self attentionprobsdropoutprob attentionprobsdropoutprob self droppathrate droppathrate self hiddenact hiddenact self useabsoluteembeddings useabsoluteembeddings self layernormeps layernormeps self initializerrange initializerrange self encoderstride encoderstride we set the hiddensize attribute in order to make swinv2 work with visionencoderdecodermodel this indicates the channel dimension after the last stage of the model self hiddensize intembeddim 2 lendepths 1 self pretrainedwindowsizes 0 0 0 0 coding utf 8 2022 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 swinv2 transformer model configuration this is the configuration class to store the configuration of a swinv2model it is used to instantiate a swin transformer v2 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 swin transformer v2 microsoft swinv2 tiny patch4 window8 256 https huggingface co microsoft swinv2 tiny patch4 window8 256 architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args image_size int optional defaults to 224 the size resolution of each image patch_size int optional defaults to 4 the size resolution of each patch num_channels int optional defaults to 3 the number of input channels embed_dim int optional defaults to 96 dimensionality of patch embedding depths list int optional defaults to 2 2 6 2 depth of each layer in the transformer encoder num_heads list int optional defaults to 3 6 12 24 number of attention heads in each layer of the transformer encoder window_size int optional defaults to 7 size of windows mlp_ratio float optional defaults to 4 0 ratio of mlp hidden dimensionality to embedding dimensionality qkv_bias bool optional defaults to true whether or not a learnable bias should be added to the queries keys and values hidden_dropout_prob float optional defaults to 0 0 the dropout probability for all fully connected layers in the embeddings and encoder attention_probs_dropout_prob float optional defaults to 0 0 the dropout ratio for the attention probabilities drop_path_rate float optional defaults to 0 1 stochastic depth rate hidden_act str or function optional defaults to gelu the non linear activation function function or string in the encoder if string gelu relu selu and gelu_new are supported use_absolute_embeddings bool optional defaults to false whether or not to add absolute position embeddings to the patch embeddings initializer_range float optional defaults to 0 02 the standard deviation of the truncated_normal_initializer for initializing all weight matrices layer_norm_eps float optional defaults to 1e 05 the epsilon used by the layer normalization layers encoder_stride int optional defaults to 32 factor to increase the spatial resolution by in the decoder head for masked image modeling example python from transformers import swinv2config swinv2model initializing a swinv2 microsoft swinv2 tiny patch4 window8 256 style configuration configuration swinv2config initializing a model with random weights from the microsoft swinv2 tiny patch4 window8 256 style configuration model swinv2model configuration accessing the model configuration configuration model config we set the hidden_size attribute in order to make swinv2 work with visionencoderdecodermodel this indicates the channel dimension after the last stage of the model
from ...configuration_utils import PretrainedConfig
from ...utils import logging


logger = logging.get_logger(__name__)

SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "microsoft/swinv2-tiny-patch4-window8-256": (
        "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json"
    ),
}


class Swinv2Config(PretrainedConfig):
    r

    model_type = "swinv2"

    attribute_map = {
        "num_attention_heads": "num_heads",
        "num_hidden_layers": "num_layers",
    }

    def __init__(
        self,
        image_size=224,
        patch_size=4,
        num_channels=3,
        embed_dim=96,
        depths=[2, 2, 6, 2],
        num_heads=[3, 6, 12, 24],
        window_size=7,
        mlp_ratio=4.0,
        qkv_bias=True,
        hidden_dropout_prob=0.0,
        attention_probs_dropout_prob=0.0,
        drop_path_rate=0.1,
        hidden_act="gelu",
        use_absolute_embeddings=False,
        initializer_range=0.02,
        layer_norm_eps=1e-5,
        encoder_stride=32,
        **kwargs,
    ):
        super().__init__(**kwargs)

        self.image_size = image_size
        self.patch_size = patch_size
        self.num_channels = num_channels
        self.embed_dim = embed_dim
        self.depths = depths
        self.num_layers = len(depths)
        self.num_heads = num_heads
        self.window_size = window_size
        self.mlp_ratio = mlp_ratio
        self.qkv_bias = qkv_bias
        self.hidden_dropout_prob = hidden_dropout_prob
        self.attention_probs_dropout_prob = attention_probs_dropout_prob
        self.drop_path_rate = drop_path_rate
        self.hidden_act = hidden_act
        self.use_absolute_embeddings = use_absolute_embeddings
        self.layer_norm_eps = layer_norm_eps
        self.initializer_range = initializer_range
        self.encoder_stride = encoder_stride
        
        
        self.hidden_size = int(embed_dim * 2 ** (len(depths) - 1))
        self.pretrained_window_sizes = (0, 0, 0, 0)