






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 							__snake_case(       unittest.TestCase   ):



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

    """simple docstring"""






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



 def     UpperCAmelCase     (						self : Any						):

    """simple docstring"""






    lowercase__    :						str       =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

    lowercase__    :						str       =			self.get_config()

    return config, pixel_values



 def     UpperCAmelCase     (						self : 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			,		image_size=self.image_size			,		)



 def     UpperCAmelCase     (						self : Dict			,		UpperCAmelCase__ : Union[str, Any]			,		UpperCAmelCase__ : Optional[Any]						):

    """simple docstring"""






    lowercase__    :						str       =			FlaxResNetModel(config=UpperCAmelCase__						)
    lowercase__    :						int       =			model(UpperCAmelCase__						)

    # 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     UpperCAmelCase     (						self : str			,		UpperCAmelCase__ : List[str]			,		UpperCAmelCase__ : Optional[int]						):

    """simple docstring"""






    lowercase__    :						List[str]       =			self.num_labels
    lowercase__    :						Optional[int]       =			FlaxResNetForImageClassification(config=UpperCAmelCase__						)
    lowercase__    :						Dict       =			model(UpperCAmelCase__						)
    self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.num_labels)						)







 def     UpperCAmelCase     (						self : Dict						):

    """simple docstring"""






    lowercase__    :						Tuple       =			self.prepare_config_and_inputs()
    lowercase__  ,       lowercase__    :						List[str]       =			config_and_inputs
    lowercase__    :						Optional[Any]       =			{"""pixel_values""": pixel_values}
    return config, inputs_dict
@require_flax
class 							__snake_case(       __UpperCAmelCase  ,							unittest.TestCase   ):
 lowercase			:							Dict		   =		(FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

 lowercase			:							Tuple		   =		False
 lowercase			:							Optional[Any]		   =		False
 lowercase			:							Union[str, Any]		   =		False



 def     UpperCAmelCase     (						self : Optional[Any]						):

    """simple docstring"""






    lowercase__    :						Any       =			FlaxResNetModelTester(self						)
    lowercase__    :						Optional[int]       =			ConfigTester(self			,		config_class=UpperCAmelCase__			,		has_text_modality=UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : int						):

    """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 : List[str]						):

    """simple docstring"""






    return



 def     UpperCAmelCase     (						self : List[Any]						):

    """simple docstring"""






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



 def     UpperCAmelCase     (						self : Tuple						):

    """simple docstring"""






    lowercase__    :						Tuple       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__						)



 @unittest.skip(reason="""ResNet does not use inputs_embeds"""						)
 def     UpperCAmelCase     (						self : List[str]						):

    """simple docstring"""






    pass



 @unittest.skip(reason="""ResNet does not support input and output embeddings"""						)
 def     UpperCAmelCase     (						self : int						):

    """simple docstring"""






    pass



 def     UpperCAmelCase     (						self : Tuple						):

    """simple docstring"""






    lowercase__  ,       lowercase__    :						Optional[Any]       =			self.model_tester.prepare_config_and_inputs_for_common()

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

       lowercase__    :						str       =			["""pixel_values"""]
       self.assertListEqual(arg_names[:1]			,		UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : str						):

    """simple docstring"""






    def check_hidden_states_output(UpperCAmelCase__ : str			,		UpperCAmelCase__ : Optional[int]			,		UpperCAmelCase__ : Tuple						):
       lowercase__    :						Dict       =			model_class(UpperCAmelCase__						)

       lowercase__    :						List[Any]       =			model(**self._prepare_for_class(UpperCAmelCase__			,		UpperCAmelCase__						)						)

       lowercase__    :						Tuple       =			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

       lowercase__    :						Tuple       =			self.model_tester.num_stages
       self.assertEqual(len(UpperCAmelCase__						)			,		expected_num_stages + 1						)



 @unittest.skip(reason="""ResNet does not use feedforward chunking"""						)
 def     UpperCAmelCase     (						self : int						):

    """simple docstring"""






    pass







 def     UpperCAmelCase     (						self : List[Any]						):

    """simple docstring"""






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

    for model_class in self.all_model_classes:
       with self.subTest(model_class.__name__						):
          lowercase__    :						Any       =			self._prepare_for_class(UpperCAmelCase__			,		UpperCAmelCase__						)
          lowercase__    :						Optional[Any]       =			model_class(UpperCAmelCase__						)

          @jax.jit
          def model_jitted(UpperCAmelCase__ : Optional[int]			,		**UpperCAmelCase__ : Optional[int]						):
             return model(pixel_values=UpperCAmelCase__			,		**UpperCAmelCase__						)

          with self.subTest("""JIT Enabled"""						):
             lowercase__    :						Union[str, Any]       =			model_jitted(**UpperCAmelCase__						).to_tuple()

          with self.subTest("""JIT Disabled"""						):
             with jax.disable_jit():
                lowercase__    :						Any       =			model_jitted(**UpperCAmelCase__						).to_tuple()

          self.assertEqual(len(UpperCAmelCase__						)			,		len(UpperCAmelCase__						)						)
          for jitted_output, output in zip(UpperCAmelCase__			,		UpperCAmelCase__						):
             self.assertEqual(jitted_output.shape			,		output.shape						)
def    a__    (	):
   lowercase__    :						Dict       =			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
   return image






@require_flax
class 							__snake_case(       unittest.TestCase   ):



 @cached_property
 def     UpperCAmelCase     (						self : Tuple						):

    """simple docstring"""






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







 @slow
 def     UpperCAmelCase     (						self : Tuple						):

    """simple docstring"""






    lowercase__    :						Dict       =			FlaxResNetForImageClassification.from_pretrained("""microsoft/resnet-50"""						)

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

    lowercase__    :						int       =			model(**UpperCAmelCase__						)

    # verify the logits
    lowercase__    :						List[str]       =			(1, 1_000)
    self.assertEqual(outputs.logits.shape			,		UpperCAmelCase__						)

    lowercase__    :						Tuple       =			jnp.array([-11.1_069, -9.7_877, -8.3_777]						)

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









import warnings

from ...utils import logging
from .image_processing_mobilenet_va import MobileNetVaImageProcessor


lowercase     :			str     						=	logging.get_logger(__name__)

class 			__A(    __UpperCAmelCase			):

     def __init__(   self,							*A,							**A   ):





            """simple docstring"""

            warnings.warn(
                '''The class MobileNetV2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
                ''' Please use MobileNetV2ImageProcessor instead.''',							A,							)
            super().__init__(*A,							**A   )






