|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2022 |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 6 | +# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 7 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +# persons to whom the Software is furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 11 | +# Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 14 | +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 16 | +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 17 | +# SOFTWARE. |
| 18 | +from __future__ import absolute_import, division, print_function, unicode_literals |
| 19 | + |
| 20 | +import logging |
| 21 | +import numpy as np |
| 22 | +import pytest |
| 23 | +import os |
| 24 | + |
| 25 | +from art.attacks.poisoning.perturbations.audio_perturbations import insert_tone_trigger, insert_audio_trigger |
| 26 | + |
| 27 | +from tests.utils import ARTTestException |
| 28 | + |
| 29 | +logger = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.framework_agnostic |
| 33 | +def test_insert_tone_trigger(art_warning): |
| 34 | + try: |
| 35 | + # test single example |
| 36 | + audio = insert_tone_trigger(x=np.zeros(3200), sampling_rate=16000) |
| 37 | + assert audio.shape == (3200,) |
| 38 | + assert np.max(audio) != 0 |
| 39 | + |
| 40 | + # test single example with differet duration, frequency, and scale |
| 41 | + audio = insert_tone_trigger(x=np.zeros(3200), sampling_rate=16000, frequency=16000, duration=0.2, scale=0.5) |
| 42 | + assert audio.shape == (3200,) |
| 43 | + assert np.max(audio) != 0 |
| 44 | + |
| 45 | + # test a batch of examples |
| 46 | + audio = insert_tone_trigger(x=np.zeros((10, 3200)), sampling_rate=16000) |
| 47 | + assert audio.shape == (10, 3200) |
| 48 | + assert np.max(audio) != 0 |
| 49 | + |
| 50 | + # test single example with shift |
| 51 | + audio = insert_tone_trigger(x=np.zeros(3200), sampling_rate=16000, shift=10) |
| 52 | + assert audio.shape == (3200,) |
| 53 | + assert np.max(audio) != 0 |
| 54 | + assert np.sum(audio[:10]) == 0 |
| 55 | + |
| 56 | + # test a batch of examples with random shift |
| 57 | + audio = insert_tone_trigger(x=np.zeros((10, 3200)), sampling_rate=16000, random=True) |
| 58 | + assert audio.shape == (10, 3200) |
| 59 | + assert np.max(audio) != 0 |
| 60 | + |
| 61 | + # test when length of backdoor is larger than that of audio signal |
| 62 | + with pytest.raises(ValueError): |
| 63 | + _ = insert_tone_trigger(x=np.zeros(3200), sampling_rate=16000, duration=0.3) |
| 64 | + |
| 65 | + # test when shift + backdoor is larger than that of audio signal |
| 66 | + with pytest.raises(ValueError): |
| 67 | + _ = insert_tone_trigger(x=np.zeros(3200), sampling_rate=16000, duration=0.2, shift=5) |
| 68 | + |
| 69 | + except ARTTestException as e: |
| 70 | + art_warning(e) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.framework_agnostic |
| 74 | +def test_insert_audio_trigger(art_warning): |
| 75 | + file_path = os.path.join(os.getcwd(), "utils/data/backdoors/cough_trigger.wav") |
| 76 | + try: |
| 77 | + # test single example |
| 78 | + audio = insert_audio_trigger(x=np.zeros(32000), sampling_rate=16000, backdoor_path=file_path) |
| 79 | + assert audio.shape == (32000,) |
| 80 | + assert np.max(audio) != 0 |
| 81 | + |
| 82 | + # test single example with differet duration and scale |
| 83 | + audio = insert_audio_trigger( |
| 84 | + x=np.zeros(32000), |
| 85 | + sampling_rate=16000, |
| 86 | + backdoor_path=file_path, |
| 87 | + duration=0.8, |
| 88 | + scale=0.5, |
| 89 | + ) |
| 90 | + assert audio.shape == (32000,) |
| 91 | + assert np.max(audio) != 0 |
| 92 | + |
| 93 | + # test a batch of examples |
| 94 | + audio = insert_audio_trigger(x=np.zeros((10, 16000)), sampling_rate=16000, backdoor_path=file_path) |
| 95 | + assert audio.shape == (10, 16000) |
| 96 | + assert np.max(audio) != 0 |
| 97 | + |
| 98 | + # test single example with shift |
| 99 | + audio = insert_audio_trigger(x=np.zeros(32000), sampling_rate=16000, backdoor_path=file_path, shift=10) |
| 100 | + assert audio.shape == (32000,) |
| 101 | + assert np.max(audio) != 0 |
| 102 | + assert np.sum(audio[:10]) == 0 |
| 103 | + |
| 104 | + # test a batch of examples with random shift |
| 105 | + audio = insert_audio_trigger( |
| 106 | + x=np.zeros((10, 32000)), |
| 107 | + sampling_rate=16000, |
| 108 | + backdoor_path=file_path, |
| 109 | + random=True, |
| 110 | + ) |
| 111 | + assert audio.shape == (10, 32000) |
| 112 | + assert np.max(audio) != 0 |
| 113 | + |
| 114 | + # test when length of backdoor is larger than that of audio signal |
| 115 | + with pytest.raises(ValueError): |
| 116 | + _ = insert_audio_trigger(x=np.zeros(15000), sampling_rate=16000, backdoor_path=file_path) |
| 117 | + |
| 118 | + # test when shift + backdoor is larger than that of audio signal |
| 119 | + with pytest.raises(ValueError): |
| 120 | + _ = insert_audio_trigger( |
| 121 | + x=np.zeros(16000), |
| 122 | + sampling_rate=16000, |
| 123 | + backdoor_path=file_path, |
| 124 | + duration=1, |
| 125 | + shift=5, |
| 126 | + ) |
| 127 | + |
| 128 | + except ARTTestException as e: |
| 129 | + art_warning(e) |
0 commit comments