
import inspect
import unittest

from transformers import MobileNetVaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import MobileNetVaForImageClassification, MobileNetVaModel
					from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import MobileNetVaImageProcessor







class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_   ):







	'''simple docstring'''





	def   __snake_case				(			self		):
					A__  :	Union[str, Any]         =							self.config_class(**self.inputs_dict		)
					self.parent.assertTrue(hasattr(UpperCamelCase__				,       '''tf_padding'''		)		)
					self.parent.assertTrue(hasattr(UpperCamelCase__				,       '''depth_multiplier'''		)		)







class  UpperCamelCase__		:







	'''simple docstring'''





	def __init__(			self				,       UpperCamelCase__				,       UpperCamelCase__=13				,       UpperCamelCase__=3				,       UpperCamelCase__=32				,       UpperCamelCase__=0.2_5				,       UpperCamelCase__=8				,       UpperCamelCase__=True				,       UpperCamelCase__=1024				,       UpperCamelCase__=32				,       UpperCamelCase__="relu6"				,       UpperCamelCase__=0.1				,       UpperCamelCase__=0.0_2				,       UpperCamelCase__=True				,       UpperCamelCase__=True				,       UpperCamelCase__=10				,       UpperCamelCase__=None				,       ):
					A__  :	Any         =							parent
					A__  :	Dict         =							batch_size
					A__  :	Union[str, Any]         =							num_channels
					A__  :	Any         =							image_size
					A__  :	Optional[int]         =							depth_multiplier
					A__  :	List[str]         =							min_depth
					A__  :	Union[str, Any]         =							tf_padding
					A__  :	Dict         =							int(last_hidden_size * depth_multiplier		)
					A__  :	int         =							output_stride
					A__  :	List[Any]         =							hidden_act
					A__  :	Dict         =							classifier_dropout_prob
					A__  :	List[Any]         =							use_labels
					A__  :	int         =							is_training
					A__  :	Tuple         =							num_labels
					A__  :	Tuple         =							initializer_range
					A__  :	int         =							scope





	def   __snake_case				(			self		):
					A__  :	int         =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

					A__  :	str         =							None
					A__  :	Union[str, Any]         =							None
					if self.use_labels:
									A__  :	Optional[int]         =							ids_tensor([self.batch_size]				,       self.num_labels		)
									A__  :	Optional[Any]         =							ids_tensor([self.batch_size, self.image_size, self.image_size]				,       self.num_labels		)

					A__  :	str         =							self.get_config()

					return config, pixel_values, labels, pixel_labels





	def   __snake_case				(			self		):
					return MobileNetVaConfig(
					    num_channels=self.num_channels				,       image_size=self.image_size				,       depth_multiplier=self.depth_multiplier				,       min_depth=self.min_depth				,       tf_padding=self.tf_padding				,       hidden_act=self.hidden_act				,       classifier_dropout_prob=self.classifier_dropout_prob				,       initializer_range=self.initializer_range				,       )





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__  :	Dict         =							MobileNetVaModel(config=UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.eval()
					A__  :	int         =							model(UpperCamelCase__		)
					self.parent.assertEqual(
					    result.last_hidden_state.shape				,       (
					        self.batch_size,
					        self.last_hidden_size,
					        self.image_size // self.output_stride,
					        self.image_size // self.output_stride,
					    )				,       )





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__  :	str         =							self.num_labels
					A__  :	int         =							MobileNetVaForImageClassification(UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.eval()
					A__  :	Union[str, Any]         =							model(UpperCamelCase__				,       labels=UpperCamelCase__		)
					self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)		)





	def   __snake_case				(			self		):
					A__  :	List[str]         =							self.prepare_config_and_inputs()
					A__       ,							A__       ,							A__       ,							A__  :	Any         =							config_and_inputs
					A__  :	Tuple         =							{'''pixel_values''': pixel_values}
					return config, inputs_dict







@require_torch
class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			unittest.TestCase   ):







	'''simple docstring'''
	_lowerCAmelCase							       =				(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
	_lowerCAmelCase							       =				(
	    {"feature-extraction": MobileNetVaModel, "image-classification": MobileNetVaForImageClassification}
	    if is_torch_available()
	    else {}
	)

	_lowerCAmelCase							       =				False
	_lowerCAmelCase							       =				False
	_lowerCAmelCase							       =				False
	_lowerCAmelCase							       =				False





	def   __snake_case				(			self		):
					A__  :	List[Any]         =							MobileNetVaModelTester(self		)
					A__  :	Dict         =							MobileNetVaConfigTester(self				,       config_class=UpperCamelCase__				,       has_text_modality=UpperCamelCase__		)





	def   __snake_case				(			self		):
					self.config_tester.run_common_tests()





	@unittest.skip(reason='''MobileNetV1 does not use inputs_embeds'''		)
	def   __snake_case				(			self		):
					pass





	@unittest.skip(reason='''MobileNetV1 does not support input and output embeddings'''		)
	def   __snake_case				(			self		):
					pass





	@unittest.skip(reason='''MobileNetV1 does not output attentions'''		)
	def   __snake_case				(			self		):
					pass





	def   __snake_case				(			self		):
					A__       ,							A__  :	int         =							self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									A__  :	Optional[Any]         =							model_class(UpperCamelCase__		)
									A__  :	Optional[int]         =							inspect.signature(model.forward		)
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									A__  :	Any         =							[*signature.parameters.keys()]

									A__  :	Tuple         =							['''pixel_values''']
									self.assertListEqual(arg_names[:1]				,       UpperCamelCase__		)





	def   __snake_case				(			self		):
					A__  :	Union[str, Any]         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*UpperCamelCase__		)





	def   __snake_case				(			self		):
					def check_hidden_states_output(UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
									A__  :	List[str]         =							model_class(UpperCamelCase__		)
									model.to(UpperCamelCase__		)
									model.eval()

									with torch.no_grad():
													A__  :	List[str]         =							model(**self._prepare_for_class(UpperCamelCase__				,       UpperCamelCase__		)		)

									A__  :	Union[str, Any]         =							outputs.hidden_states

									A__  :	Optional[int]         =							26
									self.assertEqual(len(UpperCamelCase__		)				,       UpperCamelCase__		)

					A__       ,							A__  :	Tuple         =							self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									A__  :	List[str]         =							True
									check_hidden_states_output(UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		)

									# check that output_hidden_states also work using config
									del inputs_dict["output_hidden_states"]
									A__  :	Union[str, Any]         =							True

									check_hidden_states_output(UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		)





	def   __snake_case				(			self		):
					A__  :	str         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__		)





	@slow
	def   __snake_case				(			self		):
					for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									A__  :	Optional[int]         =							MobileNetVaModel.from_pretrained(UpperCamelCase__		)
									self.assertIsNotNone(UpperCamelCase__		)




def 				SCREAMING_SNAKE_CASE     (      ) ->       Tuple:

				"""simple docstring"""







				A__  :	Union[str, Any]         =							Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
				return image







@require_torch
@require_vision
class  UpperCamelCase__		(    unittest.TestCase   ):







	'''simple docstring'''





	@cached_property
	def   __snake_case				(			self		):
					return (
					    MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v1_1.0_224'''		) if is_vision_available() else None
					)





	@slow
	def   __snake_case				(			self		):
					A__  :	Optional[int]         =							MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224'''		).to(UpperCamelCase__		)

					A__  :	Optional[int]         =							self.default_image_processor
					A__  :	Optional[int]         =							prepare_img()
					A__  :	int         =							image_processor(images=UpperCamelCase__				,       return_tensors='''pt'''		).to(UpperCamelCase__		)

					# forward pass
					with torch.no_grad():
									A__  :	List[str]         =							model(**UpperCamelCase__		)

					# verify the logits
					A__  :	Union[str, Any]         =							torch.Size((1, 1001)		)
					self.assertEqual(outputs.logits.shape				,       UpperCamelCase__		)

					A__  :	List[str]         =							torch.tensor([-4.1_7_3_9, -1.1_2_3_3, 3.1_2_0_5]		).to(UpperCamelCase__		)

					self.assertTrue(torch.allclose(outputs.logits[0, :3]				,       UpperCamelCase__				,       atol=1e-4		)		)

from numpy import exp, pi, sqrt

def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						Dict							,						__UpperCamelCase :						float = 0.0							,						__UpperCamelCase :						float = 1.0       ) ->       int:

				"""simple docstring"""







				return 1 / sqrt(2 * pi * sigma**2       ) * exp(-((x - mu) ** 2) / (2 * sigma**2)       )


if __name__ == "__main__":
					import doctest

					doctest.testmod()