From 3d7abfcb94fa7555bac6d806b05338edd857edf7 Mon Sep 17 00:00:00 2001 From: Todd Stephenson <71510807+toddstep@users.noreply.github.com> Date: Fri, 18 Sep 2020 15:55:44 +0000 Subject: [PATCH 1/3] Add test for warning from create_fb_matrix --- .../functional/functional_cpu_test.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/torchaudio_unittest/functional/functional_cpu_test.py b/test/torchaudio_unittest/functional/functional_cpu_test.py index 96aac4e770..4fbe4d871e 100644 --- a/test/torchaudio_unittest/functional/functional_cpu_test.py +++ b/test/torchaudio_unittest/functional/functional_cpu_test.py @@ -21,6 +21,23 @@ class TestLFilterFloat64(Lfilter, common_utils.PytorchTestCase): device = torch.device('cpu') +class TestCreateFBMatrix(common_utils.TorchaudioTestCase): + def test_no_warning_high_n_freq(self): + with pytest.warns(None) as w: + F.create_fb_matrix(288, 0, 8000, 128, 16000) + assert len(w) == 0 + + def test_no_warning_low_n_mels(self): + with pytest.warns(None) as w: + F.create_fb_matrix(201, 0, 8000, 89, 16000) + assert len(w) == 0 + + def test_warning(self): + with pytest.warns(None) as w: + F.create_fb_matrix(201, 0, 8000, 128, 16000) + assert len(w) == 1 + + class TestComputeDeltas(common_utils.TorchaudioTestCase): """Test suite for correctness of compute_deltas""" def test_one_channel(self): From 1dd8f0dd808b9f3cf635443712ab9582cc63b892 Mon Sep 17 00:00:00 2001 From: Todd Stephenson <71510807+toddstep@users.noreply.github.com> Date: Fri, 18 Sep 2020 15:55:57 +0000 Subject: [PATCH 2/3] Warn if create_fb_matrix produces a column whose weights are all zeros --- torchaudio/functional.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/torchaudio/functional.py b/torchaudio/functional.py index 4e8e62848e..8a7b3babfb 100644 --- a/torchaudio/functional.py +++ b/torchaudio/functional.py @@ -313,6 +313,13 @@ def create_fb_matrix( enorm = 2.0 / (f_pts[2:n_mels + 2] - f_pts[:n_mels]) fb *= enorm.unsqueeze(0) + if (fb.max(dim=0).values == 0.).any(): + warnings.warn( + "At least one mel filterbank has all zero values." + "The value for `n_mels` ({}) may be set too high." + "Or, the value for `n_freqs` ({}) may be set too low.".format(n_mels, n_freqs) + ) + return fb From 45fdc398c994f1eb5711f83a92bc35dd7d5bbd1a Mon Sep 17 00:00:00 2001 From: Todd Stephenson <71510807+toddstep@users.noreply.github.com> Date: Fri, 18 Sep 2020 18:24:19 +0000 Subject: [PATCH 3/3] Clean up formatting of Mel FB warning message --- torchaudio/functional.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/torchaudio/functional.py b/torchaudio/functional.py index 8a7b3babfb..a6514c79f2 100644 --- a/torchaudio/functional.py +++ b/torchaudio/functional.py @@ -315,9 +315,9 @@ def create_fb_matrix( if (fb.max(dim=0).values == 0.).any(): warnings.warn( - "At least one mel filterbank has all zero values." - "The value for `n_mels` ({}) may be set too high." - "Or, the value for `n_freqs` ({}) may be set too low.".format(n_mels, n_freqs) + "At least one mel filterbank has all zero values. " + f"The value for `n_mels` ({n_mels}) may be set too high. " + f"Or, the value for `n_freqs` ({n_freqs}) may be set too low." ) return fb