



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 			__lowerCAmelCase  (						__UpperCAmelCase ):




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     self.config_class(**self.inputs_dict					)
    self.parent.assertTrue(hasattr(lowerCamelCase__						,					'''tf_padding'''					)					)
    self.parent.assertTrue(hasattr(lowerCamelCase__						,					'''depth_multiplier'''					)					)



class 			__lowerCAmelCase  :




 def __init__(    self						,					__UpperCAmelCase						,					__UpperCAmelCase=13						,					__UpperCAmelCase=3						,					__UpperCAmelCase=32						,					__UpperCAmelCase=0.25						,					__UpperCAmelCase=8						,					__UpperCAmelCase=8						,					__UpperCAmelCase=6						,					__UpperCAmelCase=32						,					__UpperCAmelCase=True						,					__UpperCAmelCase=True						,					__UpperCAmelCase=True						,					__UpperCAmelCase="relu6"						,					__UpperCAmelCase=1280						,					__UpperCAmelCase=0.1						,					__UpperCAmelCase=0.02						,					__UpperCAmelCase=True						,					__UpperCAmelCase=True						,					__UpperCAmelCase=10						,					__UpperCAmelCase=None						,					):



    '''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 							lowerCamelCase   (    self					):



    '''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 							lowerCamelCase   (    self					):



    '''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						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase					):



    '''simple docstring'''
    __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 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase					):



    '''simple docstring'''
    __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 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase					):



    '''simple docstring'''
    __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 							lowerCamelCase   (    self					):



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



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

 lowerCAmelCase__							       =	False
 lowerCAmelCase__							       =	False
 lowerCAmelCase__							       =	False
 lowerCAmelCase__							       =	False




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     MobileNetVaModelTester(self					)
    __lowerCamelCase    				=     MobileNetVaConfigTester(self						,					config_class=lowerCamelCase__						,					has_text_modality=lowerCamelCase__					)




 def 							lowerCamelCase   (    self					):



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




 @unittest.skip(reason='''MobileNetV2 does not use inputs_embeds'''					)
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    pass




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



    '''simple docstring'''
    pass




 @unittest.skip(reason='''MobileNetV2 does not output attentions'''					)
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    pass




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __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 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*lowerCamelCase__					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    def check_hidden_states_output(__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase					):
       __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 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase__					)




 @slow
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowerCamelCase    				=     MobileNetVaModel.from_pretrained(lowerCamelCase__					)
       self.assertIsNotNone(lowerCamelCase__					)





def 		a__      (		):
   __lowerCamelCase    				=     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
   return image





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




 @cached_property
 def 							lowerCamelCase   (    self					):



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




 @slow
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __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, 1001)					)
    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 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __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"""


def    _lowerCamelCase   (     lowerCamelCase__	:							str  ):
 assert column_title.isupper()
 lowercase__       :       int				=						0
 lowercase__       :       Optional[int]				=						len(lowerCamelCase__  ) - 1
 lowercase__       :       int				=						0

 while index >= 0:
  lowercase__       :       List[str]				=						(ord(column_title[index]  ) - 64) * pow(26       ,		lowerCamelCase__  )
  answer += value
  power += 1
  index -= 1

 return answer


if __name__ == "__main__":
   from doctest import testmod

   testmod()