configuration_edgetam.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/edgetam/modular_edgetam.py.
  3. # Do NOT edit this file manually as any edits will be overwritten by the generation of
  4. # the file from the modular. If any change should be done, please apply the change to the
  5. # modular_edgetam.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2025 The Meta AI Authors and The HuggingFace Team. All rights reserved.
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. from huggingface_hub.dataclasses import strict
  21. from ...configuration_utils import PreTrainedConfig
  22. from ...utils import auto_docstring
  23. from ..auto import CONFIG_MAPPING, AutoConfig
  24. @auto_docstring(checkpoint="yonigozlan/EdgeTAM-hf")
  25. @strict
  26. class EdgeTamVisionConfig(PreTrainedConfig):
  27. r"""
  28. backbone_channel_list (`List[int]`, *optional*, defaults to `[384, 192, 96, 48]`):
  29. The list of channel dimensions for the backbone.
  30. backbone_feature_sizes (`List[List[int]]`, *optional*, defaults to `[[256, 256], [128, 128], [64, 64]]`):
  31. The spatial sizes of the feature maps from the backbone.
  32. fpn_hidden_size (`int`, *optional*, defaults to 256):
  33. The hidden dimension of the FPN.
  34. fpn_kernel_size (`int`, *optional*, defaults to 1):
  35. The kernel size for the convolutions in the neck.
  36. fpn_stride (`int`, *optional*, defaults to 1):
  37. The stride for the convolutions in the neck.
  38. fpn_padding (`int`, *optional*, defaults to 0):
  39. The padding for the convolutions in the neck.
  40. fpn_top_down_levels (`List[int]`, *optional*, defaults to `[2, 3]`):
  41. The levels for the top-down FPN connections.
  42. num_feature_levels (`int`, *optional*, defaults to 3):
  43. The number of feature levels from the FPN to use.
  44. """
  45. base_config_key = "vision_config"
  46. model_type = "edgetam_vision_model"
  47. sub_configs = {
  48. "backbone_config": AutoConfig,
  49. }
  50. backbone_config: dict | PreTrainedConfig | None = None
  51. backbone_channel_list: list[int] | None = None
  52. backbone_feature_sizes: list | None = None
  53. fpn_hidden_size: int = 256
  54. fpn_kernel_size: int = 1
  55. fpn_stride: int = 1
  56. fpn_padding: int = 0
  57. fpn_top_down_levels: list[int] | None = None
  58. num_feature_levels: int = 3
  59. hidden_act: str = "gelu"
  60. layer_norm_eps: float = 1e-6
  61. initializer_range: float = 0.02
  62. def __post_init__(self, **kwargs):
  63. self.backbone_channel_list = (
  64. [384, 192, 96, 48] if self.backbone_channel_list is None else self.backbone_channel_list
  65. )
  66. self.backbone_feature_sizes = (
  67. [[256, 256], [128, 128], [64, 64]] if self.backbone_feature_sizes is None else self.backbone_feature_sizes
  68. )
  69. self.fpn_top_down_levels = [2, 3] if self.fpn_top_down_levels is None else self.fpn_top_down_levels
  70. if isinstance(self.backbone_config, dict):
  71. self.backbone_config["model_type"] = self.backbone_config.get("model_type", "timm_wrapper")
  72. self.backbone_config = CONFIG_MAPPING[self.backbone_config["model_type"]](**self.backbone_config)
  73. elif self.backbone_config is None:
  74. self.backbone_config = AutoConfig.from_pretrained(
  75. "timm/repvit_m1.dist_in1k",
  76. model_args={"in_chans": 3, "features_only": True, "out_indices": [0, 1, 2, 3]},
  77. )
  78. super().__post_init__(**kwargs)
  79. @auto_docstring(checkpoint="yonigozlan/EdgeTAM-hf")
  80. @strict
  81. class EdgeTamPromptEncoderConfig(PreTrainedConfig):
  82. r"""
  83. mask_input_channels (`int`, *optional*, defaults to 16):
  84. The number of channels to be fed to the `MaskDecoder` module.
  85. num_point_embeddings (`int`, *optional*, defaults to 4):
  86. The number of point embeddings to be used.
  87. scale (`float`, *optional*, defaults to 1):
  88. The scale factor for the prompt encoder.
  89. """
  90. base_config_key = "prompt_encoder_config"
  91. hidden_size: int = 256
  92. image_size: int | list[int] | tuple[int, int] = 1024
  93. patch_size: int | list[int] | tuple[int, int] = 16
  94. mask_input_channels: int = 16
  95. num_point_embeddings: int = 4
  96. hidden_act: str = "gelu"
  97. layer_norm_eps: float = 1e-6
  98. scale: int = 1
  99. @auto_docstring(checkpoint="yonigozlan/EdgeTAM-hf")
  100. @strict
  101. class EdgeTamMaskDecoderConfig(PreTrainedConfig):
  102. r"""
  103. mlp_dim (`int`, *optional*, defaults to 2048):
  104. The dimension of the MLP in the two-way transformer.
  105. attention_downsample_rate (`int`, *optional*, defaults to 2):
  106. The downsample rate for the attention layers.
  107. num_multimask_outputs (`int`, *optional*, defaults to 3):
  108. The number of multimask outputs.
  109. iou_head_depth (`int`, *optional*, defaults to 3):
  110. The depth of the IoU head.
  111. iou_head_hidden_dim (`int`, *optional*, defaults to 256):
  112. The hidden dimension of the IoU head.
  113. dynamic_multimask_via_stability (`bool`, *optional*, defaults to `True`):
  114. Whether to use dynamic multimask via stability.
  115. dynamic_multimask_stability_delta (`float`, *optional*, defaults to 0.05):
  116. The stability delta for the dynamic multimask.
  117. dynamic_multimask_stability_thresh (`float`, *optional*, defaults to 0.98):
  118. The stability threshold for the dynamic multimask.
  119. """
  120. base_config_key = "mask_decoder_config"
  121. hidden_size: int = 256
  122. hidden_act: str = "gelu"
  123. mlp_dim: int = 2048
  124. num_hidden_layers: int = 2
  125. num_attention_heads: int = 8
  126. attention_downsample_rate: int = 2
  127. num_multimask_outputs: int = 3
  128. iou_head_depth: int = 3
  129. iou_head_hidden_dim: int = 256
  130. dynamic_multimask_via_stability: bool = True
  131. dynamic_multimask_stability_delta: float = 0.05
  132. dynamic_multimask_stability_thresh: float = 0.98
  133. @auto_docstring(checkpoint="yonigozlan/EdgeTAM-hf")
  134. @strict
  135. class EdgeTamConfig(PreTrainedConfig):
  136. r"""
  137. prompt_encoder_config (Union[`dict`, `EdgeTamPromptEncoderConfig`], *optional*):
  138. Dictionary of configuration options used to initialize [`EdgeTamPromptEncoderConfig`].
  139. mask_decoder_config (Union[`dict`, `EdgeTamMaskDecoderConfig`], *optional*):
  140. Dictionary of configuration options used to initialize [`EdgeTamMaskDecoderConfig`].
  141. Example:
  142. ```python
  143. >>> from transformers import (
  144. ... EdgeTamVisionConfig,
  145. ... EdgeTamPromptEncoderConfig,
  146. ... EdgeTamMaskDecoderConfig,
  147. ... EdgeTamModel,
  148. ... )
  149. >>> # Initializing a EdgeTamConfig with `"facebook/edgetam.1_hiera_tiny"` style configuration
  150. >>> configuration = EdgeTamConfig()
  151. >>> # Initializing a EdgeTamModel (with random weights) from the `"facebook/edgetam.1_hiera_tiny"` style configuration
  152. >>> model = EdgeTamModel(configuration)
  153. >>> # Accessing the model configuration
  154. >>> configuration = model.config
  155. >>> # We can also initialize a EdgeTamConfig from a EdgeTamVisionConfig, EdgeTamPromptEncoderConfig, and EdgeTamMaskDecoderConfig
  156. >>> # Initializing EDGETAM vision encoder, memory attention, and memory encoder configurations
  157. >>> vision_config = EdgeTamVisionConfig()
  158. >>> prompt_encoder_config = EdgeTamPromptEncoderConfig()
  159. >>> mask_decoder_config = EdgeTamMaskDecoderConfig()
  160. >>> config = EdgeTamConfig(vision_config, prompt_encoder_config, mask_decoder_config)
  161. ```
  162. """
  163. model_type = "edgetam"
  164. sub_configs = {
  165. "vision_config": AutoConfig,
  166. "prompt_encoder_config": EdgeTamPromptEncoderConfig,
  167. "mask_decoder_config": EdgeTamMaskDecoderConfig,
  168. }
  169. vision_config: dict | PreTrainedConfig | None = None
  170. prompt_encoder_config: dict | PreTrainedConfig | None = None
  171. mask_decoder_config: dict | PreTrainedConfig | None = None
  172. initializer_range: float = 0.02
  173. def __post_init__(self, **kwargs):
  174. if isinstance(self.vision_config, dict):
  175. self.vision_config["model_type"] = self.vision_config.get("model_type", "edgetam_vision_model")
  176. self.vision_config = CONFIG_MAPPING[self.vision_config["model_type"]](**self.vision_config)
  177. elif self.vision_config is None:
  178. self.vision_config = CONFIG_MAPPING["edgetam_vision_model"]()
  179. if isinstance(self.prompt_encoder_config, dict):
  180. self.prompt_encoder_config = EdgeTamPromptEncoderConfig(**self.prompt_encoder_config)
  181. elif self.prompt_encoder_config is None:
  182. self.prompt_encoder_config = EdgeTamPromptEncoderConfig()
  183. if isinstance(self.mask_decoder_config, dict):
  184. self.mask_decoder_config = EdgeTamMaskDecoderConfig(**self.mask_decoder_config)
  185. elif self.mask_decoder_config is None:
  186. self.mask_decoder_config = EdgeTamMaskDecoderConfig()
  187. super().__post_init__(**kwargs)
  188. __all__ = ["EdgeTamConfig", "EdgeTamVisionConfig", "EdgeTamPromptEncoderConfig", "EdgeTamMaskDecoderConfig"]