configuration_dinov2.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright 2023 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. """DINOv2 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="google/dinov2-base-patch16-224")
  20. @strict
  21. class Dinov2Config(BackboneConfigMixin, PreTrainedConfig):
  22. r"""
  23. layerscale_value (`float`, *optional*, defaults to 1.0):
  24. Initial value to use for layer scale.
  25. use_swiglu_ffn (`bool`, *optional*, defaults to `False`):
  26. Whether to use the SwiGLU feedforward neural network.
  27. apply_layernorm (`bool`, *optional*, defaults to `True`):
  28. Whether to apply layer normalization to the feature maps in case the model is used as backbone.
  29. reshape_hidden_states (`bool`, *optional*, defaults to `True`):
  30. Whether to reshape the feature maps to 4D tensors of shape `(batch_size, hidden_size, height, width)` in
  31. case the model is used as backbone. If `False`, the feature maps will be 3D tensors of shape `(batch_size,
  32. seq_len, hidden_size)`.
  33. use_mask_token (`bool`, *optional*, defaults to `True`):
  34. Whether to use mask_token in embeddings.
  35. Example:
  36. ```python
  37. >>> from transformers import Dinov2Config, Dinov2Model
  38. >>> # Initializing a Dinov2 dinov2-base-patch16-224 style configuration
  39. >>> configuration = Dinov2Config()
  40. >>> # Initializing a model (with random weights) from the dinov2-base-patch16-224 style configuration
  41. >>> model = Dinov2Model(configuration)
  42. >>> # Accessing the model configuration
  43. >>> configuration = model.config
  44. ```"""
  45. model_type = "dinov2"
  46. hidden_size: int = 768
  47. num_hidden_layers: int = 12
  48. num_attention_heads: int = 12
  49. mlp_ratio: int = 4
  50. hidden_act: str = "gelu"
  51. hidden_dropout_prob: float | int = 0.0
  52. attention_probs_dropout_prob: float | int = 0.0
  53. initializer_range: float = 0.02
  54. layer_norm_eps: float = 1e-6
  55. image_size: int | list[int] | tuple[int, int] = 224
  56. patch_size: int | list[int] | tuple[int, int] = 14
  57. num_channels: int = 3
  58. qkv_bias: bool = True
  59. layerscale_value: float = 1.0
  60. drop_path_rate: float | int = 0.0
  61. use_swiglu_ffn: bool = False
  62. _out_features: list[str] | None = None
  63. _out_indices: list[int] | None = None
  64. apply_layernorm: bool = True
  65. reshape_hidden_states: bool = True
  66. use_mask_token: bool = True
  67. def __post_init__(self, **kwargs):
  68. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, self.num_hidden_layers + 1)]
  69. self.set_output_features_output_indices(
  70. out_indices=kwargs.pop("out_indices", None), out_features=kwargs.pop("out_features", None)
  71. )
  72. super().__post_init__(**kwargs)
  73. __all__ = ["Dinov2Config"]