configuration_vitdet.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # coding=utf-8
  2. # Copyright 2023 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. """VitDet model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices
  19. logger = logging.get_logger(__name__)
  20. class VitDetConfig(BackboneConfigMixin, PretrainedConfig):
  21. r"""
  22. This is the configuration class to store the configuration of a [`VitDetModel`]. It is used to instantiate an
  23. VitDet model according to the specified arguments, defining the model architecture. Instantiating a configuration
  24. with the defaults will yield a similar configuration to that of the VitDet
  25. [google/vitdet-base-patch16-224](https://huggingface.co/google/vitdet-base-patch16-224) architecture.
  26. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  27. documentation from [`PretrainedConfig`] for more information.
  28. Args:
  29. hidden_size (`int`, *optional*, defaults to 768):
  30. Dimensionality of the encoder layers and the pooler layer.
  31. num_hidden_layers (`int`, *optional*, defaults to 12):
  32. Number of hidden layers in the Transformer encoder.
  33. num_attention_heads (`int`, *optional*, defaults to 12):
  34. Number of attention heads for each attention layer in the Transformer encoder.
  35. mlp_ratio (`int`, *optional*, defaults to 4):
  36. Ratio of mlp hidden dim to embedding dim.
  37. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
  38. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  39. `"relu"`, `"selu"` and `"gelu_new"` are supported.
  40. dropout_prob (`float`, *optional*, defaults to 0.0):
  41. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  42. initializer_range (`float`, *optional*, defaults to 0.02):
  43. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  44. layer_norm_eps (`float`, *optional*, defaults to 1e-06):
  45. The epsilon used by the layer normalization layers.
  46. image_size (`int`, *optional*, defaults to 224):
  47. The size (resolution) of each image.
  48. pretrain_image_size (`int`, *optional*, defaults to 224):
  49. The size (resolution) of each image during pretraining.
  50. patch_size (`int`, *optional*, defaults to 16):
  51. The size (resolution) of each patch.
  52. num_channels (`int`, *optional*, defaults to 3):
  53. The number of input channels.
  54. qkv_bias (`bool`, *optional*, defaults to `True`):
  55. Whether to add a bias to the queries, keys and values.
  56. drop_path_rate (`float`, *optional*, defaults to 0.0):
  57. Stochastic depth rate.
  58. window_block_indices (`list[int]`, *optional*, defaults to `[]`):
  59. List of indices of blocks that should have window attention instead of regular global self-attention.
  60. residual_block_indices (`list[int]`, *optional*, defaults to `[]`):
  61. List of indices of blocks that should have an extra residual block after the MLP.
  62. use_absolute_position_embeddings (`bool`, *optional*, defaults to `True`):
  63. Whether to add absolute position embeddings to the patch embeddings.
  64. use_relative_position_embeddings (`bool`, *optional*, defaults to `False`):
  65. Whether to add relative position embeddings to the attention maps.
  66. window_size (`int`, *optional*, defaults to 0):
  67. The size of the attention window.
  68. out_features (`list[str]`, *optional*):
  69. If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc.
  70. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the
  71. corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the
  72. same order as defined in the `stage_names` attribute.
  73. out_indices (`list[int]`, *optional*):
  74. If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how
  75. many stages the model has). If unset and `out_features` is set, will default to the corresponding stages.
  76. If unset and `out_features` is unset, will default to the last stage. Must be in the
  77. same order as defined in the `stage_names` attribute.
  78. Example:
  79. ```python
  80. >>> from transformers import VitDetConfig, VitDetModel
  81. >>> # Initializing a VitDet configuration
  82. >>> configuration = VitDetConfig()
  83. >>> # Initializing a model (with random weights) from the configuration
  84. >>> model = VitDetModel(configuration)
  85. >>> # Accessing the model configuration
  86. >>> configuration = model.config
  87. ```"""
  88. model_type = "vitdet"
  89. def __init__(
  90. self,
  91. hidden_size=768,
  92. num_hidden_layers=12,
  93. num_attention_heads=12,
  94. mlp_ratio=4,
  95. hidden_act="gelu",
  96. dropout_prob=0.0,
  97. initializer_range=0.02,
  98. layer_norm_eps=1e-6,
  99. image_size=224,
  100. pretrain_image_size=224,
  101. patch_size=16,
  102. num_channels=3,
  103. qkv_bias=True,
  104. drop_path_rate=0.0,
  105. window_block_indices=[],
  106. residual_block_indices=[],
  107. use_absolute_position_embeddings=True,
  108. use_relative_position_embeddings=False,
  109. window_size=0,
  110. out_features=None,
  111. out_indices=None,
  112. **kwargs,
  113. ):
  114. super().__init__(**kwargs)
  115. self.hidden_size = hidden_size
  116. self.num_hidden_layers = num_hidden_layers
  117. self.num_attention_heads = num_attention_heads
  118. self.mlp_ratio = mlp_ratio
  119. self.hidden_act = hidden_act
  120. self.dropout_prob = dropout_prob
  121. self.initializer_range = initializer_range
  122. self.layer_norm_eps = layer_norm_eps
  123. self.image_size = image_size
  124. self.pretrain_image_size = pretrain_image_size
  125. self.patch_size = patch_size
  126. self.num_channels = num_channels
  127. self.qkv_bias = qkv_bias
  128. self.drop_path_rate = drop_path_rate
  129. self.window_block_indices = window_block_indices
  130. self.residual_block_indices = residual_block_indices
  131. self.use_absolute_position_embeddings = use_absolute_position_embeddings
  132. self.use_relative_position_embeddings = use_relative_position_embeddings
  133. self.window_size = window_size
  134. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, self.num_hidden_layers + 1)]
  135. self._out_features, self._out_indices = get_aligned_output_features_output_indices(
  136. out_features=out_features, out_indices=out_indices, stage_names=self.stage_names
  137. )
  138. __all__ = ["VitDetConfig"]