configuration_upernet.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright 2022 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. """UperNet model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...backbone_utils import consolidate_backbone_kwargs_to_config
  17. from ...configuration_utils import PreTrainedConfig
  18. from ...utils import auto_docstring
  19. from ..auto.configuration_auto import AutoConfig
  20. @auto_docstring(checkpoint="openmmlab/upernet-convnext-tiny")
  21. @strict
  22. class UperNetConfig(PreTrainedConfig):
  23. r"""
  24. pool_scales (`tuple[int]`, *optional*, defaults to `[1, 2, 3, 6]`):
  25. Pooling scales used in Pooling Pyramid Module applied on the last feature map.
  26. use_auxiliary_head (`bool`, *optional*, defaults to `True`):
  27. Whether to use an auxiliary head during training.
  28. auxiliary_loss_weight (`float`, *optional*, defaults to 0.4):
  29. Weight of the cross-entropy loss of the auxiliary head.
  30. auxiliary_in_channels (`int`, *optional*, defaults to 256):
  31. Number of input channels in the auxiliary head.
  32. auxiliary_channels (`int`, *optional*, defaults to 256):
  33. Number of channels to use in the auxiliary head.
  34. auxiliary_num_convs (`int`, *optional*, defaults to 1):
  35. Number of convolutional layers to use in the auxiliary head.
  36. auxiliary_concat_input (`bool`, *optional*, defaults to `False`):
  37. Whether to concatenate the output of the auxiliary head with the input before the classification layer.
  38. loss_ignore_index (`int`, *optional*, defaults to 255):
  39. The index that is ignored by the loss function.
  40. Examples:
  41. ```python
  42. >>> from transformers import UperNetConfig, UperNetForSemanticSegmentation
  43. >>> # Initializing a configuration
  44. >>> configuration = UperNetConfig()
  45. >>> # Initializing a model (with random weights) from the configuration
  46. >>> model = UperNetForSemanticSegmentation(configuration)
  47. >>> # Accessing the model configuration
  48. >>> configuration = model.config
  49. ```"""
  50. model_type = "upernet"
  51. sub_configs = {"backbone_config": AutoConfig}
  52. backbone_config: dict | PreTrainedConfig | None = None
  53. hidden_size: int = 512
  54. initializer_range: float = 0.02
  55. pool_scales: list[int] | tuple[int, ...] = (1, 2, 3, 6)
  56. use_auxiliary_head: bool = True
  57. auxiliary_loss_weight: float = 0.4
  58. auxiliary_in_channels: int | None = None
  59. auxiliary_channels: int = 256
  60. auxiliary_num_convs: int = 1
  61. auxiliary_concat_input: bool = False
  62. loss_ignore_index: int = 255
  63. def __post_init__(self, **kwargs):
  64. self.backbone_config, kwargs = consolidate_backbone_kwargs_to_config(
  65. backbone_config=self.backbone_config,
  66. default_config_type="resnet",
  67. default_config_kwargs={
  68. "out_features": ["stage1", "stage2", "stage3", "stage4"],
  69. },
  70. **kwargs,
  71. )
  72. super().__post_init__(**kwargs)
  73. __all__ = ["UperNetConfig"]