Skip to content

Commit

Permalink
Merge pull request #1207 from saumya0303/master
Browse files Browse the repository at this point in the history
add resampling audio input transformation defense in torch
  • Loading branch information
alkaet authored Apr 20, 2021
2 parents cb5a1bf + 03f3ee2 commit 1115738
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions defenses/torch/audio/input_tranformation/resampling.py
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)

0 comments on commit 1115738

Please sign in to comment.