configuration_textnet.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # coding=utf-8
  2. # Copyright 2024 the Fast authors and 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. """TextNet model configuration"""
  16. from transformers import PretrainedConfig
  17. from transformers.utils import logging
  18. from transformers.utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices
  19. logger = logging.get_logger(__name__)
  20. class TextNetConfig(BackboneConfigMixin, PretrainedConfig):
  21. r"""
  22. This is the configuration class to store the configuration of a [`TextNextModel`]. It is used to instantiate a
  23. TextNext model according to the specified arguments, defining the model architecture. Instantiating a configuration
  24. with the defaults will yield a similar configuration to that of the
  25. [czczup/textnet-base](https://huggingface.co/czczup/textnet-base). Configuration objects inherit from
  26. [`PretrainedConfig`] and can be used to control the model outputs.Read the documentation from [`PretrainedConfig`]
  27. for more information.
  28. Args:
  29. stem_kernel_size (`int`, *optional*, defaults to 3):
  30. The kernel size for the initial convolution layer.
  31. stem_stride (`int`, *optional*, defaults to 2):
  32. The stride for the initial convolution layer.
  33. stem_num_channels (`int`, *optional*, defaults to 3):
  34. The num of channels in input for the initial convolution layer.
  35. stem_out_channels (`int`, *optional*, defaults to 64):
  36. The num of channels in out for the initial convolution layer.
  37. stem_act_func (`str`, *optional*, defaults to `"relu"`):
  38. The activation function for the initial convolution layer.
  39. image_size (`tuple[int, int]`, *optional*, defaults to `[640, 640]`):
  40. The size (resolution) of each image.
  41. conv_layer_kernel_sizes (`list[list[list[int]]]`, *optional*):
  42. A list of stage-wise kernel sizes. If `None`, defaults to:
  43. `[[[3, 3], [3, 3], [3, 3]], [[3, 3], [1, 3], [3, 3], [3, 1]], [[3, 3], [3, 3], [3, 1], [1, 3]], [[3, 3], [3, 1], [1, 3], [3, 3]]]`.
  44. conv_layer_strides (`list[list[int]]`, *optional*):
  45. A list of stage-wise strides. If `None`, defaults to:
  46. `[[1, 2, 1], [2, 1, 1, 1], [2, 1, 1, 1], [2, 1, 1, 1]]`.
  47. hidden_sizes (`list[int]`, *optional*, defaults to `[64, 64, 128, 256, 512]`):
  48. Dimensionality (hidden size) at each stage.
  49. batch_norm_eps (`float`, *optional*, defaults to 1e-05):
  50. The epsilon used by the batch normalization layers.
  51. initializer_range (`float`, *optional*, defaults to 0.02):
  52. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  53. out_features (`list[str]`, *optional*):
  54. If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc.
  55. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the
  56. corresponding stages. If unset and `out_indices` is unset, will default to the last stage.
  57. out_indices (`list[int]`, *optional*):
  58. If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how
  59. many stages the model has). If unset and `out_features` is set, will default to the corresponding stages.
  60. If unset and `out_features` is unset, will default to the last stage.
  61. Examples:
  62. ```python
  63. >>> from transformers import TextNetConfig, TextNetBackbone
  64. >>> # Initializing a TextNetConfig
  65. >>> configuration = TextNetConfig()
  66. >>> # Initializing a model (with random weights)
  67. >>> model = TextNetBackbone(configuration)
  68. >>> # Accessing the model configuration
  69. >>> configuration = model.config
  70. ```"""
  71. model_type = "textnet"
  72. def __init__(
  73. self,
  74. stem_kernel_size=3,
  75. stem_stride=2,
  76. stem_num_channels=3,
  77. stem_out_channels=64,
  78. stem_act_func="relu",
  79. image_size=[640, 640],
  80. conv_layer_kernel_sizes=None,
  81. conv_layer_strides=None,
  82. hidden_sizes=[64, 64, 128, 256, 512],
  83. batch_norm_eps=1e-5,
  84. initializer_range=0.02,
  85. out_features=None,
  86. out_indices=None,
  87. **kwargs,
  88. ):
  89. super().__init__(**kwargs)
  90. if conv_layer_kernel_sizes is None:
  91. conv_layer_kernel_sizes = [
  92. [[3, 3], [3, 3], [3, 3]],
  93. [[3, 3], [1, 3], [3, 3], [3, 1]],
  94. [[3, 3], [3, 3], [3, 1], [1, 3]],
  95. [[3, 3], [3, 1], [1, 3], [3, 3]],
  96. ]
  97. if conv_layer_strides is None:
  98. conv_layer_strides = [[1, 2, 1], [2, 1, 1, 1], [2, 1, 1, 1], [2, 1, 1, 1]]
  99. self.stem_kernel_size = stem_kernel_size
  100. self.stem_stride = stem_stride
  101. self.stem_num_channels = stem_num_channels
  102. self.stem_out_channels = stem_out_channels
  103. self.stem_act_func = stem_act_func
  104. self.image_size = image_size
  105. self.conv_layer_kernel_sizes = conv_layer_kernel_sizes
  106. self.conv_layer_strides = conv_layer_strides
  107. self.initializer_range = initializer_range
  108. self.hidden_sizes = hidden_sizes
  109. self.batch_norm_eps = batch_norm_eps
  110. self.depths = [len(layer) for layer in self.conv_layer_kernel_sizes]
  111. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, 5)]
  112. self._out_features, self._out_indices = get_aligned_output_features_output_indices(
  113. out_features=out_features, out_indices=out_indices, stage_names=self.stage_names
  114. )
  115. __all__ = ["TextNetConfig"]