configuration_swin2sr.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # coding=utf-8
  2. # Copyright 2022 The HuggingFace Inc. team. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Swin2SR Transformer model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class Swin2SRConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`Swin2SRModel`]. It is used to instantiate a Swin
  22. Transformer v2 model according to the specified arguments, defining the model architecture. Instantiating a
  23. configuration with the defaults will yield a similar configuration to that of the Swin Transformer v2
  24. [caidas/swin2sr-classicalsr-x2-64](https://huggingface.co/caidas/swin2sr-classicalsr-x2-64) architecture.
  25. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  26. documentation from [`PretrainedConfig`] for more information.
  27. Args:
  28. image_size (`int`, *optional*, defaults to 64):
  29. The size (resolution) of each image.
  30. patch_size (`int`, *optional*, defaults to 1):
  31. The size (resolution) of each patch.
  32. num_channels (`int`, *optional*, defaults to 3):
  33. The number of input channels.
  34. num_channels_out (`int`, *optional*, defaults to `num_channels`):
  35. The number of output channels. If not set, it will be set to `num_channels`.
  36. embed_dim (`int`, *optional*, defaults to 180):
  37. Dimensionality of patch embedding.
  38. depths (`list(int)`, *optional*, defaults to `[6, 6, 6, 6, 6, 6]`):
  39. Depth of each layer in the Transformer encoder.
  40. num_heads (`list(int)`, *optional*, defaults to `[6, 6, 6, 6, 6, 6]`):
  41. Number of attention heads in each layer of the Transformer encoder.
  42. window_size (`int`, *optional*, defaults to 8):
  43. Size of windows.
  44. mlp_ratio (`float`, *optional*, defaults to 2.0):
  45. Ratio of MLP hidden dimensionality to embedding dimensionality.
  46. qkv_bias (`bool`, *optional*, defaults to `True`):
  47. Whether or not a learnable bias should be added to the queries, keys and values.
  48. hidden_dropout_prob (`float`, *optional*, defaults to 0.0):
  49. The dropout probability for all fully connected layers in the embeddings and encoder.
  50. attention_probs_dropout_prob (`float`, *optional*, defaults to 0.0):
  51. The dropout ratio for the attention probabilities.
  52. drop_path_rate (`float`, *optional*, defaults to 0.1):
  53. Stochastic depth rate.
  54. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
  55. The non-linear activation function (function or string) in the encoder. If string, `"gelu"`, `"relu"`,
  56. `"selu"` and `"gelu_new"` are supported.
  57. use_absolute_embeddings (`bool`, *optional*, defaults to `False`):
  58. Whether or not to add absolute position embeddings to the patch embeddings.
  59. initializer_range (`float`, *optional*, defaults to 0.02):
  60. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  61. layer_norm_eps (`float`, *optional*, defaults to 1e-05):
  62. The epsilon used by the layer normalization layers.
  63. upscale (`int`, *optional*, defaults to 2):
  64. The upscale factor for the image. 2/3/4/8 for image super resolution, 1 for denoising and compress artifact
  65. reduction
  66. img_range (`float`, *optional*, defaults to 1.0):
  67. The range of the values of the input image.
  68. resi_connection (`str`, *optional*, defaults to `"1conv"`):
  69. The convolutional block to use before the residual connection in each stage.
  70. upsampler (`str`, *optional*, defaults to `"pixelshuffle"`):
  71. The reconstruction reconstruction module. Can be 'pixelshuffle'/'pixelshuffledirect'/'nearest+conv'/None.
  72. Example:
  73. ```python
  74. >>> from transformers import Swin2SRConfig, Swin2SRModel
  75. >>> # Initializing a Swin2SR caidas/swin2sr-classicalsr-x2-64 style configuration
  76. >>> configuration = Swin2SRConfig()
  77. >>> # Initializing a model (with random weights) from the caidas/swin2sr-classicalsr-x2-64 style configuration
  78. >>> model = Swin2SRModel(configuration)
  79. >>> # Accessing the model configuration
  80. >>> configuration = model.config
  81. ```"""
  82. model_type = "swin2sr"
  83. attribute_map = {
  84. "hidden_size": "embed_dim",
  85. "num_attention_heads": "num_heads",
  86. "num_hidden_layers": "num_layers",
  87. }
  88. def __init__(
  89. self,
  90. image_size=64,
  91. patch_size=1,
  92. num_channels=3,
  93. num_channels_out=None,
  94. embed_dim=180,
  95. depths=[6, 6, 6, 6, 6, 6],
  96. num_heads=[6, 6, 6, 6, 6, 6],
  97. window_size=8,
  98. mlp_ratio=2.0,
  99. qkv_bias=True,
  100. hidden_dropout_prob=0.0,
  101. attention_probs_dropout_prob=0.0,
  102. drop_path_rate=0.1,
  103. hidden_act="gelu",
  104. use_absolute_embeddings=False,
  105. initializer_range=0.02,
  106. layer_norm_eps=1e-5,
  107. upscale=2,
  108. img_range=1.0,
  109. resi_connection="1conv",
  110. upsampler="pixelshuffle",
  111. **kwargs,
  112. ):
  113. super().__init__(**kwargs)
  114. self.image_size = image_size
  115. self.patch_size = patch_size
  116. self.num_channels = num_channels
  117. self.num_channels_out = num_channels if num_channels_out is None else num_channels_out
  118. self.embed_dim = embed_dim
  119. self.depths = depths
  120. self.num_layers = len(depths)
  121. self.num_heads = num_heads
  122. self.window_size = window_size
  123. self.mlp_ratio = mlp_ratio
  124. self.qkv_bias = qkv_bias
  125. self.hidden_dropout_prob = hidden_dropout_prob
  126. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  127. self.drop_path_rate = drop_path_rate
  128. self.hidden_act = hidden_act
  129. self.use_absolute_embeddings = use_absolute_embeddings
  130. self.layer_norm_eps = layer_norm_eps
  131. self.initializer_range = initializer_range
  132. self.upscale = upscale
  133. self.img_range = img_range
  134. self.resi_connection = resi_connection
  135. self.upsampler = upsampler
  136. __all__ = ["Swin2SRConfig"]