-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1207 from saumya0303/master
add resampling audio input transformation defense in torch
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import torchaudio | ||
import librosa | ||
|
||
# There exist a limitation of this defense that it may lead to the problem of aliasing, and we can use the narrowband sample rate | ||
# rather than downsampling followed by upsampling. | ||
# resampling reference https://core.ac.uk/download/pdf/228298313.pdf | ||
# resampling input transformation defense for audio | ||
|
||
T = torchaudio.transforms | ||
|
||
# Read audio file | ||
audio_data = librosa.load(files, sr=16000)[0][-19456:] | ||
|
||
audio_data = torch.tensor(audio_data).float().to(device) | ||
|
||
# Discarding samples from a waveform during downsampling could remove a significant portion of the adversarial perturbation, thereby prevents an adversarial attack. | ||
|
||
# resample the audio files to 8kHz from 16kHz | ||
sample = T.Resample(16000, 8000, resampling_method="sinc_interpolation") | ||
|
||
audio_resample_1 = sample(audio_data) | ||
|
||
# resample the audio back to 16kHz | ||
sample = T.Resample(8000, 16000, resampling_method="sinc_interpolation") | ||
|
||
# Give audio_resample_2 as input to the asr model | ||
audio_resample_2 = sample(audio_resample_1) |