configuration_efficientloftr.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright 2025 The HuggingFace 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. from huggingface_hub.dataclasses import strict
  15. from ...configuration_utils import PreTrainedConfig
  16. from ...utils import auto_docstring
  17. @auto_docstring(checkpoint="zju-community/efficientloftr")
  18. @strict
  19. class EfficientLoFTRConfig(PreTrainedConfig):
  20. r"""
  21. stage_num_blocks (`List`, *optional*, defaults to [1, 2, 4, 14]):
  22. The number of blocks in each stages
  23. stage_stride (`List`, *optional*, defaults to [2, 1, 2, 2]):
  24. The stride used in each stage
  25. q_aggregation_kernel_size (`int`, *optional*, defaults to 4):
  26. The kernel size of the aggregation of query states in the fusion network
  27. kv_aggregation_kernel_size (`int`, *optional*, defaults to 4):
  28. The kernel size of the aggregation of key and value states in the fusion network
  29. q_aggregation_stride (`int`, *optional*, defaults to 4):
  30. The stride of the aggregation of query states in the fusion network
  31. kv_aggregation_stride (`int`, *optional*, defaults to 4):
  32. The stride of the aggregation of key and value states in the fusion network
  33. num_attention_layers (`int`, *optional*, defaults to 4):
  34. Number of attention layers in the LocalFeatureTransformer
  35. mlp_activation_function (`str`, *optional*, defaults to `"leaky_relu"`):
  36. Activation function used in the attention mlp layer.
  37. coarse_matching_skip_softmax (`bool`, *optional*, defaults to `False`):
  38. Whether to skip softmax or not at the coarse matching step.
  39. coarse_matching_threshold (`float`, *optional*, defaults to 0.2):
  40. The threshold for the minimum score required for a match.
  41. coarse_matching_temperature (`float`, *optional*, defaults to 0.1):
  42. The temperature to apply to the coarse similarity matrix
  43. coarse_matching_border_removal (`int`, *optional*, defaults to 2):
  44. The size of the border to remove during coarse matching
  45. fine_kernel_size (`int`, *optional*, defaults to 8):
  46. Kernel size used for the fine feature matching
  47. batch_norm_eps (`float`, *optional*, defaults to 1e-05):
  48. The epsilon used by the batch normalization layers
  49. fine_matching_slice_dim (`int`, *optional*, defaults to 8):
  50. The size of the slice used to divide the fine features for the first and second fine matching stages.
  51. fine_matching_regress_temperature (`float`, *optional*, defaults to 10.0):
  52. The temperature to apply to the fine similarity matrix
  53. Examples:
  54. ```python
  55. >>> from transformers import EfficientLoFTRConfig, EfficientLoFTRForKeypointMatching
  56. >>> # Initializing a EfficientLoFTR configuration
  57. >>> configuration = EfficientLoFTRConfig()
  58. >>> # Initializing a model from the EfficientLoFTR configuration
  59. >>> model = EfficientLoFTRForKeypointMatching(configuration)
  60. >>> # Accessing the model configuration
  61. >>> configuration = model.config
  62. ```
  63. """
  64. model_type = "efficientloftr"
  65. stage_num_blocks: list[int] | None = None
  66. out_features: list[int] | None = None
  67. stage_stride: list[int] | None = None
  68. hidden_size: int = 256
  69. activation_function: str = "relu"
  70. q_aggregation_kernel_size: int = 4
  71. kv_aggregation_kernel_size: int = 4
  72. q_aggregation_stride: int = 4
  73. kv_aggregation_stride: int = 4
  74. num_attention_layers: int = 4
  75. num_attention_heads: int = 8
  76. attention_dropout: float | int = 0.0
  77. attention_bias: bool = False
  78. mlp_activation_function: str = "leaky_relu"
  79. coarse_matching_skip_softmax: bool = False
  80. coarse_matching_threshold: float = 0.2
  81. coarse_matching_temperature: float = 0.1
  82. coarse_matching_border_removal: int = 2
  83. fine_kernel_size: int = 8
  84. batch_norm_eps: float = 1e-5
  85. rope_parameters: dict | None = None
  86. fine_matching_slice_dim: int = 8
  87. fine_matching_regress_temperature: float = 10.0
  88. initializer_range: float = 0.02
  89. def __post_init__(self, **kwargs):
  90. # Stage level of RepVGG
  91. self.stage_num_blocks = self.stage_num_blocks if self.stage_num_blocks is not None else [1, 2, 4, 14]
  92. self.stage_stride = self.stage_stride if self.stage_stride is not None else [2, 1, 2, 2]
  93. self.out_features = self.out_features if self.out_features is not None else [64, 64, 128, 256]
  94. self.stage_in_channels = [1] + self.out_features[:-1]
  95. # Block level of RepVGG
  96. self.stage_block_stride = [
  97. [stride] + [1] * (num_blocks - 1) for stride, num_blocks in zip(self.stage_stride, self.stage_num_blocks)
  98. ]
  99. self.stage_block_out_channels = [
  100. [self.out_features[stage_idx]] * num_blocks for stage_idx, num_blocks in enumerate(self.stage_num_blocks)
  101. ]
  102. self.stage_block_in_channels = [
  103. [self.stage_in_channels[stage_idx]] + self.stage_block_out_channels[stage_idx][:-1]
  104. for stage_idx in range(len(self.stage_num_blocks))
  105. ]
  106. self.num_key_value_heads = self.num_attention_heads
  107. self.fine_fusion_dims = list(reversed(self.out_features))[:-1]
  108. self.intermediate_size = self.hidden_size * 2
  109. kwargs.setdefault("partial_rotary_factor", 4.0) # assign default for BC
  110. super().__post_init__(**kwargs)
  111. def validate_architecture(self):
  112. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  113. if self.hidden_size != self.out_features[-1]:
  114. raise ValueError(
  115. f"hidden_size should be equal to the last value in out_features. hidden_size = {self.hidden_size}, out_features = {self.out_features[-1]}"
  116. )
  117. __all__ = ["EfficientLoFTRConfig"]