
'''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, 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__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""

		def       lowerCAmelCase				(      self				:			Tuple      )						->			Dict:






				"""simple docstring"""







				snake_case  :       Dict     			=						self.config_class(**self.inputs_dict      )
				self.parent.assertTrue(hasattr(UpperCamelCase__  ,	'''tf_padding'''      )      )
				self.parent.assertTrue(hasattr(UpperCamelCase__  ,	'''depth_multiplier'''      )      )







class 	snake_case__       :







		"""simple docstring"""

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






				"""simple docstring"""







				snake_case  :       Union[str, Any]     			=						parent
				snake_case  :       List[str]     			=						batch_size
				snake_case  :       Tuple     			=						num_channels
				snake_case  :       Tuple     			=						image_size
				snake_case  :       Optional[int]     			=						depth_multiplier
				snake_case  :       Optional[int]     			=						depth_divisible_by
				snake_case  :       Any     			=						min_depth
				snake_case  :       Dict     			=						expand_ratio
				snake_case  :       Any     			=						tf_padding
				snake_case  :       int     			=						output_stride
				snake_case  :       Dict     			=						first_layer_is_expansion
				snake_case  :       Tuple     			=						finegrained_output
				snake_case  :       Union[str, Any]     			=						hidden_act
				snake_case  :       Optional[int]     			=						last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier      )
				snake_case  :       int     			=						classifier_dropout_prob
				snake_case  :       int     			=						use_labels
				snake_case  :       Optional[int]     			=						is_training
				snake_case  :       str     			=						num_labels
				snake_case  :       Optional[int]     			=						initializer_range
				snake_case  :       Optional[int]     			=						scope



		def       lowerCAmelCase				(      self				:			List[str]      )						->			Tuple:






				"""simple docstring"""







				snake_case  :       List[Any]     			=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

				snake_case  :       str     			=						None
				snake_case  :       int     			=						None
				if self.use_labels:
						snake_case  :       str     			=						ids_tensor([self.batch_size]  ,	self.num_labels      )
						snake_case  :       List[str]     			=						ids_tensor([self.batch_size, self.image_size, self.image_size]  ,	self.num_labels      )

				snake_case  :       Union[str, Any]     			=						self.get_config()

				return config, pixel_values, labels, pixel_labels



		def       lowerCAmelCase				(      self				:			Optional[int]      )						->			Dict:






				"""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       lowerCAmelCase				(      self				:			int  ,	UpperCamelCase__				:			Optional[Any]  ,	UpperCamelCase__				:			Dict  ,	UpperCamelCase__				:			List[Any]  ,	UpperCamelCase__				:			Dict      )						->			Tuple:






				"""simple docstring"""







				snake_case  :       Any     			=						MobileNetVaModel(config=UpperCamelCase__      )
				model.to(UpperCamelCase__      )
				model.eval()
				snake_case  :       Dict     			=						model(UpperCamelCase__      )
				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       lowerCAmelCase				(      self				:			Optional[int]  ,	UpperCamelCase__				:			Optional[Any]  ,	UpperCamelCase__				:			List[Any]  ,	UpperCamelCase__				:			Optional[Any]  ,	UpperCamelCase__				:			Tuple      )						->			Optional[Any]:






				"""simple docstring"""







				snake_case  :       Optional[Any]     			=						self.num_labels
				snake_case  :       Optional[int]     			=						MobileNetVaForImageClassification(UpperCamelCase__      )
				model.to(UpperCamelCase__      )
				model.eval()
				snake_case  :       Union[str, Any]     			=						model(UpperCamelCase__  ,	labels=UpperCamelCase__      )
				self.parent.assertEqual(result.logits.shape  ,	(self.batch_size, self.num_labels)      )



		def       lowerCAmelCase				(      self				:			List[str]  ,	UpperCamelCase__				:			int  ,	UpperCamelCase__				:			Optional[Any]  ,	UpperCamelCase__				:			List[str]  ,	UpperCamelCase__				:			List[str]      )						->			List[Any]:






				"""simple docstring"""







				snake_case  :       Optional[int]     			=						self.num_labels
				snake_case  :       List[Any]     			=						MobileNetVaForSemanticSegmentation(UpperCamelCase__      )
				model.to(UpperCamelCase__      )
				model.eval()
				snake_case  :       List[Any]     			=						model(UpperCamelCase__      )
				self.parent.assertEqual(
				    result.logits.shape  ,	(
				        self.batch_size,
				        self.num_labels,
				        self.image_size // self.output_stride,
				        self.image_size // self.output_stride,
				    )  ,	)
				snake_case  :       int     			=						model(UpperCamelCase__  ,	labels=UpperCamelCase__      )
				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       lowerCAmelCase				(      self				:			Dict      )						->			str:






				"""simple docstring"""







				snake_case  :       Dict     			=						self.prepare_config_and_inputs()
				snake_case			,snake_case			,snake_case			,snake_case  :       List[str]     			=						config_and_inputs
				snake_case  :       Optional[Any]     			=						{'''pixel_values''': pixel_values}
				return config, inputs_dict







@require_torch
class 	snake_case__       (      __SCREAMING_SNAKE_CASE    ,					__SCREAMING_SNAKE_CASE    ,					unittest.TestCase					):







		"""simple docstring"""

		lowerCamelCase     							=						(
		    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
		    if is_torch_available()
		    else ()
		)
		lowerCamelCase     							=						(
		    {
		        """feature-extraction""": MobileNetVaModel,
		        """image-classification""": MobileNetVaForImageClassification,
		        """image-segmentation""": MobileNetVaForSemanticSegmentation,
		    }
		    if is_torch_available()
		    else {}
		)

		lowerCamelCase     							=						False
		lowerCamelCase     							=						False
		lowerCamelCase     							=						False
		lowerCamelCase     							=						False



		def       lowerCAmelCase				(      self				:			Tuple      )						->			Tuple:






				"""simple docstring"""







				snake_case  :       List[Any]     			=						MobileNetVaModelTester(self      )
				snake_case  :       List[str]     			=						MobileNetVaConfigTester(self  ,	config_class=UpperCamelCase__  ,	has_text_modality=UpperCamelCase__      )



		def       lowerCAmelCase				(      self				:			Dict      )						->			List[str]:






				"""simple docstring"""







				self.config_tester.run_common_tests()



		@unittest.skip(reason='''MobileNetV2 does not use inputs_embeds'''      )
		def       lowerCAmelCase				(      self				:			Any      )						->			Tuple:






				"""simple docstring"""







				pass



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






				"""simple docstring"""







				pass



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






				"""simple docstring"""







				pass



		def       lowerCAmelCase				(      self				:			int      )						->			str:






				"""simple docstring"""







				snake_case			,snake_case  :       Any     			=						self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
						snake_case  :       List[Any]     			=						model_class(UpperCamelCase__      )
						snake_case  :       Dict     			=						inspect.signature(model.forward      )
						# signature.parameters is an OrderedDict => so arg_names order is deterministic
						snake_case  :       Optional[int]     			=						[*signature.parameters.keys()]

						snake_case  :       List[str]     			=						['''pixel_values''']
						self.assertListEqual(arg_names[:1]  ,	UpperCamelCase__      )



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






				"""simple docstring"""







				snake_case  :       List[str]     			=						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*UpperCamelCase__      )



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






				"""simple docstring"""







				def check_hidden_states_output(UpperCamelCase__				:			Any  ,	UpperCamelCase__				:			Optional[int]  ,	UpperCamelCase__				:			Dict      ):
						snake_case  :       Union[str, Any]     			=						model_class(UpperCamelCase__      )
						model.to(UpperCamelCase__      )
						model.eval()

						with torch.no_grad():
								snake_case  :       List[str]     			=						model(**self._prepare_for_class(UpperCamelCase__  ,	UpperCamelCase__      )      )

						snake_case  :       str     			=						outputs.hidden_states

						snake_case  :       Optional[int]     			=						16
						self.assertEqual(len(UpperCamelCase__      )  ,	UpperCamelCase__      )

				snake_case			,snake_case  :       int     			=						self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
						snake_case  :       int     			=						True
						check_hidden_states_output(UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__      )

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						snake_case  :       List[Any]     			=						True

						check_hidden_states_output(UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__      )



		def       lowerCAmelCase				(      self				:			str      )						->			Any:






				"""simple docstring"""







				snake_case  :       Union[str, Any]     			=						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__      )



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






				"""simple docstring"""







				snake_case  :       Optional[Any]     			=						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_semantic_segmentation(*UpperCamelCase__      )



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






				"""simple docstring"""







				for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						snake_case  :       str     			=						MobileNetVaModel.from_pretrained(UpperCamelCase__      )
						self.assertIsNotNone(UpperCamelCase__      )







def    _UpperCamelCase						(       )  ->							Tuple:


		'''simple docstring'''


		snake_case  :       Dict     			=						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       lowerCAmelCase				(      self				:			str      )						->			Optional[int]:






				"""simple docstring"""







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



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






				"""simple docstring"""







				snake_case  :       Tuple     			=						MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v2_1.0_224'''      ).to(UpperCamelCase__      )

				snake_case  :       str     			=						self.default_image_processor
				snake_case  :       List[str]     			=						prepare_img()
				snake_case  :       Optional[Any]     			=						image_processor(images=UpperCamelCase__  ,	return_tensors='''pt'''      ).to(UpperCamelCase__      )

				# forward pass
				with torch.no_grad():
						snake_case  :       Tuple     			=						model(**UpperCamelCase__      )

				# verify the logits
				snake_case  :       Optional[Any]     			=						torch.Size((1, 1001)      )
				self.assertEqual(outputs.logits.shape  ,	UpperCamelCase__      )

				snake_case  :       Any     			=						torch.tensor([0.2_445, -1.1_993, 0.1_905]      ).to(UpperCamelCase__      )

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



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






				"""simple docstring"""







				snake_case  :       Union[str, Any]     			=						MobileNetVaForSemanticSegmentation.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''      )
				snake_case  :       Any     			=						model.to(UpperCamelCase__      )

				snake_case  :       Tuple     			=						MobileNetVaImageProcessor.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''      )

				snake_case  :       Dict     			=						prepare_img()
				snake_case  :       List[Any]     			=						image_processor(images=UpperCamelCase__  ,	return_tensors='''pt'''      ).to(UpperCamelCase__      )

				# forward pass
				with torch.no_grad():
						snake_case  :       List[Any]     			=						model(**UpperCamelCase__      )
				snake_case  :       Tuple     			=						outputs.logits

				# verify the logits
				snake_case  :       Any     			=						torch.Size((1, 21, 65, 65)      )
				self.assertEqual(logits.shape  ,	UpperCamelCase__      )

				snake_case  :       Optional[Any]     			=						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=UpperCamelCase__  ,	)

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




'''simple docstring'''
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowercase__						       =      logging.get_logger(__name__)

lowercase__						       =      {
    "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json",
    # See all YOLOS models at https://huggingface.co/models?filter=yolos
}






class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""

		lowerCamelCase     							=						"""yolos"""



		def __init__(      self				:			Any  ,	UpperCamelCase__				:			Optional[Any]=768  ,	UpperCamelCase__				:			Any=12  ,	UpperCamelCase__				:			Optional[int]=12  ,	UpperCamelCase__				:			Union[str, Any]=3072  ,	UpperCamelCase__				:			str="gelu"  ,	UpperCamelCase__				:			Union[str, Any]=0.0  ,	UpperCamelCase__				:			Union[str, Any]=0.0  ,	UpperCamelCase__				:			Union[str, Any]=0.02  ,	UpperCamelCase__				:			List[Any]=1e-1_2  ,	UpperCamelCase__				:			List[Any]=[512, 864]  ,	UpperCamelCase__				:			int=16  ,	UpperCamelCase__				:			Union[str, Any]=3  ,	UpperCamelCase__				:			Union[str, Any]=True  ,	UpperCamelCase__				:			int=100  ,	UpperCamelCase__				:			Optional[Any]=True  ,	UpperCamelCase__				:			int=False  ,	UpperCamelCase__				:			Dict=1  ,	UpperCamelCase__				:			Optional[int]=5  ,	UpperCamelCase__				:			List[str]=2  ,	UpperCamelCase__				:			Union[str, Any]=5  ,	UpperCamelCase__				:			Union[str, Any]=2  ,	UpperCamelCase__				:			int=0.1  ,	**UpperCamelCase__				:			Optional[Any]  ,	)						->			Any:






				"""simple docstring"""







				super().__init__(**UpperCamelCase__      )

				snake_case  :       List[str]     			=						hidden_size
				snake_case  :       Union[str, Any]     			=						num_hidden_layers
				snake_case  :       List[str]     			=						num_attention_heads
				snake_case  :       Any     			=						intermediate_size
				snake_case  :       int     			=						hidden_act
				snake_case  :       Optional[Any]     			=						hidden_dropout_prob
				snake_case  :       int     			=						attention_probs_dropout_prob
				snake_case  :       Union[str, Any]     			=						initializer_range
				snake_case  :       List[Any]     			=						layer_norm_eps
				snake_case  :       Tuple     			=						image_size
				snake_case  :       Any     			=						patch_size
				snake_case  :       str     			=						num_channels
				snake_case  :       Optional[int]     			=						qkv_bias
				snake_case  :       Optional[Any]     			=						num_detection_tokens
				snake_case  :       Dict     			=						use_mid_position_embeddings
				snake_case  :       List[Any]     			=						auxiliary_loss
				# Hungarian matcher
				snake_case  :       List[str]     			=						class_cost
				snake_case  :       Optional[Any]     			=						bbox_cost
				snake_case  :       Tuple     			=						giou_cost
				# Loss coefficients
				snake_case  :       Union[str, Any]     			=						bbox_loss_coefficient
				snake_case  :       List[Any]     			=						giou_loss_coefficient
				snake_case  :       Tuple     			=						eos_coefficient







class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""

		lowerCamelCase     							=						version.parse("""1.11"""					)



		@property
		def       lowerCAmelCase				(      self				:			int      )						->			Mapping[str, Mapping[int, str]]:






				"""simple docstring"""







				return OrderedDict(
				    [
				        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
				    ]      )



		@property
		def       lowerCAmelCase				(      self				:			int      )						->			float:






				"""simple docstring"""







				return 1e-4



		@property
		def       lowerCAmelCase				(      self				:			Tuple      )						->			int:






				"""simple docstring"""







				return 12


