
from typing import Optional, Union

import torch
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_mobilenet_va import MobileNetVaConfig


lowercase    :   List[str]						=			logging.get_logger(__name__)


# General docstring
lowercase    :   int						=			'MobileNetV1Config'

# Base docstring
lowercase    :   List[Any]						=			'google/mobilenet_v1_1.0_224'
lowercase    :   Any						=			[1, 1024, 7, 7]

# Image classification docstring
lowercase    :   Optional[Any]						=			'google/mobilenet_v1_1.0_224'
lowercase    :   Tuple						=			'tabby, tabby cat'


lowercase    :   List[str]						=			[
    'google/mobilenet_v1_1.0_224',
    'google/mobilenet_v1_0.75_192',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
]




def       _SCREAMING_SNAKE_CASE   (						_lowerCamelCase					:     List[Any]		,    _lowerCamelCase					:     Optional[Any]		,    _lowerCamelCase					:     List[str]=None)   ->       Dict:






							'''simple docstring'''





							__UpperCamelCase      :  List[str]       =   {}

							if isinstance(_lowerCamelCase		,    _lowerCamelCase):
														__UpperCamelCase      :  Any       =   model.mobilenet_va
							else:
														__UpperCamelCase      :  List[Any]       =   model

							__UpperCamelCase      :  str       =   "MobilenetV1/Conv2d_0/"
							__UpperCamelCase      :  Optional[Any]       =   backbone.conv_stem.convolution.weight
							__UpperCamelCase      :  Optional[int]       =   backbone.conv_stem.normalization.bias
							__UpperCamelCase      :  List[str]       =   backbone.conv_stem.normalization.weight
							__UpperCamelCase      :  Optional[int]       =   backbone.conv_stem.normalization.running_mean
							__UpperCamelCase      :  Tuple       =   backbone.conv_stem.normalization.running_var

							for i in range(13):
														__UpperCamelCase      :  Optional[int]       =   i + 1
														__UpperCamelCase      :  Any       =   i * 2

														__UpperCamelCase      :  List[str]       =   backbone.layer[pt_index]
														__UpperCamelCase      :  Dict       =   F'MobilenetV1/Conv2d_{tf_index}_depthwise/'
														__UpperCamelCase      :  Tuple       =   pointer.convolution.weight
														__UpperCamelCase      :  Any       =   pointer.normalization.bias
														__UpperCamelCase      :  str       =   pointer.normalization.weight
														__UpperCamelCase      :  Tuple       =   pointer.normalization.running_mean
														__UpperCamelCase      :  List[str]       =   pointer.normalization.running_var

														__UpperCamelCase      :  Tuple       =   backbone.layer[pt_index + 1]
														__UpperCamelCase      :  str       =   F'MobilenetV1/Conv2d_{tf_index}_pointwise/'
														__UpperCamelCase      :  List[str]       =   pointer.convolution.weight
														__UpperCamelCase      :  Dict       =   pointer.normalization.bias
														__UpperCamelCase      :  Optional[int]       =   pointer.normalization.weight
														__UpperCamelCase      :  Union[str, Any]       =   pointer.normalization.running_mean
														__UpperCamelCase      :  Optional[Any]       =   pointer.normalization.running_var

							if isinstance(_lowerCamelCase		,    _lowerCamelCase):
														__UpperCamelCase      :  Dict       =   "MobilenetV1/Logits/Conv2d_1c_1x1/"
														__UpperCamelCase      :  Tuple       =   model.classifier.weight
														__UpperCamelCase      :  List[Any]       =   model.classifier.bias

							return tf_to_pt_map




def       _SCREAMING_SNAKE_CASE   (						_lowerCamelCase					:     str		,    _lowerCamelCase					:     Any		,    _lowerCamelCase					:     Optional[Any])   ->       Dict:






							'''simple docstring'''





							try:
														import numpy as np
														import tensorflow as tf
							except ImportError:
														logger.error(
														    "Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see "
														    "https://www.tensorflow.org/install/ for installation instructions.")
														raise

							# Load weights from TF model
							__UpperCamelCase      :  int       =   tf.train.list_variables(_lowerCamelCase)
							__UpperCamelCase      :  Optional[int]       =   {}
							for name, shape in init_vars:
														logger.info(F'Loading TF weight {name} with shape {shape}')
														__UpperCamelCase      :  Tuple       =   tf.train.load_variable(_lowerCamelCase		,    _lowerCamelCase)
														__UpperCamelCase      :  int       =   array

							# Build TF to PyTorch weights loading map
							__UpperCamelCase      :  Optional[Any]       =   _build_tf_to_pytorch_map(_lowerCamelCase		,    _lowerCamelCase		,    _lowerCamelCase)

							for name, pointer in tf_to_pt_map.items():
														logger.info(F'Importing {name}')
														if name not in tf_weights:
																					logger.info(F'{name} not in tf pre-trained weights, skipping')
																					continue

														__UpperCamelCase      :  Optional[Any]       =   tf_weights[name]

														if "depthwise_weights" in name:
																					logger.info("Transposing depthwise")
																					__UpperCamelCase      :  List[str]       =   np.transpose(_lowerCamelCase		,    (2, 3, 0, 1))
														elif "weights" in name:
																					logger.info("Transposing")
																					if len(pointer.shape) == 2:  # copying into linear layer
																												__UpperCamelCase      :  List[Any]       =   array.squeeze().transpose()
																					else:
																												__UpperCamelCase      :  List[Any]       =   np.transpose(_lowerCamelCase		,    (3, 2, 0, 1))

														if pointer.shape != array.shape:
																					raise ValueError(F'Pointer shape {pointer.shape} and array shape {array.shape} mismatched')

														logger.info(F'Initialize PyTorch weight {name} {array.shape}')
														__UpperCamelCase      :  Optional[int]       =   torch.from_numpy(_lowerCamelCase)

														tf_weights.pop(_lowerCamelCase		,    _lowerCamelCase)
														tf_weights.pop(name + "/RMSProp"		,    _lowerCamelCase)
														tf_weights.pop(name + "/RMSProp_1"		,    _lowerCamelCase)
														tf_weights.pop(name + "/ExponentialMovingAverage"		,    _lowerCamelCase)

							logger.info(F'Weights not copied to PyTorch model: {", ".join(tf_weights.keys())}')
							return model




def       _SCREAMING_SNAKE_CASE   (						_lowerCamelCase					:     torch.Tensor		,    _lowerCamelCase					:     nn.Convad)   ->       torch.Tensor:






							'''simple docstring'''





							__UpperCamelCase  ,		__UpperCamelCase      :  Tuple       =   features.shape[-2:]
							__UpperCamelCase  ,		__UpperCamelCase      :  Optional[Any]       =   conv_layer.stride
							__UpperCamelCase  ,		__UpperCamelCase      :  Optional[int]       =   conv_layer.kernel_size

							if in_height % stride_height == 0:
														__UpperCamelCase      :  Optional[int]       =   max(kernel_height - stride_height		,    0)
							else:
														__UpperCamelCase      :  Optional[Any]       =   max(kernel_height - (in_height % stride_height)		,    0)

							if in_width % stride_width == 0:
														__UpperCamelCase      :  Dict       =   max(kernel_width - stride_width		,    0)
							else:
														__UpperCamelCase      :  str       =   max(kernel_width - (in_width % stride_width)		,    0)

							__UpperCamelCase      :  Tuple       =   pad_along_width // 2
							__UpperCamelCase      :  Optional[Any]       =   pad_along_width - pad_left
							__UpperCamelCase      :  List[Any]       =   pad_along_height // 2
							__UpperCamelCase      :  Optional[int]       =   pad_along_height - pad_top

							__UpperCamelCase      :  Optional[int]       =   (pad_left, pad_right, pad_top, pad_bottom)
							return nn.functional.pad(_lowerCamelCase		,    _lowerCamelCase		,    "constant"		,    0.0)




class 					lowerCamelCase__				(							nn.Module):




	'''simple docstring'''




	def __init__(   self						:List[Any]	,  a						:MobileNetVaConfig	,  a						:int	,  a						:int	,  a						:int	,  a						:Optional[int] = 1	,  a						:Optional[int] = 1	,  a						:bool = False	,  a						:Optional[bool] = True	,  a						:Optional[bool or str] = True	,  )						->					None:
								super().__init__()
								__UpperCamelCase      :  Tuple       =   config

								if in_channels % groups != 0:
															raise ValueError(f'Input channels ({in_channels}) are not divisible by {groups} groups.'		)
								if out_channels % groups != 0:
															raise ValueError(f'Output channels ({out_channels}) are not divisible by {groups} groups.'		)

								__UpperCamelCase      :  List[Any]       =   0 if config.tf_padding else int((kernel_size - 1) / 2		)

								__UpperCamelCase      :  List[str]       =   nn.Convad(
								    in_channels=a	,  out_channels=a	,  kernel_size=a	,  stride=a	,  padding=a	,  groups=a	,  bias=a	,  padding_mode="zeros"	,  )

								if use_normalization:
															__UpperCamelCase      :  Optional[int]       =   nn.BatchNormad(
															    num_features=a	,  eps=config.layer_norm_eps	,  momentum=0.9997	,  affine=a	,  track_running_stats=a	,  )
								else:
															__UpperCamelCase      :  List[Any]       =   None

								if use_activation:
															if isinstance(a	,  a		):
																						__UpperCamelCase      :  Any       =   ACTaFN[use_activation]
															elif isinstance(config.hidden_act	,  a		):
																						__UpperCamelCase      :  Any       =   ACTaFN[config.hidden_act]
															else:
																						__UpperCamelCase      :  str       =   config.hidden_act
								else:
															__UpperCamelCase      :  Tuple       =   None







	def   _lowerCamelCase							(   self						:Tuple	,  a						:torch.Tensor		)						->					torch.Tensor:
								if self.config.tf_padding:
															__UpperCamelCase      :  Any       =   apply_tf_padding(a	,  self.convolution		)
								__UpperCamelCase      :  Optional[Any]       =   self.convolution(a		)
								if self.normalization is not None:
															__UpperCamelCase      :  Optional[Any]       =   self.normalization(a		)
								if self.activation is not None:
															__UpperCamelCase      :  List[str]       =   self.activation(a		)
								return features




class 					lowerCamelCase__				(							__lowercase):




	'''simple docstring'''




	_A								=	MobileNetVaConfig
	_A								=	load_tf_weights_in_mobilenet_va
	_A								=	'mobilenet_v1'
	_A								=	'pixel_values'
	_A								=	False







	def   _lowerCamelCase							(   self						:str	,  a						:Union[nn.Linear, nn.Convad]		)						->					None:
								if isinstance(a	,  (nn.Linear, nn.Convad)		):
															module.weight.data.normal_(mean=0.0	,  std=self.config.initializer_range		)
															if module.bias is not None:
																						module.bias.data.zero_()
								elif isinstance(a	,  nn.BatchNormad		):
															module.bias.data.zero_()
															module.weight.data.fill_(1.0		)
lowercase    :   str						=			R'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

lowercase    :   str						=			R'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`MobileNetV1ImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n'




@add_start_docstrings(
    'The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.'	,		__lowercase	,		)
class 					lowerCamelCase__				(							__lowercase):




	'''simple docstring'''




	def __init__(   self						:Optional[Any]	,  a						:MobileNetVaConfig	,  a						:bool = True		)						->					Union[str, Any]:
								super().__init__(a		)
								__UpperCamelCase      :  Dict       =   config

								__UpperCamelCase      :  List[str]       =   3_2
								__UpperCamelCase      :  Optional[int]       =   max(int(depth * config.depth_multiplier		)	,  config.min_depth		)

								__UpperCamelCase      :  Optional[int]       =   MobileNetVaConvLayer(
								    a	,  in_channels=config.num_channels	,  out_channels=a	,  kernel_size=3	,  stride=2	,  )

								__UpperCamelCase      :  Optional[Any]       =   [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

								__UpperCamelCase      :  List[str]       =   nn.ModuleList()
								for i in range(1_3		):
															__UpperCamelCase      :  Dict       =   out_channels

															if strides[i] == 2 or i == 0:
																						depth *= 2
																						__UpperCamelCase      :  Any       =   max(int(depth * config.depth_multiplier		)	,  config.min_depth		)

															self.layer.append(
															    MobileNetVaConvLayer(
															        a	,  in_channels=a	,  out_channels=a	,  kernel_size=3	,  stride=strides[i]	,  groups=a	,  )		)

															self.layer.append(
															    MobileNetVaConvLayer(
															        a	,  in_channels=a	,  out_channels=a	,  kernel_size=1	,  )		)

								__UpperCamelCase      :  Optional[int]       =   nn.AdaptiveAvgPoolad((1, 1)		) if add_pooling_layer else None

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







	def   _lowerCamelCase							(   self						:Tuple	,  a						:Dict		)						->					Tuple:
								raise NotImplementedError







	@add_start_docstrings_to_model_forward(a		)
	@add_code_sample_docstrings(
	    checkpoint=_CHECKPOINT_FOR_DOC	,  output_type=a	,  config_class=_CONFIG_FOR_DOC	,  modality="vision"	,  expected_output=_EXPECTED_OUTPUT_SHAPE	,  )
	def   _lowerCamelCase							(   self						:Any	,  a						:Optional[torch.Tensor] = None	,  a						:Optional[bool] = None	,  a						:Optional[bool] = None	,  )						->					Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:
								__UpperCamelCase      :  List[Any]       =   (
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								__UpperCamelCase      :  List[str]       =   return_dict if return_dict is not None else self.config.use_return_dict

								if pixel_values is None:
															raise ValueError("You have to specify pixel_values"		)

								__UpperCamelCase      :  Dict       =   self.conv_stem(a		)

								__UpperCamelCase      :  List[str]       =   () if output_hidden_states else None

								for i, layer_module in enumerate(self.layer		):
															__UpperCamelCase      :  List[Any]       =   layer_module(a		)

															if output_hidden_states:
																						__UpperCamelCase      :  Dict       =   all_hidden_states + (hidden_states,)

								__UpperCamelCase      :  Any       =   hidden_states

								if self.pooler is not None:
															__UpperCamelCase      :  str       =   torch.flatten(self.pooler(a		)	,  start_dim=1		)
								else:
															__UpperCamelCase      :  int       =   None

								if not return_dict:
															return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None		)

								return BaseModelOutputWithPoolingAndNoAttention(
								    last_hidden_state=a	,  pooler_output=a	,  hidden_states=a	,  )




@add_start_docstrings(
    '\n    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    '	,		__lowercase	,		)
class 					lowerCamelCase__				(							__lowercase):




	'''simple docstring'''




	def __init__(   self						:Optional[Any]	,  a						:MobileNetVaConfig		)						->					None:
								super().__init__(a		)

								__UpperCamelCase      :  List[Any]       =   config.num_labels
								__UpperCamelCase      :  Any       =   MobileNetVaModel(a		)

								__UpperCamelCase      :  Tuple       =   self.mobilenet_va.layer[-1].convolution.out_channels

								# Classifier head
								__UpperCamelCase      :  List[str]       =   nn.Dropout(config.classifier_dropout_prob	,  inplace=a		)
								__UpperCamelCase      :  Optional[int]       =   nn.Linear(a	,  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(a		)
	@add_code_sample_docstrings(
	    checkpoint=_IMAGE_CLASS_CHECKPOINT	,  output_type=a	,  config_class=_CONFIG_FOR_DOC	,  expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT	,  )
	def   _lowerCamelCase							(   self						:Any	,  a						:Optional[torch.Tensor] = None	,  a						:Optional[bool] = None	,  a						:Optional[torch.Tensor] = None	,  a						:Optional[bool] = None	,  )						->					Union[tuple, ImageClassifierOutputWithNoAttention]:
								__UpperCamelCase      :  Union[str, Any]       =   return_dict if return_dict is not None else self.config.use_return_dict

								__UpperCamelCase      :  int       =   self.mobilenet_va(a	,  output_hidden_states=a	,  return_dict=a		)

								__UpperCamelCase      :  Union[str, Any]       =   outputs.pooler_output if return_dict else outputs[1]

								__UpperCamelCase      :  Dict       =   self.classifier(self.dropout(a		)		)

								__UpperCamelCase      :  Any       =   None
								if labels is not None:
															if self.config.problem_type is None:
																						if self.num_labels == 1:
																													__UpperCamelCase      :  Union[str, Any]       =   "regression"
																						elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																													__UpperCamelCase      :  List[Any]       =   "single_label_classification"
																						else:
																													__UpperCamelCase      :  List[str]       =   "multi_label_classification"

															if self.config.problem_type == "regression":
																						__UpperCamelCase      :  Dict       =   MSELoss()
																						if self.num_labels == 1:
																													__UpperCamelCase      :  List[str]       =   loss_fct(logits.squeeze()	,  labels.squeeze()		)
																						else:
																													__UpperCamelCase      :  List[str]       =   loss_fct(a	,  a		)
															elif self.config.problem_type == "single_label_classification":
																						__UpperCamelCase      :  List[str]       =   CrossEntropyLoss()
																						__UpperCamelCase      :  str       =   loss_fct(logits.view(-1	,  self.num_labels		)	,  labels.view(-1		)		)
															elif self.config.problem_type == "multi_label_classification":
																						__UpperCamelCase      :  Any       =   BCEWithLogitsLoss()
																						__UpperCamelCase      :  Dict       =   loss_fct(a	,  a		)

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

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

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


lowercase    :   Optional[int]						=			{
    'configuration_mobilevit': ['MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MobileViTConfig', 'MobileViTOnnxConfig'],
}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase    :   List[str]						=			['MobileViTFeatureExtractor']
		lowercase    :   Tuple						=			['MobileViTImageProcessor']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase    :   List[Any]						=			[
		    'MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'MobileViTForImageClassification',
		    'MobileViTForSemanticSegmentation',
		    'MobileViTModel',
		    'MobileViTPreTrainedModel',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase    :   List[Any]						=			[
		    'TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'TFMobileViTForImageClassification',
		    'TFMobileViTForSemanticSegmentation',
		    'TFMobileViTModel',
		    'TFMobileViTPreTrainedModel',
		]

if TYPE_CHECKING:
		from .configuration_mobilevit import MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileViTConfig, MobileViTOnnxConfig

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_mobilevit import MobileViTFeatureExtractor
				from .image_processing_mobilevit import MobileViTImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mobilevit import (
				    MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    MobileViTForImageClassification,
				    MobileViTForSemanticSegmentation,
				    MobileViTModel,
				    MobileViTPreTrainedModel,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_mobilevit import (
				    TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFMobileViTForImageClassification,
				    TFMobileViTForSemanticSegmentation,
				    TFMobileViTModel,
				    TFMobileViTPreTrainedModel,
				)


else:
		import sys

		lowercase    :   Optional[int]						=			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)