configuration_textnet.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright 2024 the Fast authors and 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. """TextNet model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...backbone_utils import BackboneConfigMixin
  17. from ...configuration_utils import PreTrainedConfig
  18. from ...utils import auto_docstring
  19. @auto_docstring(checkpoint="czczup/textnet-base")
  20. @strict
  21. class TextNetConfig(BackboneConfigMixin, PreTrainedConfig):
  22. r"""
  23. stem_kernel_size (`int`, *optional*, defaults to 3):
  24. The kernel size for the initial convolution layer.
  25. stem_stride (`int`, *optional*, defaults to 2):
  26. The stride for the initial convolution layer.
  27. stem_num_channels (`int`, *optional*, defaults to 3):
  28. The num of channels in input for the initial convolution layer.
  29. stem_out_channels (`int`, *optional*, defaults to 64):
  30. The num of channels in out for the initial convolution layer.
  31. stem_act_func (`str`, *optional*, defaults to `"relu"`):
  32. The activation function for the initial convolution layer.
  33. conv_layer_kernel_sizes (`list[list[list[int]]]`, *optional*):
  34. A list of stage-wise kernel sizes. If `None`, defaults to:
  35. `[[[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]]]`.
  36. conv_layer_strides (`list[list[int]]`, *optional*):
  37. A list of stage-wise strides. If `None`, defaults to:
  38. `[[1, 2, 1], [2, 1, 1, 1], [2, 1, 1, 1], [2, 1, 1, 1]]`.
  39. Examples:
  40. ```python
  41. >>> from transformers import TextNetConfig, TextNetBackbone
  42. >>> # Initializing a TextNetConfig
  43. >>> configuration = TextNetConfig()
  44. >>> # Initializing a model (with random weights)
  45. >>> model = TextNetBackbone(configuration)
  46. >>> # Accessing the model configuration
  47. >>> configuration = model.config
  48. ```"""
  49. model_type = "textnet"
  50. stem_kernel_size: int = 3
  51. stem_stride: int = 2
  52. stem_num_channels: int = 3
  53. stem_out_channels: int = 64
  54. stem_act_func: str = "relu"
  55. image_size: list[int] | tuple[int, int] | int = (640, 640)
  56. conv_layer_kernel_sizes: list | None = None
  57. conv_layer_strides: list | None = None
  58. hidden_sizes: list[int] | tuple[int, ...] = (64, 64, 128, 256, 512)
  59. batch_norm_eps: float = 1e-5
  60. initializer_range: float = 0.02
  61. _out_features: list[str] | None = None
  62. _out_indices: list[int] | None = None
  63. def __post_init__(self, **kwargs):
  64. if self.conv_layer_kernel_sizes is None:
  65. self.conv_layer_kernel_sizes = [
  66. [[3, 3], [3, 3], [3, 3]],
  67. [[3, 3], [1, 3], [3, 3], [3, 1]],
  68. [[3, 3], [3, 3], [3, 1], [1, 3]],
  69. [[3, 3], [3, 1], [1, 3], [3, 3]],
  70. ]
  71. if self.conv_layer_strides is None:
  72. self.conv_layer_strides = [[1, 2, 1], [2, 1, 1, 1], [2, 1, 1, 1], [2, 1, 1, 1]]
  73. self.depths = [len(layer) for layer in self.conv_layer_kernel_sizes]
  74. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, 5)]
  75. self.set_output_features_output_indices(
  76. out_indices=kwargs.pop("out_indices", None), out_features=kwargs.pop("out_features", None)
  77. )
  78. super().__post_init__(**kwargs)
  79. __all__ = ["TextNetConfig"]