"""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 		__A							:






     def __init__(	self       ,  a__       ,  a__=13       ,  a__=30       ,  a__=2       ,  a__=3       ,  a__=True       ,  a__=True       ,  a__=32       ,  a__=2       ,  a__=4       ,  a__=37       ,  a__="gelu"       ,  a__=0.1       ,  a__=0.1       ,  a__=10       ,  a__=0.0_2       ,  a__=3       ,  a__=None       ,  ):
       _lowerCAmelCase		: int						    =  parent
       _lowerCAmelCase		: str						    =  batch_size
       _lowerCAmelCase		: List[str]						    =  image_size
       _lowerCAmelCase		: int						    =  patch_size
       _lowerCAmelCase		: Dict						    =  num_channels
       _lowerCAmelCase		: Dict						    =  is_training
       _lowerCAmelCase		: List[str]						    =  use_labels
       _lowerCAmelCase		: int						    =  hidden_size
       _lowerCAmelCase		: Optional[int]						    =  num_hidden_layers
       _lowerCAmelCase		: List[str]						    =  num_attention_heads
       _lowerCAmelCase		: List[str]						    =  intermediate_size
       _lowerCAmelCase		: Optional[Any]						    =  hidden_act
       _lowerCAmelCase		: List[str]						    =  hidden_dropout_prob
       _lowerCAmelCase		: int						    =  attention_probs_dropout_prob
       _lowerCAmelCase		: List[Any]						    =  type_sequence_label_size
       _lowerCAmelCase		: int						    =  initializer_range
       _lowerCAmelCase		: Union[str, Any]						    =  scope

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






     def 					__A						(	self						):
       _lowerCAmelCase		: Dict						    =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

       _lowerCAmelCase		: Union[str, Any]						    =  None
       if self.use_labels:
         _lowerCAmelCase		: Optional[int]						    =  ids_tensor([self.batch_size]       ,  self.type_sequence_label_size						)

       _lowerCAmelCase		: List[str]						    =  self.get_config()

       return config, pixel_values, labels






     def 					__A						(	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=a__       ,  initializer_range=self.initializer_range       ,  )






     def 					__A						(	self       ,  a__       ,  a__       ,  a__						):
       _lowerCAmelCase		: Optional[int]						    =  TFViTModel(config=a__						)
       _lowerCAmelCase		: Optional[Any]						    =  model(a__       ,  training=a__						)
       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		: str						    =  self.image_size // 2
       _lowerCAmelCase		: Optional[Any]						    =  pixel_values[:, :, :image_size, :image_size]
       _lowerCAmelCase		: List[str]						    =  model(a__       ,  interpolate_pos_encoding=a__       ,  training=a__						)
       _lowerCAmelCase		: int						    =  (image_size // self.patch_size) ** 2 + 1
       self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, seq_length, self.hidden_size)						)






     def 					__A						(	self       ,  a__       ,  a__       ,  a__						):
       _lowerCAmelCase		: Tuple						    =  self.type_sequence_label_size
       _lowerCAmelCase		: List[str]						    =  TFViTForImageClassification(a__						)
       _lowerCAmelCase		: Any						    =  model(a__       ,  labels=a__       ,  training=a__						)
       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		: Any						    =  self.image_size // 2
       _lowerCAmelCase		: int						    =  pixel_values[:, :, :image_size, :image_size]
       _lowerCAmelCase		: Dict						    =  model(a__       ,  interpolate_pos_encoding=a__       ,  training=a__						)
       self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.type_sequence_label_size)						)

       # test greyscale images
       _lowerCAmelCase		: Optional[int]						    =  1
       _lowerCAmelCase		: Any						    =  TFViTForImageClassification(a__						)
       _lowerCAmelCase		: Dict						    =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
       _lowerCAmelCase		: int						    =  model(a__						)
       self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.type_sequence_label_size)						)






     def 					__A						(	self						):
       _lowerCAmelCase		: str						    =  self.prepare_config_and_inputs()
       _lowerCAmelCase		: Tuple						    =  config_and_inputs
       _lowerCAmelCase		: Any						    =  {"""pixel_values""": pixel_values}
       return config, inputs_dict







@require_tf
class 		__A							(				SCREAMING_SNAKE_CASE_					,			SCREAMING_SNAKE_CASE_					,			unittest.TestCase	):
     _UpperCamelCase							:    Optional[Any]	      =	(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
     _UpperCamelCase							:    List[str]	      =	(
         {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
         if is_tf_available()
         else {}
     )

     _UpperCamelCase							:    int	      =	False
     _UpperCamelCase							:    Union[str, Any]	      =	False
     _UpperCamelCase							:    List[Any]	      =	False






     def 					__A						(	self						):
       _lowerCAmelCase		: List[str]						    =  TFViTModelTester(self						)
       _lowerCAmelCase		: Tuple						    =  ConfigTester(self       ,  config_class=a__       ,  has_text_modality=a__       ,  hidden_size=37						)






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






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






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






     def 					__A						(	self						):
       _lowerCAmelCase		: Any						    =  self.model_tester.prepare_config_and_inputs_for_common()

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






     def 					__A						(	self						):
       _lowerCAmelCase		: Union[str, Any]						    =  self.model_tester.prepare_config_and_inputs_for_common()

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

         _lowerCAmelCase		: Dict						    =  ["""pixel_values"""]
         self.assertListEqual(arg_names[:1]       ,  a__						)






     def 					__A						(	self						):
       _lowerCAmelCase		: Any						    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a__						)






     def 					__A						(	self						):
       _lowerCAmelCase		: str						    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*a__						)






     @slow
     def 					__A						(	self						):
       _lowerCAmelCase		: Optional[int]						    =  TFViTModel.from_pretrained("""google/vit-base-patch16-224"""						)
       self.assertIsNotNone(a__						)







def 							SCREAMING_SNAKE_CASE  (    ) ->   Dict:
  _lowerCAmelCase		: int						    =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
  return image






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






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






     @slow
     def 					__A						(	self						):
       _lowerCAmelCase		: Dict						    =  TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""						)

       _lowerCAmelCase		: List[Any]						    =  self.default_image_processor
       _lowerCAmelCase		: Union[str, Any]						    =  prepare_img()
       _lowerCAmelCase		: List[str]						    =  image_processor(images=a__       ,  return_tensors="""tf"""						)

       # forward pass
       _lowerCAmelCase		: Tuple						    =  model(**a__						)

       # verify the logits
       _lowerCAmelCase		: int						    =  tf.TensorShape((1, 1000)						)
       self.assertEqual(outputs.logits.shape       ,  a__						)

       _lowerCAmelCase		: Optional[Any]						    =  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]       ,  a__       ,  atol=1e-4						)





"""simple docstring"""




from random import shuffle

import tensorflow as tf
from numpy import array





def 							SCREAMING_SNAKE_CASE  (    _lowerCamelCase		: Union[str, Any]    ,_lowerCamelCase		: Tuple			) ->   Dict:
  _lowerCAmelCase		: List[str]						    =  int(_lowerCamelCase			)
  assert noofclusters < len(_lowerCamelCase			)

  # Find out the dimensionality
  _lowerCAmelCase		: Any						    =  len(vectors[0]			)

  # Will help select random centroids from among the available vectors
  _lowerCAmelCase		: Any						    =  list(range(len(_lowerCamelCase			)			)			)
  shuffle(_lowerCamelCase			)

  # GRAPH OF COMPUTATION
  # We initialize a new graph and set it as the default during each run
  # of this algorithm. This ensures that as this function is called
  # multiple times, the default graph doesn't keep getting crowded with
  # unused ops and Variables from previous function calls.

  _lowerCAmelCase		: List[Any]						    =  tf.Graph()

  with graph.as_default():
    # SESSION OF COMPUTATION

    _lowerCAmelCase		: str						    =  tf.Session()

    ##CONSTRUCTING THE ELEMENTS OF COMPUTATION

    ##First lets ensure we have a Variable vector for each centroid,
    ##initialized to one of the vectors from the available data points
    _lowerCAmelCase		: List[str]						    =  [
        tf.Variable(vectors[vector_indices[i]]			) for i in range(_lowerCamelCase			)
    ]
    ##These nodes will assign the centroid Variables the appropriate
    ##values
    _lowerCAmelCase		: Union[str, Any]						    =  tf.placeholder("""float64"""    ,[dim]			)
    _lowerCAmelCase		: Optional[int]						    =  []
    for centroid in centroids:
      cent_assigns.append(tf.assign(_lowerCamelCase    ,_lowerCamelCase			)			)

    ##Variables for cluster assignments of individual vectors(initialized
    ##to 0 at first)
    _lowerCAmelCase		: Dict						    =  [tf.Variable(0			) for i in range(len(_lowerCamelCase			)			)]
    ##These nodes will assign an assignment Variable the appropriate
    ##value
    _lowerCAmelCase		: List[Any]						    =  tf.placeholder("""int32"""			)
    _lowerCAmelCase		: Any						    =  []
    for assignment in assignments:
      cluster_assigns.append(tf.assign(_lowerCamelCase    ,_lowerCamelCase			)			)

    ##Now lets construct the node that will compute the mean
    # The placeholder for the input
    _lowerCAmelCase		: Union[str, Any]						    =  tf.placeholder("""float"""    ,[None, dim]			)
    # The Node/op takes the input and computes a mean along the 0th
    # dimension, i.e. the list of input vectors
    _lowerCAmelCase		: Optional[int]						    =  tf.reduce_mean(_lowerCamelCase    ,0			)

    ##Node for computing Euclidean distances
    # Placeholders for input
    _lowerCAmelCase		: Dict						    =  tf.placeholder("""float"""    ,[dim]			)
    _lowerCAmelCase		: Any						    =  tf.placeholder("""float"""    ,[dim]			)
    _lowerCAmelCase		: List[Any]						    =  tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(_lowerCamelCase    ,_lowerCamelCase			)    ,2			)			)			)

    ##This node will figure out which cluster to assign a vector to,
    ##based on Euclidean distances of the vector from the centroids.
    # Placeholder for input
    _lowerCAmelCase		: Any						    =  tf.placeholder("""float"""    ,[noofclusters]			)
    _lowerCAmelCase		: str						    =  tf.argmin(_lowerCamelCase    ,0			)

    ##INITIALIZING STATE VARIABLES

    ##This will help initialization of all Variables defined with respect
    ##to the graph. The Variable-initializer should be defined after
    ##all the Variables have been constructed, so that each of them
    ##will be included in the initialization.
    _lowerCAmelCase		: Optional[Any]						    =  tf.initialize_all_variables()

    # Initialize all variables
    sess.run(_lowerCamelCase			)

    ##CLUSTERING ITERATIONS

    # Now perform the Expectation-Maximization steps of K-Means clustering
    # iterations. To keep things simple, we will only do a set number of
    # iterations, instead of using a Stopping Criterion.
    _lowerCAmelCase		: List[str]						    =  100
    for _ in range(_lowerCamelCase			):
      ##EXPECTATION STEP
      ##Based on the centroid locations till last iteration, compute
      ##the _expected_ centroid assignments.
      # Iterate over each vector
      for vector_n in range(len(_lowerCamelCase			)			):
        _lowerCAmelCase		: int						    =  vectors[vector_n]
        # Compute Euclidean distance between this vector and each
        # centroid. Remember that this list cannot be named
        #'centroid_distances', since that is the input to the
        # cluster assignment node.
        _lowerCAmelCase		: Any						    =  [
            sess.run(_lowerCamelCase    ,feed_dict={va: vect, va: sess.run(_lowerCamelCase			)}			)
            for centroid in centroids
        ]
        # Now use the cluster assignment node, with the distances
        # as the input
        _lowerCAmelCase		: Any						    =  sess.run(
            _lowerCamelCase    ,feed_dict={centroid_distances: distances}			)
        # Now assign the value to the appropriate state variable
        sess.run(
            cluster_assigns[vector_n]    ,feed_dict={assignment_value: assignment}			)

      ##MAXIMIZATION STEP
      # Based on the expected state computed from the Expectation Step,
      # compute the locations of the centroids so as to maximize the
      # overall objective of minimizing within-cluster Sum-of-Squares
      for cluster_n in range(_lowerCamelCase			):
        # Collect all the vectors assigned to this cluster
        _lowerCAmelCase		: List[Any]						    =  [
            vectors[i]
            for i in range(len(_lowerCamelCase			)			)
            if sess.run(assignments[i]			) == cluster_n
        ]
        # Compute new centroid location
        _lowerCAmelCase		: Optional[int]						    =  sess.run(
            _lowerCamelCase    ,feed_dict={mean_input: array(_lowerCamelCase			)}			)
        # Assign value to appropriate variable
        sess.run(
            cent_assigns[cluster_n]    ,feed_dict={centroid_value: new_location}			)

        # Return centroids and assignments
    _lowerCAmelCase		: Optional[int]						    =  sess.run(_lowerCamelCase			)
    _lowerCAmelCase		: List[Any]						    =  sess.run(_lowerCamelCase			)
    return centroids, assignments




