






'''simple docstring'''





import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ASTConfig
from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_torchaudio_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch
 from torch import nn

 from transformers import ASTForAudioClassification, ASTModel
 from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import (
     AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
 )


if is_torchaudio_available():
 import torchaudio

 from transformers import ASTFeatureExtractor
class 				lowercase       :


    '''simple docstring'''

    def __init__(					self     :     str				,     __lowerCamelCase     :     Optional[Any]				,     __lowerCamelCase     :     List[Any]=13				,     __lowerCamelCase     :     Any=2				,     __lowerCamelCase     :     Optional[Any]=24				,     __lowerCamelCase     :     Dict=16				,     __lowerCamelCase     :     str=True				,     __lowerCamelCase     :     Union[str, Any]=True				,     __lowerCamelCase     :     Dict=32				,     __lowerCamelCase     :     Optional[Any]=5				,     __lowerCamelCase     :     Optional[int]=4				,     __lowerCamelCase     :     Optional[Any]=37				,     __lowerCamelCase     :     str="gelu"				,     __lowerCamelCase     :     Dict=0.1				,     __lowerCamelCase     :     List[Any]=0.1				,     __lowerCamelCase     :     int=10				,     __lowerCamelCase     :     Optional[int]=0.0_2				,     __lowerCamelCase     :     List[str]=None				,     __lowerCamelCase     :     List[str]=2				,     __lowerCamelCase     :     Optional[int]=2				,     )							->							int:

     '''simple docstring'''




     lowerCamelCase__  =     parent
     lowerCamelCase__  =     batch_size
     lowerCamelCase__  =     patch_size
     lowerCamelCase__  =     max_length
     lowerCamelCase__  =     num_mel_bins
     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
     lowerCamelCase__  =     frequency_stride
     lowerCamelCase__  =     time_stride

     # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
     lowerCamelCase__  =     (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1
     lowerCamelCase__  =     (self.max_length - self.patch_size) // self.time_stride + 1
     lowerCamelCase__  =     frequency_out_dimension * time_out_dimension
     lowerCamelCase__  =     num_patches + 2

    def    a__				(					self     :     Tuple     )							->							str:

     '''simple docstring'''




     lowerCamelCase__  =     floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]     )

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

     lowerCamelCase__  =     self.get_config()

     return config, input_values, labels

    def    a__				(					self     :     List[Any]     )							->							Dict:

     '''simple docstring'''




     return ASTConfig(
         patch_size=self.patch_size				,     max_length=self.max_length				,     num_mel_bins=self.num_mel_bins				,     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				,     frequency_stride=self.frequency_stride				,     time_stride=self.time_stride				,     )

    def    a__				(					self     :     int				,     __lowerCamelCase     :     Dict				,     __lowerCamelCase     :     Union[str, Any]				,     __lowerCamelCase     :     Tuple     )							->							Dict:

     '''simple docstring'''




     lowerCamelCase__  =     ASTModel(config=__lowerCamelCase     )
     model.to(__lowerCamelCase     )
     model.eval()
     lowerCamelCase__  =     model(__lowerCamelCase     )
     self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)     )




    def    a__				(					self     :     Optional[int]     )							->							Tuple:

     '''simple docstring'''




     lowerCamelCase__  =     self.prepare_config_and_inputs()
     (
         (
         lowerCamelCase__	
     )  ,	(
         lowerCamelCase__	
     )  ,	(
         lowerCamelCase__	
     )  ,		
     )  =     config_and_inputs
     lowerCamelCase__  =     {"input_values": input_values}
     return config, inputs_dict
@require_torch
class 				lowercase       (							_lowerCamelCase ,       _lowerCamelCase ,       unittest.TestCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							(
        (
            ASTModel,
            ASTForAudioClassification,
        )
        if is_torch_available()
        else ()
    )
    lowerCAmelCase__      				=							(
        {"audio-classification": ASTForAudioClassification, "feature-extraction": ASTModel}
        if is_torch_available()
        else {}
    )
    lowerCAmelCase__      				=							False
    lowerCAmelCase__      				=							False
    lowerCAmelCase__      				=							False
    lowerCAmelCase__      				=							False

    def    a__				(					self     :     Tuple				,     __lowerCamelCase     :     List[Any]				,     __lowerCamelCase     :     Tuple				,     __lowerCamelCase     :     Optional[Any]				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     List[str]     )							->							List[Any]:

     '''simple docstring'''




     if pipeline_test_casse_name == "AudioClassificationPipelineTests":
      return True

     return False

    def    a__				(					self     :     Optional[Any]     )							->							Any:

     '''simple docstring'''




     lowerCamelCase__  =     ASTModelTester(self     )
     lowerCamelCase__  =     ConfigTester(self				,     config_class=__lowerCamelCase				,     has_text_modality=__lowerCamelCase				,     hidden_size=37     )

    def    a__				(					self     :     Tuple     )							->							Optional[Any]:

     '''simple docstring'''




     self.config_tester.run_common_tests()

    @unittest.skip(reason="AST does not use inputs_embeds"     )
    def    a__				(					self     :     List[str]     )							->							Optional[Any]:

     '''simple docstring'''




     pass

    def    a__				(					self     :     Dict     )							->							List[Any]:

     '''simple docstring'''




     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()				,     (nn.Module)     )
      lowerCamelCase__  =     model.get_output_embeddings()
      self.assertTrue(x is None or isinstance(__lowerCamelCase				,     nn.Linear     )     )

    def    a__				(					self     :     Optional[int]     )							->							Tuple:

     '''simple docstring'''




     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.forward     )
      # signature.parameters is an OrderedDict => so arg_names order is deterministic
      lowerCamelCase__  =     [*signature.parameters.keys()]

      lowerCamelCase__  =     ["input_values"]
      self.assertListEqual(arg_names[:1]				,     __lowerCamelCase     )

    def    a__				(					self     :     List[Any]     )							->							Any:

     '''simple docstring'''




     lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase     )




    @slow
    def    a__				(					self     :     List[Any]     )							->							int:

     '''simple docstring'''




     for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      lowerCamelCase__  =     ASTModel.from_pretrained(__lowerCamelCase     )
      self.assertIsNotNone(__lowerCamelCase     )






def    lowerCamelCase_     (    )     ->   List[Any]:
 lowerCamelCase__  =     hf_hub_download(
     repo_id="nielsr/audio-spectogram-transformer-checkpoint"				,     filename="sample_audio.flac"				,     repo_type="dataset")

 lowerCamelCase__  ,	lowerCamelCase__  =     torchaudio.load(lowercase__)

 return audio, sampling_rate
@require_torch
@require_torchaudio
class 				lowercase       (							unittest.TestCase      ):


    '''simple docstring'''

    @cached_property
    def    a__				(					self     :     Optional[int]     )							->							int:

     '''simple docstring'''




     return (
         ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"     )
         if is_torchaudio_available()
         else None
     )




    @slow
    def    a__				(					self     :     List[str]     )							->							Tuple:

     '''simple docstring'''




     lowerCamelCase__  =     self.default_feature_extractor
     lowerCamelCase__  =     ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"     ).to(__lowerCamelCase     )

     lowerCamelCase__  =     self.default_feature_extractor
     lowerCamelCase__  ,	lowerCamelCase__  =     prepare_audio()
     lowerCamelCase__  =     audio.squeeze().numpy()
     lowerCamelCase__  =     feature_extractor(__lowerCamelCase				,     sampling_rate=__lowerCamelCase				,     return_tensors="pt"     ).to(__lowerCamelCase     )

     # forward pass
     with torch.no_grad():
      lowerCamelCase__  =     model(**__lowerCamelCase     )

     # verify the logits
     lowerCamelCase__  =     torch.Size((1, 527)     )
     self.assertEqual(outputs.logits.shape				,     __lowerCamelCase     )

     lowerCamelCase__  =     torch.tensor([-0.8_7_6_0, -7.0_0_4_2, -8.6_6_0_2]     ).to(__lowerCamelCase     )

     self.assertTrue(torch.allclose(outputs.logits[0, :3]				,     __lowerCamelCase				,     atol=1E-4     )     )









'''simple docstring'''





from pathlib import Path

import cva
import numpy as np
from matplotlib import pyplot as plt






def    lowerCamelCase_     (    lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__):
 lowerCamelCase__  =     cva.getAffineTransform(lowercase__				,     lowercase__)
 return cva.warpAffine(lowercase__				,     lowercase__				,     (rows, cols))


if __name__ == "__main__":
 # read original image
 __A							:							Any  							=		cva.imread(
     str(Path(__file__).resolve().parent.parent / """image_data""" / """lena.jpg""")
 )
 # turn image in gray scale value
 __A							:							int  							=		cva.cvtColor(image, cva.COLOR_BGR2GRAY)
 # get image shape
 __A							,			__A							:							Dict  							=		gray_img.shape

 # set different points to rotate image
 __A							:							int  							=		np.array([[50, 50], [2_00, 50], [50, 2_00]], np.floataa)
 __A							:							Any  							=		np.array([[10, 1_00], [2_00, 50], [1_00, 2_50]], np.floataa)
 __A							:							str  							=		np.array([[50, 50], [1_50, 50], [1_20, 2_00]], np.floataa)
 __A							:							Optional[int]  							=		np.array([[10, 1_00], [80, 50], [1_80, 2_50]], np.floataa)

 # add all rotated images in a list
 __A							:							List[Any]  							=		[
     gray_img,
     get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
     get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
     get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
 ]

 # plot different image rotations
 __A							:							int  							=		plt.figure(1)
 __A							:							int  							=		["""Original""", """Rotation 1""", """Rotation 2""", """Rotation 3"""]
 for i, image in enumerate(images):
  plt.subplot(2, 2, i + 1), plt.imshow(image, """gray""")
  plt.title(titles[i])
  plt.axis("""off""")
  plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95)
 plt.show()

