configuration_detr.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright 2021 Facebook AI Research 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. """DETR 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 import AutoConfig
  20. @auto_docstring(checkpoint="facebook/detr-resnet-50")
  21. @strict
  22. class DetrConfig(PreTrainedConfig):
  23. r"""
  24. num_queries (`int`, *optional*, defaults to 100):
  25. Number of object queries, i.e. detection slots. This is the maximal number of objects
  26. [`ConditionalDetrModel`] can detect in a single image. For COCO, we recommend 100 queries.
  27. position_embedding_type (`str`, *optional*, defaults to `"sine"`):
  28. Type of position embeddings to be used on top of the image features. One of `"sine"` or `"learned"`.
  29. dilation (`bool`, *optional*, defaults to `False`):
  30. Whether to replace stride with dilation in the last convolutional block (DC5). Only supported when
  31. `use_timm_backbone` = `True`.
  32. Examples:
  33. ```python
  34. >>> from transformers import DetrConfig, DetrModel
  35. >>> # Initializing a DETR facebook/detr-resnet-50 style configuration
  36. >>> configuration = DetrConfig()
  37. >>> # Initializing a model (with random weights) from the facebook/detr-resnet-50 style configuration
  38. >>> model = DetrModel(configuration)
  39. >>> # Accessing the model configuration
  40. >>> configuration = model.config
  41. ```"""
  42. model_type = "detr"
  43. sub_configs = {"backbone_config": AutoConfig}
  44. keys_to_ignore_at_inference = ["past_key_values"]
  45. attribute_map = {
  46. "hidden_size": "d_model",
  47. "num_attention_heads": "encoder_attention_heads",
  48. "num_hidden_layers": "encoder_layers",
  49. }
  50. backbone_config: dict | PreTrainedConfig | None = None
  51. num_channels: int = 3
  52. num_queries: int = 100
  53. encoder_layers: int = 6
  54. encoder_ffn_dim: int = 2048
  55. encoder_attention_heads: int = 8
  56. decoder_layers: int = 6
  57. decoder_ffn_dim: int = 2048
  58. decoder_attention_heads: int = 8
  59. encoder_layerdrop: float | int = 0.0
  60. decoder_layerdrop: float | int = 0.0
  61. is_encoder_decoder: bool = True
  62. activation_function: str = "relu"
  63. d_model: int = 256
  64. dropout: float | int = 0.1
  65. attention_dropout: float | int = 0.0
  66. activation_dropout: float | int = 0.0
  67. init_std: float = 0.02
  68. init_xavier_std: float = 1.0
  69. auxiliary_loss: bool = False
  70. position_embedding_type: str = "sine"
  71. dilation: bool = False
  72. class_cost: int = 1
  73. bbox_cost: int = 5
  74. giou_cost: int = 2
  75. mask_loss_coefficient: int = 1
  76. dice_loss_coefficient: int = 1
  77. bbox_loss_coefficient: int = 5
  78. giou_loss_coefficient: int = 2
  79. eos_coefficient: float = 0.1
  80. def __post_init__(self, **kwargs):
  81. backbone_kwargs = kwargs.get("backbone_kwargs", {})
  82. timm_default_kwargs = {
  83. "num_channels": backbone_kwargs.get("num_channels", self.num_channels),
  84. "features_only": True,
  85. "use_pretrained_backbone": False,
  86. "out_indices": backbone_kwargs.get("out_indices", [1, 2, 3, 4]),
  87. }
  88. if self.dilation:
  89. timm_default_kwargs["output_stride"] = backbone_kwargs.get("output_stride", 16)
  90. self.backbone_config, kwargs = consolidate_backbone_kwargs_to_config(
  91. backbone_config=self.backbone_config,
  92. default_backbone="resnet50",
  93. default_config_type="resnet",
  94. default_config_kwargs={"out_features": ["stage4"]},
  95. timm_default_kwargs=timm_default_kwargs,
  96. **kwargs,
  97. )
  98. super().__post_init__(**kwargs)
  99. __all__ = ["DetrConfig"]