



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   __lowerCAmelCase  (		UpperCAmelCase_   ):
					"""simple docstring"""






					def      _a				(  self       :							Any       ):






								"""simple docstring"""


								A__	  =     self.config_class(**self.inputs_dict       )
								self.parent.assertTrue(hasattr(_snake_case					,       'tf_padding'       )       )
								self.parent.assertTrue(hasattr(_snake_case					,       'depth_multiplier'       )       )







class   __lowerCAmelCase  :
					"""simple docstring"""






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






								"""simple docstring"""


								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      _a				(  self       :							Optional[Any]       ):






								"""simple docstring"""


								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      _a				(  self       :							Optional[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       :							Tuple					,       _snake_case       :							List[Any]					,       _snake_case       :							Tuple					,       _snake_case       :							Optional[int]					,       _snake_case       :							Optional[Any]       ):






								"""simple docstring"""


								A__	  =     MobileNetVaModel(config=_snake_case       )
								model.to(_snake_case       )
								model.eval()
								A__	  =     model(_snake_case       )
								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       :							List[str]					,       _snake_case       :							Dict					,       _snake_case       :							Optional[Any]					,       _snake_case       :							Optional[Any]					,       _snake_case       :							List[str]       ):






								"""simple docstring"""


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



					def      _a				(  self       :							Optional[Any]					,       _snake_case       :							List[str]					,       _snake_case       :							Optional[int]					,       _snake_case       :							Tuple					,       _snake_case       :							Optional[Any]       ):






								"""simple docstring"""


								A__	  =     self.num_labels
								A__	  =     MobileNetVaForSemanticSegmentation(_snake_case       )
								model.to(_snake_case       )
								model.eval()
								A__	  =     model(_snake_case       )
								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(_snake_case					,       labels=_snake_case       )
								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       :							Optional[Any]       ):






								"""simple docstring"""


								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   __lowerCAmelCase  (		UpperCAmelCase_				,  UpperCAmelCase_				,  unittest.TestCase   ):
					"""simple docstring"""






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

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



					def      _a				(  self       :							List[str]       ):






								"""simple docstring"""


								A__	  =     MobileNetVaModelTester(self       )
								A__	  =     MobileNetVaConfigTester(self					,       config_class=_snake_case					,       has_text_modality=_snake_case       )



					def      _a				(  self       :							Union[str, Any]       ):






								"""simple docstring"""


								self.config_tester.run_common_tests()



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






								"""simple docstring"""


								pass



					@unittest.skip(reason='MobileNetV2 does not support input and output embeddings'       )
					def      _a				(  self       :							List[str]       ):






								"""simple docstring"""


								pass



					@unittest.skip(reason='MobileNetV2 does not output attentions'       )
					def      _a				(  self       :							Any       ):






								"""simple docstring"""


								pass



					def      _a				(  self       :							str       ):






								"""simple docstring"""


								A__  ,			A__	  =     self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
											A__	  =     model_class(_snake_case       )
											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]					,       _snake_case       )



					def      _a				(  self       :							str       ):






								"""simple docstring"""


								A__	  =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_snake_case       )



					def      _a				(  self       :							str       ):






								"""simple docstring"""


								def check_hidden_states_output(_snake_case       :							Any					,       _snake_case       :							List[str]					,       _snake_case       :							Any       ):
											A__	  =     model_class(_snake_case       )
											model.to(_snake_case       )
											model.eval()

											with torch.no_grad():
														A__	  =     model(**self._prepare_for_class(_snake_case					,       _snake_case       )       )

											A__	  =     outputs.hidden_states

											A__	  =     16
											self.assertEqual(len(_snake_case       )					,       _snake_case       )

								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(_snake_case					,       _snake_case					,       _snake_case       )

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

											check_hidden_states_output(_snake_case					,       _snake_case					,       _snake_case       )



					def      _a				(  self       :							Tuple       ):






								"""simple docstring"""


								A__	  =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_snake_case       )



					def      _a				(  self       :							List[str]       ):






								"""simple docstring"""


								A__	  =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*_snake_case       )



					@slow
					def      _a				(  self       :							Dict       ):






								"""simple docstring"""


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







def A    (							)    ->   Tuple:
			A__	  =     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
			return image






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






					@cached_property
					def      _a				(  self       :							Optional[int]       ):






								"""simple docstring"""


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



					@slow
					def      _a				(  self       :							Any       ):






								"""simple docstring"""


								A__	  =     MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224'       ).to(_snake_case       )

								A__	  =     self.default_image_processor
								A__	  =     prepare_img()
								A__	  =     image_processor(images=_snake_case					,       return_tensors='pt'       ).to(_snake_case       )

								# forward pass
								with torch.no_grad():
											A__	  =     model(**_snake_case       )

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

								A__	  =     torch.tensor([0.2445, -1.1993, 0.1905]       ).to(_snake_case       )

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



					@slow
					def      _a				(  self       :							List[Any]       ):






								"""simple docstring"""


								A__	  =     MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'       )
								A__	  =     model.to(_snake_case       )

								A__	  =     MobileNetVaImageProcessor.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'       )

								A__	  =     prepare_img()
								A__	  =     image_processor(images=_snake_case					,       return_tensors='pt'       ).to(_snake_case       )

								# forward pass
								with torch.no_grad():
											A__	  =     model(**_snake_case       )
								A__	  =     outputs.logits

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

								A__	  =     torch.tensor(
								    [
								        [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
								        [[-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=_snake_case					,       )

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









from __future__ import annotations


def A    (							__UpperCamelCase = 4       )    ->   list[list[int]]:
			A__	  =     abs(__UpperCamelCase       ) or 4
			return [[1 + x + y * row_size for x in range(__UpperCamelCase       )] for y in range(__UpperCamelCase       )]


def A    (							__UpperCamelCase       )    ->   list[list[int]]:
			return reverse_row(transpose(__UpperCamelCase       )       )
			# OR.. transpose(reverse_column(matrix))


def A    (							__UpperCamelCase       )    ->   list[list[int]]:
			return reverse_row(reverse_column(__UpperCamelCase       )       )
			# OR.. reverse_column(reverse_row(matrix))


def A    (							__UpperCamelCase       )    ->   list[list[int]]:
			return reverse_column(transpose(__UpperCamelCase       )       )
			# OR.. transpose(reverse_row(matrix))


def A    (							__UpperCamelCase       )    ->   list[list[int]]:
			A__	  =     [list(__UpperCamelCase       ) for x in zip(*__UpperCamelCase       )]
			return matrix


def A    (							__UpperCamelCase       )    ->   list[list[int]]:
			A__	  =     matrix[::-1]
			return matrix


def A    (							__UpperCamelCase       )    ->   list[list[int]]:
			A__	  =     [x[::-1] for x in matrix]
			return matrix


def A    (							__UpperCamelCase       )    ->   None:
			for i in matrix:
						print(*__UpperCamelCase       )


if __name__ == "__main__":
							SCREAMING_SNAKE_CASE__   			=   make_matrix()
							print('''\norigin:\n''')
							print_matrix(matrix)
							print('''\nrotate 90 counterclockwise:\n''')
							print_matrix(rotate_aa(matrix))

							SCREAMING_SNAKE_CASE__   			=   make_matrix()
							print('''\norigin:\n''')
							print_matrix(matrix)
							print('''\nrotate 180:\n''')
							print_matrix(rotate_aaa(matrix))

							SCREAMING_SNAKE_CASE__   			=   make_matrix()
							print('''\norigin:\n''')
							print_matrix(matrix)
							print('''\nrotate 270 counterclockwise:\n''')
							print_matrix(rotate_aaa(matrix))




