Skip to content

Commit 94c1c9a

Browse files
[COMPARISON TESTS] Use pre-computed values in librosa compat test (#4018)
Co-authored-by: Sam Anklesaria <[email protected]> Co-authored-by: Nicolas Hug <[email protected]>
1 parent e500f0c commit 94c1c9a

File tree

73 files changed

+116
-51
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+116
-51
lines changed

.github/scripts/unittest-linux/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fi
102102
pip install kaldi-io SoundFile librosa coverage pytest pytest-cov scipy expecttest unidecode inflect Pillow sentencepiece pytorch-lightning 'protobuf<4.21.0' demucs tinytag pyroomacoustics flashlight-text git+https://github.com/kpu/kenlm
103103

104104
# TODO: might be better to fix the single call to `pip install` above
105-
pip install "pillow<10.0" "scipy<1.10" "numpy<2.0"
105+
pip install pillow scipy "numpy>=1.26"
106106
)
107107
# Install fairseq
108108
git clone https://github.com/pytorch/fairseq

.github/workflows/unittest-linux-gpu.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ jobs:
106106
107107
pip3 install parameterized requests
108108
pip3 install kaldi-io SoundFile librosa coverage pytest pytest-cov scipy expecttest unidecode inflect Pillow sentencepiece pytorch-lightning 'protobuf<4.21.0' demucs tinytag
109-
pip3 install "pillow<10.0" "scipy<1.10" "numpy<2.0"
109+
pip3 install pillow scipy "numpy>=1.26"
110110
111111
echo "::endgroup::"
112112
echo "::group::Run tests"

test/librosa_mock.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import re
2+
import os
3+
from pathlib import Path
4+
import torch
5+
6+
def mock_function(f):
7+
"""
8+
Create a mocked version of a function from the librosa library that loads a precomputed result
9+
if it exists. The commented out part otherwise computes the result and saves it for future use.
10+
This is used to compare torchaudio functionality to the equivalent functionalty in librosa without
11+
depending on librosa after results are precomputed.
12+
"""
13+
this_file = Path(__file__).parent.resolve()
14+
expected_results_folder = this_file / "torchaudio_unittest" / "assets" / "librosa_expected_results"
15+
def wrapper(request, *args, **kwargs):
16+
mocked_results = expected_results_folder / f"{request}.pt"
17+
return torch.load(mocked_results, weights_only=False)
18+
19+
# Old definition used for generation:
20+
# if os.path.exists(mocked_results):
21+
# return torch.load(mocked_results, weights_only=False)
22+
# import librosa
23+
# result = eval(f)(*args, **kwargs)
24+
# if request is not None:
25+
# mocked_results.parent.mkdir(parents=True, exist_ok=True)
26+
# torch.save(result, mocked_results)
27+
# return result
28+
return wrapper
29+
30+
griffinlim = mock_function("librosa.griffinlim")
31+
32+
mel = mock_function("librosa.filters.mel")
33+
34+
power_to_db = mock_function("librosa.core.power_to_db")
35+
36+
amplitude_to_db = mock_function("librosa.core.amplitude_to_db")
37+
38+
phase_vocoder = mock_function("librosa.phase_vocoder")
39+
40+
spectrogram = mock_function("librosa.core.spectrum._spectrogram")
41+
42+
mel_spectrogram = mock_function("librosa.feature.melspectrogram")
43+
44+
def _mfcc_from_waveform(waveform, sample_rate, n_fft, hop_length, n_mels, n_mfcc):
45+
import librosa
46+
melspec = librosa.feature.melspectrogram(
47+
y=waveform[0].cpu().numpy(),
48+
sr=sample_rate,
49+
n_fft=n_fft,
50+
win_length=n_fft,
51+
hop_length=hop_length,
52+
n_mels=n_mels,
53+
htk=True,
54+
norm=None,
55+
pad_mode="reflect",
56+
)
57+
return librosa.feature.mfcc(S=librosa.core.power_to_db(melspec), n_mfcc=n_mfcc, dct_type=2, norm="ortho")
58+
59+
mfcc_from_waveform = mock_function("_mfcc_from_waveform")
60+
61+
62+
spectral_centroid = mock_function("librosa.feature.spectral_centroid")

0 commit comments

Comments
 (0)