Home

Awesome

torch-audiomentations

Build status Code coverage Code Style: Black DOI

Audio data augmentation in PyTorch. Inspired by audiomentations.

Setup

Python version support PyPI version Number of downloads from PyPI per month

pip install torch-audiomentations

Usage example

import torch
from torch_audiomentations import Compose, Gain, PolarityInversion


# Initialize augmentation callable
apply_augmentation = Compose(
    transforms=[
        Gain(
            min_gain_in_db=-15.0,
            max_gain_in_db=5.0,
            p=0.5,
        ),
        PolarityInversion(p=0.5)
    ]
)

torch_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Make an example tensor with white noise.
# This tensor represents 8 audio snippets with 2 channels (stereo) and 2 s of 16 kHz audio.
audio_samples = torch.rand(size=(8, 2, 32000), dtype=torch.float32, device=torch_device) - 0.5

# Apply augmentation. This varies the gain and polarity of (some of)
# the audio snippets in the batch independently.
perturbed_audio_samples = apply_augmentation(audio_samples, sample_rate=16000)

Known issues

Contribute

Contributors welcome! Join the Asteroid's slack to start discussing about torch-audiomentations with us.

Motivation: Speed

We don't want data augmentation to be a bottleneck in model training speed. Here is a comparison of the time it takes to run 1D convolution:

Convolve execution times

Note: Not all transforms have a speedup this impressive compared to CPU. In general, running audio data augmentation on GPU is not always the best option. For more info, see this article: https://iver56.github.io/audiomentations/guides/cpu_vs_gpu/

Current state

torch-audiomentations is in an early development stage, so the APIs are subject to change.

Waveform transforms

Every transform has mode, p, and p_mode -- the parameters that decide how the augmentation is performed.

This visualization shows how different combinations of mode and p_mode would perform an augmentation.

Explanation of mode, p and p_mode

AddBackgroundNoise

Added in v0.5.0

Add background noise to the input audio.

AddColoredNoise

Added in v0.7.0

Add colored noise to the input audio.

ApplyImpulseResponse

Added in v0.5.0

Convolve the given audio with impulse responses.

BandPassFilter

Added in v0.9.0

Apply band-pass filtering to the input audio.

BandStopFilter

Added in v0.10.0

Apply band-stop filtering to the input audio. Also known as notch filter.

Gain

Added in v0.1.0

Multiply the audio by a random amplitude factor to reduce or increase the volume. This technique can help a model become somewhat invariant to the overall gain of the input audio.

Warning: This transform can return samples outside the [-1, 1] range, which may lead to clipping or wrap distortion, depending on what you do with the audio in a later stage. See also https://en.wikipedia.org/wiki/Clipping_(audio)#Digital_clipping

HighPassFilter

Added in v0.8.0

Apply high-pass filtering to the input audio.

Identity

Added in v0.11.0

This transform returns the input unchanged. It can be used for simplifying the code in cases where data augmentation should be disabled.

LowPassFilter

Added in v0.8.0

Apply low-pass filtering to the input audio.

PeakNormalization

Added in v0.2.0

Apply a constant amount of gain, so that highest signal level present in each audio snippet in the batch becomes 0 dBFS, i.e. the loudest level allowed if all samples must be between -1 and 1.

This transform has an alternative mode (apply_to="only_too_loud_sounds") where it only applies to audio snippets that have extreme values outside the [-1, 1] range. This is useful for avoiding digital clipping in audio that is too loud, while leaving other audio untouched.

PitchShift

Added in v0.9.0

Pitch-shift sounds up or down without changing the tempo.

PolarityInversion

Added in v0.1.0

Flip the audio samples upside-down, reversing their polarity. In other words, multiply the waveform by -1, so negative values become positive, and vice versa. The result will sound the same compared to the original when played back in isolation. However, when mixed with other audio sources, the result may be different. This waveform inversion technique is sometimes used for audio cancellation or obtaining the difference between two waveforms. However, in the context of audio data augmentation, this transform can be useful when training phase-aware machine learning models.

Shift

Added in v0.5.0

Shift the audio forwards or backwards, with or without rollover

ShuffleChannels

Added in v0.6.0

Given multichannel audio input (e.g. stereo), shuffle the channels, e.g. so left can become right and vice versa. This transform can help combat positional bias in machine learning models that input multichannel waveforms.

If the input audio is mono, this transform does nothing except emit a warning.

TimeInversion

Added in v0.10.0

Reverse (invert) the audio along the time axis similar to random flip of an image in the visual domain. This can be relevant in the context of audio classification. It was successfully applied in the paper AudioCLIP: Extending CLIP to Image, Text and Audio

Changelog

Unreleased

Added

[v0.11.1] - 2024-02-07

Changed

Fixed

[v0.11.0] - 2022-06-29

Added

Changed

Fixed

[v0.10.1] - 2022-03-24

Added

Fixed

[v0.10.0] - 2022-02-11

Added

Changed

Fixed

[v0.9.1] - 2021-12-20

Added

[v0.9.0] - 2021-10-11

Added

Removed

[v0.8.0] - 2021-06-15

Added

Deprecated

Removed

[v0.7.0] - 2021-04-16

Added

Deprecated

[v0.6.0] - 2021-02-22

Added

[v0.5.1] - 2020-12-18

Fixed

[v0.5.0] - 2020-12-08

Added

Changed

Removed

Fixed

[v0.4.0] - 2020-11-10

Added

[v0.3.0] - 2020-10-27

Added

Changed

[v0.2.0] - 2020-10-19

Added

Changed

[v0.1.0] - 2020-10-12

Added

Development

Setup

A GPU-enabled development environment for torch-audiomentations can be created with conda:

Run tests

pytest

Conventions

Acknowledgements

The development of torch-audiomentations is kindly backed by Nomono.

Thanks to all contributors who help improving torch-audiomentations.