configuration_cvt.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. """CvT model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class CvtConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`CvtModel`]. It is used to instantiate a CvT 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 CvT
  24. [microsoft/cvt-13](https://huggingface.co/microsoft/cvt-13) 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. num_channels (`int`, *optional*, defaults to 3):
  29. The number of input channels.
  30. patch_sizes (`list[int]`, *optional*, defaults to `[7, 3, 3]`):
  31. The kernel size of each encoder's patch embedding.
  32. patch_stride (`list[int]`, *optional*, defaults to `[4, 2, 2]`):
  33. The stride size of each encoder's patch embedding.
  34. patch_padding (`list[int]`, *optional*, defaults to `[2, 1, 1]`):
  35. The padding size of each encoder's patch embedding.
  36. embed_dim (`list[int]`, *optional*, defaults to `[64, 192, 384]`):
  37. Dimension of each of the encoder blocks.
  38. num_heads (`list[int]`, *optional*, defaults to `[1, 3, 6]`):
  39. Number of attention heads for each attention layer in each block of the Transformer encoder.
  40. depth (`list[int]`, *optional*, defaults to `[1, 2, 10]`):
  41. The number of layers in each encoder block.
  42. mlp_ratios (`list[float]`, *optional*, defaults to `[4.0, 4.0, 4.0, 4.0]`):
  43. Ratio of the size of the hidden layer compared to the size of the input layer of the Mix FFNs in the
  44. encoder blocks.
  45. attention_drop_rate (`list[float]`, *optional*, defaults to `[0.0, 0.0, 0.0]`):
  46. The dropout ratio for the attention probabilities.
  47. drop_rate (`list[float]`, *optional*, defaults to `[0.0, 0.0, 0.0]`):
  48. The dropout ratio for the patch embeddings probabilities.
  49. drop_path_rate (`list[float]`, *optional*, defaults to `[0.0, 0.0, 0.1]`):
  50. The dropout probability for stochastic depth, used in the blocks of the Transformer encoder.
  51. qkv_bias (`list[bool]`, *optional*, defaults to `[True, True, True]`):
  52. The bias bool for query, key and value in attentions
  53. cls_token (`list[bool]`, *optional*, defaults to `[False, False, True]`):
  54. Whether or not to add a classification token to the output of each of the last 3 stages.
  55. qkv_projection_method (`list[string]`, *optional*, defaults to ["dw_bn", "dw_bn", "dw_bn"]`):
  56. The projection method for query, key and value Default is depth-wise convolutions with batch norm. For
  57. Linear projection use "avg".
  58. kernel_qkv (`list[int]`, *optional*, defaults to `[3, 3, 3]`):
  59. The kernel size for query, key and value in attention layer
  60. padding_kv (`list[int]`, *optional*, defaults to `[1, 1, 1]`):
  61. The padding size for key and value in attention layer
  62. stride_kv (`list[int]`, *optional*, defaults to `[2, 2, 2]`):
  63. The stride size for key and value in attention layer
  64. padding_q (`list[int]`, *optional*, defaults to `[1, 1, 1]`):
  65. The padding size for query in attention layer
  66. stride_q (`list[int]`, *optional*, defaults to `[1, 1, 1]`):
  67. The stride size for query in attention layer
  68. initializer_range (`float`, *optional*, defaults to 0.02):
  69. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  70. layer_norm_eps (`float`, *optional*, defaults to 1e-6):
  71. The epsilon used by the layer normalization layers.
  72. Example:
  73. ```python
  74. >>> from transformers import CvtConfig, CvtModel
  75. >>> # Initializing a Cvt msft/cvt style configuration
  76. >>> configuration = CvtConfig()
  77. >>> # Initializing a model (with random weights) from the msft/cvt style configuration
  78. >>> model = CvtModel(configuration)
  79. >>> # Accessing the model configuration
  80. >>> configuration = model.config
  81. ```"""
  82. model_type = "cvt"
  83. def __init__(
  84. self,
  85. num_channels=3,
  86. patch_sizes=[7, 3, 3],
  87. patch_stride=[4, 2, 2],
  88. patch_padding=[2, 1, 1],
  89. embed_dim=[64, 192, 384],
  90. num_heads=[1, 3, 6],
  91. depth=[1, 2, 10],
  92. mlp_ratio=[4.0, 4.0, 4.0],
  93. attention_drop_rate=[0.0, 0.0, 0.0],
  94. drop_rate=[0.0, 0.0, 0.0],
  95. drop_path_rate=[0.0, 0.0, 0.1],
  96. qkv_bias=[True, True, True],
  97. cls_token=[False, False, True],
  98. qkv_projection_method=["dw_bn", "dw_bn", "dw_bn"],
  99. kernel_qkv=[3, 3, 3],
  100. padding_kv=[1, 1, 1],
  101. stride_kv=[2, 2, 2],
  102. padding_q=[1, 1, 1],
  103. stride_q=[1, 1, 1],
  104. initializer_range=0.02,
  105. layer_norm_eps=1e-12,
  106. **kwargs,
  107. ):
  108. super().__init__(**kwargs)
  109. self.num_channels = num_channels
  110. self.patch_sizes = patch_sizes
  111. self.patch_stride = patch_stride
  112. self.patch_padding = patch_padding
  113. self.embed_dim = embed_dim
  114. self.num_heads = num_heads
  115. self.depth = depth
  116. self.mlp_ratio = mlp_ratio
  117. self.attention_drop_rate = attention_drop_rate
  118. self.drop_rate = drop_rate
  119. self.drop_path_rate = drop_path_rate
  120. self.qkv_bias = qkv_bias
  121. self.cls_token = cls_token
  122. self.qkv_projection_method = qkv_projection_method
  123. self.kernel_qkv = kernel_qkv
  124. self.padding_kv = padding_kv
  125. self.stride_kv = stride_kv
  126. self.padding_q = padding_q
  127. self.stride_q = stride_q
  128. self.initializer_range = initializer_range
  129. self.layer_norm_eps = layer_norm_eps
  130. __all__ = ["CvtConfig"]