Skip to content

Commit 010583b

Browse files
hwangjefffacebook-github-bot
authored andcommitted
Refactor LibriSpeech tests to accommodate different dataset classes (pytorch#2392)
Summary: Pull Request resolved: pytorch#2392 Refactors LibriSpeech tests to accommodate different dataset classes Reviewed By: xiaohui-zhang Differential Revision: D36387835 fbshipit-source-id: 73b4e7565b4a077b25f036f4bd854ac7f2194b28
1 parent 07ace38 commit 010583b

File tree

2 files changed

+98
-92
lines changed

2 files changed

+98
-92
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,7 @@
1-
import os
2-
from pathlib import Path
3-
41
from torchaudio.datasets import librispeech
5-
from torchaudio_unittest.common_utils import (
6-
get_whitenoise,
7-
normalize_wav,
8-
save_wav,
9-
TempDirMixin,
10-
TorchaudioTestCase,
11-
)
12-
13-
# Used to generate a unique transcript for each dummy audio file
14-
_NUMBERS = ["ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"]
15-
16-
17-
def get_mock_dataset(root_dir):
18-
"""
19-
root_dir: directory to the mocked dataset
20-
"""
21-
mocked_data = []
22-
dataset_dir = os.path.join(root_dir, librispeech.FOLDER_IN_ARCHIVE, librispeech.URL)
23-
os.makedirs(dataset_dir, exist_ok=True)
24-
sample_rate = 16000 # 16kHz
25-
seed = 0
26-
27-
for speaker_id in range(5):
28-
speaker_path = os.path.join(dataset_dir, str(speaker_id))
29-
os.makedirs(speaker_path, exist_ok=True)
30-
31-
for chapter_id in range(3):
32-
chapter_path = os.path.join(speaker_path, str(chapter_id))
33-
os.makedirs(chapter_path, exist_ok=True)
34-
trans_content = []
35-
36-
for utterance_id in range(10):
37-
filename = f"{speaker_id}-{chapter_id}-{utterance_id:04d}.wav"
38-
path = os.path.join(chapter_path, filename)
39-
40-
transcript = " ".join([_NUMBERS[x] for x in [speaker_id, chapter_id, utterance_id]])
41-
trans_content.append(f"{speaker_id}-{chapter_id}-{utterance_id:04d} {transcript}")
42-
43-
data = get_whitenoise(sample_rate=sample_rate, duration=0.01, n_channels=1, dtype="float32", seed=seed)
44-
save_wav(path, data, sample_rate)
45-
sample = (normalize_wav(data), sample_rate, transcript, speaker_id, chapter_id, utterance_id)
46-
mocked_data.append(sample)
47-
48-
seed += 1
49-
50-
trans_filename = f"{speaker_id}-{chapter_id}.trans.txt"
51-
trans_path = os.path.join(chapter_path, trans_filename)
52-
with open(trans_path, "w") as f:
53-
f.write("\n".join(trans_content))
54-
return mocked_data
55-
56-
57-
class TestLibriSpeech(TempDirMixin, TorchaudioTestCase):
58-
backend = "default"
59-
60-
root_dir = None
61-
samples = []
62-
63-
@classmethod
64-
def setUpClass(cls):
65-
cls.root_dir = cls.get_base_temp_dir()
66-
cls.samples = get_mock_dataset(cls.root_dir)
67-
68-
@classmethod
69-
def tearDownClass(cls):
70-
# In case of test failure
71-
librispeech.LIBRISPEECH._ext_audio = ".flac"
72-
73-
def _test_librispeech(self, dataset):
74-
num_samples = 0
75-
for i, (data, sample_rate, transcript, speaker_id, chapter_id, utterance_id) in enumerate(dataset):
76-
self.assertEqual(data, self.samples[i][0], atol=5e-5, rtol=1e-8)
77-
assert sample_rate == self.samples[i][1]
78-
assert transcript == self.samples[i][2]
79-
assert speaker_id == self.samples[i][3]
80-
assert chapter_id == self.samples[i][4]
81-
assert utterance_id == self.samples[i][5]
82-
num_samples += 1
83-
84-
assert num_samples == len(self.samples)
85-
librispeech.LIBRISPEECH._ext_audio = ".flac"
2+
from torchaudio_unittest.common_utils import TorchaudioTestCase
3+
from torchaudio_unittest.datasets.librispeech_test_impl import LibriSpeechTestMixin
864

87-
def test_librispeech_str(self):
88-
librispeech.LIBRISPEECH._ext_audio = ".wav"
89-
dataset = librispeech.LIBRISPEECH(self.root_dir)
90-
self._test_librispeech(dataset)
915

92-
def test_librispeech_path(self):
93-
librispeech.LIBRISPEECH._ext_audio = ".wav"
94-
dataset = librispeech.LIBRISPEECH(Path(self.root_dir))
95-
self._test_librispeech(dataset)
6+
class TestLibriSpeech(LibriSpeechTestMixin, TorchaudioTestCase):
7+
librispeech_cls = librispeech.LIBRISPEECH
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import os
2+
from pathlib import Path
3+
4+
from torchaudio.datasets import librispeech
5+
from torchaudio_unittest.common_utils import (
6+
get_whitenoise,
7+
normalize_wav,
8+
save_wav,
9+
TempDirMixin,
10+
)
11+
12+
# Used to generate a unique transcript for each dummy audio file
13+
_NUMBERS = ["ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"]
14+
15+
16+
def get_mock_dataset(root_dir):
17+
"""
18+
root_dir: directory to the mocked dataset
19+
"""
20+
mocked_data = []
21+
dataset_dir = os.path.join(root_dir, librispeech.FOLDER_IN_ARCHIVE, librispeech.URL)
22+
os.makedirs(dataset_dir, exist_ok=True)
23+
sample_rate = 16000 # 16kHz
24+
seed = 0
25+
26+
for speaker_id in range(5):
27+
speaker_path = os.path.join(dataset_dir, str(speaker_id))
28+
os.makedirs(speaker_path, exist_ok=True)
29+
30+
for chapter_id in range(3):
31+
chapter_path = os.path.join(speaker_path, str(chapter_id))
32+
os.makedirs(chapter_path, exist_ok=True)
33+
trans_content = []
34+
35+
for utterance_id in range(10):
36+
filename = f"{speaker_id}-{chapter_id}-{utterance_id:04d}.wav"
37+
path = os.path.join(chapter_path, filename)
38+
39+
transcript = " ".join([_NUMBERS[x] for x in [speaker_id, chapter_id, utterance_id]])
40+
trans_content.append(f"{speaker_id}-{chapter_id}-{utterance_id:04d} {transcript}")
41+
42+
data = get_whitenoise(sample_rate=sample_rate, duration=0.01, n_channels=1, dtype="float32", seed=seed)
43+
save_wav(path, data, sample_rate)
44+
sample = (normalize_wav(data), sample_rate, transcript, speaker_id, chapter_id, utterance_id)
45+
mocked_data.append(sample)
46+
47+
seed += 1
48+
49+
trans_filename = f"{speaker_id}-{chapter_id}.trans.txt"
50+
trans_path = os.path.join(chapter_path, trans_filename)
51+
with open(trans_path, "w") as f:
52+
f.write("\n".join(trans_content))
53+
return mocked_data
54+
55+
56+
class LibriSpeechTestMixin(TempDirMixin):
57+
backend = "default"
58+
59+
root_dir = None
60+
samples = []
61+
62+
@classmethod
63+
def setUpClass(cls):
64+
cls.root_dir = cls.get_base_temp_dir()
65+
cls.samples = get_mock_dataset(cls.root_dir)
66+
67+
@classmethod
68+
def tearDownClass(cls):
69+
# In case of test failure
70+
cls.librispeech_cls._ext_audio = ".flac"
71+
72+
def _test_librispeech(self, dataset):
73+
num_samples = 0
74+
for i, (data, sample_rate, transcript, speaker_id, chapter_id, utterance_id) in enumerate(dataset):
75+
self.assertEqual(data, self.samples[i][0], atol=5e-5, rtol=1e-8)
76+
assert sample_rate == self.samples[i][1]
77+
assert transcript == self.samples[i][2]
78+
assert speaker_id == self.samples[i][3]
79+
assert chapter_id == self.samples[i][4]
80+
assert utterance_id == self.samples[i][5]
81+
num_samples += 1
82+
83+
assert num_samples == len(self.samples)
84+
self.librispeech_cls._ext_audio = ".flac"
85+
86+
def test_librispeech_str(self):
87+
self.librispeech_cls._ext_audio = ".wav"
88+
dataset = self.librispeech_cls(self.root_dir)
89+
self._test_librispeech(dataset)
90+
91+
def test_librispeech_path(self):
92+
self.librispeech_cls._ext_audio = ".wav"
93+
dataset = self.librispeech_cls(Path(self.root_dir))
94+
self._test_librispeech(dataset)

0 commit comments

Comments
 (0)