



'''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 __snake_case(				_lowerCAmelCase ):


						'''simple docstring'''



						def     __snake_case   ( self				)     ->     str:
							lowerCAmelCase   =     self.config_class(**self.inputs_dict				)
							self.parent.assertTrue(hasattr(A_     ,	"""tf_padding"""				)				)
							self.parent.assertTrue(hasattr(A_     ,	"""depth_multiplier"""				)				)







class __snake_case:


						'''simple docstring'''



						def __init__( self     ,	A_     ,	A_=13     ,	A_=3     ,	A_=32     ,	A_=0.2_5     ,	A_=8     ,	A_=True     ,	A_=1024     ,	A_=32     ,	A_="relu6"     ,	A_=0.1     ,	A_=0.0_2     ,	A_=True     ,	A_=True     ,	A_=10     ,	A_=None     ,	)     ->     List[Any]:
							lowerCAmelCase   =     parent
							lowerCAmelCase   =     batch_size
							lowerCAmelCase   =     num_channels
							lowerCAmelCase   =     image_size
							lowerCAmelCase   =     depth_multiplier
							lowerCAmelCase   =     min_depth
							lowerCAmelCase   =     tf_padding
							lowerCAmelCase   =     int(last_hidden_size * depth_multiplier				)
							lowerCAmelCase   =     output_stride
							lowerCAmelCase   =     hidden_act
							lowerCAmelCase   =     classifier_dropout_prob
							lowerCAmelCase   =     use_labels
							lowerCAmelCase   =     is_training
							lowerCAmelCase   =     num_labels
							lowerCAmelCase   =     initializer_range
							lowerCAmelCase   =     scope


						def     __snake_case   ( self				)     ->     str:
							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     __snake_case   ( self				)     ->     Union[str, Any]:
							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     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     int:
							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,
							    )     ,	)


						def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Dict:
							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     __snake_case   ( self				)     ->     Tuple:
							lowerCAmelCase   =     self.prepare_config_and_inputs()
							lowerCAmelCase,       lowerCAmelCase,       lowerCAmelCase,       lowerCAmelCase   =     config_and_inputs
							lowerCAmelCase   =     {"""pixel_values""": pixel_values}
							return config, inputs_dict







@require_torch
class __snake_case(				_lowerCAmelCase		,							_lowerCAmelCase		,							unittest.TestCase ):


						'''simple docstring'''



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

						UpperCAmelCase							:   Dict									=		False
						UpperCAmelCase							:   Dict									=		False
						UpperCAmelCase							:   List[str]									=		False
						UpperCAmelCase							:   List[Any]									=		False


						def     __snake_case   ( self				)     ->     Any:
							lowerCAmelCase   =     MobileNetVaModelTester(self				)
							lowerCAmelCase   =     MobileNetVaConfigTester(self     ,	config_class=A_     ,	has_text_modality=A_				)


						def     __snake_case   ( self				)     ->     Dict:
							self.config_tester.run_common_tests()


						@unittest.skip(reason="""MobileNetV1 does not use inputs_embeds"""				)
						def     __snake_case   ( self				)     ->     int:
							pass


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


						@unittest.skip(reason="""MobileNetV1 does not output attentions"""				)
						def     __snake_case   ( self				)     ->     int:
							pass


						def     __snake_case   ( self				)     ->     Tuple:
							lowerCAmelCase,       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     __snake_case   ( self				)     ->     int:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*A_				)


						def     __snake_case   ( self				)     ->     Optional[Any]:
							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   =     26
								self.assertEqual(len(A_				)     ,	A_				)

							lowerCAmelCase,       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     __snake_case   ( self				)     ->     Any:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*A_				)




						@slow
						def     __snake_case   ( self				)     ->     Tuple:
							for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								lowerCAmelCase   =     MobileNetVaModel.from_pretrained(A_				)
								self.assertIsNotNone(A_				)







def 		_snake_case		( )      ->						str:







	"""simple docstring"""
	lowerCAmelCase   =     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     __snake_case   ( self				)     ->     str:
							return (
							    MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v1_1.0_224"""				) if is_vision_available() else None
							)




						@slow
						def     __snake_case   ( self				)     ->     str:
							lowerCAmelCase   =     MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v1_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, 1001)				)
							self.assertEqual(outputs.logits.shape     ,	A_				)

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

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




'''simple docstring'''

import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
				import torch

				from transformers import AutoModelForImageClassification

if is_vision_available():
				from transformers import AutoImageProcessor

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


						'''simple docstring'''



						@slow
						def     __snake_case   ( self				)     ->     List[str]:
							lowerCAmelCase   =     AutoImageProcessor.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip"""				)
							lowerCAmelCase   =     AutoModelForImageClassification.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip"""				)
							model.to(A_				)

							from datasets import load_dataset

							lowerCAmelCase   =     load_dataset("""nielsr/rvlcdip-demo"""				)

							lowerCAmelCase   =     dataset["""train"""][0]["""image"""].convert("""RGB"""				)

							lowerCAmelCase   =     image_processor(A_     ,	return_tensors="""pt"""				).to(A_				)

							# forward pass
							with torch.no_grad():
								lowerCAmelCase   =     model(**A_				)
								lowerCAmelCase   =     outputs.logits

							lowerCAmelCase   =     torch.Size((1, 16)				)
							self.assertEqual(logits.shape     ,	A_				)

							lowerCAmelCase   =     torch.tensor(
							    [-0.4_1_5_8, -0.4_0_9_2, -0.4_3_4_7]     ,	device=A_     ,	dtype=torch.float     ,	)
							self.assertTrue(torch.allclose(logits[0, :3]     ,	A_     ,	atol=1e-4				)				)