audiomentions is a very convenient library for my bird sound classification. As the code below:
from audiomentations import Compose, AddGaussianNoise, AddGaussianSNR, TimeStretch, PitchShift self.augment = Compose([ AddGaussianNoise(min_amplitude=0.005, max_amplitude=0.015, p=poss), AddGaussianSNR(min_snr_in_db=5.0, max_snr_in_db=40.0, p=poss), TimeStretch(min_rate=0.8, max_rate=1.2, p=poss), PitchShift(min_semitones=-2, max_semitones=2, p=poss) ])
These four augmentation methods are enough for current training. But the PitchShift
method will cost a lot of CPU resources therefore the GPU couldn’t run to full load and the CPU usage jumps to 100%.
Failed to find an audio augmentation library that uses GPU, I started to check the source code of “audiomentions” and noticed that it uses librosa as its implementation:
try: pitch_shifted_samples = librosa.effects.pitch_shift( samples, sr=sample_rate, n_steps=self.parameters["num_semitones"] ) except librosa.util.exceptions.ParameterError:
Then the code of “librosa” for “pitch_shift”:
def pitch_shift( y: np.ndarray, *, sr: float, n_steps: float, bins_per_octave: int = 12, res_type: str = "soxr_hq", scale: bool = False, **kwargs: Any, ) -> np.ndarray:
The default “res_type” for “pitch_shift” is “soxr_hq”. This is a slow resource. After changing “it”res_type” to “linear” in “audiomentions”, the CPU usage jumps back to 50% on my desktop and the GPU ramp up to 100% when training.