



"""simple docstring"""
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 							lowerCamelCase_:





					'''simple docstring'''



					def __init__(    self				,       lowerCamelCase__				,       lowerCamelCase__=1_3				,       lowerCamelCase__=3_0				,       lowerCamelCase__=2				,       lowerCamelCase__=3				,       lowerCamelCase__=True				,       lowerCamelCase__=True				,       lowerCamelCase__=3_2				,       lowerCamelCase__=2				,       lowerCamelCase__=4				,       lowerCamelCase__=3_7				,       lowerCamelCase__="gelu"				,       lowerCamelCase__=0.1				,       lowerCamelCase__=0.1				,       lowerCamelCase__=1_0				,       lowerCamelCase__=0.0_2				,       lowerCamelCase__=3				,       lowerCamelCase__=None				,       ):
									_lowerCamelCase        =				parent
									_lowerCamelCase        =				batch_size
									_lowerCamelCase        =				image_size
									_lowerCamelCase        =				patch_size
									_lowerCamelCase        =				num_channels
									_lowerCamelCase        =				is_training
									_lowerCamelCase        =				use_labels
									_lowerCamelCase        =				hidden_size
									_lowerCamelCase        =				num_hidden_layers
									_lowerCamelCase        =				num_attention_heads
									_lowerCamelCase        =				intermediate_size
									_lowerCamelCase        =				hidden_act
									_lowerCamelCase        =				hidden_dropout_prob
									_lowerCamelCase        =				attention_probs_dropout_prob
									_lowerCamelCase        =				type_sequence_label_size
									_lowerCamelCase        =				initializer_range
									_lowerCamelCase        =				scope

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

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

									_lowerCamelCase        =				self.get_config()

									return config, pixel_values, labels

					def 			snake_case__      (    self   ):
									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=lowerCamelCase__				,       initializer_range=self.initializer_range				,       )

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									_lowerCamelCase        =				TFViTModel(config=lowerCamelCase__   )
									_lowerCamelCase        =				model(lowerCamelCase__				,       training=lowerCamelCase__   )
									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.
									_lowerCamelCase        =				self.image_size // 2
									_lowerCamelCase        =				pixel_values[:, :, :image_size, :image_size]
									_lowerCamelCase        =				model(lowerCamelCase__				,       interpolate_pos_encoding=lowerCamelCase__				,       training=lowerCamelCase__   )
									_lowerCamelCase        =				(image_size // self.patch_size) ** 2 + 1
									self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, seq_length, self.hidden_size)   )

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									_lowerCamelCase        =				self.type_sequence_label_size
									_lowerCamelCase        =				TFViTForImageClassification(lowerCamelCase__   )
									_lowerCamelCase        =				model(lowerCamelCase__				,       labels=lowerCamelCase__				,       training=lowerCamelCase__   )
									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.
									_lowerCamelCase        =				self.image_size // 2
									_lowerCamelCase        =				pixel_values[:, :, :image_size, :image_size]
									_lowerCamelCase        =				model(lowerCamelCase__				,       interpolate_pos_encoding=lowerCamelCase__				,       training=lowerCamelCase__   )
									self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.type_sequence_label_size)   )

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





					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.prepare_config_and_inputs()
									_lowerCamelCase  ,     _lowerCamelCase  ,     _lowerCamelCase        =				config_and_inputs
									_lowerCamelCase        =				{'''pixel_values''': pixel_values}
									return config, inputs_dict


@require_tf
class 							lowerCamelCase_(   A__,				A__,				unittest.TestCase    ):





					'''simple docstring'''



					lowercase__   :       Dict										= (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
					lowercase__   :       Union[str, Any]										= (
					    {'feature-extraction': TFViTModel, 'image-classification': TFViTForImageClassification}
					    if is_tf_available()
					    else {}
					)

					lowercase__   :       List[str]										= False
					lowercase__   :       int										= False
					lowercase__   :       Any										= False

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				TFViTModelTester(self   )
									_lowerCamelCase        =				ConfigTester(self				,       config_class=lowerCamelCase__				,       has_text_modality=lowerCamelCase__				,       hidden_size=3_7   )

					def 			snake_case__      (    self   ):
									self.config_tester.run_common_tests()

					@unittest.skip(reason='''ViT does not use inputs_embeds'''   )
					def 			snake_case__      (    self   ):
									pass

					@unittest.skip(reason='''ViT does not use inputs_embeds'''   )
					def 			snake_case__      (    self   ):
									pass

					def 			snake_case__      (    self   ):
									_lowerCamelCase  ,     _lowerCamelCase        =				self.model_tester.prepare_config_and_inputs_for_common()

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

					def 			snake_case__      (    self   ):
									_lowerCamelCase  ,     _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.call   )
													# 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 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__   )





					@slow
					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				TFViTModel.from_pretrained('''google/vit-base-patch16-224'''   )
									self.assertIsNotNone(lowerCamelCase__   )



def        lowerCAmelCase_(   )	->       int:
				_lowerCamelCase        =				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
				return image




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





					'''simple docstring'''



					@cached_property
					def 			snake_case__      (    self   ):
									return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''   ) if is_vision_available() else None





					@slow
					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''   )

									_lowerCamelCase        =				self.default_image_processor
									_lowerCamelCase        =				prepare_img()
									_lowerCamelCase        =				image_processor(images=lowerCamelCase__				,       return_tensors='''tf'''   )

									# forward pass
									_lowerCamelCase        =				model(**lowerCamelCase__   )

									# verify the logits
									_lowerCamelCase        =				tf.TensorShape((1, 1_0_0_0)   )
									self.assertEqual(outputs.logits.shape				,       lowerCamelCase__   )

									_lowerCamelCase        =				tf.constant([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]   )

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





"""simple docstring"""
import numpy as np



def        lowerCAmelCase_(   lowercase_     :							np.array							)	->       np.array:
				return 1 / (1 + np.exp(-vector							))



def        lowerCAmelCase_(   lowercase_     :							np.array							)	->       np.array:
				return vector * sigmoid(1.7_0_2 * vector							)


if __name__ == "__main__":
			import doctest

			doctest.testmod()
