


'''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  a     (     _SCREAMING_SNAKE_CASE       ):
 def 							__UpperCAmelCase		( self						)					->							Any:
      _a			      =							self.config_class(**self.inputs_dict						)
      self.parent.assertTrue(hasattr(__A      ,				'tf_padding'						)						)
      self.parent.assertTrue(hasattr(__A      ,				'depth_multiplier'						)						)





class  a     :
 def __init__( self      ,				__magic_name__      ,				__magic_name__=13      ,				__magic_name__=3      ,				__magic_name__=32      ,				__magic_name__=0.2_5      ,				__magic_name__=8      ,				__magic_name__=8      ,				__magic_name__=6      ,				__magic_name__=32      ,				__magic_name__=True      ,				__magic_name__=True      ,				__magic_name__=True      ,				__magic_name__="relu6"      ,				__magic_name__=12_80      ,				__magic_name__=0.1      ,				__magic_name__=0.0_2      ,				__magic_name__=True      ,				__magic_name__=True      ,				__magic_name__=10      ,				__magic_name__=None      ,				)					->							Any:
      _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 							__UpperCAmelCase		( self						)					->							str:
      _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 							__UpperCAmelCase		( self						)					->							Optional[Any]:
      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 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__						)					->							Optional[Any]:
      _a			      =							MobileNetVaModel(config=__A						)
      model.to(__A						)
      model.eval()
      _a			      =							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,
          )      ,				)
      self.parent.assertEqual(
          result.pooler_output.shape      ,				(self.batch_size, self.last_hidden_size)      ,				)
 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__						)					->							Any:
      _a			      =							self.num_labels
      _a			      =							MobileNetVaForImageClassification(__A						)
      model.to(__A						)
      model.eval()
      _a			      =							model(__A      ,				labels=__A						)
      self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.num_labels)						)
 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__						)					->							Optional[Any]:
      _a			      =							self.num_labels
      _a			      =							MobileNetVaForSemanticSegmentation(__A						)
      model.to(__A						)
      model.eval()
      _a			      =							model(__A						)
      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(__A      ,				labels=__A						)
      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 							__UpperCAmelCase		( self						)					->							Optional[Any]:
      _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  a     (     _SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				unittest.TestCase       ):
 _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 							__UpperCAmelCase		( self						)					->							Union[str, Any]:
      _a			      =							MobileNetVaModelTester(self						)
      _a			      =							MobileNetVaConfigTester(self      ,				config_class=__A      ,				has_text_modality=__A						)
 def 							__UpperCAmelCase		( self						)					->							Optional[Any]:
      self.config_tester.run_common_tests()
 @unittest.skip(reason='MobileNetV2 does not use inputs_embeds'						)
 def 							__UpperCAmelCase		( self						)					->							List[Any]:
      pass
 @unittest.skip(reason='MobileNetV2 does not support input and output embeddings'						)
 def 							__UpperCAmelCase		( self						)					->							str:
      pass
 @unittest.skip(reason='MobileNetV2 does not output attentions'						)
 def 							__UpperCAmelCase		( self						)					->							int:
      pass
 def 							__UpperCAmelCase		( self						)					->							Union[str, Any]:
      _a      ,   _a			      =							self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           _a			      =							model_class(__A						)
           _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]      ,				__A						)
 def 							__UpperCAmelCase		( self						)					->							str:
      _a			      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__A						)
 def 							__UpperCAmelCase		( self						)					->							Optional[Any]:
      def check_hidden_states_output(__magic_name__      ,				__magic_name__      ,				__magic_name__						):
           _a			      =							model_class(__A						)
           model.to(__A						)
           model.eval()

           with torch.no_grad():
                _a			      =							model(**self._prepare_for_class(__A      ,				__A						)						)

           _a			      =							outputs.hidden_states

           _a			      =							16
           self.assertEqual(len(__A						)      ,				__A						)

      _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(__A      ,				__A      ,				__A						)

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

           check_hidden_states_output(__A      ,				__A      ,				__A						)
 def 							__UpperCAmelCase		( self						)					->							Dict:
      _a			      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__A						)
 def 							__UpperCAmelCase		( self						)					->							Any:
      _a			      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_semantic_segmentation(*__A						)




 @slow
 def 							__UpperCAmelCase		( self						)					->							Optional[int]:
      for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _a			      =							MobileNetVaModel.from_pretrained(__A						)
           self.assertIsNotNone(__A						)






def 	_A				()   ->  Tuple:




     '''simple docstring'''



     _a			      =							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'		)
     return image






@require_torch
@require_vision
class  a     (     unittest.TestCase       ):
 @cached_property
 def 							__UpperCAmelCase		( self						)					->							Any:
      return (
          MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v2_1.0_224'						) if is_vision_available() else None
      )
 @slow
 def 							__UpperCAmelCase		( self						)					->							Union[str, Any]:
      _a			      =							MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224'						).to(__A						)

      _a			      =							self.default_image_processor
      _a			      =							prepare_img()
      _a			      =							image_processor(images=__A      ,				return_tensors='pt'						).to(__A						)

      # forward pass
      with torch.no_grad():
           _a			      =							model(**__A						)

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

      _a			      =							torch.tensor([0.2_4_4_5, -1.1_9_9_3, 0.1_9_0_5]						).to(__A						)

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




 @slow
 def 							__UpperCAmelCase		( self						)					->							Tuple:
      _a			      =							MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'						)
      _a			      =							model.to(__A						)

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

      _a			      =							prepare_img()
      _a			      =							image_processor(images=__A      ,				return_tensors='pt'						).to(__A						)

      # forward pass
      with torch.no_grad():
           _a			      =							model(**__A						)
      _a			      =							outputs.logits

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

      _a			      =							torch.tensor(
          [
              [[1_7.5_7_9_0, 1_7.7_5_8_1, 1_8.3_3_5_5], [1_8.3_2_5_7, 1_8.4_2_3_0, 1_8.8_9_7_3], [1_8.6_1_6_9, 1_8.8_6_5_0, 1_9.2_1_8_7]],
              [[-2.1_5_9_5, -2.0_9_7_7, -2.3_7_4_1], [-2.4_2_2_6, -2.3_0_2_8, -2.6_8_3_5], [-2.7_8_1_9, -2.5_9_9_1, -2.7_7_0_6]],
              [[4.2_0_5_8, 4.8_3_1_7, 4.7_6_3_8], [4.4_1_3_6, 5.0_3_6_1, 4.9_3_8_3], [4.5_0_2_8, 4.9_6_4_4, 4.8_7_3_4]],
          ]      ,				device=__A      ,				)

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





'''simple docstring'''






from typing import Any






def 	_A				(lowerCAmelCase__  :list		)   ->  list[Any]:




     '''simple docstring'''



     if not input_list:
          return []
     _a			      =							[input_list.count(lowerCAmelCase__		) for value in input_list]
     _a			      =							max(lowerCAmelCase__		)  # Gets the maximum count in the input list.
     # Gets values of modes
     return sorted({input_list[i] for i, value in enumerate(lowerCAmelCase__		) if value == y}		)


if __name__ == "__main__":
 import doctest

 doctest.testmod()

