configuration_convnextv2.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # coding=utf-8
  2. # Copyright 2023 Meta Platforms, Inc. and 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. """ConvNeXTV2 model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices
  19. logger = logging.get_logger(__name__)
  20. class ConvNextV2Config(BackboneConfigMixin, PretrainedConfig):
  21. r"""
  22. This is the configuration class to store the configuration of a [`ConvNextV2Model`]. It is used to instantiate an
  23. ConvNeXTV2 model according to the specified arguments, defining the model architecture. Instantiating a
  24. configuration with the defaults will yield a similar configuration to that of the ConvNeXTV2
  25. [facebook/convnextv2-tiny-1k-224](https://huggingface.co/facebook/convnextv2-tiny-1k-224) architecture.
  26. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  27. documentation from [`PretrainedConfig`] for more information.
  28. Args:
  29. num_channels (`int`, *optional*, defaults to 3):
  30. The number of input channels.
  31. patch_size (`int`, *optional*, defaults to 4):
  32. Patch size to use in the patch embedding layer.
  33. num_stages (`int`, *optional*, defaults to 4):
  34. The number of stages in the model.
  35. hidden_sizes (`List[int]`, *optional*, defaults to `[96, 192, 384, 768]`):
  36. Dimensionality (hidden size) at each stage.
  37. depths (`List[int]`, *optional*, defaults to `[3, 3, 9, 3]`):
  38. Depth (number of blocks) for each stage.
  39. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
  40. The non-linear activation function (function or string) in each block. If string, `"gelu"`, `"relu"`,
  41. `"selu"` and `"gelu_new"` are supported.
  42. initializer_range (`float`, *optional*, defaults to 0.02):
  43. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  44. layer_norm_eps (`float`, *optional*, defaults to 1e-12):
  45. The epsilon used by the layer normalization layers.
  46. drop_path_rate (`float`, *optional*, defaults to 0.0):
  47. The drop rate for stochastic depth.
  48. image_size (`int`, *optional*, defaults to 224):
  49. The size (resolution) of each image.
  50. out_features (`List[str]`, *optional*):
  51. If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc.
  52. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the
  53. corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the
  54. same order as defined in the `stage_names` attribute.
  55. out_indices (`List[int]`, *optional*):
  56. If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how
  57. many stages the model has). If unset and `out_features` is set, will default to the corresponding stages.
  58. If unset and `out_features` is unset, will default to the last stage. Must be in the
  59. same order as defined in the `stage_names` attribute.
  60. Example:
  61. ```python
  62. >>> from transformers import ConvNeXTV2Config, ConvNextV2Model
  63. >>> # Initializing a ConvNeXTV2 convnextv2-tiny-1k-224 style configuration
  64. >>> configuration = ConvNeXTV2Config()
  65. >>> # Initializing a model (with random weights) from the convnextv2-tiny-1k-224 style configuration
  66. >>> model = ConvNextV2Model(configuration)
  67. >>> # Accessing the model configuration
  68. >>> configuration = model.config
  69. ```"""
  70. model_type = "convnextv2"
  71. def __init__(
  72. self,
  73. num_channels=3,
  74. patch_size=4,
  75. num_stages=4,
  76. hidden_sizes=None,
  77. depths=None,
  78. hidden_act="gelu",
  79. initializer_range=0.02,
  80. layer_norm_eps=1e-12,
  81. drop_path_rate=0.0,
  82. image_size=224,
  83. out_features=None,
  84. out_indices=None,
  85. **kwargs,
  86. ):
  87. super().__init__(**kwargs)
  88. self.num_channels = num_channels
  89. self.patch_size = patch_size
  90. self.num_stages = num_stages
  91. self.hidden_sizes = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes
  92. self.depths = [3, 3, 9, 3] if depths is None else depths
  93. self.hidden_act = hidden_act
  94. self.initializer_range = initializer_range
  95. self.layer_norm_eps = layer_norm_eps
  96. self.drop_path_rate = drop_path_rate
  97. self.image_size = image_size
  98. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, len(self.depths) + 1)]
  99. self._out_features, self._out_indices = get_aligned_output_features_output_indices(
  100. out_features=out_features, out_indices=out_indices, stage_names=self.stage_names
  101. )