




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, MobileNetVaForSemanticSegmentation, MobileNetVaModel
		from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import MobileNetVaImageProcessor




class     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				def 							__UpperCAmelCase							(      self				:     Union[str, Any]							)					->     str:
							a    =			self.config_class(**self.inputs_dict							)
							self.parent.assertTrue(hasattr(__lowerCamelCase					,      "tf_padding"							)							)
							self.parent.assertTrue(hasattr(__lowerCamelCase					,      "depth_multiplier"							)							)




class     snake_case__							:
				"""simple docstring"""



				def __init__(      self				:     List[str]					,      __lowerCamelCase				:     Union[str, Any]					,      __lowerCamelCase				:     Dict=13					,      __lowerCamelCase				:     Optional[Any]=3					,      __lowerCamelCase				:     List[Any]=32					,      __lowerCamelCase				:     str=0.25					,      __lowerCamelCase				:     Union[str, Any]=8					,      __lowerCamelCase				:     Dict=8					,      __lowerCamelCase				:     str=6					,      __lowerCamelCase				:     List[Any]=32					,      __lowerCamelCase				:     Union[str, Any]=True					,      __lowerCamelCase				:     Dict=True					,      __lowerCamelCase				:     Optional[Any]=True					,      __lowerCamelCase				:     List[Any]="relu6"					,      __lowerCamelCase				:     int=12_80					,      __lowerCamelCase				:     List[Any]=0.1					,      __lowerCamelCase				:     Optional[Any]=0.02					,      __lowerCamelCase				:     Union[str, Any]=True					,      __lowerCamelCase				:     List[Any]=True					,      __lowerCamelCase				:     Tuple=10					,      __lowerCamelCase				:     List[Any]=None					,      )					->     str:
							a    =			parent
							a    =			batch_size
							a    =			num_channels
							a    =			image_size
							a    =			depth_multiplier
							a    =			depth_divisible_by
							a    =			min_depth
							a    =			expand_ratio
							a    =			tf_padding
							a    =			output_stride
							a    =			first_layer_is_expansion
							a    =			finegrained_output
							a    =			hidden_act
							a    =			last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier							)
							a    =			classifier_dropout_prob
							a    =			use_labels
							a    =			is_training
							a    =			num_labels
							a    =			initializer_range
							a    =			scope





				def 							__UpperCAmelCase							(      self				:     str							)					->     Tuple:
							a    =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

							a    =			self.get_config()

							return config, pixel_values, labels, pixel_labels





				def 							__UpperCAmelCase							(      self				:     List[str]							)					->     Union[str, Any]:
							return MobileNetVaConfig(
							    num_channels=self.num_channels					,      image_size=self.image_size					,      depth_multiplier=self.depth_multiplier					,      depth_divisible_by=self.depth_divisible_by					,      min_depth=self.min_depth					,      expand_ratio=self.expand_ratio					,      output_stride=self.output_stride					,      first_layer_is_expansion=self.first_layer_is_expansion					,      finegrained_output=self.finegrained_output					,      hidden_act=self.hidden_act					,      tf_padding=self.tf_padding					,      classifier_dropout_prob=self.classifier_dropout_prob					,      initializer_range=self.initializer_range					,      )





				def 							__UpperCAmelCase							(      self				:     Optional[Any]					,      __lowerCamelCase				:     str					,      __lowerCamelCase				:     List[str]					,      __lowerCamelCase				:     Dict					,      __lowerCamelCase				:     Optional[int]							)					->     int:
							a    =			MobileNetVaModel(config=__lowerCamelCase							)
							model.to(__lowerCamelCase							)
							model.eval()
							a    =			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,
							    )					,      )
							self.parent.assertEqual(
							    result.pooler_output.shape					,      (self.batch_size, self.last_hidden_size)					,      )





				def 							__UpperCAmelCase							(      self				:     Optional[Any]					,      __lowerCamelCase				:     str					,      __lowerCamelCase				:     Dict					,      __lowerCamelCase				:     str					,      __lowerCamelCase				:     Dict							)					->     int:
							a    =			self.num_labels
							a    =			MobileNetVaForImageClassification(__lowerCamelCase							)
							model.to(__lowerCamelCase							)
							model.eval()
							a    =			model(__lowerCamelCase					,      labels=__lowerCamelCase							)
							self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)							)





				def 							__UpperCAmelCase							(      self				:     str					,      __lowerCamelCase				:     Optional[Any]					,      __lowerCamelCase				:     Optional[int]					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     str							)					->     int:
							a    =			self.num_labels
							a    =			MobileNetVaForSemanticSegmentation(__lowerCamelCase							)
							model.to(__lowerCamelCase							)
							model.eval()
							a    =			model(__lowerCamelCase							)
							self.parent.assertEqual(
							    result.logits.shape					,      (
							        self.batch_size,
							        self.num_labels,
							        self.image_size // self.output_stride,
							        self.image_size // self.output_stride,
							    )					,      )
							a    =			model(__lowerCamelCase					,      labels=__lowerCamelCase							)
							self.parent.assertEqual(
							    result.logits.shape					,      (
							        self.batch_size,
							        self.num_labels,
							        self.image_size // self.output_stride,
							        self.image_size // self.output_stride,
							    )					,      )





				def 							__UpperCAmelCase							(      self				:     Optional[Any]							)					->     Tuple:
							a    =			self.prepare_config_and_inputs()
							a    ,		a    ,		a    ,		a    =			config_and_inputs
							a    =			{"pixel_values": pixel_values}
							return config, inputs_dict




@require_torch
class     snake_case__							(_UpperCamelCase    ,       _UpperCamelCase    ,       unittest.TestCase   ):
				"""simple docstring"""



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

				SCREAMING_SNAKE_CASE_       :							int							 =					False
				SCREAMING_SNAKE_CASE_       :							Union[str, Any]							 =					False
				SCREAMING_SNAKE_CASE_       :							str							 =					False
				SCREAMING_SNAKE_CASE_       :							Optional[int]							 =					False





				def 							__UpperCAmelCase							(      self				:     Dict							)					->     Dict:
							a    =			MobileNetVaModelTester(self							)
							a    =			MobileNetVaConfigTester(self					,      config_class=__lowerCamelCase					,      has_text_modality=__lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     int:
							self.config_tester.run_common_tests()





				@unittest.skip(reason="MobileNetV2 does not use inputs_embeds"							)
				def 							__UpperCAmelCase							(      self				:     str							)					->     Dict:
							pass





				@unittest.skip(reason="MobileNetV2 does not support input and output embeddings"							)
				def 							__UpperCAmelCase							(      self				:     Union[str, Any]							)					->     List[str]:
							pass





				@unittest.skip(reason="MobileNetV2 does not output attentions"							)
				def 							__UpperCAmelCase							(      self				:     int							)					->     Optional[Any]:
							pass





				def 							__UpperCAmelCase							(      self				:     Tuple							)					->     int:
							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

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

										a    =			["pixel_values"]
										self.assertListEqual(arg_names[:1]					,      __lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     Dict:
							a    =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Optional[Any]							)					->     Dict:
							def check_hidden_states_output(__lowerCamelCase				:     Optional[Any]					,      __lowerCamelCase				:     str					,      __lowerCamelCase				:     Union[str, Any]							):
										a    =			model_class(__lowerCamelCase							)
										model.to(__lowerCamelCase							)
										model.eval()

										with torch.no_grad():
													a    =			model(**self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase							)							)

										a    =			outputs.hidden_states

										a    =			16
										self.assertEqual(len(__lowerCamelCase							)					,      __lowerCamelCase							)

							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										a    =			True
										check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase							)

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

										check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Any							)					->     Tuple:
							a    =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Union[str, Any]							)					->     str:
							a    =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCamelCase							)





				@slow
				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     Optional[int]:
							for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										a    =			MobileNetVaModel.from_pretrained(__lowerCamelCase							)
										self.assertIsNotNone(__lowerCamelCase							)




def 			__magic_name__				( ):






			'''simple docstring'''







			a    =			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
			return image






@require_torch
@require_vision
class     snake_case__							(unittest.TestCase   ):
				"""simple docstring"""



				@cached_property
				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     int:
							return (
							    MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v2_1.0_224"							) if is_vision_available() else None
							)





				@slow
				def 							__UpperCAmelCase							(      self				:     Optional[int]							)					->     Optional[int]:
							a    =			MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v2_1.0_224"							).to(__lowerCamelCase							)

							a    =			self.default_image_processor
							a    =			prepare_img()
							a    =			image_processor(images=__lowerCamelCase					,      return_tensors="pt"							).to(__lowerCamelCase							)

							# forward pass
							with torch.no_grad():
										a    =			model(**__lowerCamelCase							)

							# verify the logits
							a    =			torch.Size((1, 10_01)							)
							self.assertEqual(outputs.logits.shape					,      __lowerCamelCase							)

							a    =			torch.tensor([0.2_445, -1.1_993, 0.1_905]							).to(__lowerCamelCase							)

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





				@slow
				def 							__UpperCAmelCase							(      self				:     Any							)					->     Tuple:
							a    =			MobileNetVaForSemanticSegmentation.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513"							)
							a    =			model.to(__lowerCamelCase							)

							a    =			MobileNetVaImageProcessor.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513"							)

							a    =			prepare_img()
							a    =			image_processor(images=__lowerCamelCase					,      return_tensors="pt"							).to(__lowerCamelCase							)

							# forward pass
							with torch.no_grad():
										a    =			model(**__lowerCamelCase							)
							a    =			outputs.logits

							# verify the logits
							a    =			torch.Size((1, 21, 65, 65)							)
							self.assertEqual(logits.shape					,      __lowerCamelCase							)

							a    =			torch.tensor(
							    [
							        [[17.5_790, 17.7_581, 18.3_355], [18.3_257, 18.4_230, 18.8_973], [18.6_169, 18.8_650, 19.2_187]],
							        [[-2.1_595, -2.0_977, -2.3_741], [-2.4_226, -2.3_028, -2.6_835], [-2.7_819, -2.5_991, -2.7_706]],
							        [[4.2_058, 4.8_317, 4.7_638], [4.4_136, 5.0_361, 4.9_383], [4.5_028, 4.9_644, 4.8_734]],
							    ]					,      device=__lowerCamelCase					,      )

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












import warnings

from diffusers import StableDiffusionImgaImgPipeline  # noqa F401


warnings.warn(
    'The `image_to_image.py` script is outdated. Please use directly `from diffusers import'
    ' StableDiffusionImg2ImgPipeline` instead.'
)






