codingutf8 2022 microsoft research inc and the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license resnet model configuration from collections import ordereddict from typing import mapping from packaging import version from configurationutils import pretrainedconfig from onnx import onnxconfig from utils import logging from utils backboneutils import backboneconfigmixin getalignedoutputfeaturesoutputindices logger logging getloggername resnetpretrainedconfigarchivemap microsoftresnet50 https huggingface comicrosoftresnet50blobmainconfig json class resnetconfigbackboneconfigmixin pretrainedconfig r this is the configuration class to store the configuration of a resnetmodel it is used to instantiate an resnet model according to the specified arguments defining the model architecture instantiating a configuration with the defaults will yield a similar configuration to that of the resnet microsoftresnet50https huggingface comicrosoftresnet50 architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args numchannels int optional defaults to 3 the number of input channels embeddingsize int optional defaults to 64 dimensionality hidden size for the embedding layer hiddensizes listint optional defaults to 256 512 1024 2048 dimensionality hidden size at each stage depths listint optional defaults to 3 4 6 3 depth number of layers for each stage layertype str optional defaults to bottleneck the layer to use it can be either basic used for smaller models like resnet18 or resnet34 or bottleneck used for larger models like resnet50 and above hiddenact str optional defaults to relu the nonlinear activation function in each block if string gelu relu selu and gelunew are supported downsampleinfirststage bool optional defaults to false if true the first stage will downsample the inputs using a stride of 2 downsampleinbottleneck bool optional defaults to false if true the first conv 1x1 in resnetbottlenecklayer will downsample the inputs using a stride of 2 outfeatures liststr optional if used as backbone list of features to output can be any of stem stage1 stage2 etc depending on how many stages the model has if unset and outindices is set will default to the corresponding stages if unset and outindices is unset will default to the last stage outindices listint optional if used as backbone list of indices of features to output can be any of 0 1 2 etc depending on how many stages the model has if unset and outfeatures is set will default to the corresponding stages if unset and outfeatures is unset will default to the last stage example python from transformers import resnetconfig resnetmodel initializing a resnet resnet50 style configuration configuration resnetconfig initializing a model with random weights from the resnet50 style configuration model resnetmodelconfiguration accessing the model configuration configuration model config coding utf 8 2022 microsoft research inc and the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license resnet model configuration this is the configuration class to store the configuration of a resnetmodel it is used to instantiate an resnet model according to the specified arguments defining the model architecture instantiating a configuration with the defaults will yield a similar configuration to that of the resnet microsoft resnet 50 https huggingface co microsoft resnet 50 architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args num_channels int optional defaults to 3 the number of input channels embedding_size int optional defaults to 64 dimensionality hidden size for the embedding layer hidden_sizes list int optional defaults to 256 512 1024 2048 dimensionality hidden size at each stage depths list int optional defaults to 3 4 6 3 depth number of layers for each stage layer_type str optional defaults to bottleneck the layer to use it can be either basic used for smaller models like resnet 18 or resnet 34 or bottleneck used for larger models like resnet 50 and above hidden_act str optional defaults to relu the non linear activation function in each block if string gelu relu selu and gelu_new are supported downsample_in_first_stage bool optional defaults to false if true the first stage will downsample the inputs using a stride of 2 downsample_in_bottleneck bool optional defaults to false if true the first conv 1x1 in resnetbottlenecklayer will downsample the inputs using a stride of 2 out_features list str optional if used as backbone list of features to output can be any of stem stage1 stage2 etc depending on how many stages the model has if unset and out_indices is set will default to the corresponding stages if unset and out_indices is unset will default to the last stage out_indices list int optional if used as backbone list of indices of features to output can be any of 0 1 2 etc depending on how many stages the model has if unset and out_features is set will default to the corresponding stages if unset and out_features is unset will default to the last stage example python from transformers import resnetconfig resnetmodel initializing a resnet resnet 50 style configuration configuration resnetconfig initializing a model with random weights from the resnet 50 style configuration model resnetmodel configuration accessing the model configuration configuration model config
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


logger = logging.get_logger(__name__)

RESNET_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json",
}


class ResNetConfig(BackboneConfigMixin, PretrainedConfig):
    r

    model_type = "resnet"
    layer_types = ["basic", "bottleneck"]

    def __init__(
        self,
        num_channels=3,
        embedding_size=64,
        hidden_sizes=[256, 512, 1024, 2048],
        depths=[3, 4, 6, 3],
        layer_type="bottleneck",
        hidden_act="relu",
        downsample_in_first_stage=False,
        downsample_in_bottleneck=False,
        out_features=None,
        out_indices=None,
        **kwargs,
    ):
        super().__init__(**kwargs)
        if layer_type not in self.layer_types:
            raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types)}")
        self.num_channels = num_channels
        self.embedding_size = embedding_size
        self.hidden_sizes = hidden_sizes
        self.depths = depths
        self.layer_type = layer_type
        self.hidden_act = hidden_act
        self.downsample_in_first_stage = downsample_in_first_stage
        self.downsample_in_bottleneck = downsample_in_bottleneck
        self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, len(depths) + 1)]
        self._out_features, self._out_indices = get_aligned_output_features_output_indices(
            out_features=out_features, out_indices=out_indices, stage_names=self.stage_names
        )


class ResNetOnnxConfig(OnnxConfig):
    torch_onnx_minimum_version = version.parse("1.11")

    @property
    def inputs(self) -> Mapping[str, Mapping[int, str]]:
        return OrderedDict(
            [
                ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
            ]
        )

    @property
    def atol_for_validation(self) -> float:
        return 1e-3