






import warnings
from typing import Any, Dict, List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


__a      :       Optional[Any]				  =   logging.get_logger(__name__)


class       __lowercase      (	lowercase_  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				["input_values", "attention_mask"]




     def __init__(				self       :			Tuple ,	UpperCamelCase_       :			int = 1 ,	UpperCamelCase_       :			int = 16_000 ,	UpperCamelCase_       :			float = 0.0 ,	UpperCamelCase_       :			bool = False ,	UpperCamelCase_       :			int = 80 ,	UpperCamelCase_       :			int = 16 ,	UpperCamelCase_       :			int = 64 ,	UpperCamelCase_       :			str = "hann_window" ,	UpperCamelCase_       :			float = 1.0 ,	UpperCamelCase_       :			float = 80 ,	UpperCamelCase_       :			float = 7_600 ,	UpperCamelCase_       :			float = 1e-10 ,	UpperCamelCase_       :			int = 2 ,	UpperCamelCase_       :			bool = True ,	**UpperCamelCase_       :			Optional[int] ,	):


         """simple docstring"""



         super().__init__(feature_size=UpperCamelCase_ ,	sampling_rate=UpperCamelCase_ ,	padding_value=UpperCamelCase_ ,	**UpperCamelCase_				)
         __A         =	do_normalize
         __A         =	return_attention_mask

         __A         =	num_mel_bins
         __A         =	hop_length
         __A         =	win_length
         __A         =	win_function
         __A         =	frame_signal_scale
         __A         =	fmin
         __A         =	fmax
         __A         =	mel_floor
         __A         =	reduction_factor

         __A         =	win_length * sampling_rate // 1_000
         __A         =	hop_length * sampling_rate // 1_000
         __A         =	optimal_fft_length(self.sample_size				)
         __A         =	(self.n_fft // 2) + 1

         __A         =	window_function(window_length=self.sample_size ,	name=self.win_function ,	periodic=UpperCamelCase_				)

         __A         =	mel_filter_bank(
             num_frequency_bins=self.n_freqs ,	num_mel_filters=self.num_mel_bins ,	min_frequency=self.fmin ,	max_frequency=self.fmax ,	sampling_rate=self.sampling_rate ,	norm="""slaney""" ,	mel_scale="""slaney""" ,	)

         if frame_signal_scale != 1.0:
             warnings.warn(
                 """The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers""" ,	UpperCamelCase_ ,	)
         if reduction_factor != 2.0:
             warnings.warn(
                 """The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers""" ,	UpperCamelCase_ ,	)




     @staticmethod
     # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
     def 		lowerCAmelCase_     (				UpperCamelCase_       :			List[np.ndarray] ,	UpperCamelCase_       :			List[np.ndarray] ,	UpperCamelCase_       :			float = 0.0				):


         """simple docstring"""



         if attention_mask is not None:
             __A         =	np.array(UpperCamelCase_ ,	np.intaa				)
             __A         =	[]

             for vector, length in zip(UpperCamelCase_ ,	attention_mask.sum(-1				)				):
                 __A         =	(vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7				)
                 if length < normed_slice.shape[0]:
                     __A         =	padding_value

                 normed_input_values.append(UpperCamelCase_				)
         else:
             __A         =	[(x - x.mean()) / np.sqrt(x.var() + 1e-7				) for x in input_values]

         return normed_input_values




     def 		lowerCAmelCase_     (				self       :			Dict ,	UpperCamelCase_       :			np.ndarray ,	):


         """simple docstring"""



         __A         =	spectrogram(
             UpperCamelCase_ ,	window=self.window ,	frame_length=self.sample_size ,	hop_length=self.sample_stride ,	fft_length=self.n_fft ,	mel_filters=self.mel_filters ,	mel_floor=self.mel_floor ,	log_mel="""log10""" ,	)
         return log_mel_spec.T




     def __call__(				self       :			Tuple ,	UpperCamelCase_       :			Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None ,	UpperCamelCase_       :			Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None ,	UpperCamelCase_       :			Union[bool, str, PaddingStrategy] = False ,	UpperCamelCase_       :			Optional[int] = None ,	UpperCamelCase_       :			bool = False ,	UpperCamelCase_       :			Optional[int] = None ,	UpperCamelCase_       :			Optional[bool] = None ,	UpperCamelCase_       :			Optional[Union[str, TensorType]] = None ,	UpperCamelCase_       :			Optional[int] = None ,	**UpperCamelCase_       :			Optional[Any] ,	):


         """simple docstring"""



         if audio is None and audio_target is None:
             raise ValueError("""You must provide either `audio` or `audio_target` values."""				)

         if sampling_rate is not None:
             if sampling_rate != self.sampling_rate:
                 raise ValueError(
                     F"The model corresponding to this feature extractor: {self} was trained using a sampling rate of"
                     F" {self.sampling_rate}. Please make sure that the provided audio input was sampled with"
                     F" {self.sampling_rate} and not {sampling_rate}."				)
         else:
             logger.warning(
                 """It is strongly recommended to pass the ``sampling_rate`` argument to this function. """
                 """Failing to do so can result in silent errors that might be hard to debug."""				)

         if audio is not None:
             __A         =	self._process_audio(
                 UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	**UpperCamelCase_ ,	)
         else:
             __A         =	None

         if audio_target is not None:
             __A         =	self._process_audio(
                 UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_ ,	**UpperCamelCase_ ,	)

             if inputs is None:
                 return inputs_target
             else:
                 __A         =	inputs_target["""input_values"""]
                 __A         =	inputs_target.get("""attention_mask"""				)
                 if decoder_attention_mask is not None:
                     __A         =	decoder_attention_mask

         return inputs




     def 		lowerCAmelCase_     (				self       :			int ,	UpperCamelCase_       :			Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] ,	UpperCamelCase_       :			bool = False ,	UpperCamelCase_       :			Union[bool, str, PaddingStrategy] = False ,	UpperCamelCase_       :			Optional[int] = None ,	UpperCamelCase_       :			bool = False ,	UpperCamelCase_       :			Optional[int] = None ,	UpperCamelCase_       :			Optional[bool] = None ,	UpperCamelCase_       :			Optional[Union[str, TensorType]] = None ,	**UpperCamelCase_       :			Any ,	):


         """simple docstring"""



         __A         =	isinstance(UpperCamelCase_ ,	np.ndarray				) and len(speech.shape				) > 1
         if is_batched_numpy and len(speech.shape				) > 2:
             raise ValueError(F"Only mono-channel audio is supported for input to {self}"				)
         __A         =	is_batched_numpy or (
             isinstance(UpperCamelCase_ ,	(list, tuple)				) and (isinstance(speech[0] ,	(np.ndarray, tuple, list)				))
         )

         if is_batched:
             __A         =	[np.asarray(UpperCamelCase_ ,	dtype=np.floataa				) for speech in speech]
         elif not is_batched and not isinstance(UpperCamelCase_ ,	np.ndarray				):
             __A         =	np.asarray(UpperCamelCase_ ,	dtype=np.floataa				)
         elif isinstance(UpperCamelCase_ ,	np.ndarray				) and speech.dtype is np.dtype(np.floataa				):
             __A         =	speech.astype(np.floataa				)

         # always return batch
         if not is_batched:
             __A         =	[speech]

         # needed to make pad() work on spectrogram inputs
         __A         =	self.feature_size

         # convert into correct format for padding
         if is_target:
             __A         =	[self._extract_mel_features(UpperCamelCase_				) for waveform in speech]
             __A         =	BatchFeature({"""input_values""": features}				)
             __A         =	self.num_mel_bins
         else:
             __A         =	BatchFeature({"""input_values""": speech}				)

         __A         =	self.pad(
             UpperCamelCase_ ,	padding=UpperCamelCase_ ,	max_length=UpperCamelCase_ ,	truncation=UpperCamelCase_ ,	pad_to_multiple_of=UpperCamelCase_ ,	return_attention_mask=UpperCamelCase_ ,	**UpperCamelCase_ ,	)

         __A         =	feature_size_hack

         # convert input values to correct format
         __A         =	padded_inputs["""input_values"""]
         if not isinstance(input_values[0] ,	np.ndarray				):
             __A         =	[np.asarray(UpperCamelCase_ ,	dtype=np.floataa				) for array in input_values]
         elif (
             not isinstance(UpperCamelCase_ ,	np.ndarray				)
             and isinstance(input_values[0] ,	np.ndarray				)
             and input_values[0].dtype is np.dtype(np.floataa				)
         ):
             __A         =	[array.astype(np.floataa				) for array in input_values]
         elif isinstance(UpperCamelCase_ ,	np.ndarray				) and input_values.dtype is np.dtype(np.floataa				):
             __A         =	input_values.astype(np.floataa				)

         # convert attention_mask to correct format
         __A         =	padded_inputs.get("""attention_mask"""				)
         if attention_mask is not None:
             __A         =	[np.asarray(UpperCamelCase_ ,	dtype=np.intaa				) for array in attention_mask]

         # zero-mean and unit-variance normalization
         if not is_target and self.do_normalize:
             __A         =	(
                 attention_mask
                 if self._get_padding_strategies(UpperCamelCase_ ,	max_length=UpperCamelCase_				) is not PaddingStrategy.DO_NOT_PAD
                 else None
             )
             __A         =	self.zero_mean_unit_var_norm(
                 padded_inputs["""input_values"""] ,	attention_mask=UpperCamelCase_ ,	padding_value=self.padding_value				)

         if return_tensors is not None:
             __A         =	padded_inputs.convert_to_tensors(UpperCamelCase_				)

         return padded_inputs





     def 		lowerCAmelCase_     (				self       :			Tuple				):


         """simple docstring"""



         __A         =	super().to_dict()

         # Don't serialize these as they are derived from the other properties.
         __A         =	["""window""", """mel_filters""", """sample_size""", """sample_stride""", """n_fft""", """n_freqs"""]
         for name in names:
             if name in output:
                 del output[name]

         return output












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				)




