from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


a  :      int            =    logging.get_logger(__name__)

# General docstring
a  :      List[str]            =    """ResNetConfig"""

# Base docstring
a  :      Dict            =    """microsoft/resnet-50"""
a  :      Any            =    [1, 2_0_4_8, 7, 7]

# Image classification docstring
a  :      Tuple            =    """microsoft/resnet-50"""
a  :      Optional[Any]            =    """tiger cat"""

a  :      Dict            =    [
    """microsoft/resnet-50""",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]

class 					_lowercase    (     nn.Module			):






    '''simple docstring'''




    def __init__(    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__ = 3       ,						lowerCamelCase__ = 1       ,						lowerCamelCase__ = "relu" ):
       super().__init__()
       lowerCAmelCase_:							Optional[Any]       =	nn.Convad(
           lowerCamelCase__       ,						lowerCamelCase__       ,						kernel_size=lowerCamelCase__       ,						stride=lowerCamelCase__       ,						padding=kernel_size // 2       ,						bias=lowerCamelCase__ )
       lowerCAmelCase_:							Union[str, Any]       =	nn.BatchNormad(lowerCamelCase__ )
       lowerCAmelCase_:							Tuple       =	ACTaFN[activation] if activation is not None else nn.Identity()




    def      _a    (    self       ,						lowerCamelCase__ ):
       lowerCAmelCase_:							Union[str, Any]       =	self.convolution(lowerCamelCase__ )
       lowerCAmelCase_:							List[Any]       =	self.normalization(lowerCamelCase__ )
       lowerCAmelCase_:							Dict       =	self.activation(lowerCamelCase__ )
       return hidden_state





class 					_lowercase    (     nn.Module			):






    '''simple docstring'''




    def __init__(    self       ,						lowerCamelCase__ ):
       super().__init__()
       lowerCAmelCase_:							Any       =	ResNetConvLayer(
           config.num_channels       ,						config.embedding_size       ,						kernel_size=7       ,						stride=2       ,						activation=config.hidden_act )
       lowerCAmelCase_:							Tuple       =	nn.MaxPoolad(kernel_size=3       ,						stride=2       ,						padding=1 )
       lowerCAmelCase_:							Dict       =	config.num_channels




    def      _a    (    self       ,						lowerCamelCase__ ):
       lowerCAmelCase_:							Union[str, Any]       =	pixel_values.shape[1]
       if num_channels != self.num_channels:
          raise ValueError(
              "Make sure that the channel dimension of the pixel values match with the one set in the configuration." )
       lowerCAmelCase_:							Optional[int]       =	self.embedder(lowerCamelCase__ )
       lowerCAmelCase_:							Dict       =	self.pooler(lowerCamelCase__ )
       return embedding





class 					_lowercase    (     nn.Module			):






    '''simple docstring'''




    def __init__(    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__ = 2 ):
       super().__init__()
       lowerCAmelCase_:							str       =	nn.Convad(lowerCamelCase__       ,						lowerCamelCase__       ,						kernel_size=1       ,						stride=lowerCamelCase__       ,						bias=lowerCamelCase__ )
       lowerCAmelCase_:							Tuple       =	nn.BatchNormad(lowerCamelCase__ )




    def      _a    (    self       ,						lowerCamelCase__ ):
       lowerCAmelCase_:							List[Any]       =	self.convolution(lowerCamelCase__ )
       lowerCAmelCase_:							Optional[int]       =	self.normalization(lowerCamelCase__ )
       return hidden_state





class 					_lowercase    (     nn.Module			):






    '''simple docstring'''




    def __init__(    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__ = 1       ,						lowerCamelCase__ = "relu" ):
       super().__init__()
       lowerCAmelCase_:							Tuple       =	in_channels != out_channels or stride != 1
       lowerCAmelCase_:							Optional[Any]       =	(
           ResNetShortCut(lowerCamelCase__       ,						lowerCamelCase__       ,						stride=lowerCamelCase__ ) if should_apply_shortcut else nn.Identity()
       )
       lowerCAmelCase_:							str       =	nn.Sequential(
           ResNetConvLayer(lowerCamelCase__       ,						lowerCamelCase__       ,						stride=lowerCamelCase__ )       ,						ResNetConvLayer(lowerCamelCase__       ,						lowerCamelCase__       ,						activation=lowerCamelCase__ )       ,						)
       lowerCAmelCase_:							Union[str, Any]       =	ACTaFN[activation]




    def      _a    (    self       ,						lowerCamelCase__ ):
       lowerCAmelCase_:							int       =	hidden_state
       lowerCAmelCase_:							Union[str, Any]       =	self.layer(lowerCamelCase__ )
       lowerCAmelCase_:							Dict       =	self.shortcut(lowerCamelCase__ )
       hidden_state += residual
       lowerCAmelCase_:							List[Any]       =	self.activation(lowerCamelCase__ )
       return hidden_state





class 					_lowercase    (     nn.Module			):






    '''simple docstring'''




    def __init__(    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__ = 1       ,						lowerCamelCase__ = "relu"       ,						lowerCamelCase__ = 4 ):
       super().__init__()
       lowerCAmelCase_:							Optional[Any]       =	in_channels != out_channels or stride != 1
       lowerCAmelCase_:							int       =	out_channels // reduction
       lowerCAmelCase_:							int       =	(
           ResNetShortCut(lowerCamelCase__       ,						lowerCamelCase__       ,						stride=lowerCamelCase__ ) if should_apply_shortcut else nn.Identity()
       )
       lowerCAmelCase_:							Dict       =	nn.Sequential(
           ResNetConvLayer(lowerCamelCase__       ,						lowerCamelCase__       ,						kernel_size=1 )       ,						ResNetConvLayer(lowerCamelCase__       ,						lowerCamelCase__       ,						stride=lowerCamelCase__ )       ,						ResNetConvLayer(lowerCamelCase__       ,						lowerCamelCase__       ,						kernel_size=1       ,						activation=lowerCamelCase__ )       ,						)
       lowerCAmelCase_:							Dict       =	ACTaFN[activation]




    def      _a    (    self       ,						lowerCamelCase__ ):
       lowerCAmelCase_:							List[Any]       =	hidden_state
       lowerCAmelCase_:							List[Any]       =	self.layer(lowerCamelCase__ )
       lowerCAmelCase_:							Tuple       =	self.shortcut(lowerCamelCase__ )
       hidden_state += residual
       lowerCAmelCase_:							List[Any]       =	self.activation(lowerCamelCase__ )
       return hidden_state





class 					_lowercase    (     nn.Module			):






    '''simple docstring'''




    def __init__(    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__ = 2       ,						lowerCamelCase__ = 2       ,						):
       super().__init__()

       lowerCAmelCase_:							Optional[int]       =	ResNetBottleNeckLayer if config.layer_type == "bottleneck" else ResNetBasicLayer

       lowerCAmelCase_:							str       =	nn.Sequential(
           # downsampling is done in the first layer with stride of 2
           layer(lowerCamelCase__       ,						lowerCamelCase__       ,						stride=lowerCamelCase__       ,						activation=config.hidden_act )       ,						*[layer(lowerCamelCase__       ,						lowerCamelCase__       ,						activation=config.hidden_act ) for _ in range(depth - 1 )]       ,						)




    def      _a    (    self       ,						lowerCamelCase__ ):
       lowerCAmelCase_:							int       =	input
       for layer in self.layers:
          lowerCAmelCase_:							Optional[int]       =	layer(lowerCamelCase__ )
       return hidden_state





class 					_lowercase    (     nn.Module			):






    '''simple docstring'''




    def __init__(    self       ,						lowerCamelCase__ ):
       super().__init__()
       lowerCAmelCase_:							Tuple       =	nn.ModuleList([] )
       # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
       self.stages.append(
           ResNetStage(
               lowerCamelCase__       ,						config.embedding_size       ,						config.hidden_sizes[0]       ,						stride=2 if config.downsample_in_first_stage else 1       ,						depth=config.depths[0]       ,						) )
       lowerCAmelCase_:							List[Any]       =	zip(config.hidden_sizes       ,						config.hidden_sizes[1:] )
       for (in_channels, out_channels), depth in zip(lowerCamelCase__       ,						config.depths[1:] ):
          self.stages.append(ResNetStage(lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						depth=lowerCamelCase__ ) )




    def      _a    (    self       ,						lowerCamelCase__       ,						lowerCamelCase__ = False       ,						lowerCamelCase__ = True ):
       lowerCAmelCase_:							Optional[Any]       =	() if output_hidden_states else None

       for stage_module in self.stages:
          if output_hidden_states:
             lowerCAmelCase_:							Dict       =	hidden_states + (hidden_state,)

          lowerCAmelCase_:							Optional[int]       =	stage_module(lowerCamelCase__ )

       if output_hidden_states:
          lowerCAmelCase_:							Any       =	hidden_states + (hidden_state,)

       if not return_dict:
          return tuple(v for v in [hidden_state, hidden_states] if v is not None )

       return BaseModelOutputWithNoAttention(
           last_hidden_state=lowerCamelCase__       ,						hidden_states=lowerCamelCase__       ,						)





class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:				int          =    ResNetConfig
    SCREAMING_SNAKE_CASE:				Optional[int]          =    'resnet'
    SCREAMING_SNAKE_CASE:				Optional[int]          =    'pixel_values'
    SCREAMING_SNAKE_CASE:				Optional[Any]          =    True




    def      _a    (    self       ,						lowerCamelCase__ ):
       if isinstance(lowerCamelCase__       ,						nn.Convad ):
          nn.init.kaiming_normal_(module.weight       ,						mode="fan_out"       ,						nonlinearity="relu" )
       elif isinstance(lowerCamelCase__       ,						(nn.BatchNormad, nn.GroupNorm) ):
          nn.init.constant_(module.weight       ,						1 )
          nn.init.constant_(module.bias       ,						0 )




    def      _a    (    self       ,						lowerCamelCase__       ,						lowerCamelCase__=False ):
       if isinstance(lowerCamelCase__       ,						lowerCamelCase__ ):
          lowerCAmelCase_:							int       =	value





a  :      Union[str, Any]            =    r"""
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
"""

a  :      Optional[int]            =    r"""
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
"""

@add_start_docstrings(
    'The bare ResNet model outputting raw features without any specific head on top.' ,  UpperCAmelCase__ ,  )
class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''




    def __init__(    self       ,						lowerCamelCase__ ):
       super().__init__(lowerCamelCase__ )
       lowerCAmelCase_:							Any       =	config
       lowerCAmelCase_:							Tuple       =	ResNetEmbeddings(lowerCamelCase__ )
       lowerCAmelCase_:							List[Any]       =	ResNetEncoder(lowerCamelCase__ )
       lowerCAmelCase_:							Tuple       =	nn.AdaptiveAvgPoolad((1, 1) )
       # Initialize weights and apply final processing
       self.post_init()




    @add_start_docstrings_to_model_forward(lowerCamelCase__ )
    @add_code_sample_docstrings(
        checkpoint=_CHECKPOINT_FOR_DOC       ,						output_type=lowerCamelCase__       ,						config_class=_CONFIG_FOR_DOC       ,						modality="vision"       ,						expected_output=_EXPECTED_OUTPUT_SHAPE       ,						)
    def      _a    (    self       ,						lowerCamelCase__       ,						lowerCamelCase__ = None       ,						lowerCamelCase__ = None ):
       lowerCAmelCase_:							int       =	(
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       lowerCAmelCase_:							str       =	return_dict if return_dict is not None else self.config.use_return_dict

       lowerCAmelCase_:							str       =	self.embedder(lowerCamelCase__ )

       lowerCAmelCase_:							Any       =	self.encoder(
           lowerCamelCase__       ,						output_hidden_states=lowerCamelCase__       ,						return_dict=lowerCamelCase__ )

       lowerCAmelCase_:							Optional[Any]       =	encoder_outputs[0]

       lowerCAmelCase_:							int       =	self.pooler(lowerCamelCase__ )

       if not return_dict:
          return (last_hidden_state, pooled_output) + encoder_outputs[1:]

       return BaseModelOutputWithPoolingAndNoAttention(
           last_hidden_state=lowerCamelCase__       ,						pooler_output=lowerCamelCase__       ,						hidden_states=encoder_outputs.hidden_states       ,						)





@add_start_docstrings(
    '\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    ' ,  UpperCAmelCase__ ,  )
class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''




    def __init__(    self       ,						lowerCamelCase__ ):
       super().__init__(lowerCamelCase__ )
       lowerCAmelCase_:							List[str]       =	config.num_labels
       lowerCAmelCase_:							Optional[Any]       =	ResNetModel(lowerCamelCase__ )
       # classification head
       lowerCAmelCase_:							Dict       =	nn.Sequential(
           nn.Flatten()       ,						nn.Linear(config.hidden_sizes[-1]       ,						config.num_labels ) if config.num_labels > 0 else nn.Identity()       ,						)
       # initialize weights and apply final processing
       self.post_init()




    @add_start_docstrings_to_model_forward(lowerCamelCase__ )
    @add_code_sample_docstrings(
        checkpoint=_IMAGE_CLASS_CHECKPOINT       ,						output_type=lowerCamelCase__       ,						config_class=_CONFIG_FOR_DOC       ,						expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT       ,						)
    def      _a    (    self       ,						lowerCamelCase__ = None       ,						lowerCamelCase__ = None       ,						lowerCamelCase__ = None       ,						lowerCamelCase__ = None       ,						):
       lowerCAmelCase_:							Union[str, Any]       =	return_dict if return_dict is not None else self.config.use_return_dict

       lowerCAmelCase_:							List[str]       =	self.resnet(lowerCamelCase__       ,						output_hidden_states=lowerCamelCase__       ,						return_dict=lowerCamelCase__ )

       lowerCAmelCase_:							int       =	outputs.pooler_output if return_dict else outputs[1]

       lowerCAmelCase_:							Any       =	self.classifier(lowerCamelCase__ )

       lowerCAmelCase_:							str       =	None

       if labels is not None:
          if self.config.problem_type is None:
             if self.num_labels == 1:
                lowerCAmelCase_:							Optional[Any]       =	"regression"
             elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                lowerCAmelCase_:							Optional[Any]       =	"single_label_classification"
             else:
                lowerCAmelCase_:							Union[str, Any]       =	"multi_label_classification"
          if self.config.problem_type == "regression":
             lowerCAmelCase_:							Optional[int]       =	MSELoss()
             if self.num_labels == 1:
                lowerCAmelCase_:							Optional[Any]       =	loss_fct(logits.squeeze()       ,						labels.squeeze() )
             else:
                lowerCAmelCase_:							str       =	loss_fct(lowerCamelCase__       ,						lowerCamelCase__ )
          elif self.config.problem_type == "single_label_classification":
             lowerCAmelCase_:							Optional[int]       =	CrossEntropyLoss()
             lowerCAmelCase_:							Union[str, Any]       =	loss_fct(logits.view(-1       ,						self.num_labels )       ,						labels.view(-1 ) )
          elif self.config.problem_type == "multi_label_classification":
             lowerCAmelCase_:							List[Any]       =	BCEWithLogitsLoss()
             lowerCAmelCase_:							List[Any]       =	loss_fct(lowerCamelCase__       ,						lowerCamelCase__ )

       if not return_dict:
          lowerCAmelCase_:							List[Any]       =	(logits,) + outputs[2:]
          return (loss,) + output if loss is not None else output

       return ImageClassifierOutputWithNoAttention(loss=lowerCamelCase__       ,						logits=lowerCamelCase__       ,						hidden_states=outputs.hidden_states )





@add_start_docstrings(
    '\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    ' ,  UpperCAmelCase__ ,  )
class 					_lowercase    (     UpperCAmelCase__ ,  UpperCAmelCase__			):






    '''simple docstring'''




    def __init__(    self       ,						lowerCamelCase__ ):
       super().__init__(lowerCamelCase__ )
       super()._init_backbone(lowerCamelCase__ )

       lowerCAmelCase_:							Dict       =	[config.embedding_size] + config.hidden_sizes
       lowerCAmelCase_:							List[Any]       =	ResNetEmbeddings(lowerCamelCase__ )
       lowerCAmelCase_:							Any       =	ResNetEncoder(lowerCamelCase__ )

       # initialize weights and apply final processing
       self.post_init()




    @add_start_docstrings_to_model_forward(lowerCamelCase__ )
    @replace_return_docstrings(output_type=lowerCamelCase__       ,						config_class=_CONFIG_FOR_DOC )
    def      _a    (    self       ,						lowerCamelCase__       ,						lowerCamelCase__ = None       ,						lowerCamelCase__ = None ):
       lowerCAmelCase_:							Tuple       =	return_dict if return_dict is not None else self.config.use_return_dict
       lowerCAmelCase_:							Optional[int]       =	(
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )

       lowerCAmelCase_:							int       =	self.embedder(lowerCamelCase__ )

       lowerCAmelCase_:							Optional[int]       =	self.encoder(lowerCamelCase__       ,						output_hidden_states=lowerCamelCase__       ,						return_dict=lowerCamelCase__ )

       lowerCAmelCase_:							Optional[int]       =	outputs.hidden_states

       lowerCAmelCase_:							Optional[Any]       =	()
       for idx, stage in enumerate(self.stage_names ):
          if stage in self.out_features:
             feature_maps += (hidden_states[idx],)

       if not return_dict:
          lowerCAmelCase_:							str       =	(feature_maps,)
          if output_hidden_states:
             output += (outputs.hidden_states,)
          return output

       return BackboneOutput(
           feature_maps=lowerCamelCase__       ,						hidden_states=outputs.hidden_states if output_hidden_states else None       ,						attentions=lowerCamelCase__       ,						)
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


a  :      Optional[int]            =    logging.get_logger(__name__)

a  :      Optional[Any]            =    {
    """google/efficientnet-b7""": """https://huggingface.co/google/efficientnet-b7/resolve/main/config.json""",
}

class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:				Dict          =    'efficientnet'




    def __init__(    self       ,						lowerCamelCase__ = 3       ,						lowerCamelCase__ = 600       ,						lowerCamelCase__ = 2.0       ,						lowerCamelCase__ = 3.1       ,						lowerCamelCase__ = 8       ,						lowerCamelCase__ = [3, 3, 5, 3, 5, 5, 3]       ,						lowerCamelCase__ = [32, 16, 24, 40, 80, 112, 192]       ,						lowerCamelCase__ = [16, 24, 40, 80, 112, 192, 320]       ,						lowerCamelCase__ = []       ,						lowerCamelCase__ = [1, 2, 2, 2, 1, 2, 1]       ,						lowerCamelCase__ = [1, 2, 2, 3, 3, 4, 1]       ,						lowerCamelCase__ = [1, 6, 6, 6, 6, 6, 6]       ,						lowerCamelCase__ = 0.2_5       ,						lowerCamelCase__ = "swish"       ,						lowerCamelCase__ = 2_560       ,						lowerCamelCase__ = "mean"       ,						lowerCamelCase__ = 0.0_2       ,						lowerCamelCase__ = 0.0_0_1       ,						lowerCamelCase__ = 0.9_9       ,						lowerCamelCase__ = 0.5       ,						lowerCamelCase__ = 0.2       ,						**lowerCamelCase__       ,						):
       super().__init__(**lowerCamelCase__ )

       lowerCAmelCase_:							str       =	num_channels
       lowerCAmelCase_:							str       =	image_size
       lowerCAmelCase_:							int       =	width_coefficient
       lowerCAmelCase_:							Union[str, Any]       =	depth_coefficient
       lowerCAmelCase_:							int       =	depth_divisor
       lowerCAmelCase_:							List[str]       =	kernel_sizes
       lowerCAmelCase_:							Tuple       =	in_channels
       lowerCAmelCase_:							List[str]       =	out_channels
       lowerCAmelCase_:							List[str]       =	depthwise_padding
       lowerCAmelCase_:							Optional[int]       =	strides
       lowerCAmelCase_:							List[str]       =	num_block_repeats
       lowerCAmelCase_:							Any       =	expand_ratios
       lowerCAmelCase_:							List[Any]       =	squeeze_expansion_ratio
       lowerCAmelCase_:							Optional[int]       =	hidden_act
       lowerCAmelCase_:							Optional[int]       =	hidden_dim
       lowerCAmelCase_:							Dict       =	pooling_type
       lowerCAmelCase_:							Optional[Any]       =	initializer_range
       lowerCAmelCase_:							int       =	batch_norm_eps
       lowerCAmelCase_:							List[str]       =	batch_norm_momentum
       lowerCAmelCase_:							List[Any]       =	dropout_rate
       lowerCAmelCase_:							Union[str, Any]       =	drop_connect_rate
       lowerCAmelCase_:							Tuple       =	sum(lowerCamelCase__ ) * 4





class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:				Optional[Any]          =    version.parse('1.11'			)




    @property
    def      _a    (    self ):
       return OrderedDict(
           [
               ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
           ] )




    @property
    def      _a    (    self ):
       return 1E-5