configuration_swin.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright 2022 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. """Swin Transformer 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/swin-tiny-patch4-window7-224")
  20. @strict
  21. class SwinConfig(BackboneConfigMixin, PreTrainedConfig):
  22. r"""
  23. depths (`list(int)`, *optional*, defaults to `[2, 2, 6, 2]`):
  24. Depth of each layer in the Transformer encoder.
  25. num_heads (`list(int)`, *optional*, defaults to `[3, 6, 12, 24]`):
  26. Number of attention heads in each layer of the Transformer encoder.
  27. window_size (`int`, *optional*, defaults to 7):
  28. Size of windows.
  29. encoder_stride (`int`, *optional*, defaults to 32):
  30. Factor to increase the spatial resolution by in the decoder head for masked image modeling.
  31. Example:
  32. ```python
  33. >>> from transformers import SwinConfig, SwinModel
  34. >>> # Initializing a Swin microsoft/swin-tiny-patch4-window7-224 style configuration
  35. >>> configuration = SwinConfig()
  36. >>> # Initializing a model (with random weights) from the microsoft/swin-tiny-patch4-window7-224 style configuration
  37. >>> model = SwinModel(configuration)
  38. >>> # Accessing the model configuration
  39. >>> configuration = model.config
  40. ```"""
  41. model_type = "swin"
  42. attribute_map = {
  43. "num_attention_heads": "num_heads",
  44. "num_hidden_layers": "num_layers",
  45. }
  46. image_size: int | list[int] | tuple[int, int] = 224
  47. patch_size: int | list[int] | tuple[int, int] = 4
  48. num_channels: int = 3
  49. embed_dim: int = 96
  50. depths: list[int] | tuple[int, ...] = (2, 2, 6, 2)
  51. num_heads: list[int] | tuple[int, ...] = (3, 6, 12, 24)
  52. window_size: int = 7
  53. mlp_ratio: float | int = 4.0
  54. qkv_bias: bool = True
  55. hidden_dropout_prob: float | int = 0.0
  56. attention_probs_dropout_prob: float | int = 0.0
  57. drop_path_rate: float | int = 0.1
  58. hidden_act: str = "gelu"
  59. use_absolute_embeddings: bool = False
  60. initializer_range: float = 0.02
  61. layer_norm_eps: float = 1e-5
  62. encoder_stride: int = 32
  63. _out_features: list[str] | None = None
  64. _out_indices: list[int] | None = None
  65. def __post_init__(self, **kwargs):
  66. self.num_layers = len(self.depths)
  67. # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
  68. # this indicates the channel dimension after the last stage of the model
  69. self.hidden_size = int(self.embed_dim * 2 ** (len(self.depths) - 1))
  70. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, len(self.depths) + 1)]
  71. self.set_output_features_output_indices(
  72. out_indices=kwargs.pop("out_indices", None), out_features=kwargs.pop("out_features", None)
  73. )
  74. super().__post_init__(**kwargs)
  75. __all__ = ["SwinConfig"]