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

						def  A_				(			self				)					->      int:


								'''simple docstring'''

								_UpperCamelCase						    =	self.config_class(**self.inputs_dict				)
								self.parent.assertTrue(hasattr(a       ,							"""tf_padding"""				)				)
								self.parent.assertTrue(hasattr(a       ,							"""depth_multiplier"""				)				)







class        lowerCAmelCase__      :

						def __init__(			self       ,							a       ,							a=13       ,							a=3       ,							a=32       ,							a=0.25       ,							a=8       ,							a=True       ,							a=10_24       ,							a=32       ,							a="relu6"       ,							a=0.1       ,							a=0.02       ,							a=True       ,							a=True       ,							a=10       ,							a=None       ,							)					->      Dict:


								'''simple docstring'''

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

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


								'''simple docstring'''

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

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

								_UpperCamelCase						    =	self.get_config()

								return config, pixel_values, labels, pixel_labels

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


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


								'''simple docstring'''

								_UpperCamelCase						    =	MobileNetVaModel(config=a				)
								model.to(a				)
								model.eval()
								_UpperCamelCase						    =	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  A_				(			self       ,							a       ,							a       ,							a       ,							a				)					->      Tuple:


								'''simple docstring'''

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







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


								'''simple docstring'''

								_UpperCamelCase						    =	self.prepare_config_and_inputs()
								_UpperCamelCase    ,	_UpperCamelCase    ,	_UpperCamelCase    ,	_UpperCamelCase						    =	config_and_inputs
								_UpperCamelCase						    =	{"""pixel_values""": pixel_values}
								return config, inputs_dict







@require_torch
class        lowerCAmelCase__      (   __lowercase		,	__lowercase		,	unittest.TestCase	):
						UpperCamelCase_							: Tuple	=		(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
						UpperCamelCase_							: Tuple	=		(
						    {"feature-extraction": MobileNetVaModel, "image-classification": MobileNetVaForImageClassification}
						    if is_torch_available()
						    else {}
						)

						UpperCamelCase_							: Union[str, Any]	=		False
						UpperCamelCase_							: Optional[int]	=		False
						UpperCamelCase_							: Dict	=		False
						UpperCamelCase_							: Optional[Any]	=		False

						def  A_				(			self				)					->      Any:


								'''simple docstring'''

								_UpperCamelCase						    =	MobileNetVaModelTester(self				)
								_UpperCamelCase						    =	MobileNetVaConfigTester(self       ,							config_class=a       ,							has_text_modality=a				)

						def  A_				(			self				)					->      int:


								'''simple docstring'''

								self.config_tester.run_common_tests()

						@unittest.skip(reason="""MobileNetV1 does not use inputs_embeds"""				)
						def  A_				(			self				)					->      Any:


								'''simple docstring'''

								pass

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


								'''simple docstring'''

								pass

						@unittest.skip(reason="""MobileNetV1 does not output attentions"""				)
						def  A_				(			self				)					->      Union[str, Any]:


								'''simple docstring'''

								pass

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


								'''simple docstring'''

								_UpperCamelCase    ,	_UpperCamelCase						    =	self.model_tester.prepare_config_and_inputs_for_common()

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

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

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


								'''simple docstring'''

								_UpperCamelCase						    =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*a				)

						def  A_				(			self				)					->      int:


								'''simple docstring'''

								def check_hidden_states_output(a       ,							a       ,							a				):
										_UpperCamelCase						    =	model_class(a				)
										model.to(a				)
										model.eval()

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

										_UpperCamelCase						    =	outputs.hidden_states

										_UpperCamelCase						    =	26
										self.assertEqual(len(a				)       ,							a				)

								_UpperCamelCase    ,	_UpperCamelCase						    =	self.model_tester.prepare_config_and_inputs_for_common()

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

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

										check_hidden_states_output(a       ,							a       ,							a				)

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


								'''simple docstring'''

								_UpperCamelCase						    =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*a				)







						@slow
						def  A_				(			self				)					->      Optional[int]:


								'''simple docstring'''

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




def 							__A()		->				List[Any]:

		"""simple docstring"""






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







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

						@cached_property
						def  A_				(			self				)					->      List[Any]:


								'''simple docstring'''

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







						@slow
						def  A_				(			self				)					->      Optional[int]:


								'''simple docstring'''

								_UpperCamelCase						    =	MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v1_1.0_224"""				).to(a				)

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

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

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

								_UpperCamelCase						    =	torch.tensor([-4.1739, -1.1233, 3.1205]				).to(a				)

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





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


lowerCamelCase__	=							{"configuration_yolos": ["YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP", "YolosConfig", "YolosOnnxConfig"]}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowerCamelCase__	=							["YolosFeatureExtractor"]
		lowerCamelCase__	=							["YolosImageProcessor"]

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowerCamelCase__	=							[
		    "YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST",
		    "YolosForObjectDetection",
		    "YolosModel",
		    "YolosPreTrainedModel",
		]


if TYPE_CHECKING:
		from .configuration_yolos import YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP, YolosConfig, YolosOnnxConfig

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_yolos import YolosFeatureExtractor
				from .image_processing_yolos import YolosImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_yolos import (
				    YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST,
				    YolosForObjectDetection,
				    YolosModel,
				    YolosPreTrainedModel,
				)


else:
		import sys

		lowerCamelCase__	=							_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



