
"""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 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.config_class(**self.inputs_dict    )
											self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE__   ,       """tf_padding"""    )    )
											self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE__   ,       """depth_multiplier"""    )    )






class 	lowerCAmelCase_						:


				"""simple docstring"""



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



											"""simple docstring"""




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



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   None
											SCREAMING_SNAKE_CASE__						:       str           =   None
											if self.use_labels:
																		SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   ids_tensor([self.batch_size]   ,       self.num_labels    )
																		SCREAMING_SNAKE_CASE__						:       Tuple           =   ids_tensor([self.batch_size, self.image_size, self.image_size]   ,       self.num_labels    )

											SCREAMING_SNAKE_CASE__						:       Tuple           =   self.get_config()

											return config, pixel_values, labels, pixel_labels



				def 	__magic_name__    (self    )					->		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 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       str           =   MobileNetVaModel(config=SCREAMING_SNAKE_CASE__    )
											model.to(SCREAMING_SNAKE_CASE__    )
											model.eval()
											SCREAMING_SNAKE_CASE__						:       List[str]           =   model(SCREAMING_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 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Tuple           =   self.num_labels
											SCREAMING_SNAKE_CASE__						:       Any           =   MobileNetVaForImageClassification(SCREAMING_SNAKE_CASE__    )
											model.to(SCREAMING_SNAKE_CASE__    )
											model.eval()
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   model(SCREAMING_SNAKE_CASE__   ,       labels=SCREAMING_SNAKE_CASE__    )
											self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.num_labels)    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.num_labels
											SCREAMING_SNAKE_CASE__						:       Any           =   MobileNetVaForSemanticSegmentation(SCREAMING_SNAKE_CASE__    )
											model.to(SCREAMING_SNAKE_CASE__    )
											model.eval()
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   model(SCREAMING_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,
											    )   ,       )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   model(SCREAMING_SNAKE_CASE__   ,       labels=SCREAMING_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 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.prepare_config_and_inputs()
											SCREAMING_SNAKE_CASE__      ,      SCREAMING_SNAKE_CASE__      ,      SCREAMING_SNAKE_CASE__      ,      SCREAMING_SNAKE_CASE__						:       List[Any]           =   config_and_inputs
											SCREAMING_SNAKE_CASE__						:       int           =   {"""pixel_values""": pixel_values}
											return config, inputs_dict






@require_torch
class 	lowerCAmelCase_						(a__ ,					a__ ,					unittest.TestCase  ):


				"""simple docstring"""



				__UpperCamelCase      :						List[str]					       =   (
				    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
				    if is_torch_available()
				    else ()
				)
				__UpperCamelCase      :						Tuple					       =   (
				    {
				        '''feature-extraction''': MobileNetVaModel,
				        '''image-classification''': MobileNetVaForImageClassification,
				        '''image-segmentation''': MobileNetVaForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				__UpperCamelCase      :						List[Any]					       =   False
				__UpperCamelCase      :						List[Any]					       =   False
				__UpperCamelCase      :						List[Any]					       =   False
				__UpperCamelCase      :						Any					       =   False



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       int           =   MobileNetVaModelTester(self    )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   MobileNetVaConfigTester(self   ,       config_class=SCREAMING_SNAKE_CASE__   ,       has_text_modality=SCREAMING_SNAKE_CASE__    )



				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											self.config_tester.run_common_tests()



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



											"""simple docstring"""




											pass



				@unittest.skip(reason="""MobileNetV2 does not support input and output embeddings"""    )
				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											pass



				@unittest.skip(reason="""MobileNetV2 does not output attentions"""    )
				def 	__magic_name__    (self    )					->		List[str]:



											"""simple docstring"""




											pass



				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__      ,      SCREAMING_SNAKE_CASE__						:       List[Any]           =   self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		SCREAMING_SNAKE_CASE__						:       Dict           =   model_class(SCREAMING_SNAKE_CASE__    )
																		SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   inspect.signature(model.forward    )
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   [*signature.parameters.keys()]

																		SCREAMING_SNAKE_CASE__						:       Dict           =   ["""pixel_values"""]
																		self.assertListEqual(arg_names[:1]   ,       SCREAMING_SNAKE_CASE__    )



				def 	__magic_name__    (self    )					->		Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Dict           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__    )



				def 	__magic_name__    (self    )					->		Tuple:



											"""simple docstring"""




											def check_hidden_states_output(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    ):
																		SCREAMING_SNAKE_CASE__						:       Tuple           =   model_class(SCREAMING_SNAKE_CASE__    )
																		model.to(SCREAMING_SNAKE_CASE__    )
																		model.eval()

																		with torch.no_grad():
																									SCREAMING_SNAKE_CASE__						:       Tuple           =   model(**self._prepare_for_class(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )    )

																		SCREAMING_SNAKE_CASE__						:       Dict           =   outputs.hidden_states

																		SCREAMING_SNAKE_CASE__						:       List[str]           =   16
																		self.assertEqual(len(SCREAMING_SNAKE_CASE__    )   ,       SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__      ,      SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		SCREAMING_SNAKE_CASE__						:       Any           =   True
																		check_hidden_states_output(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		SCREAMING_SNAKE_CASE__						:       Optional[int]           =   True

																		check_hidden_states_output(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       str           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__    )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[Any]           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE__    )



				@slow
				def 	__magic_name__    (self    )					->		List[Any]:



											"""simple docstring"""




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






def lowercase_  (   ):
							SCREAMING_SNAKE_CASE__						:       Tuple           =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
							return image



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


				"""simple docstring"""



				@cached_property
				def 	__magic_name__    (self    )					->		Any:



											"""simple docstring"""




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



				@slow
				def 	__magic_name__    (self    )					->		Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[Any]           =   MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224"""    ).to(SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.default_image_processor
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   prepare_img()
											SCREAMING_SNAKE_CASE__						:       List[str]           =   image_processor(images=SCREAMING_SNAKE_CASE__   ,       return_tensors="""pt"""    ).to(SCREAMING_SNAKE_CASE__    )

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

											# verify the logits
											SCREAMING_SNAKE_CASE__						:       Any           =   torch.Size((1, 10_01)    )
											self.assertEqual(outputs.logits.shape   ,       SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Tuple           =   torch.tensor([0.2445, -1.1993, 0.1905]    ).to(SCREAMING_SNAKE_CASE__    )

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



				@slow
				def 	__magic_name__    (self    )					->		List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Dict           =   MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   model.to(SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Any           =   MobileNetVaImageProcessor.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""    )

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   prepare_img()
											SCREAMING_SNAKE_CASE__						:       Any           =   image_processor(images=SCREAMING_SNAKE_CASE__   ,       return_tensors="""pt"""    ).to(SCREAMING_SNAKE_CASE__    )

											# forward pass
											with torch.no_grad():
																		SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   model(**SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       int           =   outputs.logits

											# verify the logits
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   torch.Size((1, 21, 65, 65)    )
											self.assertEqual(logits.shape   ,       SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   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=SCREAMING_SNAKE_CASE__   ,       )

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






"""simple docstring"""




from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase__      :    Optional[Any]    							= logging.get_logger(__name__)

UpperCAmelCase__      :    Dict    							= {
    'MIT/ast-finetuned-audioset-10-10-0.4593': (
        'https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json'
    ),
}



class 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				__UpperCamelCase      :						Optional[int]					       =   '''audio-spectrogram-transformer'''



				def __init__(self   ,       SCREAMING_SNAKE_CASE__=7_68   ,       SCREAMING_SNAKE_CASE__=12   ,       SCREAMING_SNAKE_CASE__=12   ,       SCREAMING_SNAKE_CASE__=30_72   ,       SCREAMING_SNAKE_CASE__="gelu"   ,       SCREAMING_SNAKE_CASE__=0.0   ,       SCREAMING_SNAKE_CASE__=0.0   ,       SCREAMING_SNAKE_CASE__=0.02   ,       SCREAMING_SNAKE_CASE__=1E-12   ,       SCREAMING_SNAKE_CASE__=16   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=10   ,       SCREAMING_SNAKE_CASE__=10   ,       SCREAMING_SNAKE_CASE__=10_24   ,       SCREAMING_SNAKE_CASE__=1_28   ,       **SCREAMING_SNAKE_CASE__   ,       )					->		Tuple:



											"""simple docstring"""




											super().__init__(**SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   hidden_size
											SCREAMING_SNAKE_CASE__						:       str           =   num_hidden_layers
											SCREAMING_SNAKE_CASE__						:       int           =   num_attention_heads
											SCREAMING_SNAKE_CASE__						:       Tuple           =   intermediate_size
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   hidden_act
											SCREAMING_SNAKE_CASE__						:       Any           =   hidden_dropout_prob
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   attention_probs_dropout_prob
											SCREAMING_SNAKE_CASE__						:       int           =   initializer_range
											SCREAMING_SNAKE_CASE__						:       int           =   layer_norm_eps
											SCREAMING_SNAKE_CASE__						:       Dict           =   patch_size
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   qkv_bias
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   frequency_stride
											SCREAMING_SNAKE_CASE__						:       Any           =   time_stride
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   max_length
											SCREAMING_SNAKE_CASE__						:       Any           =   num_mel_bins




