configuration_vitmatte.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # coding=utf-8
  2. # Copyright 2023 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. """VitMatte model configuration"""
  16. import copy
  17. from typing import Optional
  18. from ...configuration_utils import PretrainedConfig
  19. from ...utils import logging
  20. from ...utils.backbone_utils import verify_backbone_config_arguments
  21. from ..auto.configuration_auto import CONFIG_MAPPING
  22. logger = logging.get_logger(__name__)
  23. class VitMatteConfig(PretrainedConfig):
  24. r"""
  25. This is the configuration class to store the configuration of [`VitMatteForImageMatting`]. It is used to
  26. instantiate a ViTMatte model according to the specified arguments, defining the model architecture. Instantiating a
  27. configuration with the defaults will yield a similar configuration to that of the ViTMatte
  28. [hustvl/vitmatte-small-composition-1k](https://huggingface.co/hustvl/vitmatte-small-composition-1k) architecture.
  29. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  30. documentation from [`PretrainedConfig`] for more information.
  31. Args:
  32. backbone_config (`PretrainedConfig` or `dict`, *optional*, defaults to `VitDetConfig()`):
  33. The configuration of the backbone model.
  34. backbone (`str`, *optional*):
  35. Name of backbone to use when `backbone_config` is `None`. If `use_pretrained_backbone` is `True`, this
  36. will load the corresponding pretrained weights from the timm or transformers library. If `use_pretrained_backbone`
  37. is `False`, this loads the backbone's config and uses that to initialize the backbone with random weights.
  38. use_pretrained_backbone (`bool`, *optional*, defaults to `False`):
  39. Whether to use pretrained weights for the backbone.
  40. use_timm_backbone (`bool`, *optional*, defaults to `False`):
  41. Whether to load `backbone` from the timm library. If `False`, the backbone is loaded from the transformers
  42. library.
  43. backbone_kwargs (`dict`, *optional*):
  44. Keyword arguments to be passed to AutoBackbone when loading from a checkpoint
  45. e.g. `{'out_indices': (0, 1, 2, 3)}`. Cannot be specified if `backbone_config` is set.
  46. hidden_size (`int`, *optional*, defaults to 384):
  47. The number of input channels of the decoder.
  48. batch_norm_eps (`float`, *optional*, defaults to 1e-05):
  49. The epsilon used by the batch norm layers.
  50. initializer_range (`float`, *optional*, defaults to 0.02):
  51. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  52. convstream_hidden_sizes (`list[int]`, *optional*, defaults to `[48, 96, 192]`):
  53. The output channels of the ConvStream module.
  54. fusion_hidden_sizes (`list[int]`, *optional*, defaults to `[256, 128, 64, 32]`):
  55. The output channels of the Fusion blocks.
  56. Example:
  57. ```python
  58. >>> from transformers import VitMatteConfig, VitMatteForImageMatting
  59. >>> # Initializing a ViTMatte hustvl/vitmatte-small-composition-1k style configuration
  60. >>> configuration = VitMatteConfig()
  61. >>> # Initializing a model (with random weights) from the hustvl/vitmatte-small-composition-1k style configuration
  62. >>> model = VitMatteForImageMatting(configuration)
  63. >>> # Accessing the model configuration
  64. >>> configuration = model.config
  65. ```"""
  66. model_type = "vitmatte"
  67. def __init__(
  68. self,
  69. backbone_config: Optional[PretrainedConfig] = None,
  70. backbone=None,
  71. use_pretrained_backbone=False,
  72. use_timm_backbone=False,
  73. backbone_kwargs=None,
  74. hidden_size: int = 384,
  75. batch_norm_eps: float = 1e-5,
  76. initializer_range: float = 0.02,
  77. convstream_hidden_sizes: list[int] = [48, 96, 192],
  78. fusion_hidden_sizes: list[int] = [256, 128, 64, 32],
  79. **kwargs,
  80. ):
  81. super().__init__(**kwargs)
  82. if backbone_config is None and backbone is None:
  83. logger.info("`backbone_config` is `None`. Initializing the config with the default `VitDet` backbone.")
  84. backbone_config = CONFIG_MAPPING["vitdet"](out_features=["stage4"])
  85. elif isinstance(backbone_config, dict):
  86. backbone_model_type = backbone_config.get("model_type")
  87. config_class = CONFIG_MAPPING[backbone_model_type]
  88. backbone_config = config_class.from_dict(backbone_config)
  89. verify_backbone_config_arguments(
  90. use_timm_backbone=use_timm_backbone,
  91. use_pretrained_backbone=use_pretrained_backbone,
  92. backbone=backbone,
  93. backbone_config=backbone_config,
  94. backbone_kwargs=backbone_kwargs,
  95. )
  96. self.backbone_config = backbone_config
  97. self.backbone = backbone
  98. self.use_pretrained_backbone = use_pretrained_backbone
  99. self.use_timm_backbone = use_timm_backbone
  100. self.backbone_kwargs = backbone_kwargs
  101. self.batch_norm_eps = batch_norm_eps
  102. self.hidden_size = hidden_size
  103. self.initializer_range = initializer_range
  104. self.convstream_hidden_sizes = convstream_hidden_sizes
  105. self.fusion_hidden_sizes = fusion_hidden_sizes
  106. @property
  107. def sub_configs(self):
  108. return (
  109. {"backbone_config": type(self.backbone_config)}
  110. if getattr(self, "backbone_config", None) is not None
  111. else {}
  112. )
  113. def to_dict(self):
  114. """
  115. Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns:
  116. `dict[str, any]`: Dictionary of all the attributes that make up this configuration instance,
  117. """
  118. output = copy.deepcopy(self.__dict__)
  119. output["backbone_config"] = self.backbone_config.to_dict()
  120. output["model_type"] = self.__class__.model_type
  121. return output
  122. __all__ = ["VitMatteConfig"]