Aluode/PerceptionLabPortable
0
1# coding=utf-82# Copyright 2022 SHI Labs and 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 at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# 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 and14# limitations under the License.15"""OneFormer model configuration"""16 17from typing import Optional18 19from ...configuration_utils import PretrainedConfig20from ...utils import logging21from ...utils.backbone_utils import verify_backbone_config_arguments22from ..auto import CONFIG_MAPPING23 24 25logger = logging.get_logger(__name__)26 27 28class OneFormerConfig(PretrainedConfig):29 r"""30 This is the configuration class to store the configuration of a [`OneFormerModel`]. It is used to instantiate a31 OneFormer model according to the specified arguments, defining the model architecture. Instantiating a32 configuration with the defaults will yield a similar configuration to that of the OneFormer33 [shi-labs/oneformer_ade20k_swin_tiny](https://huggingface.co/shi-labs/oneformer_ade20k_swin_tiny) architecture34 trained on [ADE20k-150](https://huggingface.co/datasets/scene_parse_150).35 36 Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the37 documentation from [`PretrainedConfig`] for more information.38 39 Args:40 backbone_config (`PretrainedConfig`, *optional*, defaults to `SwinConfig`):41 The configuration of the backbone model.42 backbone (`str`, *optional*):43 Name of backbone to use when `backbone_config` is `None`. If `use_pretrained_backbone` is `True`, this44 will load the corresponding pretrained weights from the timm or transformers library. If `use_pretrained_backbone`45 is `False`, this loads the backbone's config and uses that to initialize the backbone with random weights.46 use_pretrained_backbone (`bool`, *optional*, defaults to `False`):47 Whether to use pretrained weights for the backbone.48 use_timm_backbone (`bool`, *optional*, defaults to `False`):49 Whether to load `backbone` from the timm library. If `False`, the backbone is loaded from the transformers50 library.51 backbone_kwargs (`dict`, *optional*):52 Keyword arguments to be passed to AutoBackbone when loading from a checkpoint53 e.g. `{'out_indices': (0, 1, 2, 3)}`. Cannot be specified if `backbone_config` is set.54 ignore_value (`int`, *optional*, defaults to 255):55 Values to be ignored in GT label while calculating loss.56 num_queries (`int`, *optional*, defaults to 150):57 Number of object queries.58 no_object_weight (`float`, *optional*, defaults to 0.1):59 Weight for no-object class predictions.60 class_weight (`float`, *optional*, defaults to 2.0):61 Weight for Classification CE loss.62 mask_weight (`float`, *optional*, defaults to 5.0):63 Weight for binary CE loss.64 dice_weight (`float`, *optional*, defaults to 5.0):65 Weight for dice loss.66 contrastive_weight (`float`, *optional*, defaults to 0.5):67 Weight for contrastive loss.68 contrastive_temperature (`float`, *optional*, defaults to 0.07):69 Initial value for scaling the contrastive logits.70 train_num_points (`int`, *optional*, defaults to 12544):71 Number of points to sample while calculating losses on mask predictions.72 oversample_ratio (`float`, *optional*, defaults to 3.0):73 Ratio to decide how many points to oversample.74 importance_sample_ratio (`float`, *optional*, defaults to 0.75):75 Ratio of points that are sampled via importance sampling.76 init_std (`float`, *optional*, defaults to 0.02):77 Standard deviation for normal initialization.78 init_xavier_std (`float`, *optional*, defaults to 1.0):79 Standard deviation for xavier uniform initialization.80 layer_norm_eps (`float`, *optional*, defaults to 1e-05):81 Epsilon for layer normalization.82 is_training (`bool`, *optional*, defaults to `False`):83 Whether to run in training or inference mode.84 use_auxiliary_loss (`bool`, *optional*, defaults to `True`):85 Whether to calculate loss using intermediate predictions from transformer decoder.86 output_auxiliary_logits (`bool`, *optional*, defaults to `True`):87 Whether to return intermediate predictions from transformer decoder.88 strides (`list`, *optional*, defaults to `[4, 8, 16, 32]`):89 List containing the strides for feature maps in the encoder.90 task_seq_len (`int`, *optional*, defaults to 77):91 Sequence length for tokenizing text list input.92 text_encoder_width (`int`, *optional*, defaults to 256):93 Hidden size for text encoder.94 text_encoder_context_length (`int`, *optional*, defaults to 77):95 Input sequence length for text encoder.96 text_encoder_num_layers (`int`, *optional*, defaults to 6):97 Number of layers for transformer in text encoder.98 text_encoder_vocab_size (`int`, *optional*, defaults to 49408):99 Vocabulary size for tokenizer.100 text_encoder_proj_layers (`int`, *optional*, defaults to 2):101 Number of layers in MLP for project text queries.102 text_encoder_n_ctx (`int`, *optional*, defaults to 16):103 Number of learnable text context queries.104 conv_dim (`int`, *optional*, defaults to 256):105 Feature map dimension to map outputs from the backbone.106 mask_dim (`int`, *optional*, defaults to 256):107 Dimension for feature maps in pixel decoder.108 hidden_dim (`int`, *optional*, defaults to 256):109 Dimension for hidden states in transformer decoder.110 encoder_feedforward_dim (`int`, *optional*, defaults to 1024):111 Dimension for FFN layer in pixel decoder.112 norm (`str`, *optional*, defaults to `"GN"`):113 Type of normalization.114 encoder_layers (`int`, *optional*, defaults to 6):115 Number of layers in pixel decoder.116 decoder_layers (`int`, *optional*, defaults to 10):117 Number of layers in transformer decoder.118 use_task_norm (`bool`, *optional*, defaults to `True`):119 Whether to normalize the task token.120 num_attention_heads (`int`, *optional*, defaults to 8):121 Number of attention heads in transformer layers in the pixel and transformer decoders.122 dropout (`float`, *optional*, defaults to 0.1):123 Dropout probability for pixel and transformer decoders.124 dim_feedforward (`int`, *optional*, defaults to 2048):125 Dimension for FFN layer in transformer decoder.126 pre_norm (`bool`, *optional*, defaults to `False`):127 Whether to normalize hidden states before attention layers in transformer decoder.128 enforce_input_proj (`bool`, *optional*, defaults to `False`):129 Whether to project hidden states in transformer decoder.130 query_dec_layers (`int`, *optional*, defaults to 2):131 Number of layers in query transformer.132 common_stride (`int`, *optional*, defaults to 4):133 Common stride used for features in pixel decoder.134 135 Examples:136 ```python137 >>> from transformers import OneFormerConfig, OneFormerModel138 139 >>> # Initializing a OneFormer shi-labs/oneformer_ade20k_swin_tiny configuration140 >>> configuration = OneFormerConfig()141 >>> # Initializing a model (with random weights) from the shi-labs/oneformer_ade20k_swin_tiny style configuration142 >>> model = OneFormerModel(configuration)143 >>> # Accessing the model configuration144 >>> configuration = model.config145 ```146 """147 148 model_type = "oneformer"149 attribute_map = {"hidden_size": "hidden_dim"}150 151 def __init__(152 self,153 backbone_config: Optional[dict] = None,154 backbone: Optional[str] = None,155 use_pretrained_backbone: bool = False,156 use_timm_backbone: bool = False,157 backbone_kwargs: Optional[dict] = None,158 ignore_value: int = 255,159 num_queries: int = 150,160 no_object_weight: int = 0.1,161 class_weight: float = 2.0,162 mask_weight: float = 5.0,163 dice_weight: float = 5.0,164 contrastive_weight: float = 0.5,165 contrastive_temperature: float = 0.07,166 train_num_points: int = 12544,167 oversample_ratio: float = 3.0,168 importance_sample_ratio: float = 0.75,169 init_std: float = 0.02,170 init_xavier_std: float = 1.0,171 layer_norm_eps: float = 1e-05,172 is_training: bool = False,173 use_auxiliary_loss: bool = True,174 output_auxiliary_logits: bool = True,175 strides: Optional[list] = [4, 8, 16, 32],176 task_seq_len: int = 77,177 text_encoder_width: int = 256,178 text_encoder_context_length: int = 77,179 text_encoder_num_layers: int = 6,180 text_encoder_vocab_size: int = 49408,181 text_encoder_proj_layers: int = 2,182 text_encoder_n_ctx: int = 16,183 conv_dim: int = 256,184 mask_dim: int = 256,185 hidden_dim: int = 256,186 encoder_feedforward_dim: int = 1024,187 norm: str = "GN",188 encoder_layers: int = 6,189 decoder_layers: int = 10,190 use_task_norm: bool = True,191 num_attention_heads: int = 8,192 dropout: float = 0.1,193 dim_feedforward: int = 2048,194 pre_norm: bool = False,195 enforce_input_proj: bool = False,196 query_dec_layers: int = 2,197 common_stride: int = 4,198 **kwargs,199 ):200 if backbone_config is None and backbone is None:201 logger.info("`backbone_config` is unset. Initializing the config with the default `Swin` backbone.")202 backbone_config = CONFIG_MAPPING["swin"](203 image_size=224,204 num_channels=3,205 patch_size=4,206 embed_dim=96,207 depths=[2, 2, 6, 2],208 num_heads=[3, 6, 12, 24],209 window_size=7,210 drop_path_rate=0.3,211 use_absolute_embeddings=False,212 out_features=["stage1", "stage2", "stage3", "stage4"],213 )214 elif isinstance(backbone_config, dict):215 backbone_model_type = backbone_config.get("model_type")216 config_class = CONFIG_MAPPING[backbone_model_type]217 backbone_config = config_class.from_dict(backbone_config)218 219 verify_backbone_config_arguments(220 use_timm_backbone=use_timm_backbone,221 use_pretrained_backbone=use_pretrained_backbone,222 backbone=backbone,223 backbone_config=backbone_config,224 backbone_kwargs=backbone_kwargs,225 )226 227 self.backbone_config = backbone_config228 self.backbone = backbone229 self.use_pretrained_backbone = use_pretrained_backbone230 self.use_timm_backbone = use_timm_backbone231 self.backbone_kwargs = backbone_kwargs232 self.ignore_value = ignore_value233 self.num_queries = num_queries234 self.no_object_weight = no_object_weight235 self.class_weight = class_weight236 self.mask_weight = mask_weight237 self.dice_weight = dice_weight238 self.contrastive_weight = contrastive_weight239 self.contrastive_temperature = contrastive_temperature240 self.train_num_points = train_num_points241 self.oversample_ratio = oversample_ratio242 self.importance_sample_ratio = importance_sample_ratio243 self.init_std = init_std244 self.init_xavier_std = init_xavier_std245 self.layer_norm_eps = layer_norm_eps246 self.is_training = is_training247 self.use_auxiliary_loss = use_auxiliary_loss248 self.output_auxiliary_logits = output_auxiliary_logits249 self.strides = strides250 self.task_seq_len = task_seq_len251 self.text_encoder_width = text_encoder_width252 self.text_encoder_context_length = text_encoder_context_length253 self.text_encoder_num_layers = text_encoder_num_layers254 self.text_encoder_vocab_size = text_encoder_vocab_size255 self.text_encoder_proj_layers = text_encoder_proj_layers256 self.text_encoder_n_ctx = text_encoder_n_ctx257 self.conv_dim = conv_dim258 self.mask_dim = mask_dim259 self.hidden_dim = hidden_dim260 self.encoder_feedforward_dim = encoder_feedforward_dim261 self.norm = norm262 self.encoder_layers = encoder_layers263 self.decoder_layers = decoder_layers264 self.use_task_norm = use_task_norm265 self.num_attention_heads = num_attention_heads266 self.dropout = dropout267 self.dim_feedforward = dim_feedforward268 self.pre_norm = pre_norm269 self.enforce_input_proj = enforce_input_proj270 self.query_dec_layers = query_dec_layers271 self.common_stride = common_stride272 self.num_hidden_layers = decoder_layers273 274 super().__init__(**kwargs)275 276 @property277 def sub_configs(self):278 return (279 {"backbone_config": type(self.backbone_config)}280 if getattr(self, "backbone_config", None) is not None281 else {}282 )283 284 285__all__ = ["OneFormerConfig"]286 