





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





      def        _snake_case     (			self					)						->	List[str]:
            lowerCAmelCase    		=			self.config_class(**self.inputs_dict					)
            self.parent.assertTrue(hasattr(__lowerCamelCase ,		"""tf_padding"""					)					)
            self.parent.assertTrue(hasattr(__lowerCamelCase ,		"""depth_multiplier"""					)					)







class 						lowercase   :





      def __init__(			self ,		lowercase ,		lowercase=13 ,		lowercase=3 ,		lowercase=32 ,		lowercase=0.25 ,		lowercase=8 ,		lowercase=8 ,		lowercase=6 ,		lowercase=32 ,		lowercase=True ,		lowercase=True ,		lowercase=True ,		lowercase="relu6" ,		lowercase=1_280 ,		lowercase=0.1 ,		lowercase=0.02 ,		lowercase=True ,		lowercase=True ,		lowercase=10 ,		lowercase=None ,		)						->	Any:
            lowerCAmelCase    		=			parent
            lowerCAmelCase    		=			batch_size
            lowerCAmelCase    		=			num_channels
            lowerCAmelCase    		=			image_size
            lowerCAmelCase    		=			depth_multiplier
            lowerCAmelCase    		=			depth_divisible_by
            lowerCAmelCase    		=			min_depth
            lowerCAmelCase    		=			expand_ratio
            lowerCAmelCase    		=			tf_padding
            lowerCAmelCase    		=			output_stride
            lowerCAmelCase    		=			first_layer_is_expansion
            lowerCAmelCase    		=			finegrained_output
            lowerCAmelCase    		=			hidden_act
            lowerCAmelCase    		=			last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier					)
            lowerCAmelCase    		=			classifier_dropout_prob
            lowerCAmelCase    		=			use_labels
            lowerCAmelCase    		=			is_training
            lowerCAmelCase    		=			num_labels
            lowerCAmelCase    		=			initializer_range
            lowerCAmelCase    		=			scope





      def        _snake_case     (			self					)						->	List[Any]:
            lowerCAmelCase    		=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

            lowerCAmelCase    		=			self.get_config()

            return config, pixel_values, labels, pixel_labels





      def        _snake_case     (			self					)						->	List[str]:
            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        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase					)						->	int:
            lowerCAmelCase    		=			MobileNetVaModel(config=__lowerCamelCase					)
            model.to(__lowerCamelCase					)
            model.eval()
            lowerCAmelCase    		=			model(__lowerCamelCase					)
            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        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase					)						->	Union[str, Any]:
            lowerCAmelCase    		=			self.num_labels
            lowerCAmelCase    		=			MobileNetVaForImageClassification(__lowerCamelCase					)
            model.to(__lowerCamelCase					)
            model.eval()
            lowerCAmelCase    		=			model(__lowerCamelCase ,		labels=__lowerCamelCase					)
            self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.num_labels)					)





      def        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase					)						->	List[Any]:
            lowerCAmelCase    		=			self.num_labels
            lowerCAmelCase    		=			MobileNetVaForSemanticSegmentation(__lowerCamelCase					)
            model.to(__lowerCamelCase					)
            model.eval()
            lowerCAmelCase    		=			model(__lowerCamelCase					)
            self.parent.assertEqual(
                result.logits.shape ,		(
                    self.batch_size,
                    self.num_labels,
                    self.image_size // self.output_stride,
                    self.image_size // self.output_stride,
                ) ,		)
            lowerCAmelCase    		=			model(__lowerCamelCase ,		labels=__lowerCamelCase					)
            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        _snake_case     (			self					)						->	str:
            lowerCAmelCase    		=			self.prepare_config_and_inputs()
            lowerCAmelCase    		=			config_and_inputs
            lowerCAmelCase    		=			{'pixel_values': pixel_values}
            return config, inputs_dict







@require_torch
class 						lowercase   (   lowerCAmelCase_	,    lowerCAmelCase_	,    unittest.TestCase   ):
      _SCREAMING_SNAKE_CASE							      = (
          (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
          if is_torch_available()
          else ()
      )
      _SCREAMING_SNAKE_CASE							      = (
          {
              'feature-extraction': MobileNetVaModel,
              'image-classification': MobileNetVaForImageClassification,
              'image-segmentation': MobileNetVaForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      _SCREAMING_SNAKE_CASE							      = False
      _SCREAMING_SNAKE_CASE							      = False
      _SCREAMING_SNAKE_CASE							      = False
      _SCREAMING_SNAKE_CASE							      = False





      def        _snake_case     (			self					)						->	Any:
            lowerCAmelCase    		=			MobileNetVaModelTester(self					)
            lowerCAmelCase    		=			MobileNetVaConfigTester(self ,		config_class=__lowerCamelCase ,		has_text_modality=__lowerCamelCase					)





      def        _snake_case     (			self					)						->	Optional[Any]:
            self.config_tester.run_common_tests()





      @unittest.skip(reason="""MobileNetV2 does not use inputs_embeds"""					)
      def        _snake_case     (			self					)						->	Optional[Any]:
            pass





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





      @unittest.skip(reason="""MobileNetV2 does not output attentions"""					)
      def        _snake_case     (			self					)						->	int:
            pass





      def        _snake_case     (			self					)						->	Optional[int]:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs_for_common()

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

                  lowerCAmelCase    		=			['pixel_values']
                  self.assertListEqual(arg_names[:1] ,		__lowerCamelCase					)





      def        _snake_case     (			self					)						->	List[str]:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__lowerCamelCase					)





      def        _snake_case     (			self					)						->	Any:
            def check_hidden_states_output(lowercase ,		lowercase ,		lowercase					):
                  lowerCAmelCase    		=			model_class(__lowerCamelCase					)
                  model.to(__lowerCamelCase					)
                  model.eval()

                  with torch.no_grad():
                        lowerCAmelCase    		=			model(**self._prepare_for_class(__lowerCamelCase ,		__lowerCamelCase					)					)

                  lowerCAmelCase    		=			outputs.hidden_states

                  lowerCAmelCase    		=			16
                  self.assertEqual(len(__lowerCamelCase					) ,		__lowerCamelCase					)

            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                  lowerCAmelCase    		=			True
                  check_hidden_states_output(__lowerCamelCase ,		__lowerCamelCase ,		__lowerCamelCase					)

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

                  check_hidden_states_output(__lowerCamelCase ,		__lowerCamelCase ,		__lowerCamelCase					)





      def        _snake_case     (			self					)						->	List[str]:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase					)





      def        _snake_case     (			self					)						->	List[Any]:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCamelCase					)





      @slow
      def        _snake_case     (			self					)						->	int:
            for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowerCAmelCase    		=			MobileNetVaModel.from_pretrained(__lowerCamelCase					)
                  self.assertIsNotNone(__lowerCamelCase					)







def      UpperCAmelCase__				(    ):







      '''simple docstring'''




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






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





      @cached_property
      def        _snake_case     (			self					)						->	str:
            return (
                MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v2_1.0_224"""					) if is_vision_available() else None
            )





      @slow
      def        _snake_case     (			self					)						->	int:
            lowerCAmelCase    		=			MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224"""					).to(__lowerCamelCase					)

            lowerCAmelCase    		=			self.default_image_processor
            lowerCAmelCase    		=			prepare_img()
            lowerCAmelCase    		=			image_processor(images=__lowerCamelCase ,		return_tensors="""pt"""					).to(__lowerCamelCase					)

            # forward pass
            with torch.no_grad():
                  lowerCAmelCase    		=			model(**__lowerCamelCase					)

            # verify the logits
            lowerCAmelCase    		=			torch.Size((1, 1_001)					)
            self.assertEqual(outputs.logits.shape ,		__lowerCamelCase					)

            lowerCAmelCase    		=			torch.tensor([0.2_445, -1.1_993, 0.1_905]					).to(__lowerCamelCase					)

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





      @slow
      def        _snake_case     (			self					)						->	List[Any]:
            lowerCAmelCase    		=			MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""					)
            lowerCAmelCase    		=			model.to(__lowerCamelCase					)

            lowerCAmelCase    		=			MobileNetVaImageProcessor.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""					)

            lowerCAmelCase    		=			prepare_img()
            lowerCAmelCase    		=			image_processor(images=__lowerCamelCase ,		return_tensors="""pt"""					).to(__lowerCamelCase					)

            # forward pass
            with torch.no_grad():
                  lowerCAmelCase    		=			model(**__lowerCamelCase					)
            lowerCAmelCase    		=			outputs.logits

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

            lowerCAmelCase    		=			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=__lowerCamelCase ,		)

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












'''simple docstring'''






from .configuration_bert_masked import MaskedBertConfig
from .modeling_bert_masked import (
    MaskedBertForMultipleChoice,
    MaskedBertForQuestionAnswering,
    MaskedBertForSequenceClassification,
    MaskedBertForTokenClassification,
    MaskedBertModel,
)
from .modules import *
