configuration_focalnet.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2023 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. """FocalNet model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...backbone_utils import BackboneConfigMixin
  17. from ...configuration_utils import PreTrainedConfig
  18. from ...utils import auto_docstring
  19. @auto_docstring(checkpoint="microsoft/focalnet-tiny")
  20. @strict
  21. class FocalNetConfig(BackboneConfigMixin, PreTrainedConfig):
  22. r"""
  23. use_conv_embed (`bool`, *optional*, defaults to `False`):
  24. Whether to use convolutional embedding. The authors noted that using convolutional embedding usually
  25. improve the performance, but it's not used by default.
  26. focal_levels (`list(int)`, *optional*, defaults to `[2, 2, 2, 2]`):
  27. Number of focal levels in each layer of the respective stages in the encoder.
  28. focal_windows (`list(int)`, *optional*, defaults to `[3, 3, 3, 3]`):
  29. Focal window size in each layer of the respective stages in the encoder.
  30. hidden_dropout_prob (`float`, *optional*, defaults to 0.0):
  31. The dropout probability for all fully connected layers in the embeddings and encoder.
  32. use_layerscale (`bool`, *optional*, defaults to `False`):
  33. Whether to use layer scale in the encoder.
  34. layerscale_value (`float`, *optional*, defaults to 0.0001):
  35. The initial value of the layer scale.
  36. use_post_layernorm (`bool`, *optional*, defaults to `False`):
  37. Whether to use post layer normalization in the encoder.
  38. use_post_layernorm_in_modulation (`bool`, *optional*, defaults to `False`):
  39. Whether to use post layer normalization in the modulation layer.
  40. normalize_modulator (`bool`, *optional*, defaults to `False`):
  41. Whether to normalize the modulator.
  42. encoder_stride (`int`, *optional*, defaults to 32):
  43. Factor to increase the spatial resolution by in the decoder head for masked image modeling.
  44. Example:
  45. ```python
  46. >>> from transformers import FocalNetConfig, FocalNetModel
  47. >>> # Initializing a FocalNet microsoft/focalnet-tiny style configuration
  48. >>> configuration = FocalNetConfig()
  49. >>> # Initializing a model (with random weights) from the microsoft/focalnet-tiny style configuration
  50. >>> model = FocalNetModel(configuration)
  51. >>> # Accessing the model configuration
  52. >>> configuration = model.config
  53. ```"""
  54. model_type = "focalnet"
  55. image_size: int | list[int] | tuple[int, int] = 224
  56. patch_size: int | list[int] | tuple[int, int] = 4
  57. num_channels: int = 3
  58. embed_dim: int = 96
  59. use_conv_embed: bool = False
  60. hidden_sizes: list[int] | tuple[int, ...] = (192, 384, 768, 768)
  61. depths: list[int] | tuple[int, ...] = (2, 2, 6, 2)
  62. focal_levels: list[int] | tuple[int, ...] = (2, 2, 2, 2)
  63. focal_windows: list[int] | tuple[int, ...] = (3, 3, 3, 3)
  64. hidden_act: str = "gelu"
  65. mlp_ratio: float = 4.0
  66. hidden_dropout_prob: float | int = 0.0
  67. drop_path_rate: float | int = 0.1
  68. use_layerscale: bool = False
  69. layerscale_value: float = 1e-4
  70. use_post_layernorm: bool = False
  71. use_post_layernorm_in_modulation: bool = False
  72. normalize_modulator: bool = False
  73. initializer_range: float = 0.02
  74. layer_norm_eps: float = 1e-5
  75. encoder_stride: int = 32
  76. _out_features: list[str] | None = None
  77. _out_indices: list[int] | None = None
  78. def __post_init__(self, **kwargs):
  79. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, len(self.depths) + 1)]
  80. self.set_output_features_output_indices(
  81. out_indices=kwargs.pop("out_indices", None), out_features=kwargs.pop("out_features", None)
  82. )
  83. super().__post_init__(**kwargs)
  84. __all__ = ["FocalNetConfig"]