configuration_lightglue.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/lightglue/modular_lightglue.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_lightglue.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2025 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. from ..superpoint import SuperPointConfig
  25. @auto_docstring(checkpoint="ETH-CVG/lightglue_superpoint")
  26. @strict
  27. class LightGlueConfig(PreTrainedConfig):
  28. r"""
  29. keypoint_detector_config (`Union[AutoConfig, dict]`, *optional*, defaults to `SuperPointConfig`):
  30. The config object or dictionary of the keypoint detector.
  31. descriptor_dim (`int`, *optional*, defaults to 256):
  32. The dimension of the descriptors.
  33. depth_confidence (`float`, *optional*, defaults to 0.95):
  34. The confidence threshold used to perform early stopping
  35. width_confidence (`float`, *optional*, defaults to 0.99):
  36. The confidence threshold used to prune points
  37. filter_threshold (`float`, *optional*, defaults to 0.1):
  38. The confidence threshold used to filter matches
  39. Examples:
  40. ```python
  41. >>> from transformers import LightGlueConfig, LightGlueForKeypointMatching
  42. >>> # Initializing a LightGlue style configuration
  43. >>> configuration = LightGlueConfig()
  44. >>> # Initializing a model from the LightGlue style configuration
  45. >>> model = LightGlueForKeypointMatching(configuration)
  46. >>> # Accessing the model configuration
  47. >>> configuration = model.config
  48. ```
  49. """
  50. model_type = "lightglue"
  51. sub_configs = {"keypoint_detector_config": AutoConfig}
  52. keypoint_detector_config: dict | SuperPointConfig | None = None
  53. descriptor_dim: int = 256
  54. num_hidden_layers: int = 9
  55. num_attention_heads: int = 4
  56. num_key_value_heads: int | None = None
  57. depth_confidence: float = 0.95
  58. width_confidence: float = 0.99
  59. filter_threshold: float = 0.1
  60. initializer_range: float = 0.02
  61. hidden_act: str = "gelu"
  62. attention_dropout: float | int = 0.0
  63. attention_bias: bool = True
  64. def __post_init__(self, **kwargs):
  65. if self.num_key_value_heads is None:
  66. self.num_key_value_heads = self.num_attention_heads
  67. # Keypoint Detector is forced into eager attention mode because SuperPoint does not have Attention
  68. # See https://github.com/huggingface/transformers/pull/31718#discussion_r2109733153
  69. if isinstance(self.keypoint_detector_config, dict):
  70. self.keypoint_detector_config["model_type"] = self.keypoint_detector_config.get("model_type", "superpoint")
  71. self.keypoint_detector_config = CONFIG_MAPPING[self.keypoint_detector_config["model_type"]](
  72. **self.keypoint_detector_config, attn_implementation="eager"
  73. )
  74. elif self.keypoint_detector_config is None:
  75. self.keypoint_detector_config = CONFIG_MAPPING["superpoint"](attn_implementation="eager")
  76. self.intermediate_size = self.descriptor_dim * 2
  77. self.hidden_size = self.descriptor_dim
  78. super().__post_init__(**kwargs)
  79. def validate_architecture(self):
  80. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  81. if self.descriptor_dim % self.num_attention_heads != 0:
  82. raise ValueError("descriptor_dim % num_heads is different from zero")
  83. __all__ = ["LightGlueConfig"]