Aluode/PerceptionLabPortable
0
1# coding=utf-82# Copyright 2022 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"""BiT model configuration"""16 17from ...configuration_utils import PretrainedConfig18from ...utils import logging19from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices20 21 22logger = logging.get_logger(__name__)23 24 25class BitConfig(BackboneConfigMixin, PretrainedConfig):26 r"""27 This is the configuration class to store the configuration of a [`BitModel`]. It is used to instantiate an BiT28 model according to the specified arguments, defining the model architecture. Instantiating a configuration with the29 defaults will yield a similar configuration to that of the BiT30 [google/bit-50](https://huggingface.co/google/bit-50) architecture.31 32 Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the33 documentation from [`PretrainedConfig`] for more information.34 35 Args:36 num_channels (`int`, *optional*, defaults to 3):37 The number of input channels.38 embedding_size (`int`, *optional*, defaults to 64):39 Dimensionality (hidden size) for the embedding layer.40 hidden_sizes (`list[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`):41 Dimensionality (hidden size) at each stage.42 depths (`list[int]`, *optional*, defaults to `[3, 4, 6, 3]`):43 Depth (number of layers) for each stage.44 layer_type (`str`, *optional*, defaults to `"preactivation"`):45 The layer to use, it can be either `"preactivation"` or `"bottleneck"`.46 hidden_act (`str`, *optional*, defaults to `"relu"`):47 The non-linear activation function in each block. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"`48 are supported.49 global_padding (`str`, *optional*):50 Padding strategy to use for the convolutional layers. Can be either `"valid"`, `"same"`, or `None`.51 num_groups (`int`, *optional*, defaults to 32):52 Number of groups used for the `BitGroupNormActivation` layers.53 drop_path_rate (`float`, *optional*, defaults to 0.0):54 The drop path rate for the stochastic depth.55 embedding_dynamic_padding (`bool`, *optional*, defaults to `False`):56 Whether or not to make use of dynamic padding for the embedding layer.57 output_stride (`int`, *optional*, defaults to 32):58 The output stride of the model.59 width_factor (`int`, *optional*, defaults to 1):60 The width factor for the model.61 out_features (`list[str]`, *optional*):62 If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc.63 (depending on how many stages the model has). If unset and `out_indices` is set, will default to the64 corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the65 same order as defined in the `stage_names` attribute.66 out_indices (`list[int]`, *optional*):67 If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how68 many stages the model has). If unset and `out_features` is set, will default to the corresponding stages.69 If unset and `out_features` is unset, will default to the last stage. Must be in the70 same order as defined in the `stage_names` attribute.71 72 Example:73 ```python74 >>> from transformers import BitConfig, BitModel75 76 >>> # Initializing a BiT bit-50 style configuration77 >>> configuration = BitConfig()78 79 >>> # Initializing a model (with random weights) from the bit-50 style configuration80 >>> model = BitModel(configuration)81 82 >>> # Accessing the model configuration83 >>> configuration = model.config84 ```85 """86 87 model_type = "bit"88 layer_types = ["preactivation", "bottleneck"]89 supported_padding = ["SAME", "VALID"]90 91 def __init__(92 self,93 num_channels=3,94 embedding_size=64,95 hidden_sizes=[256, 512, 1024, 2048],96 depths=[3, 4, 6, 3],97 layer_type="preactivation",98 hidden_act="relu",99 global_padding=None,100 num_groups=32,101 drop_path_rate=0.0,102 embedding_dynamic_padding=False,103 output_stride=32,104 width_factor=1,105 out_features=None,106 out_indices=None,107 **kwargs,108 ):109 super().__init__(**kwargs)110 if layer_type not in self.layer_types:111 raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types)}")112 if global_padding is not None:113 if global_padding.upper() in self.supported_padding:114 global_padding = global_padding.upper()115 else:116 raise ValueError(f"Padding strategy {global_padding} not supported")117 self.num_channels = num_channels118 self.embedding_size = embedding_size119 self.hidden_sizes = hidden_sizes120 self.depths = depths121 self.layer_type = layer_type122 self.hidden_act = hidden_act123 self.global_padding = global_padding124 self.num_groups = num_groups125 self.drop_path_rate = drop_path_rate126 self.embedding_dynamic_padding = embedding_dynamic_padding127 self.output_stride = output_stride128 self.width_factor = width_factor129 130 self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, len(depths) + 1)]131 self._out_features, self._out_indices = get_aligned_output_features_output_indices(132 out_features=out_features, out_indices=out_indices, stage_names=self.stage_names133 )134 135 136__all__ = ["BitConfig"]137 