|
| 1 | +# Copyright 2024 Huawei Technologies Co., Ltd |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================ |
| 15 | +"""PoolFormer model configuration""" |
| 16 | + |
| 17 | +from ...configuration_utils import PretrainedConfig |
| 18 | + |
| 19 | + |
| 20 | +class PoolFormerConfig(PretrainedConfig): |
| 21 | + r""" |
| 22 | + This is the configuration class to store the configuration of [`PoolFormerModel`]. It is used to instantiate a |
| 23 | + PoolFormer model according to the specified arguments, defining the model architecture. Instantiating a |
| 24 | + configuration with the defaults will yield a similar configuration to that of the PoolFormer |
| 25 | + [sail/poolformer_s12](https://huggingface.co/sail/poolformer_s12) architecture. |
| 26 | +
|
| 27 | + Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the |
| 28 | + documentation from [`PretrainedConfig`] for more information. |
| 29 | +
|
| 30 | +
|
| 31 | + Args: |
| 32 | + num_channels (`int`, *optional*, defaults to 3): |
| 33 | + The number of channels in the input image. |
| 34 | + patch_size (`int`, *optional*, defaults to 16): |
| 35 | + The size of the input patch. |
| 36 | + stride (`int`, *optional*, defaults to 16): |
| 37 | + The stride of the input patch. |
| 38 | + pool_size (`int`, *optional*, defaults to 3): |
| 39 | + The size of the pooling window. |
| 40 | + mlp_ratio (`float`, *optional*, defaults to 4.0): |
| 41 | + The ratio of the number of channels in the output of the MLP to the number of channels in the input. |
| 42 | + depths (`list`, *optional*, defaults to `[2, 2, 6, 2]`): |
| 43 | + The depth of each encoder block. |
| 44 | + hidden_sizes (`list`, *optional*, defaults to `[64, 128, 320, 512]`): |
| 45 | + The hidden sizes of each encoder block. |
| 46 | + patch_sizes (`list`, *optional*, defaults to `[7, 3, 3, 3]`): |
| 47 | + The size of the input patch for each encoder block. |
| 48 | + strides (`list`, *optional*, defaults to `[4, 2, 2, 2]`): |
| 49 | + The stride of the input patch for each encoder block. |
| 50 | + padding (`list`, *optional*, defaults to `[2, 1, 1, 1]`): |
| 51 | + The padding of the input patch for each encoder block. |
| 52 | + num_encoder_blocks (`int`, *optional*, defaults to 4): |
| 53 | + The number of encoder blocks. |
| 54 | + drop_path_rate (`float`, *optional*, defaults to 0.0): |
| 55 | + The dropout rate for the dropout layers. |
| 56 | + hidden_act (`str`, *optional*, defaults to `"gelu"`): |
| 57 | + The activation function for the hidden layers. |
| 58 | + use_layer_scale (`bool`, *optional*, defaults to `True`): |
| 59 | + Whether to use layer scale. |
| 60 | + layer_scale_init_value (`float`, *optional*, defaults to 1e-05): |
| 61 | + The initial value for the layer scale. |
| 62 | + initializer_range (`float`, *optional*, defaults to 0.02): |
| 63 | + The initializer range for the weights. |
| 64 | +
|
| 65 | + Example: |
| 66 | +
|
| 67 | + ```python |
| 68 | + >>> from transformers import PoolFormerConfig, PoolFormerModel |
| 69 | +
|
| 70 | + >>> # Initializing a PoolFormer sail/poolformer_s12 style configuration |
| 71 | + >>> configuration = PoolFormerConfig() |
| 72 | +
|
| 73 | + >>> # Initializing a model (with random weights) from the sail/poolformer_s12 style configuration |
| 74 | + >>> model = PoolFormerModel(configuration) |
| 75 | +
|
| 76 | + >>> # Accessing the model configuration |
| 77 | + >>> configuration = model.config |
| 78 | + ``` |
| 79 | + """ |
| 80 | + |
| 81 | + model_type = "poolformer" |
| 82 | + |
| 83 | + def __init__( |
| 84 | + self, |
| 85 | + num_channels=3, |
| 86 | + patch_size=16, |
| 87 | + stride=16, |
| 88 | + pool_size=3, |
| 89 | + mlp_ratio=4.0, |
| 90 | + depths=[2, 2, 6, 2], |
| 91 | + hidden_sizes=[64, 128, 320, 512], |
| 92 | + patch_sizes=[7, 3, 3, 3], |
| 93 | + strides=[4, 2, 2, 2], |
| 94 | + padding=[2, 1, 1, 1], |
| 95 | + num_encoder_blocks=4, |
| 96 | + drop_path_rate=0.0, |
| 97 | + hidden_act="gelu", |
| 98 | + use_layer_scale=True, |
| 99 | + layer_scale_init_value=1e-5, |
| 100 | + initializer_range=0.02, |
| 101 | + **kwargs, |
| 102 | + ): |
| 103 | + self.num_channels = num_channels |
| 104 | + self.patch_size = patch_size |
| 105 | + self.stride = stride |
| 106 | + self.padding = padding |
| 107 | + self.pool_size = pool_size |
| 108 | + self.hidden_sizes = hidden_sizes |
| 109 | + self.mlp_ratio = mlp_ratio |
| 110 | + self.depths = depths |
| 111 | + self.patch_sizes = patch_sizes |
| 112 | + self.strides = strides |
| 113 | + self.num_encoder_blocks = num_encoder_blocks |
| 114 | + self.drop_path_rate = drop_path_rate |
| 115 | + self.hidden_act = hidden_act |
| 116 | + self.use_layer_scale = use_layer_scale |
| 117 | + self.layer_scale_init_value = layer_scale_init_value |
| 118 | + self.initializer_range = initializer_range |
| 119 | + super().__init__(**kwargs) |
| 120 | + |
| 121 | + |
| 122 | +__all__ = [ |
| 123 | + "PoolFormerConfig", |
| 124 | +] |
0 commit comments