configuration_van.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # coding=utf-8
  2. # Copyright 2022 The HuggingFace Inc. team. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """VAN model configuration"""
  16. from ....configuration_utils import PretrainedConfig
  17. from ....utils import logging
  18. logger = logging.get_logger(__name__)
  19. class VanConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`VanModel`]. It is used to instantiate a VAN model
  22. according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  23. defaults will yield a similar configuration to that of the VAN
  24. [Visual-Attention-Network/van-base](https://huggingface.co/Visual-Attention-Network/van-base) architecture.
  25. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  26. documentation from [`PretrainedConfig`] for more information.
  27. Args:
  28. image_size (`int`, *optional*, defaults to 224):
  29. The size (resolution) of each image.
  30. num_channels (`int`, *optional*, defaults to 3):
  31. The number of input channels.
  32. patch_sizes (`List[int]`, *optional*, defaults to `[7, 3, 3, 3]`):
  33. Patch size to use in each stage's embedding layer.
  34. strides (`List[int]`, *optional*, defaults to `[4, 2, 2, 2]`):
  35. Stride size to use in each stage's embedding layer to downsample the input.
  36. hidden_sizes (`List[int]`, *optional*, defaults to `[64, 128, 320, 512]`):
  37. Dimensionality (hidden size) at each stage.
  38. depths (`List[int]`, *optional*, defaults to `[3, 3, 12, 3]`):
  39. Depth (number of layers) for each stage.
  40. mlp_ratios (`List[int]`, *optional*, defaults to `[8, 8, 4, 4]`):
  41. The expansion ratio for mlp layer at each stage.
  42. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
  43. The non-linear activation function (function or string) in each layer. If string, `"gelu"`, `"relu"`,
  44. `"selu"` and `"gelu_new"` are supported.
  45. initializer_range (`float`, *optional*, defaults to 0.02):
  46. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  47. layer_norm_eps (`float`, *optional*, defaults to 1e-06):
  48. The epsilon used by the layer normalization layers.
  49. layer_scale_init_value (`float`, *optional*, defaults to 0.01):
  50. The initial value for layer scaling.
  51. drop_path_rate (`float`, *optional*, defaults to 0.0):
  52. The dropout probability for stochastic depth.
  53. dropout_rate (`float`, *optional*, defaults to 0.0):
  54. The dropout probability for dropout.
  55. Example:
  56. ```python
  57. >>> from transformers import VanModel, VanConfig
  58. >>> # Initializing a VAN van-base style configuration
  59. >>> configuration = VanConfig()
  60. >>> # Initializing a model from the van-base style configuration
  61. >>> model = VanModel(configuration)
  62. >>> # Accessing the model configuration
  63. >>> configuration = model.config
  64. ```"""
  65. model_type = "van"
  66. def __init__(
  67. self,
  68. image_size=224,
  69. num_channels=3,
  70. patch_sizes=[7, 3, 3, 3],
  71. strides=[4, 2, 2, 2],
  72. hidden_sizes=[64, 128, 320, 512],
  73. depths=[3, 3, 12, 3],
  74. mlp_ratios=[8, 8, 4, 4],
  75. hidden_act="gelu",
  76. initializer_range=0.02,
  77. layer_norm_eps=1e-6,
  78. layer_scale_init_value=1e-2,
  79. drop_path_rate=0.0,
  80. dropout_rate=0.0,
  81. **kwargs,
  82. ):
  83. super().__init__(**kwargs)
  84. self.image_size = image_size
  85. self.num_channels = num_channels
  86. self.patch_sizes = patch_sizes
  87. self.strides = strides
  88. self.hidden_sizes = hidden_sizes
  89. self.depths = depths
  90. self.mlp_ratios = mlp_ratios
  91. self.hidden_act = hidden_act
  92. self.initializer_range = initializer_range
  93. self.layer_norm_eps = layer_norm_eps
  94. self.layer_scale_init_value = layer_scale_init_value
  95. self.drop_path_rate = drop_path_rate
  96. self.dropout_rate = dropout_rate