configuration_resnet.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright 2022 Microsoft 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. """ResNet model configuration"""
  15. from typing import ClassVar
  16. from huggingface_hub.dataclasses import strict
  17. from ...backbone_utils import BackboneConfigMixin
  18. from ...configuration_utils import PreTrainedConfig
  19. from ...utils import auto_docstring
  20. @auto_docstring(checkpoint="microsoft/resnet-50")
  21. @strict
  22. class ResNetConfig(BackboneConfigMixin, PreTrainedConfig):
  23. r"""
  24. layer_type (`str`, *optional*, defaults to `"bottleneck"`):
  25. The layer to use, it can be either `"basic"` (used for smaller models, like resnet-18 or resnet-34) or
  26. `"bottleneck"` (used for larger models like resnet-50 and above).
  27. downsample_in_first_stage (`bool`, *optional*, defaults to `False`):
  28. If `True`, the first stage will downsample the inputs using a `stride` of 2.
  29. downsample_in_bottleneck (`bool`, *optional*, defaults to `False`):
  30. If `True`, the first conv 1x1 in ResNetBottleNeckLayer will downsample the inputs using a `stride` of 2.
  31. Example:
  32. ```python
  33. >>> from transformers import ResNetConfig, ResNetModel
  34. >>> # Initializing a ResNet resnet-50 style configuration
  35. >>> configuration = ResNetConfig()
  36. >>> # Initializing a model (with random weights) from the resnet-50 style configuration
  37. >>> model = ResNetModel(configuration)
  38. >>> # Accessing the model configuration
  39. >>> configuration = model.config
  40. ```
  41. """
  42. model_type = "resnet"
  43. layer_types: ClassVar[list[str]] = ["basic", "bottleneck"]
  44. num_channels: int = 3
  45. embedding_size: int = 64
  46. hidden_sizes: list[int] | tuple[int, ...] | None = (256, 512, 1024, 2048)
  47. depths: list[int] | tuple[int, ...] | None = (3, 4, 6, 3)
  48. layer_type: str = "bottleneck"
  49. hidden_act: str = "relu"
  50. downsample_in_first_stage: bool = False
  51. downsample_in_bottleneck: bool = False
  52. def __post_init__(self, **kwargs):
  53. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, len(self.depths) + 1)]
  54. self.set_output_features_output_indices(
  55. out_indices=kwargs.pop("out_indices", None), out_features=kwargs.pop("out_features", None)
  56. )
  57. self.hidden_sizes = list(self.hidden_sizes)
  58. super().__post_init__(**kwargs)
  59. def validate_layer_type(self):
  60. """Check that `layer_types` is correctly defined."""
  61. if self.layer_type not in self.layer_types:
  62. raise ValueError(f"layer_type={self.layer_type} is not one of {','.join(self.layer_types)}")
  63. __all__ = ["ResNetConfig"]