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       lowercase__		(     UpperCamelCase_):


						def  __A ( self			:       List[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					int           =							self.config_class(**self.inputs_dict		)
												self.parent.assertTrue(hasattr(__UpperCamelCase					,	'''tf_padding'''		)		)
												self.parent.assertTrue(hasattr(__UpperCamelCase					,	'''depth_multiplier'''		)		)






class       lowercase__		:


						def __init__( self			:       str					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       int=13					,	UpperCamelCase__			:       Dict=3					,	UpperCamelCase__			:       int=32					,	UpperCamelCase__			:       Any=0.25					,	UpperCamelCase__			:       Tuple=8					,	UpperCamelCase__			:       int=True					,	UpperCamelCase__			:       str=1024					,	UpperCamelCase__			:       List[Any]=32					,	UpperCamelCase__			:       Union[str, Any]="relu6"					,	UpperCamelCase__			:       Optional[Any]=0.1					,	UpperCamelCase__			:       int=0.02					,	UpperCamelCase__			:       List[Any]=True					,	UpperCamelCase__			:       Any=True					,	UpperCamelCase__			:       List[Any]=10					,	UpperCamelCase__			:       List[Any]=None					,	):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Any           =							parent
												SCREAMING_SNAKE_CASE							:					str           =							batch_size
												SCREAMING_SNAKE_CASE							:					List[str]           =							num_channels
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							image_size
												SCREAMING_SNAKE_CASE							:					Tuple           =							depth_multiplier
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							min_depth
												SCREAMING_SNAKE_CASE							:					Any           =							tf_padding
												SCREAMING_SNAKE_CASE							:					List[str]           =							int(last_hidden_size * depth_multiplier		)
												SCREAMING_SNAKE_CASE							:					List[Any]           =							output_stride
												SCREAMING_SNAKE_CASE							:					List[Any]           =							hidden_act
												SCREAMING_SNAKE_CASE							:					str           =							classifier_dropout_prob
												SCREAMING_SNAKE_CASE							:					str           =							use_labels
												SCREAMING_SNAKE_CASE							:					List[str]           =							is_training
												SCREAMING_SNAKE_CASE							:					Dict           =							num_labels
												SCREAMING_SNAKE_CASE							:					List[str]           =							initializer_range
												SCREAMING_SNAKE_CASE							:					Dict           =							scope


						def  __A ( self			:       Tuple		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							self.get_config()

												return config, pixel_values, labels, pixel_labels


						def  __A ( self			:       int		):





												'''simple docstring'''





												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  __A ( self			:       int					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       int		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							MobileNetVaModel(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												SCREAMING_SNAKE_CASE							:					List[Any]           =							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  __A ( self			:       List[str]					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       int		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							self.num_labels
												SCREAMING_SNAKE_CASE							:					str           =							MobileNetVaForImageClassification(__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												SCREAMING_SNAKE_CASE							:					Any           =							model(__UpperCamelCase					,	labels=__UpperCamelCase		)
												self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.num_labels)		)



						def  __A ( self			:       List[str]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							self.prepare_config_and_inputs()
												SCREAMING_SNAKE_CASE       ,						SCREAMING_SNAKE_CASE       ,						SCREAMING_SNAKE_CASE       ,						SCREAMING_SNAKE_CASE							:					Dict           =							config_and_inputs
												SCREAMING_SNAKE_CASE							:					Tuple           =							{'''pixel_values''': pixel_values}
												return config, inputs_dict






@require_torch
class       lowercase__		(     UpperCamelCase_			,							UpperCamelCase_			,							unittest.TestCase):
						UpperCamelCase_  =	(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
						UpperCamelCase_  =	(
						    {"""feature-extraction""": MobileNetVaModel, """image-classification""": MobileNetVaForImageClassification}
						    if is_torch_available()
						    else {}
						)

						UpperCamelCase_  =	False
						UpperCamelCase_  =	False
						UpperCamelCase_  =	False
						UpperCamelCase_  =	False


						def  __A ( self			:       Dict		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							MobileNetVaModelTester(self		)
												SCREAMING_SNAKE_CASE							:					Any           =							MobileNetVaConfigTester(self					,	config_class=__UpperCamelCase					,	has_text_modality=__UpperCamelCase		)


						def  __A ( self			:       Optional[int]		):





												'''simple docstring'''





												self.config_tester.run_common_tests()


						@unittest.skip(reason='''MobileNetV1 does not use inputs_embeds'''		)
						def  __A ( self			:       Optional[Any]		):





												'''simple docstring'''





												pass


						@unittest.skip(reason='''MobileNetV1 does not support input and output embeddings'''		)
						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												pass


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





												'''simple docstring'''





												pass


						def  __A ( self			:       Any		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE       ,						SCREAMING_SNAKE_CASE							:					str           =							self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							model_class(__UpperCamelCase		)
																		SCREAMING_SNAKE_CASE							:					Tuple           =							inspect.signature(model.forward		)
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		SCREAMING_SNAKE_CASE							:					Dict           =							[*signature.parameters.keys()]

																		SCREAMING_SNAKE_CASE							:					str           =							['''pixel_values''']
																		self.assertListEqual(arg_names[:1]					,	__UpperCamelCase		)


						def  __A ( self			:       Optional[int]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Tuple           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__UpperCamelCase		)


						def  __A ( self			:       List[Any]		):





												'''simple docstring'''





												def check_hidden_states_output(UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       int		):
																		SCREAMING_SNAKE_CASE							:					Optional[Any]           =							model_class(__UpperCamelCase		)
																		model.to(__UpperCamelCase		)
																		model.eval()

																		with torch.no_grad():
																								SCREAMING_SNAKE_CASE							:					Optional[int]           =							model(**self._prepare_for_class(__UpperCamelCase					,	__UpperCamelCase		)		)

																		SCREAMING_SNAKE_CASE							:					str           =							outputs.hidden_states

																		SCREAMING_SNAKE_CASE							:					Any           =							26
																		self.assertEqual(len(__UpperCamelCase		)					,	__UpperCamelCase		)

												SCREAMING_SNAKE_CASE       ,						SCREAMING_SNAKE_CASE							:					Optional[Any]           =							self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		SCREAMING_SNAKE_CASE							:					str           =							True
																		check_hidden_states_output(__UpperCamelCase					,	__UpperCamelCase					,	__UpperCamelCase		)

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		SCREAMING_SNAKE_CASE							:					Optional[Any]           =							True

																		check_hidden_states_output(__UpperCamelCase					,	__UpperCamelCase					,	__UpperCamelCase		)


						def  __A ( self			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase		)



						@slow
						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							MobileNetVaModel.from_pretrained(__UpperCamelCase		)
																		self.assertIsNotNone(__UpperCamelCase		)






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





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


						@cached_property
						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												return (
												    MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v1_1.0_224'''		) if is_vision_available() else None
												)



						@slow
						def  __A ( self			:       Optional[int]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					int           =							MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224'''		).to(__UpperCamelCase		)

												SCREAMING_SNAKE_CASE							:					int           =							self.default_image_processor
												SCREAMING_SNAKE_CASE							:					Tuple           =							prepare_img()
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							image_processor(images=__UpperCamelCase					,	return_tensors='''pt'''		).to(__UpperCamelCase		)

												# forward pass
												with torch.no_grad():
																		SCREAMING_SNAKE_CASE							:					str           =							model(**__UpperCamelCase		)

												# verify the logits
												SCREAMING_SNAKE_CASE							:					List[Any]           =							torch.Size((1, 1001)		)
												self.assertEqual(outputs.logits.shape					,	__UpperCamelCase		)

												SCREAMING_SNAKE_CASE							:					Dict           =							torch.tensor([-4.1739, -1.1233, 3.1205]		).to(__UpperCamelCase		)

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






import os

try:
					from .build_directory_md import good_file_paths
except ImportError:
					from build_directory_md import good_file_paths  # type: ignore

__UpperCamelCase	: Dict    		=  list(good_file_paths())
assert filepaths, "good_file_paths() failed!"

__UpperCamelCase	: Tuple    		=  [file for file in filepaths if file != file.lower()]
if upper_files:
					print(f"""{len(upper_files)} files contain uppercase characters:""")
					print('\n'.join(upper_files) + '\n')

__UpperCamelCase	: List[Any]    		=  [file for file in filepaths if ' ' in file]
if space_files:
					print(f"""{len(space_files)} files contain space characters:""")
					print('\n'.join(space_files) + '\n')

__UpperCamelCase	: List[Any]    		=  [file for file in filepaths if '-' in file]
if hyphen_files:
					print(f"""{len(hyphen_files)} files contain hyphen characters:""")
					print('\n'.join(hyphen_files) + '\n')

__UpperCamelCase	: List[Any]    		=  [file for file in filepaths if os.sep not in file]
if nodir_files:
					print(f"""{len(nodir_files)} files are not in a directory:""")
					print('\n'.join(nodir_files) + '\n')

__UpperCamelCase	: Optional[Any]    		=  len(upper_files + space_files + hyphen_files + nodir_files)
if bad_files:
					import sys

					sys.exit(bad_files)





