configuration_beit.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright Microsoft Research and The HuggingFace Inc. team. All rights reserved.
  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. """BEiT model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...backbone_utils import BackboneConfigMixin
  17. from ...configuration_utils import PreTrainedConfig
  18. from ...utils import auto_docstring
  19. @auto_docstring(checkpoint="microsoft/beit-base-patch16-224-pt22k")
  20. @strict
  21. class BeitConfig(BackboneConfigMixin, PreTrainedConfig):
  22. r"""
  23. use_mask_token (`bool`, *optional*, defaults to `False`):
  24. Whether to use a mask token for masked image modeling.
  25. use_relative_position_bias (`bool`, *optional*, defaults to `False`):
  26. Whether to use T5-style relative position embeddings in the self-attention layers.
  27. use_shared_relative_position_bias (`bool`, *optional*, defaults to `False`):
  28. Whether to use the same relative position embeddings across all self-attention layers of the Transformer.
  29. use_mean_pooling (`bool`, *optional*, defaults to `True`):
  30. Whether to mean pool the final hidden states of the patches instead of using the final hidden state of the
  31. CLS token, before applying the classification head.
  32. pool_scales (`tuple[int]`, *optional*, defaults to `[1, 2, 3, 6]`):
  33. Pooling scales used in Pooling Pyramid Module applied on the last feature map.
  34. use_auxiliary_head (`bool`, *optional*, defaults to `True`):
  35. Whether to use an auxiliary head during training.
  36. auxiliary_loss_weight (`float`, *optional*, defaults to 0.4):
  37. Weight of the cross-entropy loss of the auxiliary head.
  38. auxiliary_channels (`int`, *optional*, defaults to 256):
  39. Number of channels to use in the auxiliary head.
  40. auxiliary_num_convs (`int`, *optional*, defaults to 1):
  41. Number of convolutional layers to use in the auxiliary head.
  42. auxiliary_concat_input (`bool`, *optional*, defaults to `False`):
  43. Whether to concatenate the output of the auxiliary head with the input before the classification layer.
  44. add_fpn (`bool`, *optional*, defaults to `False`):
  45. Whether to add a FPN as part of the backbone. Only relevant for [`BeitBackbone`].
  46. reshape_hidden_states (`bool`, *optional*, defaults to `True`):
  47. Whether to reshape the feature maps to 4D tensors of shape `(batch_size, hidden_size, height, width)` in
  48. case the model is used as backbone. If `False`, the feature maps will be 3D tensors of shape `(batch_size,
  49. seq_len, hidden_size)`. Only relevant for [`BeitBackbone`].
  50. Example:
  51. ```python
  52. >>> from transformers import BeitConfig, BeitModel
  53. >>> # Initializing a BEiT beit-base-patch16-224-pt22k style configuration
  54. >>> configuration = BeitConfig()
  55. >>> # Initializing a model (with random weights) from the beit-base-patch16-224-pt22k style configuration
  56. >>> model = BeitModel(configuration)
  57. >>> # Accessing the model configuration
  58. >>> configuration = model.config
  59. ```"""
  60. model_type = "beit"
  61. vocab_size: int = 8192
  62. hidden_size: int = 768
  63. num_hidden_layers: int = 12
  64. num_attention_heads: int = 12
  65. intermediate_size: int = 3072
  66. hidden_act: str = "gelu"
  67. hidden_dropout_prob: float | int = 0.0
  68. attention_probs_dropout_prob: float | int = 0.0
  69. initializer_range: float = 0.02
  70. layer_norm_eps: float = 1e-12
  71. image_size: int | list[int] | tuple[int, int] = 224
  72. patch_size: int | list[int] | tuple[int, int] = 16
  73. num_channels: int = 3
  74. use_mask_token: bool = False
  75. use_absolute_position_embeddings: bool = False
  76. use_relative_position_bias: bool = False
  77. use_shared_relative_position_bias: bool = False
  78. layer_scale_init_value: float = 0.1
  79. drop_path_rate: float | int = 0.1
  80. use_mean_pooling: bool = True
  81. pool_scales: list[int] | tuple[int, ...] = (1, 2, 3, 6)
  82. use_auxiliary_head: bool = True
  83. auxiliary_loss_weight: float = 0.4
  84. auxiliary_channels: int = 256
  85. auxiliary_num_convs: int = 1
  86. auxiliary_concat_input: bool = False
  87. semantic_loss_ignore_index: int = 255
  88. _out_features: list[str] | None = None
  89. _out_indices: list[int] | None = None
  90. add_fpn: bool = False
  91. reshape_hidden_states: bool = True
  92. def __post_init__(self, **kwargs):
  93. if "segmentation_indices" in kwargs and kwargs.get("out_indices") is None:
  94. kwargs["out_indices"] = kwargs.pop("segmentation_indices")
  95. # backbone attributes
  96. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, self.num_hidden_layers + 1)]
  97. self.set_output_features_output_indices(
  98. out_indices=kwargs.pop("out_indices", None), out_features=kwargs.pop("out_features", None)
  99. )
  100. super().__post_init__(**kwargs)
  101. __all__ = ["BeitConfig"]