


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

     '''simple docstring'''


     def 							_UpperCAmelCase      (      self							)       ->							List[str]:
          _a    	=				self.config_class(**self.inputs_dict							)
          self.parent.assertTrue(hasattr(__UpperCAmelCase    ,				'''tf_padding'''							)							)
          self.parent.assertTrue(hasattr(__UpperCAmelCase    ,				'''depth_multiplier'''							)							)







class     __lowerCamelCase  :

     '''simple docstring'''


     def __init__(      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase=13    ,				__UpperCAmelCase=3    ,				__UpperCAmelCase=32    ,				__UpperCAmelCase=0.25    ,				__UpperCAmelCase=8    ,				__UpperCAmelCase=True    ,				__UpperCAmelCase=1024    ,				__UpperCAmelCase=32    ,				__UpperCAmelCase="relu6"    ,				__UpperCAmelCase=0.1    ,				__UpperCAmelCase=0.02    ,				__UpperCAmelCase=True    ,				__UpperCAmelCase=True    ,				__UpperCAmelCase=10    ,				__UpperCAmelCase=None    ,				)       ->							str:
          _a    	=				parent
          _a    	=				batch_size
          _a    	=				num_channels
          _a    	=				image_size
          _a    	=				depth_multiplier
          _a    	=				min_depth
          _a    	=				tf_padding
          _a    	=				int(last_hidden_size * depth_multiplier							)
          _a    	=				output_stride
          _a    	=				hidden_act
          _a    	=				classifier_dropout_prob
          _a    	=				use_labels
          _a    	=				is_training
          _a    	=				num_labels
          _a    	=				initializer_range
          _a    	=				scope


     def 							_UpperCAmelCase      (      self							)       ->							Any:
          _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							)       ->							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 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase							)       ->							str:
          _a    	=				MobileNetVaModel(config=__UpperCAmelCase							)
          model.to(__UpperCAmelCase							)
          model.eval()
          _a    	=				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,
              )    ,				)


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase							)       ->							List[str]:
          _a    	=				self.num_labels
          _a    	=				MobileNetVaForImageClassification(__UpperCAmelCase							)
          model.to(__UpperCAmelCase							)
          model.eval()
          _a    	=				model(__UpperCAmelCase    ,				labels=__UpperCAmelCase							)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.num_labels)							)





     def 							_UpperCAmelCase      (      self							)       ->							Optional[int]:
          _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     __lowerCamelCase  (					a__  ,					a__  ,					unittest.TestCase				):

     '''simple docstring'''
     A_       :	Optional[Any]    =		(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
     A_       :	Any    =		(
         {'feature-extraction': MobileNetVaModel, 'image-classification': MobileNetVaForImageClassification}
         if is_torch_available()
         else {}
     )

     A_       :	Optional[Any]    =		False
     A_       :	int    =		False
     A_       :	List[str]    =		False
     A_       :	List[Any]    =		False


     def 							_UpperCAmelCase      (      self							)       ->							int:
          _a    	=				MobileNetVaModelTester(self							)
          _a    	=				MobileNetVaConfigTester(self    ,				config_class=__UpperCAmelCase    ,				has_text_modality=__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							List[str]:
          self.config_tester.run_common_tests()


     @unittest.skip(reason='''MobileNetV1 does not use inputs_embeds'''							)
     def 							_UpperCAmelCase      (      self							)       ->							Dict:
          pass


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


     @unittest.skip(reason='''MobileNetV1 does not output attentions'''							)
     def 							_UpperCAmelCase      (      self							)       ->							Tuple:
          pass


     def 							_UpperCAmelCase      (      self							)       ->							List[str]:
          _a	,      _a    	=				self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               _a    	=				model_class(__UpperCAmelCase							)
               _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]    ,				__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							str:
          _a    	=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							Dict:
          def check_hidden_states_output(__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase							):
               _a    	=				model_class(__UpperCAmelCase							)
               model.to(__UpperCAmelCase							)
               model.eval()

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

               _a    	=				outputs.hidden_states

               _a    	=				26
               self.assertEqual(len(__UpperCAmelCase							)    ,				__UpperCAmelCase							)

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

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

               check_hidden_states_output(__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							Optional[Any]:
          _a    	=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase							)





     @slow
     def 							_UpperCAmelCase      (      self							)       ->							List[Any]:
          for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _a    	=				MobileNetVaModel.from_pretrained(__UpperCAmelCase							)
               self.assertIsNotNone(__UpperCAmelCase							)







def       A_   (   ):






     """simple docstring"""

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







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

     '''simple docstring'''


     @cached_property
     def 							_UpperCAmelCase      (      self							)       ->							int:
          return (
              MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v1_1.0_224'''							) if is_vision_available() else None
          )





     @slow
     def 							_UpperCAmelCase      (      self							)       ->							List[str]:
          _a    	=				MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224'''							).to(__UpperCAmelCase							)

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

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

          # verify the logits
          _a    	=				torch.Size((1, 1001)							)
          self.assertEqual(outputs.logits.shape    ,				__UpperCAmelCase							)

          _a    	=				torch.tensor([-4.1739, -1.1233, 3.1205]							).to(__UpperCAmelCase							)

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



"""simple docstring"""






from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels
from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features
from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor
from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels