codingutf8 2023 google 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 efficientnet model configuration from collections import ordereddict from typing import list mapping from packaging import version from configurationutils import pretrainedconfig from onnx import onnxconfig from utils import logging logger logging getloggername efficientnetpretrainedconfigarchivemap googleefficientnetb7 https huggingface cogoogleefficientnetb7resolvemainconfig json class efficientnetconfigpretrainedconfig r this is the configuration class to store the configuration of a efficientnetmodel it is used to instantiate an efficientnet 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 efficientnet googleefficientnetb7https huggingface cogoogleefficientnetb7 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 imagesize int optional defaults to 600 the input image size widthcoefficient float optional defaults to 2 0 scaling coefficient for network width at each stage depthcoefficient float optional defaults to 3 1 scaling coefficient for network depth at each stage depthdivisor int optional defaults to 8 a unit of network width kernelsizes listint optional defaults to 3 3 5 3 5 5 3 list of kernel sizes to be used in each block inchannels listint optional defaults to 32 16 24 40 80 112 192 list of input channel sizes to be used in each block for convolutional layers outchannels listint optional defaults to 16 24 40 80 112 192 320 list of output channel sizes to be used in each block for convolutional layers depthwisepadding listint optional defaults to list of block indices with square padding strides listint optional defaults to 1 2 2 2 1 2 1 list of stride sizes to be used in each block for convolutional layers numblockrepeats listint optional defaults to 1 2 2 3 3 4 1 list of the number of times each block is to repeated expandratios listint optional defaults to 1 6 6 6 6 6 6 list of scaling coefficient of each block squeezeexpansionratio float optional defaults to 0 25 squeeze expansion ratio hiddenact str or function optional defaults to silu the nonlinear activation function function or string in each block if string gelu relu selu gelunew silu and mish are supported hiddemdim int optional defaults to 1280 the hidden dimension of the layer before the classification head poolingtype str or function optional defaults to mean type of final pooling to be applied before the dense classification head available options are mean max initializerrange float optional defaults to 0 02 the standard deviation of the truncatednormalinitializer for initializing all weight matrices batchnormeps float optional defaults to 1e3 the epsilon used by the batch normalization layers batchnormmomentum float optional defaults to 0 99 the momentum used by the batch normalization layers dropoutrate float optional defaults to 0 5 the dropout rate to be applied before final classifier layer dropconnectrate float optional defaults to 0 2 the drop rate for skip connections example python from transformers import efficientnetconfig efficientnetmodel initializing a efficientnet efficientnetb7 style configuration configuration efficientnetconfig initializing a model with random weights from the efficientnetb7 style configuration model efficientnetmodelconfiguration accessing the model configuration configuration model config modeltype efficientnet def init self numchannels int 3 imagesize int 600 widthcoefficient float 2 0 depthcoefficient float 3 1 depthdivisor int 8 kernelsizes listint 3 3 5 3 5 5 3 inchannels listint 32 16 24 40 80 112 192 outchannels listint 16 24 40 80 112 192 320 depthwisepadding listint strides listint 1 2 2 2 1 2 1 numblockrepeats listint 1 2 2 3 3 4 1 expandratios listint 1 6 6 6 6 6 6 squeezeexpansionratio float 0 25 hiddenact str swish hiddendim int 2560 poolingtype str mean initializerrange float 0 02 batchnormeps float 0 001 batchnormmomentum float 0 99 dropoutrate float 0 5 dropconnectrate float 0 2 kwargs super initkwargs self numchannels numchannels self imagesize imagesize self widthcoefficient widthcoefficient self depthcoefficient depthcoefficient self depthdivisor depthdivisor self kernelsizes kernelsizes self inchannels inchannels self outchannels outchannels self depthwisepadding depthwisepadding self strides strides self numblockrepeats numblockrepeats self expandratios expandratios self squeezeexpansionratio squeezeexpansionratio self hiddenact hiddenact self hiddendim hiddendim self poolingtype poolingtype self initializerrange initializerrange self batchnormeps batchnormeps self batchnormmomentum batchnormmomentum self dropoutrate dropoutrate self dropconnectrate dropconnectrate self numhiddenlayers sumnumblockrepeats 4 class efficientnetonnxconfigonnxconfig torchonnxminimumversion version parse1 11 property def inputsself mappingstr mappingint str return ordereddict pixelvalues 0 batch 1 numchannels 2 height 3 width property def atolforvalidationself float return 1e5 coding utf 8 2023 google 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 efficientnet model configuration this is the configuration class to store the configuration of a efficientnetmodel it is used to instantiate an efficientnet 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 efficientnet google efficientnet b7 https huggingface co google efficientnet b7 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 image_size int optional defaults to 600 the input image size width_coefficient float optional defaults to 2 0 scaling coefficient for network width at each stage depth_coefficient float optional defaults to 3 1 scaling coefficient for network depth at each stage depth_divisor int optional defaults to 8 a unit of network width kernel_sizes list int optional defaults to 3 3 5 3 5 5 3 list of kernel sizes to be used in each block in_channels list int optional defaults to 32 16 24 40 80 112 192 list of input channel sizes to be used in each block for convolutional layers out_channels list int optional defaults to 16 24 40 80 112 192 320 list of output channel sizes to be used in each block for convolutional layers depthwise_padding list int optional defaults to list of block indices with square padding strides list int optional defaults to 1 2 2 2 1 2 1 list of stride sizes to be used in each block for convolutional layers num_block_repeats list int optional defaults to 1 2 2 3 3 4 1 list of the number of times each block is to repeated expand_ratios list int optional defaults to 1 6 6 6 6 6 6 list of scaling coefficient of each block squeeze_expansion_ratio float optional defaults to 0 25 squeeze expansion ratio hidden_act str or function optional defaults to silu the non linear activation function function or string in each block if string gelu relu selu gelu_new silu and mish are supported hiddem_dim int optional defaults to 1280 the hidden dimension of the layer before the classification head pooling_type str or function optional defaults to mean type of final pooling to be applied before the dense classification head available options are mean max initializer_range float optional defaults to 0 02 the standard deviation of the truncated_normal_initializer for initializing all weight matrices batch_norm_eps float optional defaults to 1e 3 the epsilon used by the batch normalization layers batch_norm_momentum float optional defaults to 0 99 the momentum used by the batch normalization layers dropout_rate float optional defaults to 0 5 the dropout rate to be applied before final classifier layer drop_connect_rate float optional defaults to 0 2 the drop rate for skip connections example python from transformers import efficientnetconfig efficientnetmodel initializing a efficientnet efficientnet b7 style configuration configuration efficientnetconfig initializing a model with random weights from the efficientnet b7 style configuration model efficientnetmodel configuration accessing the model configuration configuration model config
from collections import OrderedDict
from typing import List, Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


logger = logging.get_logger(__name__)

EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "google/efficientnet-b7": "https://huggingface.co/google/efficientnet-b7/resolve/main/config.json",
}


class EfficientNetConfig(PretrainedConfig):
    r

    model_type = "efficientnet"

    def __init__(
        self,
        num_channels: int = 3,
        image_size: int = 600,
        width_coefficient: float = 2.0,
        depth_coefficient: float = 3.1,
        depth_divisor: int = 8,
        kernel_sizes: List[int] = [3, 3, 5, 3, 5, 5, 3],
        in_channels: List[int] = [32, 16, 24, 40, 80, 112, 192],
        out_channels: List[int] = [16, 24, 40, 80, 112, 192, 320],
        depthwise_padding: List[int] = [],
        strides: List[int] = [1, 2, 2, 2, 1, 2, 1],
        num_block_repeats: List[int] = [1, 2, 2, 3, 3, 4, 1],
        expand_ratios: List[int] = [1, 6, 6, 6, 6, 6, 6],
        squeeze_expansion_ratio: float = 0.25,
        hidden_act: str = "swish",
        hidden_dim: int = 2560,
        pooling_type: str = "mean",
        initializer_range: float = 0.02,
        batch_norm_eps: float = 0.001,
        batch_norm_momentum: float = 0.99,
        dropout_rate: float = 0.5,
        drop_connect_rate: float = 0.2,
        **kwargs,
    ):
        super().__init__(**kwargs)

        self.num_channels = num_channels
        self.image_size = image_size
        self.width_coefficient = width_coefficient
        self.depth_coefficient = depth_coefficient
        self.depth_divisor = depth_divisor
        self.kernel_sizes = kernel_sizes
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.depthwise_padding = depthwise_padding
        self.strides = strides
        self.num_block_repeats = num_block_repeats
        self.expand_ratios = expand_ratios
        self.squeeze_expansion_ratio = squeeze_expansion_ratio
        self.hidden_act = hidden_act
        self.hidden_dim = hidden_dim
        self.pooling_type = pooling_type
        self.initializer_range = initializer_range
        self.batch_norm_eps = batch_norm_eps
        self.batch_norm_momentum = batch_norm_momentum
        self.dropout_rate = dropout_rate
        self.drop_connect_rate = drop_connect_rate
        self.num_hidden_layers = sum(num_block_repeats) * 4


class EfficientNetOnnxConfig(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-5