configuration_owlvit.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright 2022 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. """OWL-ViT model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring, logging
  18. logger = logging.get_logger(__name__)
  19. @auto_docstring(checkpoint="google/owlvit-base-patch16")
  20. @strict
  21. class OwlViTTextConfig(PreTrainedConfig):
  22. r"""
  23. Example:
  24. ```python
  25. >>> from transformers import OwlViTTextConfig, OwlViTTextModel
  26. >>> # Initializing a OwlViTTextModel with google/owlvit-base-patch32 style configuration
  27. >>> configuration = OwlViTTextConfig()
  28. >>> # Initializing a OwlViTTextConfig from the google/owlvit-base-patch32 style configuration
  29. >>> model = OwlViTTextModel(configuration)
  30. >>> # Accessing the model configuration
  31. >>> configuration = model.config
  32. ```"""
  33. model_type = "owlvit_text_model"
  34. base_config_key = "text_config"
  35. vocab_size: int = 49408
  36. hidden_size: int = 512
  37. intermediate_size: int = 2048
  38. num_hidden_layers: int = 12
  39. num_attention_heads: int = 8
  40. max_position_embeddings: int = 16
  41. hidden_act: str = "quick_gelu"
  42. layer_norm_eps: float = 1e-5
  43. attention_dropout: float | int = 0.0
  44. initializer_range: float = 0.02
  45. initializer_factor: float = 1.0
  46. pad_token_id: int | None = 0
  47. bos_token_id: int | None = 49406
  48. eos_token_id: int | list[int] | None = 49407
  49. @auto_docstring(checkpoint="google/owlvit-base-patch16")
  50. @strict
  51. class OwlViTVisionConfig(PreTrainedConfig):
  52. r"""
  53. Example:
  54. ```python
  55. >>> from transformers import OwlViTVisionConfig, OwlViTVisionModel
  56. >>> # Initializing a OwlViTVisionModel with google/owlvit-base-patch32 style configuration
  57. >>> configuration = OwlViTVisionConfig()
  58. >>> # Initializing a OwlViTVisionModel model from the google/owlvit-base-patch32 style configuration
  59. >>> model = OwlViTVisionModel(configuration)
  60. >>> # Accessing the model configuration
  61. >>> configuration = model.config
  62. ```"""
  63. model_type = "owlvit_vision_model"
  64. base_config_key = "vision_config"
  65. hidden_size: int = 768
  66. intermediate_size: int = 3072
  67. num_hidden_layers: int = 12
  68. num_attention_heads: int = 12
  69. num_channels: int = 3
  70. image_size: int | list[int] | tuple[int, int] = 768
  71. patch_size: int | list[int] | tuple[int, int] = 32
  72. hidden_act: str = "quick_gelu"
  73. layer_norm_eps: float = 1e-5
  74. attention_dropout: float | int = 0.0
  75. initializer_range: float = 0.02
  76. initializer_factor: float = 1.0
  77. @auto_docstring(checkpoint="google/owlvit-base-patch16")
  78. @strict
  79. class OwlViTConfig(PreTrainedConfig):
  80. model_type = "owlvit"
  81. sub_configs = {"text_config": OwlViTTextConfig, "vision_config": OwlViTVisionConfig}
  82. text_config: dict | PreTrainedConfig | None = None
  83. vision_config: dict | PreTrainedConfig | None = None
  84. projection_dim: int = 512
  85. logit_scale_init_value: float = 2.6592
  86. return_dict: bool = True
  87. initializer_factor: float = 1.0
  88. def __post_init__(self, **kwargs):
  89. if self.text_config is None:
  90. self.text_config = OwlViTTextConfig()
  91. logger.info("`text_config` is `None`. initializing the `OwlViTTextConfig` with default values.")
  92. elif isinstance(self.text_config, dict):
  93. self.text_config = OwlViTTextConfig(**self.text_config)
  94. if self.vision_config is None:
  95. self.vision_config = OwlViTVisionConfig()
  96. logger.info("`vision_config` is `None`. initializing the `OwlViTVisionConfig` with default values.")
  97. elif isinstance(self.vision_config, dict):
  98. self.vision_config = OwlViTVisionConfig(**self.vision_config)
  99. super().__post_init__(**kwargs)
  100. __all__ = ["OwlViTConfig", "OwlViTTextConfig", "OwlViTVisionConfig"]