configuration_superglue.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright 2024 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. from ..auto import CONFIG_MAPPING, AutoConfig
  18. @auto_docstring(checkpoint="magic-leap-community/superglue_indoor")
  19. @strict
  20. class SuperGlueConfig(PreTrainedConfig):
  21. r"""
  22. keypoint_detector_config (`Union[AutoConfig, dict]`, *optional*, defaults to `SuperPointConfig`):
  23. The config object or dictionary of the keypoint detector.
  24. keypoint_encoder_sizes (`list[int]`, *optional*, defaults to `[32, 64, 128, 256]`):
  25. The sizes of the keypoint encoder layers.
  26. gnn_layers_types (`list[str]`, *optional*, defaults to `['self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross']`):
  27. The types of the GNN layers. Must be either 'self' or 'cross'.
  28. sinkhorn_iterations (`int`, *optional*, defaults to 100):
  29. The number of Sinkhorn iterations.
  30. matching_threshold (`float`, *optional*, defaults to 0.0):
  31. The matching threshold.
  32. Examples:
  33. ```python
  34. >>> from transformers import SuperGlueConfig, SuperGlueModel
  35. >>> # Initializing a SuperGlue superglue style configuration
  36. >>> configuration = SuperGlueConfig()
  37. >>> # Initializing a model from the superglue style configuration
  38. >>> model = SuperGlueModel(configuration)
  39. >>> # Accessing the model configuration
  40. >>> configuration = model.config
  41. ```
  42. """
  43. model_type = "superglue"
  44. sub_configs = {"keypoint_detector_config": AutoConfig}
  45. keypoint_detector_config: dict | PreTrainedConfig | None = None
  46. hidden_size: int = 256
  47. keypoint_encoder_sizes: list[int] | None = None
  48. gnn_layers_types: list[str] | None = None
  49. num_attention_heads: int = 4
  50. sinkhorn_iterations: int = 100
  51. matching_threshold: float = 0.0
  52. initializer_range: float = 0.02
  53. is_decoder: bool = False
  54. attention_probs_dropout_prob: int | float = 0.0
  55. def __post_init__(self, **kwargs):
  56. self.gnn_layers_types = self.gnn_layers_types if self.gnn_layers_types is not None else ["self", "cross"] * 9
  57. self.keypoint_encoder_sizes = (
  58. self.keypoint_encoder_sizes if self.keypoint_encoder_sizes is not None else [32, 64, 128, 256]
  59. )
  60. if isinstance(self.keypoint_detector_config, dict):
  61. self.keypoint_detector_config["model_type"] = self.keypoint_detector_config.get("model_type", "superpoint")
  62. self.keypoint_detector_config = CONFIG_MAPPING[self.keypoint_detector_config["model_type"]](
  63. **self.keypoint_detector_config
  64. )
  65. elif self.keypoint_detector_config is None:
  66. self.keypoint_detector_config = CONFIG_MAPPING["superpoint"]()
  67. super().__post_init__(**kwargs)
  68. def validate_architecture(self):
  69. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  70. # Check whether all gnn_layers_types are either 'self' or 'cross'
  71. if not all(layer_type in ["self", "cross"] for layer_type in self.gnn_layers_types):
  72. raise ValueError("All gnn_layers_types must be either 'self' or 'cross'")
  73. if self.hidden_size % self.num_attention_heads != 0:
  74. raise ValueError("hidden_size % num_attention_heads is different from zero")
  75. __all__ = ["SuperGlueConfig"]