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				( __lowercase ):

     '''simple docstring'''

     def 		UpperCamelCase_						(					self						) ->				List[str]:






           """simple docstring"""
           _lowerCamelCase			    =				self.config_class(**self.inputs_dict						)
           self.parent.assertTrue(hasattr(A_				,				'''tf_padding'''						)						)
           self.parent.assertTrue(hasattr(A_				,				'''depth_multiplier'''						)						)





class  UpperCamelCase				:

     '''simple docstring'''

     def __init__(					self				,				A_				,				A_=13				,				A_=3				,				A_=32				,				A_=0.25				,				A_=8				,				A_=8				,				A_=6				,				A_=32				,				A_=True				,				A_=True				,				A_=True				,				A_="relu6"				,				A_=12_80				,				A_=0.1				,				A_=0.02				,				A_=True				,				A_=True				,				A_=10				,				A_=None				,				) ->				Any:






           """simple docstring"""
           _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 		UpperCamelCase_						(					self						) ->				Optional[Any]:






           """simple docstring"""
           _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 		UpperCamelCase_						(					self						) ->				int:






           """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 		UpperCamelCase_						(					self				,				A_				,				A_				,				A_				,				A_						) ->				List[str]:






           """simple docstring"""
           _lowerCamelCase			    =				MobileNetVaModel(config=A_						)
           model.to(A_						)
           model.eval()
           _lowerCamelCase			    =				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				,				A_				,				A_				,				A_				,				A_						) ->				Union[str, Any]:






           """simple docstring"""
           _lowerCamelCase			    =				self.num_labels
           _lowerCamelCase			    =				MobileNetVaForImageClassification(A_						)
           model.to(A_						)
           model.eval()
           _lowerCamelCase			    =				model(A_				,				labels=A_						)
           self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels)						)



     def 		UpperCamelCase_						(					self				,				A_				,				A_				,				A_				,				A_						) ->				List[str]:






           """simple docstring"""
           _lowerCamelCase			    =				self.num_labels
           _lowerCamelCase			    =				MobileNetVaForSemanticSegmentation(A_						)
           model.to(A_						)
           model.eval()
           _lowerCamelCase			    =				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,
               )				,				)
           _lowerCamelCase			    =				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						) ->				int:






           """simple docstring"""
           _lowerCamelCase			    =				self.prepare_config_and_inputs()
           _lowerCamelCase    ,					_lowerCamelCase    ,					_lowerCamelCase    ,					_lowerCamelCase			    =				config_and_inputs
           _lowerCamelCase			    =				{'''pixel_values''': pixel_values}
           return config, inputs_dict





@require_torch
class  UpperCamelCase				( __lowercase   ,  __lowercase   ,  unittest.TestCase ):

     '''simple docstring'''

     A_										=  (
         (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
         if is_torch_available()
         else ()
     )
     A_										=  (
         {
             'feature-extraction': MobileNetVaModel,
             'image-classification': MobileNetVaForImageClassification,
             'image-segmentation': MobileNetVaForSemanticSegmentation,
         }
         if is_torch_available()
         else {}
     )

     A_										=  False
     A_										=  False
     A_										=  False
     A_										=  False



     def 		UpperCamelCase_						(					self						) ->				Union[str, Any]:






           """simple docstring"""
           _lowerCamelCase			    =				MobileNetVaModelTester(self						)
           _lowerCamelCase			    =				MobileNetVaConfigTester(self				,				config_class=A_				,				has_text_modality=A_						)



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






           """simple docstring"""
           self.config_tester.run_common_tests()



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






           """simple docstring"""
           pass



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






           """simple docstring"""
           pass



     @unittest.skip(reason='''MobileNetV2 does not output attentions'''						)
     def 		UpperCamelCase_						(					self						) ->				Optional[Any]:






           """simple docstring"""
           pass



     def 		UpperCamelCase_						(					self						) ->				Tuple:






           """simple docstring"""
           _lowerCamelCase    ,					_lowerCamelCase			    =				self.model_tester.prepare_config_and_inputs_for_common()

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



     def 		UpperCamelCase_						(					self						) ->				str:






           """simple docstring"""
           _lowerCamelCase			    =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*A_						)



     def 		UpperCamelCase_						(					self						) ->				Dict:






           """simple docstring"""
           def check_hidden_states_output(A_				,				A_				,				A_						):
                 _lowerCamelCase			    =				model_class(A_						)
                 model.to(A_						)
                 model.eval()

                 with torch.no_grad():
                       _lowerCamelCase			    =				model(**self._prepare_for_class(A_				,				A_						)						)

                 _lowerCamelCase			    =				outputs.hidden_states

                 _lowerCamelCase			    =				16
                 self.assertEqual(len(A_						)				,				A_						)

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

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

                 check_hidden_states_output(A_				,				A_				,				A_						)



     def 		UpperCamelCase_						(					self						) ->				str:






           """simple docstring"""
           _lowerCamelCase			    =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*A_						)



     def 		UpperCamelCase_						(					self						) ->				int:






           """simple docstring"""
           _lowerCamelCase			    =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_semantic_segmentation(*A_						)





     @slow
     def 		UpperCamelCase_						(					self						) ->				Tuple:






           """simple docstring"""
           for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _lowerCamelCase			    =				MobileNetVaModel.from_pretrained(A_						)
                 self.assertIsNotNone(A_						)





def       __magic_name__(  )						->    str:



      '''simple docstring'''

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




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

     '''simple docstring'''

     @cached_property
     def 		UpperCamelCase_						(					self						) ->				List[Any]:






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



     @slow
     def 		UpperCamelCase_						(					self						) ->				str:






           """simple docstring"""
           _lowerCamelCase			    =				MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v2_1.0_224'''						).to(A_						)

           _lowerCamelCase			    =				self.default_image_processor
           _lowerCamelCase			    =				prepare_img()
           _lowerCamelCase			    =				image_processor(images=A_				,				return_tensors='''pt'''						).to(A_						)

           # forward pass
           with torch.no_grad():
                 _lowerCamelCase			    =				model(**A_						)

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

           _lowerCamelCase			    =				torch.tensor([0.2445, -1.1993, 0.1905]						).to(A_						)

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





     @slow
     def 		UpperCamelCase_						(					self						) ->				Dict:






           """simple docstring"""
           _lowerCamelCase			    =				MobileNetVaForSemanticSegmentation.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''						)
           _lowerCamelCase			    =				model.to(A_						)

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

           _lowerCamelCase			    =				prepare_img()
           _lowerCamelCase			    =				image_processor(images=A_				,				return_tensors='''pt'''						).to(A_						)

           # forward pass
           with torch.no_grad():
                 _lowerCamelCase			    =				model(**A_						)
           _lowerCamelCase			    =				outputs.logits

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

           _lowerCamelCase			    =				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=A_				,				)

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

import numpy as np
import pytest
from scipy.linalg import eigh

logging.basicConfig(level=logging.INFO, format='%(message)s')
def       __magic_name__(  __UpperCAmelCase		)						->    np.ndarray:



      '''simple docstring'''

      return input_array.reshape((input_array.size, 1)		)
def       __magic_name__(  __UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		)						->    np.ndarray:



      '''simple docstring'''

      _lowerCamelCase			    =				np.nan
      for i in range(__UpperCAmelCase		):
            _lowerCamelCase			    =				features[:, labels == i]
            _lowerCamelCase			    =				data.mean(1		)
            # Centralize the data of class i
            _lowerCamelCase			    =				data - column_reshape(__UpperCAmelCase		)
            if i > 0:
                  # If covariance_sum is not None
                  covariance_sum += np.dot(__UpperCAmelCase ,						centered_data.T		)
            else:
                  # If covariance_sum is np.nan (i.e. first loop)
                  _lowerCamelCase			    =				np.dot(__UpperCAmelCase ,						centered_data.T		)

      return covariance_sum / features.shape[1]
def       __magic_name__(  __UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		)						->    np.ndarray:



      '''simple docstring'''

      _lowerCamelCase			    =				features.mean(1		)
      _lowerCamelCase			    =				np.nan
      for i in range(__UpperCAmelCase		):
            _lowerCamelCase			    =				features[:, labels == i]
            _lowerCamelCase			    =				data.shape[1]
            _lowerCamelCase			    =				data.mean(1		)
            if i > 0:
                  # If covariance_sum is not None
                  covariance_sum += device_data * np.dot(
                      column_reshape(__UpperCAmelCase		) - column_reshape(__UpperCAmelCase		) ,						(column_reshape(__UpperCAmelCase		) - column_reshape(__UpperCAmelCase		)).T ,						)
            else:
                  # If covariance_sum is np.nan (i.e. first loop)
                  _lowerCamelCase			    =				device_data * np.dot(
                      column_reshape(__UpperCAmelCase		) - column_reshape(__UpperCAmelCase		) ,						(column_reshape(__UpperCAmelCase		) - column_reshape(__UpperCAmelCase		)).T ,						)

      return covariance_sum / features.shape[1]
def       __magic_name__(  __UpperCAmelCase ,						__UpperCAmelCase		)						->    np.ndarray:



      '''simple docstring'''

      if features.any():
            _lowerCamelCase			    =				features.mean(1		)
            # Center the dataset
            _lowerCamelCase			    =				features - np.reshape(__UpperCAmelCase ,						(data_mean.size, 1)		)
            _lowerCamelCase			    =				np.dot(__UpperCAmelCase ,						centered_data.T		) / features.shape[1]
            _lowerCamelCase    ,					_lowerCamelCase			    =				np.linalg.eigh(__UpperCAmelCase		)
            # Take all the columns in the reverse order (-1), and then takes only the first
            _lowerCamelCase			    =				eigenvectors[:, ::-1][:, 0:dimensions]
            # Project the database on the new space
            _lowerCamelCase			    =				np.dot(filtered_eigenvectors.T ,						__UpperCAmelCase		)
            logging.info('''Principal Component Analysis computed'''		)

            return projected_data
      else:
            logging.basicConfig(level=logging.ERROR ,						format='''%(message)s''' ,						force=__UpperCAmelCase		)
            logging.error('''Dataset empty'''		)
            raise AssertionError
def       __magic_name__(  __UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		)						->    np.ndarray:



      '''simple docstring'''

      assert classes > dimensions

      # Check if features have been already loaded
      if features.any:
            _lowerCamelCase    ,					_lowerCamelCase			    =				eigh(
                covariance_between_classes(__UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		) ,						covariance_within_classes(__UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		) ,						)
            _lowerCamelCase			    =				eigenvectors[:, ::-1][:, :dimensions]
            _lowerCamelCase    ,					_lowerCamelCase    ,					_lowerCamelCase			    =				np.linalg.svd(__UpperCAmelCase		)
            _lowerCamelCase			    =				svd_matrix[:, 0:dimensions]
            _lowerCamelCase			    =				np.dot(filtered_svd_matrix.T ,						__UpperCAmelCase		)
            logging.info('''Linear Discriminant Analysis computed'''		)

            return projected_data
      else:
            logging.basicConfig(level=logging.ERROR ,						format='''%(message)s''' ,						force=__UpperCAmelCase		)
            logging.error('''Dataset empty'''		)
            raise AssertionError
def       __magic_name__(  )						->    None:



      '''simple docstring'''

      _lowerCamelCase			    =				np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]		)
      _lowerCamelCase			    =				np.array([0, 0, 0, 1, 1]		)
      _lowerCamelCase			    =				2
      _lowerCamelCase			    =				2

      # Assert that the function raises an AssertionError if dimensions > classes
      with pytest.raises(__UpperCAmelCase		) as error_info:
            _lowerCamelCase			    =				linear_discriminant_analysis(
                __UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase ,						__UpperCAmelCase		)
            if isinstance(__UpperCAmelCase ,						np.ndarray		):
                  raise AssertionError(
                      '''Did not raise AssertionError for dimensions > classes'''		)
            assert error_info.type is AssertionError
def       __magic_name__(  )						->    None:



      '''simple docstring'''

      _lowerCamelCase			    =				np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]		)
      _lowerCamelCase			    =				2
      _lowerCamelCase			    =				np.array([[6.9_2_8_2_0_3_2_3, 8.6_6_0_2_5_4_0_4, 1_0.3_9_2_3_0_4_8_5], [3.0, 3.0, 3.0]]		)

      with pytest.raises(__UpperCAmelCase		) as error_info:
            _lowerCamelCase			    =				principal_component_analysis(__UpperCAmelCase ,						__UpperCAmelCase		)
            if not np.allclose(__UpperCAmelCase ,						__UpperCAmelCase		):
                  raise AssertionError
            assert error_info.type is AssertionError


if __name__ == "__main__":
     import doctest

     doctest.testmod()