


'''simple docstring'''







import inspect
import unittest

from transformers import ResNetConfig
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_backbone_common import BackboneTesterMixin
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 torch import nn

   from transformers import ResNetBackbone, ResNetForImageClassification, ResNetModel
   from transformers.models.resnet.modeling_resnet import RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor







class A__	:






      def __init__(			self      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :Optional[Any]=3					,						SCREAMING_SNAKE_CASE      :List[Any]=3_2					,						SCREAMING_SNAKE_CASE      :str=3					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=1_0					,						SCREAMING_SNAKE_CASE      :List[str]=[1_0, 2_0, 3_0, 4_0]					,						SCREAMING_SNAKE_CASE      :Any=[1, 1, 2, 1]					,						SCREAMING_SNAKE_CASE      :str=True					,						SCREAMING_SNAKE_CASE      :Optional[Any]=True					,						SCREAMING_SNAKE_CASE      :Optional[int]="relu"					,						SCREAMING_SNAKE_CASE      :List[str]=3					,						SCREAMING_SNAKE_CASE      :int=None					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=["stage2", "stage3", "stage4"]					,						SCREAMING_SNAKE_CASE      :List[str]=[2, 3, 4]					,						)	-> Union[str, Any]:




             '''simple docstring'''
             _a   :			Optional[int]					 =parent
             _a   :			Dict					 =batch_size
             _a   :			Union[str, Any]					 =image_size
             _a   :			Optional[Any]					 =num_channels
             _a   :			Optional[int]					 =embeddings_size
             _a   :			Any					 =hidden_sizes
             _a   :			Optional[Any]					 =depths
             _a   :			Dict					 =is_training
             _a   :			Any					 =use_labels
             _a   :			Dict					 =hidden_act
             _a   :			int					 =num_labels
             _a   :			Union[str, Any]					 =scope
             _a   :			Any					 =len(_snake_case     )
             _a   :			List[str]					 =out_features
             _a   :			Optional[int]					 =out_indices






      def __UpperCAmelCase	(			self      :Tuple     )	-> Tuple:




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

             _a   :			Tuple					 =None
             if self.use_labels:
                    _a   :			Union[str, Any]					 =ids_tensor([self.batch_size]					,						self.num_labels     )

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

             return config, pixel_values, labels






      def __UpperCAmelCase	(			self      :Union[str, Any]     )	-> Optional[int]:




             '''simple docstring'''
             return ResNetConfig(
                 num_channels=self.num_channels					,						embeddings_size=self.embeddings_size					,						hidden_sizes=self.hidden_sizes					,						depths=self.depths					,						hidden_act=self.hidden_act					,						num_labels=self.num_labels					,						out_features=self.out_features					,						out_indices=self.out_indices					,						)






      def __UpperCAmelCase	(			self      :Optional[int]					,						SCREAMING_SNAKE_CASE      :List[Any]					,						SCREAMING_SNAKE_CASE      :Optional[int]					,						SCREAMING_SNAKE_CASE      :str     )	-> int:




             '''simple docstring'''
             _a   :			Dict					 =ResNetModel(config=_snake_case     )
             model.to(_snake_case     )
             model.eval()
             _a   :			Tuple					 =model(_snake_case     )
             # expected last hidden states: B, C, H // 32, W // 32
             self.parent.assertEqual(
                 result.last_hidden_state.shape					,						(self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2)					,						)






      def __UpperCAmelCase	(			self      :Tuple					,						SCREAMING_SNAKE_CASE      :Any					,						SCREAMING_SNAKE_CASE      :Optional[Any]					,						SCREAMING_SNAKE_CASE      :Tuple     )	-> List[str]:




             '''simple docstring'''
             _a   :			Dict					 =self.num_labels
             _a   :			List[str]					 =ResNetForImageClassification(_snake_case     )
             model.to(_snake_case     )
             model.eval()
             _a   :			List[str]					 =model(_snake_case					,						labels=_snake_case     )
             self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_labels)     )






      def __UpperCAmelCase	(			self      :Any					,						SCREAMING_SNAKE_CASE      :List[Any]					,						SCREAMING_SNAKE_CASE      :Any					,						SCREAMING_SNAKE_CASE      :Tuple     )	-> int:




             '''simple docstring'''
             _a   :			List[str]					 =ResNetBackbone(config=_snake_case     )
             model.to(_snake_case     )
             model.eval()
             _a   :			Tuple					 =model(_snake_case     )

             # verify feature maps
             self.parent.assertEqual(len(result.feature_maps     )					,						len(config.out_features     )     )
             self.parent.assertListEqual(list(result.feature_maps[0].shape     )					,						[self.batch_size, self.hidden_sizes[1], 4, 4]     )

             # verify channels
             self.parent.assertEqual(len(model.channels     )					,						len(config.out_features     )     )
             self.parent.assertListEqual(model.channels					,						config.hidden_sizes[1:]     )

             # verify backbone works with out_features=None
             _a   :			Optional[Any]					 =None
             _a   :			int					 =ResNetBackbone(config=_snake_case     )
             model.to(_snake_case     )
             model.eval()
             _a   :			List[str]					 =model(_snake_case     )

             # verify feature maps
             self.parent.assertEqual(len(result.feature_maps     )					,						1     )
             self.parent.assertListEqual(list(result.feature_maps[0].shape     )					,						[self.batch_size, self.hidden_sizes[-1], 1, 1]     )

             # verify channels
             self.parent.assertEqual(len(model.channels     )					,						1     )
             self.parent.assertListEqual(model.channels					,						[config.hidden_sizes[-1]]     )






      def __UpperCAmelCase	(			self      :Tuple     )	-> Dict:




             '''simple docstring'''
             _a   :			str					 =self.prepare_config_and_inputs()
             _a   :			List[Any]					 =config_and_inputs
             _a   :			Any					 ={'pixel_values': pixel_values}
             return config, inputs_dict







@require_torch
class A__	(						lowerCAmelCase_					,					lowerCAmelCase_					,					unittest.TestCase				):
      __UpperCamelCase	:  Optional[int]     	=							(
          (
              ResNetModel,
              ResNetForImageClassification,
              ResNetBackbone,
          )
          if is_torch_available()
          else ()
      )
      __UpperCamelCase	:  Union[str, Any]     	=							(
          {"feature-extraction": ResNetModel, "image-classification": ResNetForImageClassification}
          if is_torch_available()
          else {}
      )

      __UpperCamelCase	:  Any     	=							True
      __UpperCamelCase	:  List[Any]     	=							False
      __UpperCamelCase	:  int     	=							False
      __UpperCamelCase	:  str     	=							False
      __UpperCamelCase	:  List[str]     	=							False






      def __UpperCAmelCase	(			self      :int     )	-> Optional[Any]:




             '''simple docstring'''
             _a   :			Optional[int]					 =ResNetModelTester(self     )
             _a   :			str					 =ConfigTester(self					,						config_class=_snake_case					,						has_text_modality=_snake_case     )






      def __UpperCAmelCase	(			self      :str     )	-> Optional[Any]:




             '''simple docstring'''
             self.create_and_test_config_common_properties()
             self.config_tester.create_and_test_config_to_json_string()
             self.config_tester.create_and_test_config_to_json_file()
             self.config_tester.create_and_test_config_from_and_save_pretrained()
             self.config_tester.create_and_test_config_with_num_labels()
             self.config_tester.check_config_can_be_init_without_params()
             self.config_tester.check_config_arguments_init()






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




             '''simple docstring'''
             return






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




             '''simple docstring'''
             pass






      @unittest.skip(reason="""ResNet does not support input and output embeddings"""     )
      def __UpperCAmelCase	(			self      :List[Any]     )	-> Tuple:




             '''simple docstring'''
             pass






      def __UpperCAmelCase	(			self      :str     )	-> Dict:




             '''simple docstring'''
             _a   :			int					 =self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    _a   :			Optional[int]					 =model_class(_snake_case     )
                    _a   :			Dict					 =inspect.signature(model.forward     )
                    # signature.parameters is an OrderedDict => so arg_names order is deterministic
                    _a   :			str					 =[*signature.parameters.keys()]

                    _a   :			Union[str, Any]					 =['pixel_values']
                    self.assertListEqual(arg_names[:1]					,						_snake_case     )






      def __UpperCAmelCase	(			self      :Dict     )	-> Optional[int]:




             '''simple docstring'''
             _a   :			int					 =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*_snake_case     )






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




             '''simple docstring'''
             _a   :			int					 =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_backbone(*_snake_case     )






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




             '''simple docstring'''
             _a   :			Tuple					 =self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    _a   :			int					 =model_class(config=_snake_case     )
                    for name, module in model.named_modules():
                           if isinstance(_snake_case					,						(nn.BatchNormad, nn.GroupNorm)     ):
                                  self.assertTrue(
                                      torch.all(module.weight == 1     )					,						msg=f"Parameter {name} of model {model_class} seems not properly initialized"					,						)
                                  self.assertTrue(
                                      torch.all(module.bias == 0     )					,						msg=f"Parameter {name} of model {model_class} seems not properly initialized"					,						)






      def __UpperCAmelCase	(			self      :Any     )	-> int:




             '''simple docstring'''
             def check_hidden_states_output(SCREAMING_SNAKE_CASE      :Optional[Any]					,						SCREAMING_SNAKE_CASE      :int					,						SCREAMING_SNAKE_CASE      :List[str]     ):
                    _a   :			Optional[Any]					 =model_class(_snake_case     )
                    model.to(_snake_case     )
                    model.eval()

                    with torch.no_grad():
                           _a   :			Any					 =model(**self._prepare_for_class(_snake_case					,						_snake_case     )     )

                    _a   :			Optional[Any]					 =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                    _a   :			Optional[Any]					 =self.model_tester.num_stages
                    self.assertEqual(len(_snake_case     )					,						expected_num_stages + 1     )

                    # ResNet's feature maps are of shape (batch_size, num_channels, height, width)
                    self.assertListEqual(
                        list(hidden_states[0].shape[-2:]     )					,						[self.model_tester.image_size // 4, self.model_tester.image_size // 4]					,						)

             _a   :			Any					 =self.model_tester.prepare_config_and_inputs_for_common()
             _a   :			Union[str, Any]					 =['basic', 'bottleneck']
             for model_class in self.all_model_classes:
                    for layer_type in layers_type:
                           _a   :			str					 =layer_type
                           _a   :			List[Any]					 =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"]
                           _a   :			List[str]					 =True

                           check_hidden_states_output(_snake_case					,						_snake_case					,						_snake_case     )






      @unittest.skip(reason="""ResNet does not use feedforward chunking"""     )
      def __UpperCAmelCase	(			self      :Union[str, Any]     )	-> Tuple:




             '''simple docstring'''
             pass






      def __UpperCAmelCase	(			self      :Dict     )	-> int:




             '''simple docstring'''
             _a   :			Any					 =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*_snake_case     )






      @slow
      def __UpperCAmelCase	(			self      :str     )	-> Optional[int]:




             '''simple docstring'''
             for model_name in RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _a   :			Optional[int]					 =ResNetModel.from_pretrained(_snake_case     )
                    self.assertIsNotNone(_snake_case     )


def      SCREAMING_SNAKE_CASE_ (  )  ->      str:
       _a   :			List[str]					 =Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
       return image







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






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




             '''simple docstring'''
             return (
                 AutoImageProcessor.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]     )
                 if is_vision_available()
                 else None
             )






      @slow
      def __UpperCAmelCase	(			self      :List[str]     )	-> List[str]:




             '''simple docstring'''
             _a   :			Tuple					 =ResNetForImageClassification.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]     ).to(_snake_case     )

             _a   :			Tuple					 =self.default_image_processor
             _a   :			Union[str, Any]					 =prepare_img()
             _a   :			Tuple					 =image_processor(images=_snake_case					,						return_tensors="""pt"""     ).to(_snake_case     )

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

             # verify the logits
             _a   :			Union[str, Any]					 =torch.Size((1, 1_0_0_0)     )
             self.assertEqual(outputs.logits.shape					,						_snake_case     )

             _a   :			Optional[Any]					 =torch.tensor([-1_1.1_0_6_9, -9.7_877, -8.3_777]     ).to(_snake_case     )

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







@require_torch
class A__	(						lowerCAmelCase_					,					unittest.TestCase				):
      __UpperCamelCase	:  List[Any]     	=							(ResNetBackbone,) if is_torch_available() else ()
      __UpperCamelCase	:  Optional[int]     	=							False
      __UpperCamelCase	:  Dict     	=							ResNetConfig






      def __UpperCAmelCase	(			self      :List[str]     )	-> Optional[Any]:




             '''simple docstring'''
             _a   :			Dict					 =ResNetModelTester(self     )






'''simple docstring'''







import math


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     int  )  ->      int:
       return sum(int(_UpperCAmelCase  ) for digit in str(_UpperCAmelCase  )  )


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     int = 30  )  ->      int:
       _a   :			List[Any]					 =[]
       for digit in range(2   ,100  ):
              for power in range(2   ,100  ):
                     _a   :			Optional[Any]					 =int(math.pow(_UpperCAmelCase   ,_UpperCAmelCase  )  )
                     if digit == digit_sum(_UpperCAmelCase  ):
                            digit_to_powers.append(_UpperCAmelCase  )

       digit_to_powers.sort()
       return digit_to_powers[n - 1]


if __name__ == "__main__":
   print(solution())


