configuration_zoedepth.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright 2024 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. """ZoeDepth model configuration"""
  15. from typing import Literal
  16. from huggingface_hub.dataclasses import strict
  17. from ...backbone_utils import consolidate_backbone_kwargs_to_config
  18. from ...configuration_utils import PreTrainedConfig
  19. from ...utils import auto_docstring
  20. from ..auto.configuration_auto import AutoConfig
  21. ZOEDEPTH_PRETRAINED_CONFIG_ARCHIVE_MAP = {
  22. "Intel/zoedepth-nyu": "https://huggingface.co/Intel/zoedepth-nyu/resolve/main/config.json",
  23. }
  24. @auto_docstring(checkpoint="Intel/zoedepth-nyu")
  25. @strict
  26. class ZoeDepthConfig(PreTrainedConfig):
  27. r"""
  28. readout_type (`str`, *optional*, defaults to `"project"`):
  29. The readout type to use when processing the readout token (CLS token) of the intermediate hidden states of
  30. the ViT backbone. Can be one of [`"ignore"`, `"add"`, `"project"`].
  31. - "ignore" simply ignores the CLS token.
  32. - "add" passes the information from the CLS token to all other tokens by adding the representations.
  33. - "project" passes information to the other tokens by concatenating the readout to all other tokens before
  34. projecting the
  35. representation to the original feature dimension D using a linear layer followed by a GELU non-linearity.
  36. reassemble_factors (`list[int]`, *optional*, defaults to `[4, 2, 1, 0.5]`):
  37. The up/downsampling factors of the reassemble layers.
  38. neck_hidden_sizes (`list[str]`, *optional*, defaults to `[96, 192, 384, 768]`):
  39. The hidden sizes to project to for the feature maps of the backbone.
  40. fusion_hidden_size (`int`, *optional*, defaults to 256):
  41. The number of channels before fusion.
  42. head_in_index (`int`, *optional*, defaults to -1):
  43. The index of the features to use in the heads.
  44. use_batch_norm_in_fusion_residual (`bool`, *optional*, defaults to `False`):
  45. Whether to use batch normalization in the pre-activate residual units of the fusion blocks.
  46. use_bias_in_fusion_residual (`bool`, *optional*, defaults to `True`):
  47. Whether to use bias in the pre-activate residual units of the fusion blocks.
  48. num_relative_features (`int`, *optional*, defaults to 32):
  49. The number of features to use in the relative depth estimation head.
  50. add_projection (`bool`, *optional*, defaults to `False`):
  51. Whether to add a projection layer before the depth estimation head.
  52. bottleneck_features (`int`, *optional*, defaults to 256):
  53. The number of features in the bottleneck layer.
  54. num_attractors (`list[int], *optional*, defaults to `[16, 8, 4, 1]`):
  55. The number of attractors to use in each stage.
  56. bin_embedding_dim (`int`, *optional*, defaults to 128):
  57. The dimension of the bin embeddings.
  58. attractor_alpha (`int`, *optional*, defaults to 1000):
  59. The alpha value to use in the attractor.
  60. attractor_gamma (`int`, *optional*, defaults to 2):
  61. The gamma value to use in the attractor.
  62. attractor_kind (`str`, *optional*, defaults to `"mean"`):
  63. The kind of attractor to use. Can be one of [`"mean"`, `"sum"`].
  64. min_temp (`float`, *optional*, defaults to 0.0212):
  65. The minimum temperature value to consider.
  66. max_temp (`float`, *optional*, defaults to 50.0):
  67. The maximum temperature value to consider.
  68. bin_centers_type (`str`, *optional*, defaults to `"softplus"`):
  69. Activation type used for bin centers. Can be "normed" or "softplus". For "normed" bin centers, linear normalization trick
  70. is applied. This results in bounded bin centers. For "softplus", softplus activation is used and thus are unbounded.
  71. bin_configurations (`list[dict]`, *optional*, defaults to `[{'n_bins': 64, 'min_depth': 0.001, 'max_depth': 10.0}]`):
  72. Configuration for each of the bin heads.
  73. Each configuration should consist of the following keys:
  74. - name (`str`): The name of the bin head - only required in case of multiple bin configurations.
  75. - `n_bins` (`int`): The number of bins to use.
  76. - `min_depth` (`float`): The minimum depth value to consider.
  77. - `max_depth` (`float`): The maximum depth value to consider.
  78. In case only a single configuration is passed, the model will use a single head with the specified configuration.
  79. In case multiple configurations are passed, the model will use multiple heads with the specified configurations.
  80. num_patch_transformer_layers (`int`, *optional*):
  81. The number of transformer layers to use in the patch transformer. Only used in case of multiple bin configurations.
  82. patch_transformer_hidden_size (`int`, *optional*):
  83. The hidden size to use in the patch transformer. Only used in case of multiple bin configurations.
  84. patch_transformer_intermediate_size (`int`, *optional*):
  85. The intermediate size to use in the patch transformer. Only used in case of multiple bin configurations.
  86. patch_transformer_num_attention_heads (`int`, *optional*):
  87. The number of attention heads to use in the patch transformer. Only used in case of multiple bin configurations.
  88. Example:
  89. ```python
  90. >>> from transformers import ZoeDepthConfig, ZoeDepthForDepthEstimation
  91. >>> # Initializing a ZoeDepth zoedepth-large style configuration
  92. >>> configuration = ZoeDepthConfig()
  93. >>> # Initializing a model from the zoedepth-large style configuration
  94. >>> model = ZoeDepthForDepthEstimation(configuration)
  95. >>> # Accessing the model configuration
  96. >>> configuration = model.config
  97. ```"""
  98. model_type = "zoedepth"
  99. sub_configs = {"backbone_config": AutoConfig}
  100. backbone_config: dict | PreTrainedConfig | None = None
  101. hidden_act: str = "gelu"
  102. initializer_range: float = 0.02
  103. batch_norm_eps: float = 1e-05
  104. readout_type: Literal["ignore", "add", "project"] = "project"
  105. reassemble_factors: list[int | float] | tuple[int | float, ...] = (4, 2, 1, 0.5)
  106. neck_hidden_sizes: list[int] | tuple[int, ...] = (96, 192, 384, 768)
  107. fusion_hidden_size: int = 256
  108. head_in_index: int = -1
  109. use_batch_norm_in_fusion_residual: bool = False
  110. use_bias_in_fusion_residual: bool | None = None
  111. num_relative_features: int = 32
  112. add_projection: bool = False
  113. bottleneck_features: int = 256
  114. num_attractors: list[int] | tuple[int, ...] = (16, 8, 4, 1)
  115. bin_embedding_dim: int = 128
  116. attractor_alpha: int = 1000
  117. attractor_gamma: int = 2
  118. attractor_kind: Literal["mean", "sum"] = "mean"
  119. min_temp: float = 0.0212
  120. max_temp: float = 50.0
  121. bin_centers_type: str = "softplus"
  122. bin_configurations: list[dict] | None = None
  123. num_patch_transformer_layers: int | None = None
  124. patch_transformer_hidden_size: int | None = None
  125. patch_transformer_intermediate_size: int | None = None
  126. patch_transformer_num_attention_heads: int | None = None
  127. def __post_init__(self, **kwargs):
  128. self.backbone_config, kwargs = consolidate_backbone_kwargs_to_config(
  129. backbone_config=self.backbone_config,
  130. default_config_type="beit",
  131. default_config_kwargs={
  132. "image_size": 384,
  133. "num_hidden_layers": 24,
  134. "hidden_size": 1024,
  135. "intermediate_size": 4096,
  136. "num_attention_heads": 16,
  137. "use_relative_position_bias": True,
  138. "reshape_hidden_states": False,
  139. "out_features": ["stage6", "stage12", "stage18", "stage24"],
  140. },
  141. **kwargs,
  142. )
  143. self.bin_configurations = self.bin_configurations or [{"n_bins": 64, "min_depth": 0.001, "max_depth": 10.0}]
  144. super().__post_init__(**kwargs)
  145. __all__ = ["ZOEDEPTH_PRETRAINED_CONFIG_ARCHIVE_MAP", "ZoeDepthConfig"]