configuration_efficientnet.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright 2023 Google Research, Inc. and 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. """EfficientNet model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring
  18. @auto_docstring(checkpoint="google/efficientnet-b7")
  19. @strict
  20. class EfficientNetConfig(PreTrainedConfig):
  21. r"""
  22. width_coefficient (`float`, *optional*, defaults to 2.0):
  23. Scaling coefficient for network width at each stage.
  24. depth_coefficient (`float`, *optional*, defaults to 3.1):
  25. Scaling coefficient for network depth at each stage.
  26. depth_divisor (`int`, *optional*, defaults to 8):
  27. A unit of network width.
  28. kernel_sizes (`list[int]`, *optional*, defaults to `[3, 3, 5, 3, 5, 5, 3]`):
  29. List of kernel sizes to be used in each block.
  30. out_channels (`list[int]`, *optional*, defaults to `[16, 24, 40, 80, 112, 192, 320]`):
  31. List of output channel sizes to be used in each block for convolutional layers.
  32. depthwise_padding (`list[int]`, *optional*, defaults to `[]`):
  33. List of block indices with square padding.
  34. num_block_repeats (`list[int]`, *optional*, defaults to `[1, 2, 2, 3, 3, 4, 1]`):
  35. List of the number of times each block is to repeated.
  36. expand_ratios (`list[int]`, *optional*, defaults to `[1, 6, 6, 6, 6, 6, 6]`):
  37. List of scaling coefficient of each block.
  38. squeeze_expansion_ratio (`float`, *optional*, defaults to 0.25):
  39. Squeeze expansion ratio.
  40. pooling_type (`str` or `function`, *optional*, defaults to `"mean"`):
  41. Type of final pooling to be applied before the dense classification head. Available options are [`"mean"`,
  42. `"max"`]
  43. batch_norm_momentum (`float`, *optional*, defaults to 0.99):
  44. The momentum used by the batch normalization layers.
  45. drop_connect_rate (`float`, *optional*, defaults to 0.2):
  46. The drop rate for skip connections.
  47. Example:
  48. ```python
  49. >>> from transformers import EfficientNetConfig, EfficientNetModel
  50. >>> # Initializing a EfficientNet efficientnet-b7 style configuration
  51. >>> configuration = EfficientNetConfig()
  52. >>> # Initializing a model (with random weights) from the efficientnet-b7 style configuration
  53. >>> model = EfficientNetModel(configuration)
  54. >>> # Accessing the model configuration
  55. >>> configuration = model.config
  56. ```"""
  57. model_type = "efficientnet"
  58. num_channels: int = 3
  59. image_size: int | list[int] | tuple[int, int] = 600
  60. width_coefficient: float = 2.0
  61. depth_coefficient: float = 3.1
  62. depth_divisor: int = 8
  63. kernel_sizes: list[int] | tuple[int, ...] = (3, 3, 5, 3, 5, 5, 3)
  64. in_channels: list[int] | tuple[int, ...] = (32, 16, 24, 40, 80, 112, 192)
  65. out_channels: list[int] | tuple[int, ...] = (16, 24, 40, 80, 112, 192, 320)
  66. depthwise_padding: list[int] | tuple[int, ...] = ()
  67. strides: list[int] | tuple[int, ...] = (1, 2, 2, 2, 1, 2, 1)
  68. num_block_repeats: list[int] | tuple[int, ...] = (1, 2, 2, 3, 3, 4, 1)
  69. expand_ratios: list[int] | tuple[int, ...] = (1, 6, 6, 6, 6, 6, 6)
  70. squeeze_expansion_ratio: float = 0.25
  71. hidden_act: str = "swish"
  72. hidden_dim: int = 2560
  73. pooling_type: str = "mean"
  74. initializer_range: float = 0.02
  75. batch_norm_eps: float = 0.001
  76. batch_norm_momentum: float = 0.99
  77. dropout_rate: float | int = 0.5
  78. drop_connect_rate: float | int = 0.2
  79. def __post_init__(self, **kwargs):
  80. super().__post_init__(**kwargs)
  81. self.num_hidden_layers = sum(self.num_block_repeats) * 4
  82. __all__ = ["EfficientNetConfig"]