configuration_vitpose.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # coding=utf-8
  2. # Copyright 2024 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. """VitPose model configuration"""
  16. from typing import Optional
  17. from ...configuration_utils import PretrainedConfig
  18. from ...utils import logging
  19. from ...utils.backbone_utils import verify_backbone_config_arguments
  20. from ..auto.configuration_auto import CONFIG_MAPPING
  21. logger = logging.get_logger(__name__)
  22. class VitPoseConfig(PretrainedConfig):
  23. r"""
  24. This is the configuration class to store the configuration of a [`VitPoseForPoseEstimation`]. It is used to instantiate a
  25. VitPose model according to the specified arguments, defining the model architecture. Instantiating a configuration
  26. with the defaults will yield a similar configuration to that of the VitPose
  27. [usyd-community/vitpose-base-simple](https://huggingface.co/usyd-community/vitpose-base-simple) architecture.
  28. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  29. documentation from [`PretrainedConfig`] for more information.
  30. Args:
  31. backbone_config (`PretrainedConfig` or `dict`, *optional*, defaults to `VitPoseBackboneConfig()`):
  32. The configuration of the backbone model. Currently, only `backbone_config` with `vitpose_backbone` as `model_type` is supported.
  33. backbone (`str`, *optional*):
  34. Name of backbone to use when `backbone_config` is `None`. If `use_pretrained_backbone` is `True`, this
  35. will load the corresponding pretrained weights from the timm or transformers library. If `use_pretrained_backbone`
  36. is `False`, this loads the backbone's config and uses that to initialize the backbone with random weights.
  37. use_pretrained_backbone (`bool`, *optional*, defaults to `False`):
  38. Whether to use pretrained weights for the backbone.
  39. use_timm_backbone (`bool`, *optional*, defaults to `False`):
  40. Whether to load `backbone` from the timm library. If `False`, the backbone is loaded from the transformers
  41. library.
  42. backbone_kwargs (`dict`, *optional*):
  43. Keyword arguments to be passed to AutoBackbone when loading from a checkpoint
  44. e.g. `{'out_indices': (0, 1, 2, 3)}`. Cannot be specified if `backbone_config` is set.
  45. initializer_range (`float`, *optional*, defaults to 0.02):
  46. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  47. scale_factor (`int`, *optional*, defaults to 4):
  48. Factor to upscale the feature maps coming from the ViT backbone.
  49. use_simple_decoder (`bool`, *optional*, defaults to `True`):
  50. Whether to use a `VitPoseSimpleDecoder` to decode the feature maps from the backbone into heatmaps. Otherwise it uses `VitPoseClassicDecoder`.
  51. Example:
  52. ```python
  53. >>> from transformers import VitPoseConfig, VitPoseForPoseEstimation
  54. >>> # Initializing a VitPose configuration
  55. >>> configuration = VitPoseConfig()
  56. >>> # Initializing a model (with random weights) from the configuration
  57. >>> model = VitPoseForPoseEstimation(configuration)
  58. >>> # Accessing the model configuration
  59. >>> configuration = model.config
  60. ```"""
  61. model_type = "vitpose"
  62. def __init__(
  63. self,
  64. backbone_config: Optional[PretrainedConfig] = None,
  65. backbone: Optional[str] = None,
  66. use_pretrained_backbone: bool = False,
  67. use_timm_backbone: bool = False,
  68. backbone_kwargs: Optional[dict] = None,
  69. initializer_range: float = 0.02,
  70. scale_factor: int = 4,
  71. use_simple_decoder: bool = True,
  72. **kwargs,
  73. ):
  74. super().__init__(**kwargs)
  75. if use_pretrained_backbone:
  76. logger.info(
  77. "`use_pretrained_backbone` is `True`. For the pure inference purpose of VitPose weight do not set this value."
  78. )
  79. if use_timm_backbone:
  80. raise ValueError("use_timm_backbone set `True` is not supported at the moment.")
  81. if backbone_config is None and backbone is None:
  82. logger.info("`backbone_config` is `None`. Initializing the config with the default `VitPose` backbone.")
  83. backbone_config = CONFIG_MAPPING["vitpose_backbone"](out_indices=[4])
  84. elif isinstance(backbone_config, dict):
  85. backbone_model_type = backbone_config.get("model_type")
  86. config_class = CONFIG_MAPPING[backbone_model_type]
  87. backbone_config = config_class.from_dict(backbone_config)
  88. verify_backbone_config_arguments(
  89. use_timm_backbone=use_timm_backbone,
  90. use_pretrained_backbone=use_pretrained_backbone,
  91. backbone=backbone,
  92. backbone_config=backbone_config,
  93. backbone_kwargs=backbone_kwargs,
  94. )
  95. self.backbone_config = backbone_config
  96. self.backbone = backbone
  97. self.use_pretrained_backbone = use_pretrained_backbone
  98. self.use_timm_backbone = use_timm_backbone
  99. self.backbone_kwargs = backbone_kwargs
  100. self.initializer_range = initializer_range
  101. self.scale_factor = scale_factor
  102. self.use_simple_decoder = use_simple_decoder
  103. @property
  104. def sub_configs(self):
  105. return (
  106. {"backbone_config": type(self.backbone_config)}
  107. if getattr(self, "backbone_config", None) is not None
  108. else {}
  109. )
  110. __all__ = ["VitPoseConfig"]