'''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       UpperCAmelCase  (						a__ ):





       '''simple docstring'''



       def _lowerCAmelCase(						self			)  ->  Union[str, Any]:
        lowercase__    :   int						=				self.config_class(**self.inputs_dict			)
        self.parent.assertTrue(hasattr(__lowerCAmelCase     ,       '''tf_padding'''			)			)
        self.parent.assertTrue(hasattr(__lowerCAmelCase     ,       '''depth_multiplier'''			)			)



class       UpperCAmelCase  :





       '''simple docstring'''



       def __init__(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase=13     ,       __lowerCAmelCase=3     ,       __lowerCAmelCase=32     ,       __lowerCAmelCase=0.2_5     ,       __lowerCAmelCase=8     ,       __lowerCAmelCase=8     ,       __lowerCAmelCase=6     ,       __lowerCAmelCase=32     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase="relu6"     ,       __lowerCAmelCase=1280     ,       __lowerCAmelCase=0.1     ,       __lowerCAmelCase=0.0_2     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=10     ,       __lowerCAmelCase=None     ,       )  ->  List[Any]:
        lowercase__    :   str						=				parent
        lowercase__    :   Union[str, Any]						=				batch_size
        lowercase__    :   Tuple						=				num_channels
        lowercase__    :   Union[str, Any]						=				image_size
        lowercase__    :   int						=				depth_multiplier
        lowercase__    :   Dict						=				depth_divisible_by
        lowercase__    :   Optional[Any]						=				min_depth
        lowercase__    :   int						=				expand_ratio
        lowercase__    :   Union[str, Any]						=				tf_padding
        lowercase__    :   int						=				output_stride
        lowercase__    :   List[Any]						=				first_layer_is_expansion
        lowercase__    :   List[Any]						=				finegrained_output
        lowercase__    :   List[Any]						=				hidden_act
        lowercase__    :   int						=				last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier			)
        lowercase__    :   int						=				classifier_dropout_prob
        lowercase__    :   Optional[int]						=				use_labels
        lowercase__    :   Optional[Any]						=				is_training
        lowercase__    :   Tuple						=				num_labels
        lowercase__    :   Union[str, Any]						=				initializer_range
        lowercase__    :   Any						=				scope

       def _lowerCAmelCase(						self			)  ->  List[str]:
        lowercase__    :   Optional[int]						=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

        lowercase__    :   List[Any]						=				self.get_config()

        return config, pixel_values, labels, pixel_labels

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

       def _lowerCAmelCase(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase			)  ->  List[Any]:
        lowercase__    :   Union[str, Any]						=				self.num_labels
        lowercase__    :   List[str]						=				MobileNetVaForSemanticSegmentation(__lowerCAmelCase			)
        model.to(__lowerCAmelCase			)
        model.eval()
        lowercase__    :   int						=				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,
            )     ,       )
        lowercase__    :   Optional[Any]						=				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 _lowerCAmelCase(						self			)  ->  Optional[Any]:
        lowercase__    :   Any						=				self.prepare_config_and_inputs()
        lowercase__    :   Dict						=				config_and_inputs
        lowercase__    :   Union[str, Any]						=				{'''pixel_values''': pixel_values}
        return config, inputs_dict



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





       '''simple docstring'''



       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 _lowerCAmelCase(						self			)  ->  Optional[int]:
        lowercase__    :   Union[str, Any]						=				MobileNetVaModelTester(self			)
        lowercase__    :   Dict						=				MobileNetVaConfigTester(self     ,       config_class=__lowerCAmelCase     ,       has_text_modality=__lowerCAmelCase			)

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

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

       @unittest.skip(reason='''MobileNetV2 does not support input and output embeddings'''			)
       def _lowerCAmelCase(						self			)  ->  int:
        pass

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

       def _lowerCAmelCase(						self			)  ->  Any:
        lowercase__    :   Tuple						=				self.model_tester.prepare_config_and_inputs_for_common()

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

         lowercase__    :   List[Any]						=				['''pixel_values''']
         self.assertListEqual(arg_names[:1]     ,       __lowerCAmelCase			)

       def _lowerCAmelCase(						self			)  ->  Optional[int]:
        lowercase__    :   List[str]						=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__lowerCAmelCase			)

       def _lowerCAmelCase(						self			)  ->  str:
        def check_hidden_states_output(__lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase			):
         lowercase__    :   Optional[Any]						=				model_class(__lowerCAmelCase			)
         model.to(__lowerCAmelCase			)
         model.eval()

         with torch.no_grad():
          lowercase__    :   Optional[Any]						=				model(**self._prepare_for_class(__lowerCAmelCase     ,       __lowerCAmelCase			)			)

         lowercase__    :   Dict						=				outputs.hidden_states

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

        lowercase__    :   Optional[int]						=				self.model_tester.prepare_config_and_inputs_for_common()

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

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

         check_hidden_states_output(__lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase			)

       def _lowerCAmelCase(						self			)  ->  Union[str, Any]:
        lowercase__    :   Optional[Any]						=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase			)

       def _lowerCAmelCase(						self			)  ->  Optional[Any]:
        lowercase__    :   str						=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCAmelCase			)


       @slow
       def _lowerCAmelCase(						self			)  ->  str:
        for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         lowercase__    :   List[Any]						=				MobileNetVaModel.from_pretrained(__lowerCAmelCase			)
         self.assertIsNotNone(__lowerCAmelCase			)


def       __UpperCamelCase	(       ):
 lowercase__    :   Optional[Any]						=				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
 return image





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





       '''simple docstring'''



       @cached_property
       def _lowerCAmelCase(						self			)  ->  Optional[Any]:
        return (
            MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v2_1.0_224'''			) if is_vision_available() else None
        )

       @slow
       def _lowerCAmelCase(						self			)  ->  Any:
        lowercase__    :   List[Any]						=				MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v2_1.0_224'''			).to(__lowerCAmelCase			)

        lowercase__    :   Tuple						=				self.default_image_processor
        lowercase__    :   Any						=				prepare_img()
        lowercase__    :   Union[str, Any]						=				image_processor(images=__lowerCAmelCase     ,       return_tensors='''pt'''			).to(__lowerCAmelCase			)

        # forward pass
        with torch.no_grad():
         lowercase__    :   List[Any]						=				model(**__lowerCAmelCase			)

        # verify the logits
        lowercase__    :   Union[str, Any]						=				torch.Size((1, 1001)			)
        self.assertEqual(outputs.logits.shape     ,       __lowerCAmelCase			)

        lowercase__    :   List[str]						=				torch.tensor([0.2_4_4_5, -1.1_9_9_3, 0.1_9_0_5]			).to(__lowerCAmelCase			)

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


       @slow
       def _lowerCAmelCase(						self			)  ->  int:
        lowercase__    :   str						=				MobileNetVaForSemanticSegmentation.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''			)
        lowercase__    :   str						=				model.to(__lowerCAmelCase			)

        lowercase__    :   Optional[Any]						=				MobileNetVaImageProcessor.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''			)

        lowercase__    :   List[str]						=				prepare_img()
        lowercase__    :   Optional[int]						=				image_processor(images=__lowerCAmelCase     ,       return_tensors='''pt'''			).to(__lowerCAmelCase			)

        # forward pass
        with torch.no_grad():
         lowercase__    :   int						=				model(**__lowerCAmelCase			)
        lowercase__    :   List[str]						=				outputs.logits

        # verify the logits
        lowercase__    :   List[Any]						=				torch.Size((1, 21, 65, 65)			)
        self.assertEqual(logits.shape     ,       __lowerCAmelCase			)

        lowercase__    :   str						=				torch.tensor(
            [
                [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
                [[-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=__lowerCAmelCase     ,       )

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


'''simple docstring'''

import numpy as np
def       __UpperCamelCase	(       UpperCAmelCase					):
 return 1 / (1 + np.exp(-vector					))
def       __UpperCamelCase	(       UpperCAmelCase					):
 return vector * sigmoid(UpperCAmelCase					)


if __name__ == "__main__":
  import doctest

  doctest.testmod()

