-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrispeech_prepare.py
456 lines (379 loc) · 12.8 KB
/
librispeech_prepare.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
"""
Data preparation.
Download: http://www.openslr.org/12
Author
------
Mirco Ravanelli, Ju-Chieh Chou, Loren Lugosch 2020
"""
import os
import csv
import random
from collections import Counter
from dataclasses import dataclass
import functools
import logging
from speechbrain.utils.data_utils import download_file, get_all_files
from speechbrain.dataio.dataio import (
load_pkl,
save_pkl,
merge_csvs,
read_audio_info,
)
from speechbrain.utils.parallel import parallel_map
logger = logging.getLogger(__name__)
OPT_FILE = "opt_librispeech_prepare.pkl"
SAMPLERATE = 16000
def prepare_librispeech(
data_folder,
save_folder,
tr_splits=[],
dev_splits=[],
te_splits=[],
select_n_sentences=None,
merge_lst=[],
merge_name=None,
create_lexicon=False,
skip_prep=False,
):
"""
This class prepares the csv files for the LibriSpeech dataset.
Download link: http://www.openslr.org/12
Arguments
---------
data_folder : str
Path to the folder where the original LibriSpeech dataset is stored.
tr_splits : list
List of train splits to prepare from ['test-others','train-clean-100',
'train-clean-360','train-other-500'].
dev_splits : list
List of dev splits to prepare from ['dev-clean','dev-others'].
te_splits : list
List of test splits to prepare from ['test-clean','test-others'].
save_folder : str
The directory where to store the csv files.
select_n_sentences : int
Default : None
If not None, only pick this many sentences.
merge_lst : list
List of librispeech splits (e.g, train-clean, train-clean-360,..) to
merge in a singe csv file.
merge_name: str
Name of the merged csv file.
create_lexicon: bool
If True, it outputs csv files containing mapping between grapheme
to phonemes. Use it for training a G2P system.
skip_prep: bool
If True, data preparation is skipped.
Example
-------
>>> data_folder = 'datasets/LibriSpeech'
>>> tr_splits = ['train-clean-100']
>>> dev_splits = ['dev-clean']
>>> te_splits = ['test-clean']
>>> save_folder = 'librispeech_prepared'
>>> prepare_librispeech(data_folder, save_folder, tr_splits, dev_splits, te_splits)
"""
if skip_prep:
return
data_folder = data_folder
splits = tr_splits + dev_splits + te_splits
save_folder = save_folder
select_n_sentences = select_n_sentences
conf = {
"select_n_sentences": select_n_sentences,
}
# Other variables
# Saving folder
if not os.path.exists(save_folder):
os.makedirs(save_folder)
save_opt = os.path.join(save_folder, OPT_FILE)
# Check if this phase is already done (if so, skip it)
if skip(splits, save_folder, conf):
logger.info("Skipping preparation, completed in previous run.")
return
else:
logger.info("Data_preparation...")
# Additional checks to make sure the data folder contains Librispeech
check_librispeech_folders(data_folder, splits)
# create csv files for each split
all_texts = {}
for split_index in range(len(splits)):
split = splits[split_index]
wav_lst = get_all_files(
os.path.join(data_folder, split), match_and=[".flac"]
)
text_lst = get_all_files(
os.path.join(data_folder, split), match_and=["trans.txt"]
)
text_dict = text_to_dict(text_lst)
all_texts.update(text_dict)
if select_n_sentences is not None:
n_sentences = select_n_sentences[split_index]
else:
n_sentences = len(wav_lst)
create_csv(
save_folder, wav_lst, text_dict, split, n_sentences,
)
# Merging csv file if needed
if merge_lst and merge_name is not None:
merge_files = [split_libri + ".csv" for split_libri in merge_lst]
merge_csvs(
data_folder=save_folder, csv_lst=merge_files, merged_csv=merge_name,
)
# Create lexicon.csv and oov.csv
if create_lexicon:
create_lexicon_and_oov_csv(all_texts, data_folder, save_folder)
# saving options
save_pkl(conf, save_opt)
def create_lexicon_and_oov_csv(all_texts, data_folder, save_folder):
"""
Creates lexicon csv files useful for training and testing a
grapheme-to-phoneme (G2P) model.
Arguments
---------
all_text : dict
Dictionary containing text from the librispeech transcriptions
data_folder : str
Path to the folder where the original LibriSpeech dataset is stored.
save_folder : str
The directory where to store the csv files.
Returns
-------
None
"""
# If the lexicon file does not exist, download it
lexicon_url = "http://www.openslr.org/resources/11/librispeech-lexicon.txt"
lexicon_path = os.path.join(save_folder, "librispeech-lexicon.txt")
if not os.path.isfile(lexicon_path):
logger.info(
"Lexicon file not found. Downloading from %s." % lexicon_url
)
download_file(lexicon_url, lexicon_path)
# Get list of all words in the transcripts
transcript_words = Counter()
for key in all_texts:
transcript_words.update(all_texts[key].split("_"))
# Get list of all words in the lexicon
lexicon_words = []
lexicon_pronunciations = []
with open(lexicon_path, "r") as f:
lines = f.readlines()
for line in lines:
word = line.split()[0]
pronunciation = line.split()[1:]
lexicon_words.append(word)
lexicon_pronunciations.append(pronunciation)
# Create lexicon.csv
header = "ID,duration,char,phn\n"
lexicon_csv_path = os.path.join(save_folder, "lexicon.csv")
with open(lexicon_csv_path, "w") as f:
f.write(header)
for idx in range(len(lexicon_words)):
separated_graphemes = [c for c in lexicon_words[idx]]
duration = len(separated_graphemes)
graphemes = " ".join(separated_graphemes)
pronunciation_no_numbers = [
p.strip("0123456789") for p in lexicon_pronunciations[idx]
]
phonemes = " ".join(pronunciation_no_numbers)
line = (
",".join([str(idx), str(duration), graphemes, phonemes]) + "\n"
)
f.write(line)
logger.info("Lexicon written to %s." % lexicon_csv_path)
# Split lexicon.csv in train, validation, and test splits
split_lexicon(save_folder, [98, 1, 1])
def split_lexicon(data_folder, split_ratio):
"""
Splits the lexicon.csv file into train, validation, and test csv files
Arguments
---------
data_folder : str
Path to the folder containing the lexicon.csv file to split.
split_ratio : list
List containing the training, validation, and test split ratio. Set it
to [80, 10, 10] for having 80% of material for training, 10% for valid,
and 10 for test.
Returns
-------
None
"""
# Reading lexicon.csv
lexicon_csv_path = os.path.join(data_folder, "lexicon.csv")
with open(lexicon_csv_path, "r") as f:
lexicon_lines = f.readlines()
# Remove header
lexicon_lines = lexicon_lines[1:]
# Shuffle entries
random.shuffle(lexicon_lines)
# Selecting lines
header = "ID,duration,char,phn\n"
tr_snts = int(0.01 * split_ratio[0] * len(lexicon_lines))
train_lines = [header] + lexicon_lines[0:tr_snts]
valid_snts = int(0.01 * split_ratio[1] * len(lexicon_lines))
valid_lines = [header] + lexicon_lines[tr_snts : tr_snts + valid_snts]
test_lines = [header] + lexicon_lines[tr_snts + valid_snts :]
# Saving files
with open(os.path.join(data_folder, "lexicon_tr.csv"), "w") as f:
f.writelines(train_lines)
with open(os.path.join(data_folder, "lexicon_dev.csv"), "w") as f:
f.writelines(valid_lines)
with open(os.path.join(data_folder, "lexicon_test.csv"), "w") as f:
f.writelines(test_lines)
@dataclass
class LSRow:
snt_id: str
spk_id: str
duration: float
file_path: str
words: str
def process_line(wav_file, text_dict) -> LSRow:
snt_id = wav_file.split("/")[-1].replace(".flac", "")
spk_id = "-".join(snt_id.split("-")[0:2])
wrds = text_dict[snt_id]
wrds = " ".join(wrds.split("_"))
info = read_audio_info(wav_file)
duration = info.num_frames / info.sample_rate
return LSRow(
snt_id=snt_id,
spk_id=spk_id,
duration=duration,
file_path=wav_file,
words=wrds,
)
def create_csv(
save_folder, wav_lst, text_dict, split, select_n_sentences,
):
"""
Create the dataset csv file given a list of wav files.
Arguments
---------
save_folder : str
Location of the folder for storing the csv.
wav_lst : list
The list of wav files of a given data split.
text_dict : list
The dictionary containing the text of each sentence.
split : str
The name of the current data split.
select_n_sentences : int, optional
The number of sentences to select.
Returns
-------
None
"""
# Setting path for the csv file
csv_file = os.path.join(save_folder, split + ".csv")
if os.path.exists(csv_file):
logger.info("Csv file %s already exists, not recreating." % csv_file)
return
# Preliminary prints
msg = "Creating csv lists in %s..." % (csv_file)
logger.info(msg)
csv_lines = [["ID", "duration", "wav", "spk_id", "wrd"]]
snt_cnt = 0
line_processor = functools.partial(process_line, text_dict=text_dict)
# Processing all the wav files in wav_lst
# FLAC metadata reading is already fast, so we set a high chunk size
# to limit main thread CPU bottlenecks
for row in parallel_map(line_processor, wav_lst, chunk_size=8192):
csv_line = [
row.snt_id,
str(row.duration),
row.file_path,
row.spk_id,
row.words,
]
# Appending current file to the csv_lines list
csv_lines.append(csv_line)
snt_cnt = snt_cnt + 1
# parallel_map guarantees element ordering so we're OK
if snt_cnt == select_n_sentences:
break
# Writing the csv_lines
with open(csv_file, mode="w") as csv_f:
csv_writer = csv.writer(
csv_f, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
for line in csv_lines:
csv_writer.writerow(line)
# Final print
msg = "%s successfully created!" % (csv_file)
logger.info(msg)
def skip(splits, save_folder, conf):
"""
Detect when the librispeech data prep can be skipped.
Arguments
---------
splits : list
A list of the splits expected in the preparation.
save_folder : str
The location of the seave directory
conf : dict
The configuration options to ensure they haven't changed.
Returns
-------
bool
if True, the preparation phase can be skipped.
if False, it must be done.
"""
# Checking csv files
skip = True
for split in splits:
if not os.path.isfile(os.path.join(save_folder, split + ".csv")):
skip = False
# Checking saved options
save_opt = os.path.join(save_folder, OPT_FILE)
if skip is True:
if os.path.isfile(save_opt):
opts_old = load_pkl(save_opt)
if opts_old == conf:
skip = True
else:
skip = False
else:
skip = False
return skip
def text_to_dict(text_lst):
"""
This converts lines of text into a dictionary-
Arguments
---------
text_lst : str
Path to the file containing the librispeech text transcription.
Returns
-------
dict
The dictionary containing the text transcriptions for each sentence.
"""
# Initialization of the text dictionary
text_dict = {}
# Reading all the transcription files is text_lst
for file in text_lst:
with open(file, "r") as f:
# Reading all line of the transcription file
for line in f:
line_lst = line.strip().split(" ")
text_dict[line_lst[0]] = "_".join(line_lst[1:])
return text_dict
def check_librispeech_folders(data_folder, splits):
"""
Check if the data folder actually contains the LibriSpeech dataset.
If it does not, an error is raised.
Returns
-------
None
Raises
------
OSError
If LibriSpeech is not found at the specified path.
"""
# Checking if all the splits exist
for split in splits:
split_folder = os.path.join(data_folder, split)
if not os.path.exists(split_folder):
err_msg = (
"the folder %s does not exist (it is expected in the "
"Librispeech dataset)" % split_folder
)
raise OSError(err_msg)