codingutf8 2023 s wenhai wang enze xie xiang li dengping fan kaitao song ding liang tong lu ping luo ling shao 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 pvt model configuration from collections import ordereddict from typing import callable list mapping from packaging import version from configurationutils import pretrainedconfig from onnx import onnxconfig from utils import logging logger logging getloggername pvtpretrainedconfigarchivemap pvttiny224 https huggingface cozetatechpvttiny224 see all pvt models at https huggingface comodels filterpvt class pvtconfigpretrainedconfig r this is the configuration class to store the configuration of a pvtmodel it is used to instantiate an pvt 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 pvt xrenyapvttiny224https huggingface coxrenyapvttiny224 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 input image size numchannels int optional defaults to 3 the number of input channels numencoderblocks int optional defaults to 4 the number of encoder blocks i e stages in the mix transformer encoder depths listint optional defaults to 2 2 2 2 the number of layers in each encoder block sequencereductionratios listint optional defaults to 8 4 2 1 sequence reduction ratios in each encoder block hiddensizes listint optional defaults to 64 128 320 512 dimension of each of the encoder blocks patchsizes listint optional defaults to 4 2 2 2 patch size before each encoder block strides listint optional defaults to 4 2 2 2 stride before each encoder block numattentionheads listint optional defaults to 1 2 5 8 number of attention heads for each attention layer in each block of the transformer encoder mlpratios listint optional defaults to 8 8 4 4 ratio of the size of the hidden layer compared to the size of the input layer of the mix ffns in the encoder blocks hiddenact str or function optional defaults to gelu the nonlinear activation function function or string in the encoder and pooler if string gelu relu selu and gelunew are supported hiddendropoutprob float optional defaults to 0 0 the dropout probability for all fully connected layers in the embeddings encoder and pooler attentionprobsdropoutprob float optional defaults to 0 0 the dropout ratio for the attention probabilities initializerrange float optional defaults to 0 02 the standard deviation of the truncatednormalinitializer for initializing all weight matrices droppathrate float optional defaults to 0 0 the dropout probability for stochastic depth used in the blocks of the transformer encoder layernormeps float optional defaults to 1e06 the epsilon used by the layer normalization layers qkvbias bool optional defaults to true whether or not a learnable bias should be added to the queries keys and values numlabels int optional defaults to 1000 the number of classes example python from transformers import pvtmodel pvtconfig initializing a pvt xrenyapvttiny224 style configuration configuration pvtconfig initializing a model from the xrenyapvttiny224 style configuration model pvtmodelconfiguration accessing the model configuration configuration model config modeltype pvt def init self imagesize int 224 numchannels int 3 numencoderblocks int 4 depths listint 2 2 2 2 sequencereductionratios listint 8 4 2 1 hiddensizes listint 64 128 320 512 patchsizes listint 4 2 2 2 strides listint 4 2 2 2 numattentionheads listint 1 2 5 8 mlpratios listint 8 8 4 4 hiddenact mappingstr callable gelu hiddendropoutprob float 0 0 attentionprobsdropoutprob float 0 0 initializerrange float 0 02 droppathrate float 0 0 layernormeps float 1e6 qkvbias bool true numlabels int 1000 kwargs super initkwargs self imagesize imagesize self numchannels numchannels self numencoderblocks numencoderblocks self depths depths self sequencereductionratios sequencereductionratios self hiddensizes hiddensizes self patchsizes patchsizes self strides strides self mlpratios mlpratios self numattentionheads numattentionheads self hiddenact hiddenact self hiddendropoutprob hiddendropoutprob self attentionprobsdropoutprob attentionprobsdropoutprob self initializerrange initializerrange self droppathrate droppathrate self layernormeps layernormeps self numlabels numlabels self qkvbias qkvbias class pvtonnxconfigonnxconfig torchonnxminimumversion version parse1 11 property def inputsself mappingstr mappingint str return ordereddict pixelvalues 0 batch 1 numchannels 2 height 3 width property def atolforvalidationself float return 1e4 property def defaultonnxopsetself int return 12 coding utf 8 2023 s wenhai wang enze xie xiang li deng ping fan kaitao song ding liang tong lu ping luo ling shao 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 pvt model configuration see all pvt models at https huggingface co models filter pvt this is the configuration class to store the configuration of a pvtmodel it is used to instantiate an pvt 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 pvt xrenya pvt tiny 224 https huggingface co xrenya pvt tiny 224 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 input image size num_channels int optional defaults to 3 the number of input channels num_encoder_blocks int optional defaults to 4 the number of encoder blocks i e stages in the mix transformer encoder depths list int optional defaults to 2 2 2 2 the number of layers in each encoder block sequence_reduction_ratios list int optional defaults to 8 4 2 1 sequence reduction ratios in each encoder block hidden_sizes list int optional defaults to 64 128 320 512 dimension of each of the encoder blocks patch_sizes list int optional defaults to 4 2 2 2 patch size before each encoder block strides list int optional defaults to 4 2 2 2 stride before each encoder block num_attention_heads list int optional defaults to 1 2 5 8 number of attention heads for each attention layer in each block of the transformer encoder mlp_ratios list int optional defaults to 8 8 4 4 ratio of the size of the hidden layer compared to the size of the input layer of the mix ffns in the encoder blocks hidden_act str or function optional defaults to gelu the non linear activation function function or string in the encoder and pooler if string gelu relu selu and gelu_new are supported hidden_dropout_prob float optional defaults to 0 0 the dropout probability for all fully connected layers in the embeddings encoder and pooler attention_probs_dropout_prob float optional defaults to 0 0 the dropout ratio for the attention probabilities initializer_range float optional defaults to 0 02 the standard deviation of the truncated_normal_initializer for initializing all weight matrices drop_path_rate float optional defaults to 0 0 the dropout probability for stochastic depth used in the blocks of the transformer encoder layer_norm_eps float optional defaults to 1e 06 the epsilon used by the layer normalization layers qkv_bias bool optional defaults to true whether or not a learnable bias should be added to the queries keys and values num_labels int optional defaults to 1000 the number of classes example python from transformers import pvtmodel pvtconfig initializing a pvt xrenya pvt tiny 224 style configuration configuration pvtconfig initializing a model from the xrenya pvt tiny 224 style configuration model pvtmodel configuration accessing the model configuration configuration model config
from collections import OrderedDict
from typing import Callable, List, Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


logger = logging.get_logger(__name__)

PVT_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "pvt-tiny-224": "https://huggingface.co/Zetatech/pvt-tiny-224",
    
}


class PvtConfig(PretrainedConfig):
    r

    model_type = "pvt"

    def __init__(
        self,
        image_size: int = 224,
        num_channels: int = 3,
        num_encoder_blocks: int = 4,
        depths: List[int] = [2, 2, 2, 2],
        sequence_reduction_ratios: List[int] = [8, 4, 2, 1],
        hidden_sizes: List[int] = [64, 128, 320, 512],
        patch_sizes: List[int] = [4, 2, 2, 2],
        strides: List[int] = [4, 2, 2, 2],
        num_attention_heads: List[int] = [1, 2, 5, 8],
        mlp_ratios: List[int] = [8, 8, 4, 4],
        hidden_act: Mapping[str, Callable] = "gelu",
        hidden_dropout_prob: float = 0.0,
        attention_probs_dropout_prob: float = 0.0,
        initializer_range: float = 0.02,
        drop_path_rate: float = 0.0,
        layer_norm_eps: float = 1e-6,
        qkv_bias: bool = True,
        num_labels: int = 1000,
        **kwargs,
    ):
        super().__init__(**kwargs)

        self.image_size = image_size
        self.num_channels = num_channels
        self.num_encoder_blocks = num_encoder_blocks
        self.depths = depths
        self.sequence_reduction_ratios = sequence_reduction_ratios
        self.hidden_sizes = hidden_sizes
        self.patch_sizes = patch_sizes
        self.strides = strides
        self.mlp_ratios = mlp_ratios
        self.num_attention_heads = num_attention_heads
        self.hidden_act = hidden_act
        self.hidden_dropout_prob = hidden_dropout_prob
        self.attention_probs_dropout_prob = attention_probs_dropout_prob
        self.initializer_range = initializer_range
        self.drop_path_rate = drop_path_rate
        self.layer_norm_eps = layer_norm_eps
        self.num_labels = num_labels
        self.qkv_bias = qkv_bias


class PvtOnnxConfig(OnnxConfig):
    torch_onnx_minimum_version = version.parse("1.11")

    @property
    def inputs(self) -> Mapping[str, Mapping[int, str]]:
        return OrderedDict(
            [
                ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
            ]
        )

    @property
    def atol_for_validation(self) -> float:
        return 1e-4

    @property
    def default_onnx_opset(self) -> int:
        return 12