forked from NVIDIA/NeMo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_unit_asr.py
328 lines (292 loc) · 11.9 KB
/
test_unit_asr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# =============================================================================
# Copyright 2020 NVIDIA. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
import os
import shutil
import tarfile
import unittest
from unittest import TestCase
import pytest
from ruamel.yaml import YAML
import nemo
import nemo.collections.asr as nemo_asr
from nemo.collections.asr.parts import AudioDataset, WaveformFeaturizer, collections, parsers
from nemo.core import DeviceType
logging = nemo.logging
freq = 16000
@pytest.mark.usefixtures("neural_factory")
class TestASRPytorch(TestCase):
labels = [
" ",
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
"'",
]
manifest_filepath = os.path.abspath(os.path.join(os.path.dirname(__file__), "../data/asr/an4_train.json"))
featurizer_config = {
'window': 'hann',
'dither': 1e-05,
'normalize': 'per_feature',
'frame_splicing': 1,
'int_values': False,
'window_stride': 0.01,
'sample_rate': freq,
'features': 64,
'n_fft': 512,
'window_size': 0.02,
}
yaml = YAML(typ="safe")
@classmethod
def setUpClass(cls) -> None:
super().setUpClass()
data_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "../data/"))
logging.info("Looking up for test ASR data")
if not os.path.exists(os.path.join(data_folder, "asr")):
logging.info("Extracting ASR data to: {0}".format(os.path.join(data_folder, "asr")))
tar = tarfile.open(os.path.join(data_folder, "asr.tar.gz"), "r:gz")
tar.extractall(path=data_folder)
tar.close()
else:
logging.info("ASR data found in: {0}".format(os.path.join(data_folder, "asr")))
@pytest.mark.unit
def test_transcript_normalizers(self):
# Create test json
test_strings = [
"TEST CAPITALIZATION",
'!\\"#$%&\'()*+,-./:;<=>?@[\\\\]^_`{|}~',
"3+3=10",
"3 + 3 = 10",
"why is \\t whitepsace\\tsuch a problem why indeed",
"\\\"Can you handle quotes?,\\\" says the boy",
"I Jump!!!!With joy?Now.",
"Maybe I want to learn periods.",
"$10 10.90 1-800-000-0000",
"18000000000 one thousand 2020",
"1 10 100 1000 10000 100000 1000000",
"Î ĻƠvɆȩȅĘ ÀÁÃ Ą ÇĊňńŤŧș",
"‘’“”❛❜❝❞「 」 〈 〉 《 》 【 】 〔 〕 ⦗ ⦘ 😙 👀 🔨",
"It only costs $1 000 000! Cheap right?",
"2500, 3000 are separate but 200, 125 is not",
"1",
"1 2",
"1 2 3",
"10:00pm is 10:00 pm is 22:00 but not 10: 00 pm",
"10:00 10:01pm 10:10am 10:90pm",
"Mr. Expand me!",
"Mr Don't Expand me!",
]
normalized_strings = [
"test capitalization",
'percent and \' plus',
"three plus three ten",
"three plus three ten",
"why is whitepsace such a problem why indeed",
"can you handle quotes says the boy",
"i jump with joy now",
"maybe i want to learn periods",
"ten dollars ten point nine zero one eight hundred zero zero",
"eighteen billion one thousand two thousand and twenty",
# Two line string below
"one ten thousand one hundred one thousand ten thousand one hundred thousand one million",
"i loveeee aaa a ccnntts",
"''",
"it only costs one million dollars cheap right",
# Two line string below
"two thousand five hundred three thousand are separate but two "
"hundred thousand one hundred and twenty five is not",
"one",
"one two",
"one two three",
"ten pm is ten pm is twenty two but not ten zero pm",
"ten ten one pm ten ten am ten ninety pm",
"mister expand me",
"mr don't expand me",
]
manifest_paths = os.path.abspath(os.path.join(os.path.dirname(__file__), "../data/asr/manifest_test.json"))
def remove_test_json():
os.remove(manifest_paths)
self.addCleanup(remove_test_json)
with open(manifest_paths, "w") as f:
for s in test_strings:
f.write('{"audio_filepath": "", "duration": 1.0, "text": ' f'"{s}"}}\n')
parser = parsers.make_parser(self.labels, 'en')
manifest = collections.ASRAudioText(manifests_files=[manifest_paths], parser=parser,)
for i, s in enumerate(normalized_strings):
self.assertTrue(manifest[i].text_tokens == parser(s))
@pytest.mark.unit
def test_pytorch_audio_dataset(self):
featurizer = WaveformFeaturizer.from_config(self.featurizer_config)
ds = AudioDataset(manifest_filepath=self.manifest_filepath, labels=self.labels, featurizer=featurizer,)
for i in range(len(ds)):
if i == 5:
logging.info(ds[i])
# logging.info(ds[i][0].shape)
# self.assertEqual(freq, ds[i][0].shape[0])
@pytest.mark.unit
def test_dataloader(self):
batch_size = 4
dl = nemo_asr.AudioToTextDataLayer(
# featurizer_config=self.featurizer_config,
manifest_filepath=self.manifest_filepath,
labels=self.labels,
batch_size=batch_size,
# placement=DeviceType.GPU,
drop_last=True,
)
for ind, data in enumerate(dl.data_iterator):
# With num_workers update, this is no longer true
# Moving to GPU is handled by AudioPreprocessor
# data is on GPU
# self.assertTrue(data[0].is_cuda)
# self.assertTrue(data[1].is_cuda)
# self.assertTrue(data[2].is_cuda)
# self.assertTrue(data[3].is_cuda)
# first dimension is batch
self.assertTrue(data[0].size(0) == batch_size)
self.assertTrue(data[1].size(0) == batch_size)
self.assertTrue(data[2].size(0) == batch_size)
self.assertTrue(data[3].size(0) == batch_size)
@pytest.mark.unit
def test_preprocessor_errors(self):
def create_broken_preprocessor_1():
nemo_asr.AudioToMelSpectrogramPreprocessor(window_size=2, n_window_size=2)
def create_broken_preprocessor_2():
nemo_asr.AudioToMelSpectrogramPreprocessor(window_stride=2, n_window_stride=2)
def create_broken_preprocessor_3():
nemo_asr.AudioToMelSpectrogramPreprocessor(n_window_stride=2)
def create_good_preprocessor_1():
nemo_asr.AudioToMelSpectrogramPreprocessor(window_size=0.02, window_stride=0.01)
def create_good_preprocessor_2():
nemo_asr.AudioToMelSpectrogramPreprocessor(
window_size=None, window_stride=None, n_window_size=256, n_window_stride=32,
)
self.assertRaises(ValueError, create_broken_preprocessor_1)
self.assertRaises(ValueError, create_broken_preprocessor_2)
self.assertRaises(ValueError, create_broken_preprocessor_3)
create_good_preprocessor_1()
create_good_preprocessor_2()
@pytest.mark.unit
def test_kaldi_dataloader(self):
batch_size = 4
dl = nemo_asr.KaldiFeatureDataLayer(
kaldi_dir=os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/asr/kaldi_an4/')),
labels=self.labels,
batch_size=batch_size,
)
for data in dl.data_iterator:
self.assertTrue(data[0].size(0) == batch_size)
dl_test_min = nemo_asr.KaldiFeatureDataLayer(
kaldi_dir=os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/asr/kaldi_an4/')),
labels=self.labels,
batch_size=batch_size,
min_duration=1.0,
)
self.assertTrue(len(dl_test_min) == 18)
dl_test_max = nemo_asr.KaldiFeatureDataLayer(
kaldi_dir=os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/asr/kaldi_an4/')),
labels=self.labels,
batch_size=batch_size,
max_duration=5.0,
)
self.assertTrue(len(dl_test_max) == 19)
@pytest.mark.unit
def test_trim_silence(self):
batch_size = 4
normal_dl = nemo_asr.AudioToTextDataLayer(
# featurizer_config=self.featurizer_config,
manifest_filepath=self.manifest_filepath,
labels=self.labels,
batch_size=batch_size,
# placement=DeviceType.GPU,
drop_last=True,
shuffle=False,
)
trimmed_dl = nemo_asr.AudioToTextDataLayer(
# featurizer_config=self.featurizer_config,
manifest_filepath=self.manifest_filepath,
trim_silence=True,
labels=self.labels,
batch_size=batch_size,
# placement=DeviceType.GPU,
drop_last=True,
shuffle=False,
)
for norm, trim in zip(normal_dl.data_iterator, trimmed_dl.data_iterator):
for point in range(batch_size):
self.assertTrue(norm[1][point].data >= trim[1][point].data)
@pytest.mark.unit
def test_audio_preprocessors(self):
batch_size = 5
dl = nemo_asr.AudioToTextDataLayer(
# featurizer_config=self.featurizer_config,
manifest_filepath=self.manifest_filepath,
labels=self.labels,
batch_size=batch_size,
# placement=DeviceType.GPU,
drop_last=True,
shuffle=False,
)
installed_torchaudio = True
try:
import torchaudio
except ModuleNotFoundError:
installed_torchaudio = False
with self.assertRaises(ModuleNotFoundError):
to_spectrogram = nemo_asr.AudioToSpectrogramPreprocessor(n_fft=400, window=None)
with self.assertRaises(ModuleNotFoundError):
to_mfcc = nemo_asr.AudioToMFCCPreprocessor(n_mfcc=15)
if installed_torchaudio:
to_spectrogram = nemo_asr.AudioToSpectrogramPreprocessor(n_fft=400, window=None)
to_mfcc = nemo_asr.AudioToMFCCPreprocessor(n_mfcc=15)
to_melspec = nemo_asr.AudioToMelSpectrogramPreprocessor(features=50)
for batch in dl.data_iterator:
input_signals, seq_lengths, _, _ = batch
input_signals = input_signals.to(to_melspec._device)
seq_lengths = seq_lengths.to(to_melspec._device)
melspec = to_melspec.forward(input_signals, seq_lengths)
if installed_torchaudio:
spec = to_spectrogram.forward(input_signals, seq_lengths)
mfcc = to_mfcc.forward(input_signals, seq_lengths)
# Check that number of features is what we expect
self.assertTrue(melspec[0].shape[1] == 50)
if installed_torchaudio:
self.assertTrue(spec[0].shape[1] == 201) # n_fft // 2 + 1 bins
self.assertTrue(mfcc[0].shape[1] == 15)