






# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__a      :       Union[str, Any]				  =   {
    "configuration_efficientnet": [
        "EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "EfficientNetConfig",
        "EfficientNetOnnxConfig",
    ]
}

try:
  if not is_vision_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __a      :       Union[str, Any]				  =   ["EfficientNetImageProcessor"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __a      :       Tuple				  =   [
      "EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST",
      "EfficientNetForImageClassification",
      "EfficientNetModel",
      "EfficientNetPreTrainedModel",
  ]

if TYPE_CHECKING:
  from .configuration_efficientnet import (
      EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP,
      EfficientNetConfig,
      EfficientNetOnnxConfig,
  )

  try:
    if not is_vision_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .image_processing_efficientnet import EfficientNetImageProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_efficientnet import (
        EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        EfficientNetForImageClassification,
        EfficientNetModel,
        EfficientNetPreTrainedModel,
    )

else:
  import sys

  __a      :       int				  =   _LazyModule(__name__, globals()["__file__"], _import_structure)












from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
  from PIL import Image

  from transformers import ViTImageProcessor


class       __lowercase      :







     '''simple docstring'''




     def __init__(				self       :			Tuple ,	UpperCamelCase_       :			Tuple ,	UpperCamelCase_       :			Union[str, Any]=13 ,	UpperCamelCase_       :			Optional[Any]=30 ,	UpperCamelCase_       :			Optional[int]=2 ,	UpperCamelCase_       :			Optional[Any]=3 ,	UpperCamelCase_       :			int=True ,	UpperCamelCase_       :			str=True ,	UpperCamelCase_       :			str=32 ,	UpperCamelCase_       :			Tuple=2 ,	UpperCamelCase_       :			List[Any]=4 ,	UpperCamelCase_       :			List[str]=37 ,	UpperCamelCase_       :			Any="gelu" ,	UpperCamelCase_       :			Any=0.1 ,	UpperCamelCase_       :			Optional[Any]=0.1 ,	UpperCamelCase_       :			Any=10 ,	UpperCamelCase_       :			str=0.02 ,	UpperCamelCase_       :			Dict=3 ,	UpperCamelCase_       :			int=None ,	):


         """simple docstring"""



         __A         =	parent
         __A         =	batch_size
         __A         =	image_size
         __A         =	patch_size
         __A         =	num_channels
         __A         =	is_training
         __A         =	use_labels
         __A         =	hidden_size
         __A         =	num_hidden_layers
         __A         =	num_attention_heads
         __A         =	intermediate_size
         __A         =	hidden_act
         __A         =	hidden_dropout_prob
         __A         =	attention_probs_dropout_prob
         __A         =	type_sequence_label_size
         __A         =	initializer_range
         __A         =	scope

         # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
         __A         =	(image_size // patch_size) ** 2
         __A         =	num_patches + 1




     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         __A         =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

         __A         =	None
         if self.use_labels:
             __A         =	ids_tensor([self.batch_size] ,	self.type_sequence_label_size				)

         __A         =	self.get_config()

         return config, pixel_values, labels




     def 		lowerCAmelCase_     (				self       :			int				):


         """simple docstring"""



         return ViTConfig(
             image_size=self.image_size ,	patch_size=self.patch_size ,	num_channels=self.num_channels ,	hidden_size=self.hidden_size ,	num_hidden_layers=self.num_hidden_layers ,	num_attention_heads=self.num_attention_heads ,	intermediate_size=self.intermediate_size ,	hidden_act=self.hidden_act ,	hidden_dropout_prob=self.hidden_dropout_prob ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob ,	is_decoder=UpperCamelCase_ ,	initializer_range=self.initializer_range ,	)




     def 		lowerCAmelCase_     (				self       :			Tuple ,	UpperCamelCase_       :			int ,	UpperCamelCase_       :			List[str] ,	UpperCamelCase_       :			Optional[int]				):


         """simple docstring"""



         __A         =	TFViTModel(config=UpperCamelCase_				)
         __A         =	model(UpperCamelCase_ ,	training=UpperCamelCase_				)
         self.parent.assertEqual(result.last_hidden_state.shape ,	(self.batch_size, self.seq_length, self.hidden_size)				)

         # Test with an image with different size than the one specified in config.
         __A         =	self.image_size // 2
         __A         =	pixel_values[:, :, :image_size, :image_size]
         __A         =	model(UpperCamelCase_ ,	interpolate_pos_encoding=UpperCamelCase_ ,	training=UpperCamelCase_				)
         __A         =	(image_size // self.patch_size) ** 2 + 1
         self.parent.assertEqual(result.last_hidden_state.shape ,	(self.batch_size, seq_length, self.hidden_size)				)




     def 		lowerCAmelCase_     (				self       :			Tuple ,	UpperCamelCase_       :			Union[str, Any] ,	UpperCamelCase_       :			Optional[Any] ,	UpperCamelCase_       :			List[Any]				):


         """simple docstring"""



         __A         =	self.type_sequence_label_size
         __A         =	TFViTForImageClassification(UpperCamelCase_				)
         __A         =	model(UpperCamelCase_ ,	labels=UpperCamelCase_ ,	training=UpperCamelCase_				)
         self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.type_sequence_label_size)				)

         # Test with an image with different size than the one specified in config.
         __A         =	self.image_size // 2
         __A         =	pixel_values[:, :, :image_size, :image_size]
         __A         =	model(UpperCamelCase_ ,	interpolate_pos_encoding=UpperCamelCase_ ,	training=UpperCamelCase_				)
         self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.type_sequence_label_size)				)

         # test greyscale images
         __A         =	1
         __A         =	TFViTForImageClassification(UpperCamelCase_				)
         __A         =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
         __A         =	model(UpperCamelCase_				)
         self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.type_sequence_label_size)				)





     def 		lowerCAmelCase_     (				self       :			Optional[int]				):


         """simple docstring"""



         __A         =	self.prepare_config_and_inputs()
         __A    ,	__A    ,	__A         =	config_and_inputs
         __A         =	{"""pixel_values""": pixel_values}
         return config, inputs_dict


@require_tf
class       __lowercase      (	lowercase_			,      lowercase_			,      unittest.TestCase  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
     SCREAMING_SNAKE_CASE   						=				(
         {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
         if is_tf_available()
         else {}
     )

     SCREAMING_SNAKE_CASE   						=				False
     SCREAMING_SNAKE_CASE   						=				False
     SCREAMING_SNAKE_CASE   						=				False




     def 		lowerCAmelCase_     (				self       :			List[Any]				):


         """simple docstring"""



         __A         =	TFViTModelTester(self				)
         __A         =	ConfigTester(self ,	config_class=UpperCamelCase_ ,	has_text_modality=UpperCamelCase_ ,	hidden_size=37				)




     def 		lowerCAmelCase_     (				self       :			Tuple				):


         """simple docstring"""



         self.config_tester.run_common_tests()




     @unittest.skip(reason="""ViT does not use inputs_embeds"""				)
     def 		lowerCAmelCase_     (				self       :			Dict				):


         """simple docstring"""



         pass




     @unittest.skip(reason="""ViT does not use inputs_embeds"""				)
     def 		lowerCAmelCase_     (				self       :			Any				):


         """simple docstring"""



         pass




     def 		lowerCAmelCase_     (				self       :			Tuple				):


         """simple docstring"""



         __A    ,	__A         =	self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             __A         =	model_class(UpperCamelCase_				)
             self.assertIsInstance(model.get_input_embeddings() ,	(tf.keras.layers.Layer)				)
             __A         =	model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(UpperCamelCase_ ,	tf.keras.layers.Layer				)				)




     def 		lowerCAmelCase_     (				self       :			List[str]				):


         """simple docstring"""



         __A    ,	__A         =	self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             __A         =	model_class(UpperCamelCase_				)
             __A         =	inspect.signature(model.call				)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             __A         =	[*signature.parameters.keys()]

             __A         =	["""pixel_values"""]
             self.assertListEqual(arg_names[:1] ,	UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         __A         =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			str				):


         """simple docstring"""



         __A         =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_				)





     @slow
     def 		lowerCAmelCase_     (				self       :			Any				):


         """simple docstring"""



         __A         =	TFViTModel.from_pretrained("""google/vit-base-patch16-224"""				)
         self.assertIsNotNone(UpperCamelCase_				)



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





    """simple docstring"""

    __A         =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
    return image






@require_tf
@require_vision
class       __lowercase      (	unittest.TestCase  ):







     '''simple docstring'''




     @cached_property
     def 		lowerCAmelCase_     (				self       :			List[Any]				):


         """simple docstring"""



         return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""				) if is_vision_available() else None





     @slow
     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         __A         =	TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""				)

         __A         =	self.default_image_processor
         __A         =	prepare_img()
         __A         =	image_processor(images=UpperCamelCase_ ,	return_tensors="""tf"""				)

         # forward pass
         __A         =	model(**UpperCamelCase_				)

         # verify the logits
         __A         =	tf.TensorShape((1, 1_000)				)
         self.assertEqual(outputs.logits.shape ,	UpperCamelCase_				)

         __A         =	tf.constant([-0.2744, 0.8215, -0.0836]				)

         tf.debugging.assert_near(outputs.logits[0, :3] ,	UpperCamelCase_ ,	atol=1e-4				)




