





'''simple docstring'''



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       (      lowerCAmelCase__						):







	'''simple docstring'''

	def 						_UpperCAmelCase     (self )							->							Optional[Any]:







				'''simple docstring'''





				__lowercase										=							self.config_class(**self.inputs_dict )
				self.parent.assertTrue(hasattr(_lowerCamelCase     ,'''tf_padding''' ) )
				self.parent.assertTrue(hasattr(_lowerCamelCase     ,'''depth_multiplier''' ) )






class 							__lowercase       :







	'''simple docstring'''

	def __init__(self     ,_lowerCamelCase     ,_lowerCamelCase=13     ,_lowerCamelCase=3     ,_lowerCamelCase=32     ,_lowerCamelCase=0.2_5     ,_lowerCamelCase=8     ,_lowerCamelCase=True     ,_lowerCamelCase=1024     ,_lowerCamelCase=32     ,_lowerCamelCase="relu6"     ,_lowerCamelCase=0.1     ,_lowerCamelCase=0.0_2     ,_lowerCamelCase=True     ,_lowerCamelCase=True     ,_lowerCamelCase=10     ,_lowerCamelCase=None     ,)							->							int:







				'''simple docstring'''





				__lowercase										=							parent
				__lowercase										=							batch_size
				__lowercase										=							num_channels
				__lowercase										=							image_size
				__lowercase										=							depth_multiplier
				__lowercase										=							min_depth
				__lowercase										=							tf_padding
				__lowercase										=							int(last_hidden_size * depth_multiplier )
				__lowercase										=							output_stride
				__lowercase										=							hidden_act
				__lowercase										=							classifier_dropout_prob
				__lowercase										=							use_labels
				__lowercase										=							is_training
				__lowercase										=							num_labels
				__lowercase										=							initializer_range
				__lowercase										=							scope


	def 						_UpperCAmelCase     (self )							->							Tuple:







				'''simple docstring'''





				__lowercase										=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

				__lowercase										=							None
				__lowercase										=							None
				if self.use_labels:
							__lowercase										=							ids_tensor([self.batch_size]     ,self.num_labels )
							__lowercase										=							ids_tensor([self.batch_size, self.image_size, self.image_size]     ,self.num_labels )

				__lowercase										=							self.get_config()

				return config, pixel_values, labels, pixel_labels


	def 						_UpperCAmelCase     (self )							->							Optional[Any]:







				'''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 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )							->							List[Any]:







				'''simple docstring'''





				__lowercase										=							MobileNetVaModel(config=_lowerCamelCase )
				model.to(_lowerCamelCase )
				model.eval()
				__lowercase										=							model(_lowerCamelCase )
				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 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )							->							List[Any]:







				'''simple docstring'''





				__lowercase										=							self.num_labels
				__lowercase										=							MobileNetVaForImageClassification(_lowerCamelCase )
				model.to(_lowerCamelCase )
				model.eval()
				__lowercase										=							model(_lowerCamelCase     ,labels=_lowerCamelCase )
				self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.num_labels) )


	def 						_UpperCAmelCase     (self )							->							Dict:







				'''simple docstring'''





				__lowercase										=							self.prepare_config_and_inputs()
				__lowercase						,		__lowercase						,		__lowercase						,		__lowercase										=							config_and_inputs
				__lowercase										=							{'''pixel_values''': pixel_values}
				return config, inputs_dict






@require_torch
class 							__lowercase       (      lowerCAmelCase__ ,      lowerCAmelCase__ ,      unittest.TestCase						):







	'''simple docstring'''

	a    :				List[str]											=       (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
	a    :				Dict											=       (
	    {"feature-extraction": MobileNetVaModel, "image-classification": MobileNetVaForImageClassification}
	    if is_torch_available()
	    else {}
	)

	a    :				Tuple											=       False
	a    :				List[Any]											=       False
	a    :				Optional[Any]											=       False
	a    :				Tuple											=       False


	def 						_UpperCAmelCase     (self )							->							int:







				'''simple docstring'''





				__lowercase										=							MobileNetVaModelTester(self )
				__lowercase										=							MobileNetVaConfigTester(self     ,config_class=_lowerCamelCase     ,has_text_modality=_lowerCamelCase )


	def 						_UpperCAmelCase     (self )							->							List[Any]:







				'''simple docstring'''





				self.config_tester.run_common_tests()


	@unittest.skip(reason='''MobileNetV1 does not use inputs_embeds''' )
	def 						_UpperCAmelCase     (self )							->							Tuple:







				'''simple docstring'''





				pass


	@unittest.skip(reason='''MobileNetV1 does not support input and output embeddings''' )
	def 						_UpperCAmelCase     (self )							->							Optional[Any]:







				'''simple docstring'''





				pass


	@unittest.skip(reason='''MobileNetV1 does not output attentions''' )
	def 						_UpperCAmelCase     (self )							->							List[str]:







				'''simple docstring'''





				pass


	def 						_UpperCAmelCase     (self )							->							Tuple:







				'''simple docstring'''





				__lowercase						,		__lowercase										=							self.model_tester.prepare_config_and_inputs_for_common()

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

							__lowercase										=							['''pixel_values''']
							self.assertListEqual(arg_names[:1]     ,_lowerCamelCase )


	def 						_UpperCAmelCase     (self )							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase										=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_lowerCamelCase )


	def 						_UpperCAmelCase     (self )							->							Optional[int]:







				'''simple docstring'''





				def check_hidden_states_output(_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase ):
							__lowercase										=							model_class(_lowerCamelCase )
							model.to(_lowerCamelCase )
							model.eval()

							with torch.no_grad():
										__lowercase										=							model(**self._prepare_for_class(_lowerCamelCase     ,_lowerCamelCase ) )

							__lowercase										=							outputs.hidden_states

							__lowercase										=							26
							self.assertEqual(len(_lowerCamelCase )     ,_lowerCamelCase )

				__lowercase						,		__lowercase										=							self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							__lowercase										=							True
							check_hidden_states_output(_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							__lowercase										=							True

							check_hidden_states_output(_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )


	def 						_UpperCAmelCase     (self )							->							List[Any]:







				'''simple docstring'''





				__lowercase										=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase )


	@slow
	def 						_UpperCAmelCase     (self )							->							Dict:







				'''simple docstring'''





				for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__lowercase										=							MobileNetVaModel.from_pretrained(_lowerCamelCase )
							self.assertIsNotNone(_lowerCamelCase )





def    _lowerCAmelCase						(				):
			__lowercase										=							Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
			return image






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







	'''simple docstring'''

	@cached_property
	def 						_UpperCAmelCase     (self )							->							Tuple:







				'''simple docstring'''





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


	@slow
	def 						_UpperCAmelCase     (self )							->							Any:







				'''simple docstring'''





				__lowercase										=							MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224''' ).to(_lowerCamelCase )

				__lowercase										=							self.default_image_processor
				__lowercase										=							prepare_img()
				__lowercase										=							image_processor(images=_lowerCamelCase     ,return_tensors='''pt''' ).to(_lowerCamelCase )

				# forward pass
				with torch.no_grad():
							__lowercase										=							model(**_lowerCamelCase )

				# verify the logits
				__lowercase										=							torch.Size((1, 1001) )
				self.assertEqual(outputs.logits.shape     ,_lowerCamelCase )

				__lowercase										=							torch.tensor([-4.1_7_3_9, -1.1_2_3_3, 3.1_2_0_5] ).to(_lowerCamelCase )

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












'''simple docstring'''



from argparse import ArgumentParser

from .env import EnvironmentCommand




def    _lowerCAmelCase						(				):
			__lowercase										=							ArgumentParser('''Diffusers CLI tool'''		,       usage='''diffusers-cli <command> [<args>]'''				)
			__lowercase										=							parser.add_subparsers(help='''diffusers-cli command helpers'''				)

			# Register commands
			EnvironmentCommand.register_subcommand(lowerCamelCase_				)

			# Let's go
			__lowercase										=							parser.parse_args()

			if not hasattr(lowerCamelCase_		,       '''func'''				):
						parser.print_help()
						exit(1				)

			# Run
			__lowercase										=							args.func(lowerCamelCase_				)
			service.run()


if __name__ == "__main__":
	main()





