
"""simple docstring"""







import inspect
import unittest

from transformers import ResNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
       import jax
       import jax.numpy as jnp

       from transformers.models.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor



class 		UpperCAmelCase_	(unittest.TestCase						):
 """simple docstring"""




 def __init__( self       : Dict     ,   a_       : Any     ,   a_       : Optional[Any]=3     ,   a_       : int=32     ,   a_       : Optional[Any]=3     ,   a_       : Optional[int]=10     ,   a_       : List[Any]=[10, 20, 30, 40]     ,   a_       : List[str]=[1, 1, 2, 1]     ,   a_       : Optional[int]=True     ,   a_       : Union[str, Any]=True     ,   a_       : Tuple="relu"     ,   a_       : int=3     ,   a_       : Any=None     ,   )->					Optional[int]:



      """simple docstring"""

      UpperCAmelCase_						:    List[str]    					=  parent
      UpperCAmelCase_						:    List[Any]    					=  batch_size
      UpperCAmelCase_						:    Optional[int]    					=  image_size
      UpperCAmelCase_						:    List[str]    					=  num_channels
      UpperCAmelCase_						:    str    					=  embeddings_size
      UpperCAmelCase_						:    str    					=  hidden_sizes
      UpperCAmelCase_						:    Optional[int]    					=  depths
      UpperCAmelCase_						:    str    					=  is_training
      UpperCAmelCase_						:    Union[str, Any]    					=  use_labels
      UpperCAmelCase_						:    Union[str, Any]    					=  hidden_act
      UpperCAmelCase_						:    str    					=  num_labels
      UpperCAmelCase_						:    Union[str, Any]    					=  scope
      UpperCAmelCase_						:    int    					=  len(a_ )




 def   a      ( self       : int )->					Any:



      """simple docstring"""

      UpperCAmelCase_						:    Union[str, Any]    					=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

      UpperCAmelCase_						:    Dict    					=  self.get_config()

      return config, pixel_values




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



      """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     ,   image_size=self.image_size     ,   )




 def   a      ( self       : Union[str, Any]     ,   a_       : Union[str, Any]     ,   a_       : List[str] )->					int:



      """simple docstring"""

      UpperCAmelCase_						:    List[str]    					=  FlaxResNetModel(config=a_ )
      UpperCAmelCase_						:    Any    					=  model(a_ )

      # Output shape (b, c, h, w)
      self.parent.assertEqual(
          result.last_hidden_state.shape     ,   (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)     ,   )




 def   a      ( self       : Optional[int]     ,   a_       : str     ,   a_       : Dict )->					str:



      """simple docstring"""

      UpperCAmelCase_						:    str    					=  self.num_labels
      UpperCAmelCase_						:    Any    					=  FlaxResNetForImageClassification(config=a_ )
      UpperCAmelCase_						:    Dict    					=  model(a_ )
      self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.num_labels) )






 def   a      ( self       : Tuple )->					Any:



      """simple docstring"""

      UpperCAmelCase_						:    List[Any]    					=  self.prepare_config_and_inputs()
      UpperCAmelCase_					,UpperCAmelCase_						:    int    					=  config_and_inputs
      UpperCAmelCase_						:    str    					=  {"""pixel_values""": pixel_values}
      return config, inputs_dict



@require_flax
class 		UpperCAmelCase_	(lowerCamelCase_  , unittest.TestCase						):
 """simple docstring"""




 UpperCamelCase_						:     List[Any]				   =    (FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

 UpperCamelCase_						:     Any				   =    False
 UpperCamelCase_						:     Optional[int]				   =    False
 UpperCamelCase_						:     List[str]				   =    False




 def   a      ( self       : Dict )->					None:



      """simple docstring"""

      UpperCAmelCase_						:    Optional[Any]    					=  FlaxResNetModelTester(self )
      UpperCAmelCase_						:    Union[str, Any]    					=  ConfigTester(self     ,   config_class=a_     ,   has_text_modality=a_ )




 def   a      ( self       : int )->					Tuple:



      """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   a      ( self       : Tuple )->					Optional[Any]:



      """simple docstring"""

      return




 def   a      ( self       : Any )->					int:



      """simple docstring"""

      UpperCAmelCase_						:    Union[str, Any]    					=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*a_ )




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



      """simple docstring"""

      UpperCAmelCase_						:    Optional[Any]    					=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*a_ )




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



      """simple docstring"""

      pass




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



      """simple docstring"""

      pass




 def   a      ( self       : Dict )->					Dict:



      """simple docstring"""

      UpperCAmelCase_					,UpperCAmelCase_						:    Any    					=  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           UpperCAmelCase_						:    List[Any]    					=  model_class(a_ )
           UpperCAmelCase_						:    Optional[int]    					=  inspect.signature(model.__call__ )
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           UpperCAmelCase_						:    str    					=  [*signature.parameters.keys()]

           UpperCAmelCase_						:    Any    					=  ["""pixel_values"""]
           self.assertListEqual(arg_names[:1]     ,   a_ )




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



      """simple docstring"""

      def check_hidden_states_output(a_       : int     ,   a_       : Tuple     ,   a_       : Optional[Any] ):
           UpperCAmelCase_						:    Optional[Any]    					=  model_class(a_ )

           UpperCAmelCase_						:    Any    					=  model(**self._prepare_for_class(a_     ,   a_ ) )

           UpperCAmelCase_						:    str    					=  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

           UpperCAmelCase_						:    Optional[int]    					=  self.model_tester.num_stages
           self.assertEqual(len(a_ )     ,   expected_num_stages + 1 )




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



      """simple docstring"""

      pass






 def   a      ( self       : Dict )->					List[str]:



      """simple docstring"""

      UpperCAmelCase_					,UpperCAmelCase_						:    Tuple    					=  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           with self.subTest(model_class.__name__ ):
                UpperCAmelCase_						:    List[str]    					=  self._prepare_for_class(a_     ,   a_ )
                UpperCAmelCase_						:    int    					=  model_class(a_ )

                @jax.jit
                def model_jitted(a_       : Any     ,   **a_       : Any ):
                     return model(pixel_values=a_     ,   **a_ )

                with self.subTest("""JIT Enabled""" ):
                     UpperCAmelCase_						:    Tuple    					=  model_jitted(**a_ ).to_tuple()

                with self.subTest("""JIT Disabled""" ):
                     with jax.disable_jit():
                          UpperCAmelCase_						:    int    					=  model_jitted(**a_ ).to_tuple()

                self.assertEqual(len(a_ )     ,   len(a_ ) )
                for jitted_output, output in zip(a_     ,   a_ ):
                     self.assertEqual(jitted_output.shape     ,   output.shape )




def 			A_			(					)     ->							List[str]:


     """simple docstring"""







     UpperCAmelCase_						:    Optional[Any]    					=  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
     return image



@require_flax
class 		UpperCAmelCase_	(unittest.TestCase						):
 """simple docstring"""




 @cached_property
 def   a      ( self       : List[str] )->					Optional[Any]:



      """simple docstring"""

      return AutoImageProcessor.from_pretrained("""microsoft/resnet-50""" ) if is_vision_available() else None






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



      """simple docstring"""

      UpperCAmelCase_						:    Union[str, Any]    					=  FlaxResNetForImageClassification.from_pretrained("""microsoft/resnet-50""" )

      UpperCAmelCase_						:    str    					=  self.default_image_processor
      UpperCAmelCase_						:    Union[str, Any]    					=  prepare_img()
      UpperCAmelCase_						:    List[Any]    					=  image_processor(images=a_     ,   return_tensors="""np""" )

      UpperCAmelCase_						:    Dict    					=  model(**a_ )

      # verify the logits
      UpperCAmelCase_						:    Union[str, Any]    					=  (1, 10_00)
      self.assertEqual(outputs.logits.shape     ,   a_ )

      UpperCAmelCase_						:    Any    					=  jnp.array([-11.1069, -9.7877, -8.3777] )

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



"""simple docstring"""







import warnings

from .generation import GenerationMixin



class 		UpperCAmelCase_	(lowerCamelCase_						):
 """simple docstring"""




 warnings.warn(
     """Importing `GenerationMixin` from `src/transformers/generation_utils.py` is deprecated and will """
     """be removed in Transformers v5. Import as `from transformers import GenerationMixin` instead."""  , lowerCamelCase_  , )

