# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# 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.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_UpperCAmelCase          =						{
    """configuration_efficientnet""": [
        """EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """EfficientNetConfig""",
        """EfficientNetOnnxConfig""",
    ]
}

try:
	if not is_vision_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_UpperCAmelCase          =						["""EfficientNetImageProcessor"""]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_UpperCAmelCase          =						[
	    """EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """EfficientNetForImageClassification""",
	    """EfficientNetModel""",
	    """EfficientNetPreTrainedModel""",
	]

if TYPE_CHECKING:
	from .configuration_efficientnet import (
	    EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    EfficientNetConfig,
	    EfficientNetOnnxConfig,
	)

	try:
		if not is_vision_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .image_processing_efficientnet import EfficientNetImageProcessor

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_efficientnet import (
		    EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST,
		    EfficientNetForImageClassification,
		    EfficientNetModel,
		    EfficientNetPreTrainedModel,
		)

else:
	import sys

	_UpperCAmelCase          =						_LazyModule(__name__, globals()["""__file__"""], _import_structure)







from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFSequenceClassifierOutput,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import logging
from .configuration_regnet import RegNetConfig


_UpperCAmelCase          =						logging.get_logger(__name__)

# General docstring
_UpperCAmelCase          =						"""RegNetConfig"""

# Base docstring
_UpperCAmelCase          =						"""facebook/regnet-y-040"""
_UpperCAmelCase          =						[1, 1088, 7, 7]

# Image classification docstring
_UpperCAmelCase          =						"""facebook/regnet-y-040"""
_UpperCAmelCase          =						"""tabby, tabby cat"""

_UpperCAmelCase          =						[
    """facebook/regnet-y-040""",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]





class UpperCAmelCase		(   tf.keras.layers.Layer    ):


				'''simple docstring'''






				def __init__(		self						,      lowercase						,      lowercase = 3						,      lowercase = 1						,      lowercase = 1						,      lowercase = "relu"						,      **lowercase						,      ):



						"""simple docstring"""





						super().__init__(**lowercase   )
						# The padding and conv has been verified in
						# https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb
						A_						:							List[str]      = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2   )
						A_						:							Tuple      = tf.keras.layers.ConvaD(
						    filters=lowercase						,      kernel_size=lowercase						,      strides=lowercase						,      padding='VALID'						,      groups=lowercase						,      use_bias=lowercase						,      name='convolution'						,      )
						A_						:							Dict      = tf.keras.layers.BatchNormalization(epsilon=1E-5						,      momentum=0.9						,      name='normalization'   )
						A_						:							Optional[int]      = ACTaFN[activation] if activation is not None else tf.identity







				def     lowerCAmelCase_  (		self						,      lowercase   ):



						"""simple docstring"""





						A_						:							int      = self.convolution(self.padding(lowercase   )   )
						A_						:							Optional[Any]      = self.normalization(lowercase   )
						A_						:							Union[str, Any]      = self.activation(lowercase   )
						return hidden_state





class UpperCAmelCase		(   tf.keras.layers.Layer    ):


				'''simple docstring'''






				def __init__(		self						,      lowercase						,      **lowercase   ):



						"""simple docstring"""





						super().__init__(**lowercase   )
						A_						:							Dict      = config.num_channels
						A_						:							Dict      = TFRegNetConvLayer(
						    out_channels=config.embedding_size						,      kernel_size=3						,      stride=2						,      activation=config.hidden_act						,      name='embedder'						,      )







				def     lowerCAmelCase_  (		self						,      lowercase   ):



						"""simple docstring"""





						A_						:							Union[str, Any]      = shape_list(lowercase   )[1]
						if tf.executing_eagerly() and 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.'   )

						# When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format.
						# So change the input format from `NCHW` to `NHWC`.
						# shape = (batch_size, in_height, in_width, in_channels=num_channels)
						A_						:							Optional[Any]      = tf.transpose(lowercase						,      perm=(0, 2, 3, 1)   )
						A_						:							Dict      = self.embedder(lowercase   )
						return hidden_state





class UpperCAmelCase		(   tf.keras.layers.Layer    ):


				'''simple docstring'''






				def __init__(		self						,      lowercase						,      lowercase = 2						,      **lowercase   ):



						"""simple docstring"""





						super().__init__(**lowercase   )
						A_						:							Optional[int]      = tf.keras.layers.ConvaD(
						    filters=lowercase						,      kernel_size=1						,      strides=lowercase						,      use_bias=lowercase						,      name='convolution'   )
						A_						:							Tuple      = tf.keras.layers.BatchNormalization(epsilon=1E-5						,      momentum=0.9						,      name='normalization'   )







				def     lowerCAmelCase_  (		self						,      lowercase						,      lowercase = False   ):



						"""simple docstring"""





						return self.normalization(self.convolution(lowercase   )						,      training=lowercase   )





class UpperCAmelCase		(   tf.keras.layers.Layer    ):


				'''simple docstring'''






				def __init__(		self						,      lowercase						,      lowercase						,      **lowercase   ):



						"""simple docstring"""





						super().__init__(**lowercase   )
						A_						:							List[Any]      = tf.keras.layers.GlobalAveragePoolingaD(keepdims=lowercase						,      name='pooler'   )
						A_						:							Any      = [
						    tf.keras.layers.ConvaD(filters=lowercase						,      kernel_size=1						,      activation='relu'						,      name='attention.0'   ),
						    tf.keras.layers.ConvaD(filters=lowercase						,      kernel_size=1						,      activation='sigmoid'						,      name='attention.2'   ),
						]







				def     lowerCAmelCase_  (		self						,      lowercase   ):



						"""simple docstring"""





						A_						:							str      = self.pooler(lowercase   )
						for layer_module in self.attention:
								A_						:							List[str]      = layer_module(lowercase   )
						A_						:							str      = hidden_state * pooled
						return hidden_state





class UpperCAmelCase		(   tf.keras.layers.Layer    ):


				'''simple docstring'''






				def __init__(		self						,      lowercase						,      lowercase						,      lowercase						,      lowercase = 1						,      **lowercase   ):



						"""simple docstring"""





						super().__init__(**lowercase   )
						A_						:							Union[str, Any]      = in_channels != out_channels or stride != 1
						A_						:							Optional[Any]      = max(1						,      out_channels // config.groups_width   )
						A_						:							List[Any]      = (
						    TFRegNetShortCut(lowercase						,      stride=lowercase						,      name='shortcut'   )
						    if should_apply_shortcut
						    else tf.keras.layers.Activation('linear'						,      name='shortcut'   )
						)
						# `self.layers` instead of `self.layer` because that is a reserved argument.
						A_						:							Optional[Any]      = [
						    TFRegNetConvLayer(lowercase						,      kernel_size=1						,      activation=config.hidden_act						,      name='layer.0'   ),
						    TFRegNetConvLayer(
						        lowercase						,      stride=lowercase						,      groups=lowercase						,      activation=config.hidden_act						,      name='layer.1'   ),
						    TFRegNetConvLayer(lowercase						,      kernel_size=1						,      activation=lowercase						,      name='layer.2'   ),
						]
						A_						:							int      = ACTaFN[config.hidden_act]







				def     lowerCAmelCase_  (		self						,      lowercase   ):



						"""simple docstring"""





						A_						:							Optional[Any]      = hidden_state
						for layer_module in self.layers:
								A_						:							Union[str, Any]      = layer_module(lowercase   )
						A_						:							int      = self.shortcut(lowercase   )
						hidden_state += residual
						A_						:							Optional[int]      = self.activation(lowercase   )
						return hidden_state





class UpperCAmelCase		(   tf.keras.layers.Layer    ):


				'''simple docstring'''






				def __init__(		self						,      lowercase						,      lowercase						,      lowercase						,      lowercase = 1						,      **lowercase   ):



						"""simple docstring"""





						super().__init__(**lowercase   )
						A_						:							Any      = in_channels != out_channels or stride != 1
						A_						:							Union[str, Any]      = max(1						,      out_channels // config.groups_width   )
						A_						:							str      = (
						    TFRegNetShortCut(lowercase						,      stride=lowercase						,      name='shortcut'   )
						    if should_apply_shortcut
						    else tf.keras.layers.Activation('linear'						,      name='shortcut'   )
						)
						A_						:							Optional[int]      = [
						    TFRegNetConvLayer(lowercase						,      kernel_size=1						,      activation=config.hidden_act						,      name='layer.0'   ),
						    TFRegNetConvLayer(
						        lowercase						,      stride=lowercase						,      groups=lowercase						,      activation=config.hidden_act						,      name='layer.1'   ),
						    TFRegNetSELayer(lowercase						,      reduced_channels=int(round(in_channels / 4   )   )						,      name='layer.2'   ),
						    TFRegNetConvLayer(lowercase						,      kernel_size=1						,      activation=lowercase						,      name='layer.3'   ),
						]
						A_						:							str      = ACTaFN[config.hidden_act]







				def     lowerCAmelCase_  (		self						,      lowercase   ):



						"""simple docstring"""





						A_						:							Any      = hidden_state
						for layer_module in self.layers:
								A_						:							Optional[Any]      = layer_module(lowercase   )
						A_						:							Optional[Any]      = self.shortcut(lowercase   )
						hidden_state += residual
						A_						:							Tuple      = self.activation(lowercase   )
						return hidden_state





class UpperCAmelCase		(   tf.keras.layers.Layer    ):


				'''simple docstring'''






				def __init__(		self						,      lowercase						,      lowercase						,      lowercase						,      lowercase = 2						,      lowercase = 2						,      **lowercase   ):



						"""simple docstring"""





						super().__init__(**lowercase   )

						A_						:							List[Any]      = TFRegNetXLayer if config.layer_type == 'x' else TFRegNetYLayer
						A_						:							int      = [
						    # downsampling is done in the first layer with stride of 2
						    layer(lowercase						,      lowercase						,      lowercase						,      stride=lowercase						,      name='layers.0'   ),
						    *[layer(lowercase						,      lowercase						,      lowercase						,      name=F'''layers.{i+1}'''   ) for i in range(depth - 1   )],
						]







				def     lowerCAmelCase_  (		self						,      lowercase   ):



						"""simple docstring"""





						for layer_module in self.layers:
								A_						:							List[str]      = layer_module(lowercase   )
						return hidden_state





class UpperCAmelCase		(   tf.keras.layers.Layer    ):


				'''simple docstring'''






				def __init__(		self						,      lowercase						,      **lowercase   ):



						"""simple docstring"""





						super().__init__(**lowercase   )
						A_						:							Any      = []
						# based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
						self.stages.append(
						    TFRegNetStage(
						        lowercase						,      config.embedding_size						,      config.hidden_sizes[0]						,      stride=2 if config.downsample_in_first_stage else 1						,      depth=config.depths[0]						,      name='stages.0'						,      )   )
						A_						:							int      = zip(config.hidden_sizes						,      config.hidden_sizes[1:]   )
						for i, ((in_channels, out_channels), depth) in enumerate(zip(lowercase						,      config.depths[1:]   )   ):
								self.stages.append(TFRegNetStage(lowercase						,      lowercase						,      lowercase						,      depth=lowercase						,      name=F'''stages.{i+1}'''   )   )







				def     lowerCAmelCase_  (		self						,      lowercase						,      lowercase = False						,      lowercase = True   ):



						"""simple docstring"""





						A_						:							Optional[Any]      = () if output_hidden_states else None

						for stage_module in self.stages:
								if output_hidden_states:
										A_						:							Optional[Any]      = hidden_states + (hidden_state,)

								A_						:							List[Any]      = stage_module(lowercase   )

						if output_hidden_states:
								A_						:							str      = hidden_states + (hidden_state,)

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

						return TFBaseModelOutputWithNoAttention(last_hidden_state=lowercase						,      hidden_states=lowercase   )





@keras_serializable
class UpperCAmelCase		(   tf.keras.layers.Layer    ):


				'''simple docstring'''






				lowerCamelCase_        =   RegNetConfig
				def __init__(		self						,      lowercase						,      **lowercase   ):



						"""simple docstring"""





						super().__init__(**lowercase   )
						A_						:							List[Any]      = config
						A_						:							List[str]      = TFRegNetEmbeddings(lowercase						,      name='embedder'   )
						A_						:							Dict      = TFRegNetEncoder(lowercase						,      name='encoder'   )
						A_						:							Any      = tf.keras.layers.GlobalAveragePoolingaD(keepdims=lowercase						,      name='pooler'   )







				@unpack_inputs
				def     lowerCAmelCase_  (		self						,      lowercase						,      lowercase = None						,      lowercase = None						,      lowercase = False						,      ):



						"""simple docstring"""





						A_						:							Dict      = (
						    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
						)
						A_						:							Any      = return_dict if return_dict is not None else self.config.use_return_dict

						A_						:							int      = self.embedder(lowercase						,      training=lowercase   )

						A_						:							int      = self.encoder(
						    lowercase						,      output_hidden_states=lowercase						,      return_dict=lowercase						,      training=lowercase   )

						A_						:							List[Any]      = encoder_outputs[0]
						A_						:							Any      = self.pooler(lowercase   )

						# Change to NCHW output format have uniformity in the modules
						A_						:							Optional[int]      = tf.transpose(lowercase						,      perm=(0, 3, 1, 2)   )
						A_						:							List[str]      = tf.transpose(lowercase						,      perm=(0, 3, 1, 2)   )

						# Change the other hidden state outputs to NCHW as well
						if output_hidden_states:
								A_						:							Dict      = tuple([tf.transpose(lowercase						,      perm=(0, 3, 1, 2)   ) for h in encoder_outputs[1]]   )

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

						return TFBaseModelOutputWithPoolingAndNoAttention(
						    last_hidden_state=lowercase						,      pooler_output=lowercase						,      hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states						,      )





class UpperCAmelCase		(   __A    ):


				'''simple docstring'''






				lowerCamelCase_        =   RegNetConfig
				lowerCamelCase_        =   '''regnet'''
				lowerCamelCase_        =   '''pixel_values'''
				@property
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_2_4, 2_2_4)						,      dtype=tf.floataa   )}

_UpperCAmelCase          =						r"""
    Parameters:
    This model is a Tensorflow
    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a
    regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and
    behavior.
        config ([`RegNetConfig`]): 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 [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.
"""

_UpperCAmelCase          =						r"""
    Args:
        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConveNextImageProcessor.__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 RegNet model outputting raw features without any specific head on top.'''   ,			__A   ,			)
class UpperCAmelCase		(   __A    ):


				'''simple docstring'''






				def __init__(		self						,      lowercase						,      *lowercase						,      **lowercase   ):



						"""simple docstring"""





						super().__init__(lowercase						,      *lowercase						,      **lowercase   )
						A_						:							Union[str, Any]      = TFRegNetMainLayer(lowercase						,      name='regnet'   )







				@unpack_inputs
				@add_start_docstrings_to_model_forward(lowercase   )
				@add_code_sample_docstrings(
				    checkpoint=_CHECKPOINT_FOR_DOC						,      output_type=lowercase						,      config_class=_CONFIG_FOR_DOC						,      modality='vision'						,      expected_output=_EXPECTED_OUTPUT_SHAPE						,      )
				def     lowerCAmelCase_  (		self						,      lowercase						,      lowercase = None						,      lowercase = None						,      lowercase=False						,      ):



						"""simple docstring"""





						A_						:							Union[str, Any]      = (
						    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
						)
						A_						:							str      = return_dict if return_dict is not None else self.config.use_return_dict

						A_						:							int      = self.regnet(
						    pixel_values=lowercase						,      output_hidden_states=lowercase						,      return_dict=lowercase						,      training=lowercase						,      )
						if not return_dict:
								return (outputs[0],) + outputs[1:]

						return TFBaseModelOutputWithPoolingAndNoAttention(
						    last_hidden_state=outputs.last_hidden_state						,      pooler_output=outputs.pooler_output						,      hidden_states=outputs.hidden_states						,      )





@add_start_docstrings(
    '''
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    '''   ,			__A   ,			)
class UpperCAmelCase		(   __A   ,			__A    ):


				'''simple docstring'''






				def __init__(		self						,      lowercase						,      *lowercase						,      **lowercase   ):



						"""simple docstring"""





						super().__init__(lowercase						,      *lowercase						,      **lowercase   )
						A_						:							str      = config.num_labels
						A_						:							Optional[Any]      = TFRegNetMainLayer(lowercase						,      name='regnet'   )
						# classification head
						A_						:							int      = [
						    tf.keras.layers.Flatten(),
						    tf.keras.layers.Dense(config.num_labels						,      name='classifier.1'   ) if config.num_labels > 0 else tf.identity,
						]







				@unpack_inputs
				@add_start_docstrings_to_model_forward(lowercase   )
				@add_code_sample_docstrings(
				    checkpoint=_IMAGE_CLASS_CHECKPOINT						,      output_type=lowercase						,      config_class=_CONFIG_FOR_DOC						,      expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT						,      )
				def     lowerCAmelCase_  (		self						,      lowercase = None						,      lowercase = None						,      lowercase = None						,      lowercase = None						,      lowercase=False						,      ):



						"""simple docstring"""





						A_						:							Any      = (
						    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
						)
						A_						:							Union[str, Any]      = return_dict if return_dict is not None else self.config.use_return_dict

						A_						:							Dict      = self.regnet(
						    lowercase						,      output_hidden_states=lowercase						,      return_dict=lowercase						,      training=lowercase   )

						A_						:							Any      = outputs.pooler_output if return_dict else outputs[1]

						A_						:							Union[str, Any]      = self.classifier[0](lowercase   )
						A_						:							Dict      = self.classifier[1](lowercase   )

						A_						:							Dict      = None if labels is None else self.hf_compute_loss(labels=lowercase						,      logits=lowercase   )

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

						return TFSequenceClassifierOutput(loss=lowercase						,      logits=lowercase						,      hidden_states=outputs.hidden_states   )






