


'''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  __magic_name__							(		lowerCAmelCase    ):






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




          '''simple docstring'''
          _UpperCAmelCase       :       int							   =self.config_class(**self.inputs_dict)
          self.parent.assertTrue(hasattr(snake_case  ,							'tf_padding'))
          self.parent.assertTrue(hasattr(snake_case  ,							'depth_multiplier'))






class  __magic_name__							:






       def __init__(      self  ,							snake_case  ,							snake_case=1_3  ,							snake_case=3  ,							snake_case=3_2  ,							snake_case=0.25  ,							snake_case=8  ,							snake_case=8  ,							snake_case=6  ,							snake_case=3_2  ,							snake_case=True  ,							snake_case=True  ,							snake_case=True  ,							snake_case="relu6"  ,							snake_case=1_2_8_0  ,							snake_case=0.1  ,							snake_case=0.02  ,							snake_case=True  ,							snake_case=True  ,							snake_case=1_0  ,							snake_case=None  ,							)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       List[Any]							   =parent
          _UpperCAmelCase       :       int							   =batch_size
          _UpperCAmelCase       :       int							   =num_channels
          _UpperCAmelCase       :       Union[str, Any]							   =image_size
          _UpperCAmelCase       :       Tuple							   =depth_multiplier
          _UpperCAmelCase       :       Optional[int]							   =depth_divisible_by
          _UpperCAmelCase       :       Tuple							   =min_depth
          _UpperCAmelCase       :       Tuple							   =expand_ratio
          _UpperCAmelCase       :       Any							   =tf_padding
          _UpperCAmelCase       :       Union[str, Any]							   =output_stride
          _UpperCAmelCase       :       Optional[Any]							   =first_layer_is_expansion
          _UpperCAmelCase       :       Optional[Any]							   =finegrained_output
          _UpperCAmelCase       :       List[Any]							   =hidden_act
          _UpperCAmelCase       :       str							   =last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier)
          _UpperCAmelCase       :       str							   =classifier_dropout_prob
          _UpperCAmelCase       :       Optional[Any]							   =use_labels
          _UpperCAmelCase       :       Optional[Any]							   =is_training
          _UpperCAmelCase       :       List[str]							   =num_labels
          _UpperCAmelCase       :       Optional[Any]							   =initializer_range
          _UpperCAmelCase       :       str							   =scope






       def       lowerCAmelCase		(      self)						->  List[Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

          _UpperCAmelCase       :       Optional[Any]							   =self.get_config()

          return config, pixel_values, labels, pixel_labels






       def       lowerCAmelCase		(      self)						->  Dict:




          '''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       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case)						->  List[Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =MobileNetVaModel(config=snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase       :       Optional[int]							   =model(snake_case)
          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  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case)						->  List[str]:




          '''simple docstring'''
          _UpperCAmelCase       :       Tuple							   =self.num_labels
          _UpperCAmelCase       :       Tuple							   =MobileNetVaForImageClassification(snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase       :       Any							   =model(snake_case  ,							labels=snake_case)
          self.parent.assertEqual(result.logits.shape  ,							(self.batch_size, self.num_labels))






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case)						->  List[Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       List[str]							   =self.num_labels
          _UpperCAmelCase       :       List[str]							   =MobileNetVaForSemanticSegmentation(snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase       :       List[str]							   =model(snake_case)
          self.parent.assertEqual(
              result.logits.shape  ,							(
                  self.batch_size,
                  self.num_labels,
                  self.image_size // self.output_stride,
                  self.image_size // self.output_stride,
              )  ,							)
          _UpperCAmelCase       :       int							   =model(snake_case  ,							labels=snake_case)
          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[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[Any]							   =self.prepare_config_and_inputs()
          _UpperCAmelCase   ,	_UpperCAmelCase   ,	_UpperCAmelCase   ,	_UpperCAmelCase       :       Any							   =config_and_inputs
          _UpperCAmelCase       :       Optional[int]							   ={'pixel_values': pixel_values}
          return config, inputs_dict






@require_torch
class  __magic_name__							(		lowerCAmelCase ,lowerCAmelCase ,unittest.TestCase    ):
       UpperCAmelCase										=(
           (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
           if is_torch_available()
           else ()
       )
       UpperCAmelCase										=(
           {
               "feature-extraction": MobileNetVaModel,
               "image-classification": MobileNetVaForImageClassification,
               "image-segmentation": MobileNetVaForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

       UpperCAmelCase										=False
       UpperCAmelCase										=False
       UpperCAmelCase										=False
       UpperCAmelCase										=False






       def       lowerCAmelCase		(      self)						->  str:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =MobileNetVaModelTester(self)
          _UpperCAmelCase       :       int							   =MobileNetVaConfigTester(self  ,							config_class=snake_case  ,							has_text_modality=snake_case)






       def       lowerCAmelCase		(      self)						->  int:




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






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




          '''simple docstring'''
          pass






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




          '''simple docstring'''
          pass






       @unittest.skip(reason='MobileNetV2 does not output attentions')
       def       lowerCAmelCase		(      self)						->  List[str]:




          '''simple docstring'''
          pass






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




          '''simple docstring'''
          _UpperCAmelCase   ,	_UpperCAmelCase       :       List[str]							   =self.model_tester.prepare_config_and_inputs_for_common()

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

             _UpperCAmelCase       :       Any							   =['pixel_values']
             self.assertListEqual(arg_names[:1]  ,							snake_case)






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




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[Any]							   =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case)






       def       lowerCAmelCase		(      self)						->  str:




          '''simple docstring'''
          def check_hidden_states_output(snake_case  ,							snake_case  ,							snake_case):
             _UpperCAmelCase       :       Optional[Any]							   =model_class(snake_case)
             model.to(snake_case)
             model.eval()

             with torch.no_grad():
                _UpperCAmelCase       :       Tuple							   =model(**self._prepare_for_class(snake_case  ,							snake_case))

             _UpperCAmelCase       :       Dict							   =outputs.hidden_states

             _UpperCAmelCase       :       Any							   =1_6
             self.assertEqual(len(snake_case)  ,							snake_case)

          _UpperCAmelCase   ,	_UpperCAmelCase       :       int							   =self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             _UpperCAmelCase       :       Dict							   =True
             check_hidden_states_output(snake_case  ,							snake_case  ,							snake_case)

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             _UpperCAmelCase       :       Any							   =True

             check_hidden_states_output(snake_case  ,							snake_case  ,							snake_case)






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




          '''simple docstring'''
          _UpperCAmelCase       :       Tuple							   =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*snake_case)






       def       lowerCAmelCase		(      self)						->  Dict:




          '''simple docstring'''
          _UpperCAmelCase       :       List[Any]							   =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_semantic_segmentation(*snake_case)






       @slow
       def       lowerCAmelCase		(      self)						->  str:




          '''simple docstring'''
          for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCAmelCase       :       Tuple							   =MobileNetVaModel.from_pretrained(snake_case)
             self.assertIsNotNone(snake_case)






def lowerCamelCase__      (						):







   '''simple docstring'''




   _UpperCAmelCase       :       List[Any]							   =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
   return image






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






       @cached_property
       def       lowerCAmelCase		(      self)						->  Dict:




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






       @slow
       def       lowerCAmelCase		(      self)						->  Tuple:




          '''simple docstring'''
          _UpperCAmelCase       :       int							   =MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224').to(snake_case)

          _UpperCAmelCase       :       List[Any]							   =self.default_image_processor
          _UpperCAmelCase       :       Optional[Any]							   =prepare_img()
          _UpperCAmelCase       :       Dict							   =image_processor(images=snake_case  ,							return_tensors='pt').to(snake_case)

          # forward pass
          with torch.no_grad():
             _UpperCAmelCase       :       int							   =model(**snake_case)

          # verify the logits
          _UpperCAmelCase       :       Dict							   =torch.Size((1, 1_0_0_1))
          self.assertEqual(outputs.logits.shape  ,							snake_case)

          _UpperCAmelCase       :       str							   =torch.tensor([0.24_45, -1.19_93, 0.19_05]).to(snake_case)

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






       @slow
       def       lowerCAmelCase		(      self)						->  Optional[Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513')
          _UpperCAmelCase       :       List[str]							   =model.to(snake_case)

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

          _UpperCAmelCase       :       Optional[Any]							   =prepare_img()
          _UpperCAmelCase       :       List[str]							   =image_processor(images=snake_case  ,							return_tensors='pt').to(snake_case)

          # forward pass
          with torch.no_grad():
             _UpperCAmelCase       :       Optional[Any]							   =model(**snake_case)
          _UpperCAmelCase       :       Tuple							   =outputs.logits

          # verify the logits
          _UpperCAmelCase       :       str							   =torch.Size((1, 2_1, 6_5, 6_5))
          self.assertEqual(logits.shape  ,							snake_case)

          _UpperCAmelCase       :       Optional[Any]							   =torch.tensor(
              [
                  [[17.57_90, 17.75_81, 18.33_55], [18.32_57, 18.42_30, 18.89_73], [18.61_69, 18.86_50, 19.21_87]],
                  [[-2.15_95, -2.09_77, -2.37_41], [-2.42_26, -2.30_28, -2.68_35], [-2.78_19, -2.59_91, -2.77_06]],
                  [[4.20_58, 4.83_17, 4.76_38], [4.41_36, 5.03_61, 4.93_83], [4.50_28, 4.96_44, 4.87_34]],
              ]  ,							device=snake_case  ,							)

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





'''simple docstring'''






from string import ascii_uppercase

lowercase							={str(ord(c) - 55): c for c in ascii_uppercase}





def lowerCamelCase__      (						__lowerCamelCase	:							int	,   __lowerCamelCase	:							int							):







   '''simple docstring'''




   if isinstance(__lowerCamelCase	,   __lowerCamelCase							):
      raise TypeError('int() can\'t convert non-string with explicit base'							)
   if num < 0:
      raise ValueError('parameter must be positive int'							)
   if isinstance(__lowerCamelCase	,   __lowerCamelCase							):
      raise TypeError('\'str\' object cannot be interpreted as an integer'							)
   if isinstance(__lowerCamelCase	,   __lowerCamelCase							):
      raise TypeError('\'float\' object cannot be interpreted as an integer'							)
   if base in (0, 1):
      raise ValueError('base must be >= 2'							)
   if base > 3_6:
      raise ValueError('base must be <= 36'							)
   _UpperCAmelCase       :       Union[str, Any]							   =''
   _UpperCAmelCase       :       Optional[int]							   =0
   _UpperCAmelCase       :       str							   =0
   while div != 1:
      _UpperCAmelCase   ,	_UpperCAmelCase       :       int							   =divmod(__lowerCamelCase	,   __lowerCamelCase							)
      if base >= 1_1 and 9 < mod < 3_6:
         _UpperCAmelCase       :       str							   =ALPHABET_VALUES[str(__lowerCamelCase							)]
      else:
         _UpperCAmelCase       :       Any							   =str(__lowerCamelCase							)
      new_value += actual_value
      _UpperCAmelCase       :       Union[str, Any]							   =num // base
      _UpperCAmelCase       :       Dict							   =div
      if div == 0:
         return str(new_value[::-1]							)
      elif div == 1:
         new_value += str(__lowerCamelCase							)
         return str(new_value[::-1]							)

   return new_value[::-1]


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 for base in range(2, 37):
  for num in range(1000):
   assert int(decimal_to_any(num, base), base) == num, (
       num,
       base,
       decimal_to_any(num, base),
       int(decimal_to_any(num, base), base),
   )

