chendl/compositional_test
1
1# Copyright 2021 The HuggingFace Team. All rights reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14 15import unittest16 17import numpy as np18 19from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING20from transformers.pipelines import AudioClassificationPipeline, pipeline21from transformers.testing_utils import (22 is_pipeline_test,23 nested_simplify,24 require_tf,25 require_torch,26 require_torchaudio,27 slow,28)29 30from .test_pipelines_common import ANY31 32 33@is_pipeline_test34@require_torch35class AudioClassificationPipelineTests(unittest.TestCase):36 model_mapping = MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING37 38 def get_test_pipeline(self, model, tokenizer, processor):39 audio_classifier = AudioClassificationPipeline(model=model, feature_extractor=processor)40 41 # test with a raw waveform42 audio = np.zeros((34000,))43 audio2 = np.zeros((14000,))44 return audio_classifier, [audio2, audio]45 46 def run_pipeline_test(self, audio_classifier, examples):47 audio2, audio = examples48 output = audio_classifier(audio)49 # by default a model is initialized with num_labels=250 self.assertEqual(51 output,52 [53 {"score": ANY(float), "label": ANY(str)},54 {"score": ANY(float), "label": ANY(str)},55 ],56 )57 output = audio_classifier(audio, top_k=1)58 self.assertEqual(59 output,60 [61 {"score": ANY(float), "label": ANY(str)},62 ],63 )64 65 self.run_torchaudio(audio_classifier)66 67 @require_torchaudio68 def run_torchaudio(self, audio_classifier):69 import datasets70 71 # test with a local file72 dataset = datasets.load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")73 audio = dataset[0]["audio"]["array"]74 output = audio_classifier(audio)75 self.assertEqual(76 output,77 [78 {"score": ANY(float), "label": ANY(str)},79 {"score": ANY(float), "label": ANY(str)},80 ],81 )82 83 @require_torch84 def test_small_model_pt(self):85 model = "anton-l/wav2vec2-random-tiny-classifier"86 87 audio_classifier = pipeline("audio-classification", model=model)88 89 audio = np.ones((8000,))90 output = audio_classifier(audio, top_k=4)91 92 EXPECTED_OUTPUT = [93 {"score": 0.0842, "label": "no"},94 {"score": 0.0838, "label": "up"},95 {"score": 0.0837, "label": "go"},96 {"score": 0.0834, "label": "right"},97 ]98 EXPECTED_OUTPUT_PT_2 = [99 {"score": 0.0845, "label": "stop"},100 {"score": 0.0844, "label": "on"},101 {"score": 0.0841, "label": "right"},102 {"score": 0.0834, "label": "left"},103 ]104 self.assertIn(nested_simplify(output, decimals=4), [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2])105 106 @require_torch107 @slow108 def test_large_model_pt(self):109 import datasets110 111 model = "superb/wav2vec2-base-superb-ks"112 113 audio_classifier = pipeline("audio-classification", model=model)114 dataset = datasets.load_dataset("anton-l/superb_dummy", "ks", split="test")115 116 audio = np.array(dataset[3]["speech"], dtype=np.float32)117 output = audio_classifier(audio, top_k=4)118 self.assertEqual(119 nested_simplify(output, decimals=3),120 [121 {"score": 0.981, "label": "go"},122 {"score": 0.007, "label": "up"},123 {"score": 0.006, "label": "_unknown_"},124 {"score": 0.001, "label": "down"},125 ],126 )127 128 @require_tf129 @unittest.skip("Audio classification is not implemented for TF")130 def test_small_model_tf(self):131 pass132 