configuration_vitdet.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. """VitDet 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/vitdet-base-patch16-224")
  20. @strict
  21. class VitDetConfig(BackboneConfigMixin, PreTrainedConfig):
  22. r"""
  23. pretrain_image_size (`int`, *optional*, defaults to 224):
  24. The size (resolution) of each image during pretraining.
  25. window_block_indices (`list[int]`, *optional*, defaults to `[]`):
  26. List of indices of blocks that should have window attention instead of regular global self-attention.
  27. residual_block_indices (`list[int]`, *optional*, defaults to `[]`):
  28. List of indices of blocks that should have an extra residual block after the MLP.
  29. use_relative_position_embeddings (`bool`, *optional*, defaults to `False`):
  30. Whether to add relative position embeddings to the attention maps.
  31. window_size (`int`, *optional*, defaults to 0):
  32. The size of the attention window.
  33. Example:
  34. ```python
  35. >>> from transformers import VitDetConfig, VitDetModel
  36. >>> # Initializing a VitDet configuration
  37. >>> configuration = VitDetConfig()
  38. >>> # Initializing a model (with random weights) from the configuration
  39. >>> model = VitDetModel(configuration)
  40. >>> # Accessing the model configuration
  41. >>> configuration = model.config
  42. ```"""
  43. model_type = "vitdet"
  44. hidden_size: int = 768
  45. num_hidden_layers: int = 12
  46. num_attention_heads: int = 12
  47. mlp_ratio: int = 4
  48. hidden_act: str = "gelu"
  49. dropout_prob: float | int = 0.0
  50. initializer_range: float = 0.02
  51. layer_norm_eps: float = 1e-6
  52. image_size: int | list[int] | tuple[int, int] = 224
  53. pretrain_image_size: int | list[int] | tuple[int, int] = 224
  54. patch_size: int | list[int] | tuple[int, int] = 16
  55. num_channels: int = 3
  56. qkv_bias: bool = True
  57. drop_path_rate: float | int = 0.0
  58. window_block_indices: list[int] | tuple[int, ...] = ()
  59. residual_block_indices: list[int] | tuple[int, ...] = ()
  60. use_absolute_position_embeddings: bool = True
  61. use_relative_position_embeddings: bool = False
  62. window_size: int = 0
  63. _out_features: list[str] | None = None
  64. _out_indices: list[int] | None = None
  65. def __post_init__(self, **kwargs):
  66. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, self.num_hidden_layers + 1)]
  67. self.set_output_features_output_indices(
  68. out_indices=kwargs.pop("out_indices", None), out_features=kwargs.pop("out_features", None)
  69. )
  70. super().__post_init__(**kwargs)
  71. __all__ = ["VitDetConfig"]