configuration_swinv2.py 3.4 KB

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