-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding some audio transforms and augmentations to tonic #273
Merged
Merged
Changes from 20 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
b9c6618
new transform added: SwapAxes
MinaKh b89f146
audio_augmentation module is added: with RandomTimeStretch
MinaKh fbe2b61
RandomPitchShift transfrom added
MinaKh 2cce804
RandomAmplitudeScale is added
MinaKh ebaadad
RIR transform (room impulse response) added
MinaKh df239fa
AmplitudeScale and RobustAmplitudeScale transforms added
MinaKh 60cd283
Noise augmentations added
MinaKh a52c334
typos fixed in docstrings
MinaKh 9fc506a
fixes in docstrings
MinaKh 6cb92f7
tests for added transforms
MinaKh 80e3da8
tests for audio augmentations
MinaKh d2c0436
tests_passed
MinaKh af12bb4
torchaudio added to requirements
MinaKh 8465bb7
removing torchaudio dependency temporarily to run the tests
MinaKh 10760db
requirments updated with torch and torchaudio versions
MinaKh 78837f0
removing hard coded room impulse audio from RIR transform
MinaKh b7f76b6
RIR test updated
MinaKh 377a3c9
updated the required version of torch
MinaKh 59952bb
removing conflicting requirements
MinaKh d8813da
adding torchaudio
MinaKh 4361b57
cpu version is spesified for torchaudio
MinaKh d57d67f
Trying to make torchaudio work
fabrizio-ottati 370a584
merge
MinaKh 51a500e
Update requirements.txt
fabrizio-ottati 9ba4ff3
Update requirements.txt
fabrizio-ottati 5bd4a36
Create torch_requirements.txt
fabrizio-ottati ea025f0
Update requirements.txt
fabrizio-ottati 89dc597
Update torch_requirements.txt
fabrizio-ottati 21e241f
Update ci-pipeline.yml
fabrizio-ottati 94f641f
Update torch_requirements.txt
fabrizio-ottati f54fd8c
Update ci-pipeline.yml
fabrizio-ottati 0f8be68
Update torch_requirements.txt
fabrizio-ottati 23bdd42
Update ci-pipeline.yml
fabrizio-ottati 04aac21
Testing with python>=3.8 and python<=3.11
fabrizio-ottati 628bb55
noise related augmentations removed
MinaKh 8dc340b
sample_length was removed from some transforms (when not needed)
MinaKh f57d4d5
bug fixed in test
MinaKh 6db78de
tutorial added for audio transforms/augmentations
MinaKh c1e53dc
Merge remote-tracking branch 'origin/main' into add_audio_transforms
biphasic 0af124a
shorten GH actions pipeline to three Python versions
biphasic b81f886
add torch requirements to documentation github action
biphasic 2fb1664
pin torchvision version to something compatible with torch 2.1
biphasic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pytest | ||
coverage | ||
torch | ||
torchaudio | ||
matplotlib | ||
hdf5plugin | ||
imageio | ||
|
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,144 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
|
||
def test_random_time_stretch(): | ||
"""Tests the time_stretch transform with synthetic data for 2 scenarions: slowing down and | ||
speeding up. | ||
|
||
- verifies if the output of transform is different than the input data | ||
- verifies if the length of signal has chanched according to the stretch factor (and it should remain fixed if fix_length flag is True) | ||
""" | ||
from tonic.audio_augmentations import RandomTimeStretch | ||
|
||
np.random.seed(123) | ||
|
||
sr = 16_000 # sample rate | ||
sl = 1 # sample length | ||
data = np.random.rand(1, sr * sl) | ||
|
||
for fix_length in [False, True]: | ||
# verify length of stretched signal | ||
slowing_down = RandomTimeStretch( | ||
samplerate=sr, sample_length=sl, factors=[0.5], fix_length=fix_length | ||
) | ||
slow = slowing_down(data) | ||
|
||
assert slow is not data | ||
|
||
if fix_length: | ||
assert slow.shape[1] == data.shape[1] | ||
else: | ||
assert np.allclose( | ||
slow.shape[1], | ||
data.shape[1] / (slowing_down.factors[0]), | ||
rtol=1e-2, | ||
atol=1e-3, | ||
) | ||
|
||
speeding_up = RandomTimeStretch( | ||
samplerate=sr, sample_length=sl, factors=[1.5], fix_length=fix_length | ||
) | ||
fast = speeding_up(data) | ||
|
||
assert fast is not data | ||
|
||
if fix_length: | ||
assert fast.shape[1] == data.shape[1] | ||
else: | ||
assert np.allclose( | ||
fast.shape[1], | ||
data.shape[1] / (speeding_up.factors[0]), | ||
rtol=1e-2, | ||
atol=1e-3, | ||
) | ||
|
||
|
||
def test_random_pitch_shift(): | ||
"""Tests the pitch_shift transform with synthetic data. | ||
|
||
- verifies if the output of transform is different than the input data | ||
- verifies that the size has not changed | ||
""" | ||
from tonic.audio_augmentations import RandomPitchShift | ||
|
||
np.random.seed(123) | ||
|
||
sr = 16_000 # sample rate | ||
sl = 1 # sample length | ||
data = np.random.rand(1, sr * sl) | ||
|
||
aug = RandomPitchShift(sample_length=sl, samplerate=sl) | ||
pitch_shifted = aug(data) | ||
|
||
assert pitch_shifted is not data | ||
|
||
assert pitch_shifted.shape[1] == data.shape[1] | ||
|
||
|
||
def test_random_amplitude_scale(): | ||
"""Tests the amplitude_scale transform with synthetic data. | ||
|
||
- verifies if the output of transform is different than the input data | ||
- verifies that the size has not changed | ||
- verifies that maximum amplitude is in the defined range | ||
""" | ||
from tonic.audio_augmentations import RandomAmplitudeScale | ||
|
||
np.random.seed(123) | ||
|
||
sr = 16_000 # sample rate | ||
sl = 1 # sample length | ||
data = np.ones((1, sr * sl)) | ||
min_amp, max_amp = 0.05, 0.15 | ||
|
||
aug = RandomAmplitudeScale( | ||
sample_length=sl, samplerate=sl, min_amp=min_amp, max_amp=max_amp | ||
) | ||
amp_scaled = aug(data) | ||
|
||
assert amp_scaled is not data | ||
assert amp_scaled.shape[1] == data.shape[1] | ||
assert amp_scaled.max() <= max_amp | ||
|
||
|
||
def test_add_white_noise(): | ||
"""Tests the add_white_noise transform with synthetic data. | ||
|
||
- verifies if the output of transform is different than the input data | ||
- verifies that the size has not changed | ||
""" | ||
from tonic.audio_augmentations import AddWhiteNoise | ||
|
||
np.random.seed(123) | ||
|
||
sr = 16_000 # sample rate | ||
sl = 1 # sample length | ||
data = np.random.rand(1, sr * sl) | ||
|
||
aug = AddWhiteNoise(samplerate=sr) | ||
noisy = aug(data) | ||
assert noisy is not data | ||
assert noisy.shape[1] == data.shape[1] | ||
|
||
|
||
def test_RIR(): | ||
"""Tests the RIR transform with a synthetic data. | ||
|
||
- verifies if the output of transform is different than the input data | ||
- verifies that the size has not changed | ||
""" | ||
from tonic.audio_augmentations import RIR | ||
|
||
np.random.seed(123) | ||
|
||
sr = 16_000 # sample rate | ||
sl = 1 # sample length | ||
data = np.random.rand(1, sr * sl).astype("float32") | ||
rir_audio_path = ( | ||
"tutorial-assets/Lab41-SRI-VOiCES-rm1-impulse-mc01-stu-clo-8000hz.wav" | ||
) | ||
aug = RIR(samplerate=sr, rir_audio=rir_audio_path) | ||
RIR_augmented = aug(data) | ||
assert RIR_augmented is not data | ||
assert RIR_augmented.shape[1] == data.shape[1] |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MinaKh here's where you have to specify the CPU version of
torchaudio
. The CI pipeline installs the dependencies from this file.