codingutf8 2023 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 focalnet model configuration from configurationutils import pretrainedconfig from utils import logging from utils backboneutils import backboneconfigmixin getalignedoutputfeaturesoutputindices logger logging getloggername focalnetpretrainedconfigarchivemap microsoftfocalnettiny https huggingface comicrosoftfocalnettinyresolvemainconfig json class focalnetconfigbackboneconfigmixin pretrainedconfig r this is the configuration class to store the configuration of a focalnetmodel it is used to instantiate a focalnet 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 focalnet microsoftfocalnettinyhttps huggingface comicrosoftfocalnettiny 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 in the embeddings layer numchannels int optional defaults to 3 the number of input channels embeddim int optional defaults to 96 dimensionality of patch embedding useconvembed bool optional defaults to false whether to use convolutional embedding the s noted that using convolutional embedding usually improve the performance but it s not used by default hiddensizes listint optional defaults to 192 384 768 768 dimensionality hidden size at each stage depths listint optional defaults to 2 2 6 2 depth number of layers of each stage in the encoder focallevels listint optional defaults to 2 2 2 2 number of focal levels in each layer of the respective stages in the encoder focalwindows listint optional defaults to 3 3 3 3 focal window size in each layer of the respective stages in the encoder 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 mlpratio float optional defaults to 4 0 ratio of mlp hidden dimensionality to embedding dimensionality hiddendropoutprob float optional defaults to 0 0 the dropout probability for all fully connected layers in the embeddings and encoder droppathrate float optional defaults to 0 1 stochastic depth rate uselayerscale bool optional defaults to false whether to use layer scale in the encoder layerscalevalue float optional defaults to 0 0001 the initial value of the layer scale usepostlayernorm bool optional defaults to false whether to use post layer normalization in the encoder usepostlayernorminmodulation bool optional defaults to false whether to use post layer normalization in the modulation layer normalizemodulator bool optional defaults to false whether to normalize the modulator 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 outfeatures liststr optional if used as backbone list of features to output can be any of stem stage1 stage2 etc depending on how many stages the model has if unset and outindices is set will default to the corresponding stages if unset and outindices is unset will default to the last stage outindices listint optional if used as backbone list of indices of features to output can be any of 0 1 2 etc depending on how many stages the model has if unset and outfeatures is set will default to the corresponding stages if unset and outfeatures is unset will default to the last stage example python from transformers import focalnetconfig focalnetmodel initializing a focalnet microsoftfocalnettiny style configuration configuration focalnetconfig initializing a model with random weights from the microsoftfocalnettiny style configuration model focalnetmodelconfiguration accessing the model configuration configuration model config modeltype focalnet def init self imagesize224 patchsize4 numchannels3 embeddim96 useconvembedfalse hiddensizes192 384 768 768 depths2 2 6 2 focallevels2 2 2 2 focalwindows3 3 3 3 hiddenactgelu mlpratio4 0 hiddendropoutprob0 0 droppathrate0 1 uselayerscalefalse layerscalevalue1e4 usepostlayernormfalse usepostlayernorminmodulationfalse normalizemodulatorfalse initializerrange0 02 layernormeps1e5 encoderstride32 outfeaturesnone outindicesnone kwargs super initkwargs self imagesize imagesize self patchsize patchsize self numchannels numchannels self embeddim embeddim self useconvembed useconvembed self hiddensizes hiddensizes self depths depths self focallevels focallevels self focalwindows focalwindows self hiddenact hiddenact self mlpratio mlpratio self hiddendropoutprob hiddendropoutprob self droppathrate droppathrate self uselayerscale uselayerscale self layerscalevalue layerscalevalue self usepostlayernorm usepostlayernorm self usepostlayernorminmodulation usepostlayernorminmodulation self normalizemodulator normalizemodulator self initializerrange initializerrange self layernormeps layernormeps self encoderstride encoderstride self stagenames stem fstageidx for idx in range1 lenself depths 1 self outfeatures self outindices getalignedoutputfeaturesoutputindices outfeaturesoutfeatures outindicesoutindices stagenamesself stagenames coding utf 8 2023 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 focalnet model configuration this is the configuration class to store the configuration of a focalnetmodel it is used to instantiate a focalnet 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 focalnet microsoft focalnet tiny https huggingface co microsoft focalnet tiny 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 in the embeddings layer num_channels int optional defaults to 3 the number of input channels embed_dim int optional defaults to 96 dimensionality of patch embedding use_conv_embed bool optional defaults to false whether to use convolutional embedding the s noted that using convolutional embedding usually improve the performance but it s not used by default hidden_sizes list int optional defaults to 192 384 768 768 dimensionality hidden size at each stage depths list int optional defaults to 2 2 6 2 depth number of layers of each stage in the encoder focal_levels list int optional defaults to 2 2 2 2 number of focal levels in each layer of the respective stages in the encoder focal_windows list int optional defaults to 3 3 3 3 focal window size in each layer of the respective stages in the encoder 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 mlp_ratio float optional defaults to 4 0 ratio of mlp hidden dimensionality to embedding dimensionality hidden_dropout_prob float optional defaults to 0 0 the dropout probability for all fully connected layers in the embeddings and encoder drop_path_rate float optional defaults to 0 1 stochastic depth rate use_layerscale bool optional defaults to false whether to use layer scale in the encoder layerscale_value float optional defaults to 0 0001 the initial value of the layer scale use_post_layernorm bool optional defaults to false whether to use post layer normalization in the encoder use_post_layernorm_in_modulation bool optional defaults to false whether to use post layer normalization in the modulation layer normalize_modulator bool optional defaults to false whether to normalize the modulator 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 out_features list str optional if used as backbone list of features to output can be any of stem stage1 stage2 etc depending on how many stages the model has if unset and out_indices is set will default to the corresponding stages if unset and out_indices is unset will default to the last stage out_indices list int optional if used as backbone list of indices of features to output can be any of 0 1 2 etc depending on how many stages the model has if unset and out_features is set will default to the corresponding stages if unset and out_features is unset will default to the last stage example python from transformers import focalnetconfig focalnetmodel initializing a focalnet microsoft focalnet tiny style configuration configuration focalnetconfig initializing a model with random weights from the microsoft focalnet tiny style configuration model focalnetmodel configuration accessing the model configuration configuration model config
from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


logger = logging.get_logger(__name__)

FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "microsoft/focalnet-tiny": "https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json",
}


class FocalNetConfig(BackboneConfigMixin, PretrainedConfig):
    r

    model_type = "focalnet"

    def __init__(
        self,
        image_size=224,
        patch_size=4,
        num_channels=3,
        embed_dim=96,
        use_conv_embed=False,
        hidden_sizes=[192, 384, 768, 768],
        depths=[2, 2, 6, 2],
        focal_levels=[2, 2, 2, 2],
        focal_windows=[3, 3, 3, 3],
        hidden_act="gelu",
        mlp_ratio=4.0,
        hidden_dropout_prob=0.0,
        drop_path_rate=0.1,
        use_layerscale=False,
        layerscale_value=1e-4,
        use_post_layernorm=False,
        use_post_layernorm_in_modulation=False,
        normalize_modulator=False,
        initializer_range=0.02,
        layer_norm_eps=1e-5,
        encoder_stride=32,
        out_features=None,
        out_indices=None,
        **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.use_conv_embed = use_conv_embed
        self.hidden_sizes = hidden_sizes
        self.depths = depths
        self.focal_levels = focal_levels
        self.focal_windows = focal_windows
        self.hidden_act = hidden_act
        self.mlp_ratio = mlp_ratio
        self.hidden_dropout_prob = hidden_dropout_prob
        self.drop_path_rate = drop_path_rate
        self.use_layerscale = use_layerscale
        self.layerscale_value = layerscale_value
        self.use_post_layernorm = use_post_layernorm
        self.use_post_layernorm_in_modulation = use_post_layernorm_in_modulation
        self.normalize_modulator = normalize_modulator
        self.initializer_range = initializer_range
        self.layer_norm_eps = layer_norm_eps
        self.encoder_stride = encoder_stride
        self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, len(self.depths) + 1)]
        self._out_features, self._out_indices = get_aligned_output_features_output_indices(
            out_features=out_features, out_indices=out_indices, stage_names=self.stage_names
        )