configuration_regnet.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # coding=utf-8
  2. # Copyright 2022 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. """RegNet model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class RegNetConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`RegNetModel`]. It is used to instantiate a RegNet
  22. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  23. defaults will yield a similar configuration to that of the RegNet
  24. [facebook/regnet-y-040](https://huggingface.co/facebook/regnet-y-040) 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. num_channels (`int`, *optional*, defaults to 3):
  29. The number of input channels.
  30. embedding_size (`int`, *optional*, defaults to 64):
  31. Dimensionality (hidden size) for the embedding layer.
  32. hidden_sizes (`list[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`):
  33. Dimensionality (hidden size) at each stage.
  34. depths (`list[int]`, *optional*, defaults to `[3, 4, 6, 3]`):
  35. Depth (number of layers) for each stage.
  36. layer_type (`str`, *optional*, defaults to `"y"`):
  37. The layer to use, it can be either `"x" or `"y"`. An `x` layer is a ResNet's BottleNeck layer with
  38. `reduction` fixed to `1`. While a `y` layer is a `x` but with squeeze and excitation. Please refer to the
  39. paper for a detailed explanation of how these layers were constructed.
  40. hidden_act (`str`, *optional*, defaults to `"relu"`):
  41. The non-linear activation function in each block. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"`
  42. are supported.
  43. downsample_in_first_stage (`bool`, *optional*, defaults to `False`):
  44. If `True`, the first stage will downsample the inputs using a `stride` of 2.
  45. Example:
  46. ```python
  47. >>> from transformers import RegNetConfig, RegNetModel
  48. >>> # Initializing a RegNet regnet-y-40 style configuration
  49. >>> configuration = RegNetConfig()
  50. >>> # Initializing a model from the regnet-y-40 style configuration
  51. >>> model = RegNetModel(configuration)
  52. >>> # Accessing the model configuration
  53. >>> configuration = model.config
  54. ```
  55. """
  56. model_type = "regnet"
  57. layer_types = ["x", "y"]
  58. def __init__(
  59. self,
  60. num_channels=3,
  61. embedding_size=32,
  62. hidden_sizes=[128, 192, 512, 1088],
  63. depths=[2, 6, 12, 2],
  64. groups_width=64,
  65. layer_type="y",
  66. hidden_act="relu",
  67. **kwargs,
  68. ):
  69. super().__init__(**kwargs)
  70. if layer_type not in self.layer_types:
  71. raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types)}")
  72. self.num_channels = num_channels
  73. self.embedding_size = embedding_size
  74. self.hidden_sizes = hidden_sizes
  75. self.depths = depths
  76. self.groups_width = groups_width
  77. self.layer_type = layer_type
  78. self.hidden_act = hidden_act
  79. # always downsample in the first stage
  80. self.downsample_in_first_stage = True
  81. __all__ = ["RegNetConfig"]