


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     __lowercase    (				__UpperCamelCase   ):



		"""simple docstring"""

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







					'''simple docstring'''







					lowerCamelCase  						=   self.config_class(**self.inputs_dict     )
					self.parent.assertTrue(hasattr(A						,	"""tf_padding"""     )     )
					self.parent.assertTrue(hasattr(A						,	"""depth_multiplier"""     )     )







class     __lowercase    :



		"""simple docstring"""

		def __init__(      self						,	A						,	A=13						,	A=3						,	A=32						,	A=0.25						,	A=8						,	A=8						,	A=6						,	A=32						,	A=True						,	A=True						,	A=True						,	A="relu6"						,	A=12_80						,	A=0.1						,	A=0.02						,	A=True						,	A=True						,	A=10						,	A=None						,	)   ->      Optional[Any]:







					'''simple docstring'''







					lowerCamelCase  						=   parent
					lowerCamelCase  						=   batch_size
					lowerCamelCase  						=   num_channels
					lowerCamelCase  						=   image_size
					lowerCamelCase  						=   depth_multiplier
					lowerCamelCase  						=   depth_divisible_by
					lowerCamelCase  						=   min_depth
					lowerCamelCase  						=   expand_ratio
					lowerCamelCase  						=   tf_padding
					lowerCamelCase  						=   output_stride
					lowerCamelCase  						=   first_layer_is_expansion
					lowerCamelCase  						=   finegrained_output
					lowerCamelCase  						=   hidden_act
					lowerCamelCase  						=   last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier     )
					lowerCamelCase  						=   classifier_dropout_prob
					lowerCamelCase  						=   use_labels
					lowerCamelCase  						=   is_training
					lowerCamelCase  						=   num_labels
					lowerCamelCase  						=   initializer_range
					lowerCamelCase  						=   scope







		def  __A					(      self     )   ->      Dict:







					'''simple docstring'''







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

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

					lowerCamelCase  						=   self.get_config()

					return config, pixel_values, labels, pixel_labels







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







					'''simple docstring'''







					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  __A					(      self						,	A						,	A						,	A						,	A     )   ->      Any:







					'''simple docstring'''







					lowerCamelCase  						=   MobileNetVaModel(config=A     )
					model.to(A     )
					model.eval()
					lowerCamelCase  						=   model(A     )
					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  __A					(      self						,	A						,	A						,	A						,	A     )   ->      Union[str, Any]:







					'''simple docstring'''







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







		def  __A					(      self						,	A						,	A						,	A						,	A     )   ->      Tuple:







					'''simple docstring'''







					lowerCamelCase  						=   self.num_labels
					lowerCamelCase  						=   MobileNetVaForSemanticSegmentation(A     )
					model.to(A     )
					model.eval()
					lowerCamelCase  						=   model(A     )
					self.parent.assertEqual(
					    result.logits.shape						,	(
					        self.batch_size,
					        self.num_labels,
					        self.image_size // self.output_stride,
					        self.image_size // self.output_stride,
					    )						,	)
					lowerCamelCase  						=   model(A						,	labels=A     )
					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  __A					(      self     )   ->      Union[str, Any]:







					'''simple docstring'''







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







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



		"""simple docstring"""

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

		UpperCamelCase			: List[Any]           =						False
		UpperCamelCase			: List[Any]           =						False
		UpperCamelCase			: List[str]           =						False
		UpperCamelCase			: Optional[Any]           =						False







		def  __A					(      self     )   ->      int:







					'''simple docstring'''







					lowerCamelCase  						=   MobileNetVaModelTester(self     )
					lowerCamelCase  						=   MobileNetVaConfigTester(self						,	config_class=A						,	has_text_modality=A     )







		def  __A					(      self     )   ->      List[str]:







					'''simple docstring'''







					self.config_tester.run_common_tests()







		@unittest.skip(reason="""MobileNetV2 does not use inputs_embeds"""     )
		def  __A					(      self     )   ->      List[Any]:







					'''simple docstring'''







					pass







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







					'''simple docstring'''







					pass







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







					'''simple docstring'''







					pass







		def  __A					(      self     )   ->      Tuple:







					'''simple docstring'''







					lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs_for_common()

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

								lowerCamelCase  						=   ['pixel_values']
								self.assertListEqual(arg_names[:1]						,	A     )







		def  __A					(      self     )   ->      Tuple:







					'''simple docstring'''







					lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*A     )







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







					'''simple docstring'''







					def check_hidden_states_output(A						,	A						,	A     ):
								lowerCamelCase  						=   model_class(A     )
								model.to(A     )
								model.eval()

								with torch.no_grad():
											lowerCamelCase  						=   model(**self._prepare_for_class(A						,	A     )     )

								lowerCamelCase  						=   outputs.hidden_states

								lowerCamelCase  						=   16
								self.assertEqual(len(A     )						,	A     )

					lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
								lowerCamelCase  						=   True
								check_hidden_states_output(A						,	A						,	A     )

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

								check_hidden_states_output(A						,	A						,	A     )







		def  __A					(      self     )   ->      str:







					'''simple docstring'''







					lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*A     )







		def  __A					(      self     )   ->      int:







					'''simple docstring'''







					lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_semantic_segmentation(*A     )







		@slow
		def  __A					(      self     )   ->      Tuple:







					'''simple docstring'''







					for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								lowerCamelCase  						=   MobileNetVaModel.from_pretrained(A     )
								self.assertIsNotNone(A     )






def      __lowerCamelCase		(				):





			'''simple docstring'''




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







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



		"""simple docstring"""

		@cached_property
		def  __A					(      self     )   ->      List[str]:







					'''simple docstring'''







					return (
					    MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v2_1.0_224"""     ) if is_vision_available() else None
					)







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







					'''simple docstring'''







					lowerCamelCase  						=   MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224"""     ).to(A     )

					lowerCamelCase  						=   self.default_image_processor
					lowerCamelCase  						=   prepare_img()
					lowerCamelCase  						=   image_processor(images=A						,	return_tensors="""pt"""     ).to(A     )

					# forward pass
					with torch.no_grad():
								lowerCamelCase  						=   model(**A     )

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

					lowerCamelCase  						=   torch.tensor([0.2445, -1.1993, 0.1905]     ).to(A     )

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







		@slow
		def  __A					(      self     )   ->      Optional[Any]:







					'''simple docstring'''







					lowerCamelCase  						=   MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""     )
					lowerCamelCase  						=   model.to(A     )

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

					lowerCamelCase  						=   prepare_img()
					lowerCamelCase  						=   image_processor(images=A						,	return_tensors="""pt"""     ).to(A     )

					# forward pass
					with torch.no_grad():
								lowerCamelCase  						=   model(**A     )
					lowerCamelCase  						=   outputs.logits

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

					lowerCamelCase  						=   torch.tensor(
					    [
					        [[17.57_90, 17.75_81, 18.33_55], [18.32_57, 18.42_30, 18.89_73], [18.61_69, 18.86_50, 19.21_87]],
					        [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
					        [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
					    ]						,	device=A						,	)

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








def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase = 100_0000 )			->     int:
    lowerCamelCase__    :      int					 =		limit + 1
    lowerCamelCase__    :      Optional[Any]					 =		[0] * limit
    for first_term in range(1						,	_UpperCAmelCase ):
        for n in range(_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase ):
            lowerCamelCase__    :      Optional[Any]					 =		first_term + n / first_term
            if common_difference % 4:  # d must be divisble by 4
                continue
            else:
                common_difference /= 4
                if (
                    first_term > common_difference
                    and first_term < 4 * common_difference
                ):  # since x,y,z are positive integers
                    frequency[n] += 1  # so z>0 and a>d ,also 4d<a

    lowerCamelCase__    :      List[str]					 =		sum(1 for x in frequency[1:limit] if x == 10 )

    return count


if __name__ == "__main__":
    print(F"""{solution() = }""")





