forked from speechbrain/speechbrain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrescuespeech_prepare.py
executable file
·629 lines (518 loc) · 16.7 KB
/
rescuespeech_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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
"""
Data preparation script for RescueSpeech dataset. This
script prepares CSV files for ASR and Speech Enhancement.
In the generated CSV files the column-
`clean_noisy_mix` : alternates between the paths to the clean and
noisy speech recordings in the same order as they appear in the dataset.
By using this script, you can easily prepare the necessary CSV files
for training and evaluating ASR models on the RescueSpeech dataset.
Author
------
Sangeet Sagar 2023
(while some functions have been
adapted from the CommonVoice recipe)
"""
import csv
import glob
import os
import re
import unicodedata
import torchaudio
from tqdm import tqdm
from tqdm.contrib import tzip
from speechbrain.dataio.dataio import read_audio
from speechbrain.utils.logger import get_logger
logger = get_logger(__name__)
def prepare_RescueSpeech(
data_folder,
save_folder,
train_tsv_file=None,
dev_tsv_file=None,
test_tsv_file=None,
accented_letters=False,
skip_prep=False,
sample_rate=16000,
task="asr",
):
"""
Prepares the csv files for RescueSpeech audio data.
Arguments
---------
data_folder : str
Path to the folder where the original dataset is stored.
save_folder : str
The directory where to store the csv files.
train_tsv_file : str, optional
Path to the Train RescueSpeech .tsv file (cs)
dev_tsv_file : str, optional
Path to the Dev RescueSpeech .tsv file (cs)
test_tsv_file : str, optional
Path to the Test RescueSpeech .tsv file (cs)
accented_letters : bool, optional
Defines if accented letters will be kept as individual letters or
transformed to the closest non-accented letters.
skip_prep: bool
If True, skip data preparation.
sample_rate: int, optional
Sample rate of the wav files.
task: str, optional
States the task for which data prepration is being done.
It can either be 'asr' or 'enhance'
Returns
-------
None
"""
if skip_prep:
return
# If not specified point toward standard location
if train_tsv_file is None:
train_tsv_file = data_folder + "/train.tsv"
else:
train_tsv_file = train_tsv_file
if dev_tsv_file is None:
dev_tsv_file = data_folder + "/dev.tsv"
else:
dev_tsv_file = dev_tsv_file
if test_tsv_file is None:
test_tsv_file = data_folder + "/test.tsv"
else:
test_tsv_file = test_tsv_file
# Setting the save folder
if not os.path.exists(save_folder):
os.makedirs(save_folder)
# Setting output files
save_csv_train = save_folder + "/train.csv"
save_csv_dev = save_folder + "/dev.csv"
save_csv_test = save_folder + "/test.csv"
# If csv already exists, we skip the data preparation
if skip(save_csv_train, save_csv_dev, save_csv_test):
msg = "%s already exists, skipping data preparation!" % (save_csv_train)
logger.info(msg)
msg = "%s already exists, skipping data preparation!" % (save_csv_dev)
logger.info(msg)
msg = "%s already exists, skipping data preparation!" % (save_csv_test)
logger.info(msg)
return
# Additional checks to make sure the data folder contains RescueSpeech data.
check_RescueSpeech_data_folders(data_folder)
# Creating csv files for {train, dev, test} data
file_pairs = zip(
[train_tsv_file, dev_tsv_file, test_tsv_file],
[save_csv_train, save_csv_dev, save_csv_test],
)
if task == "asr":
for tsv_file, save_csv in file_pairs:
# Prepare CSV files
create_asr_csv(tsv_file, save_csv, data_folder, accented_letters)
elif task == "enhance":
create_enhance_csv(data_folder, save_csv_train, "train", sample_rate)
create_enhance_csv(data_folder, save_csv_dev, "valid", sample_rate)
create_enhance_csv(data_folder, save_csv_test, "test", sample_rate)
def skip(save_csv_train, save_csv_dev, save_csv_test):
"""
Detects if the RescueSpeech data preparation has been already done.
If the preparation has been done, we can skip it.
Arguments
---------
save_csv_train : str
Path to train csv
save_csv_dev : str
Path to dev csv
save_csv_test : str
Path to test csv
Returns
-------
bool
if True, the preparation phase can be skipped.
if False, it must be done.
"""
# Checking folders and save options
skip = False
if (
os.path.isfile(save_csv_train)
and os.path.isfile(save_csv_dev)
and os.path.isfile(save_csv_test)
):
skip = True
return skip
def create_asr_csv(
orig_tsv_file, csv_file, data_folder, accented_letters=False
):
"""
Creates the csv file given a list of wav files.
Arguments
---------
orig_tsv_file : str
Path to the RescueSpeech tsv file (standard file).
csv_file: str
Path to csv file that will be saved.
data_folder : str
Path of the RescueSpeech domain dataset (clean, noisy, noise).
accented_letters : bool, optional
Defines if accented letters will be kept as individual letters or
transformed to the closest non-accented letters.
"""
# Check if the given files exists
if not os.path.isfile(orig_tsv_file):
msg = "\t%s doesn't exist, verify your dataset!" % (orig_tsv_file)
logger.info(msg)
raise FileNotFoundError(msg)
# We load and skip the header
loaded_csv = open(orig_tsv_file, "r").readlines()[1:]
nb_samples = str(len(loaded_csv))
msg = "Preparing CSV files for %s samples ..." % (str(nb_samples))
logger.info(msg)
# Adding some Prints
msg = "Creating csv lists in %s ..." % (csv_file)
logger.info(msg)
csv_lines = [
[
"ID",
"duration",
"clean_wav",
"noisy_wav",
"clean_noisy_mix",
"noise_wav",
"noise_type",
"snr_level",
"spk_id",
"wrd",
]
]
# Noise types
noise_types = [
"Breathing-noise",
"Emergency-vehicle-and-siren-noise",
"Engine-noise",
"Chopper-noise",
"Static-radio-noise",
]
idx = 0
# Start processing lines
total_duration = 0.0
for line in tzip(loaded_csv):
line = line[0]
clean_data_fp = os.path.join(data_folder, "audio_files/clean")
noisy_data_fp = os.path.join(data_folder, "audio_files/noisy")
noise_data_fp = os.path.join(data_folder, "audio_files/noise")
clean_fp = os.path.join(clean_data_fp, line.split("\t")[1])
file_name = ".".join(clean_fp.split(".")).split("/")[-1]
spk_id = line.split("\t")[0]
snt_id = file_name
# Retrieve the corresponding noisy file from noisy data file path
clean_wav_bname = os.path.splitext(file_name)[0] + "_"
noisy_file = [
filename
for filename in os.listdir(noisy_data_fp)
if filename.startswith(clean_wav_bname)
]
noisy_file = noisy_file[0]
noisy_fp = os.path.join(noisy_data_fp, noisy_file)
# alternate between clean and noisy wav
idx += 1
if idx % 2 == 0:
clean_noisy_mix = clean_fp
else:
clean_noisy_mix = noisy_fp
# Get corresponding noise file
fields = os.path.splitext(noisy_file)[0].split("_")
fileid = fields[fields.index("fileid") + 1]
noise_file = "noise_fileid_" + str(fileid) + ".wav"
# clean_file = "clean_fileid_" + str(fileid) + ".wav"
noise_fp = os.path.join(noise_data_fp, noise_file)
# Get noise type
for item in noise_types:
if item in noisy_file:
noise_type = item
break
# Get SNR level
for item in fields:
if "snr" in item:
snr_level = item.replace("snr", "")
break
# Reading the signal (to retrieve duration in seconds)
if os.path.isfile(clean_fp):
info = torchaudio.info(clean_fp)
info_noisy = torchaudio.info(noisy_fp)
else:
msg = "\tError loading: %s" % (str(len(file_name)))
logger.info(msg)
idx += 1
continue
duration = info.num_frames / info.sample_rate
# Do some sanity check duration of clean, and noisy must be same
duration_noisy = info_noisy.num_frames / info_noisy.sample_rate
if round(duration, 3) != round(duration_noisy, 3):
print("Length mismatch detected")
total_duration += duration
# Getting transcript
words = line.split("\t")[2]
# Unicode Normalization
words = unicode_normalisation(words)
# Perform data cleaning
words = data_cleaning(words)
# Remove accents if specified
if not accented_letters:
words = strip_accents(words)
words = words.replace("'", " ")
words = words.replace("’", " ")
# Remove multiple spaces
words = re.sub(" +", " ", words)
# Remove spaces at the beginning and the end of the sentence
words = words.lstrip().rstrip()
# Getting chars
chars = words.replace(" ", "_")
chars = " ".join([char for char in chars][:])
# Remove too short sentences (or empty):
if len(words.split(" ")) < 3:
idx += 1
continue
# Composition of the csv_line
csv_line = [
snt_id,
str(duration),
clean_fp,
noisy_fp,
clean_noisy_mix,
noise_fp,
noise_type,
str(snr_level),
spk_id,
str(words),
]
# Adding this line to the csv_lines list
csv_lines.append(csv_line)
# Writing the csv lines
with open(csv_file, mode="w", encoding="utf-8") 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 prints
msg = "%s successfully created!" % (csv_file)
logger.info(msg)
msg = "Number of samples: %s " % (str(len(loaded_csv)))
logger.info(msg)
msg = "Total duration: %s Hours" % (str(round(total_duration / 3600, 2)))
logger.info(msg)
def create_enhance_csv(data_folder, csv_file, split, fs=16000):
"""
Create CSV files for train, valid and test set.
Arguments
---------
data_folder : str
Path to synthesized RescuSpeech data for task enhancement
csv_file : str
Save csv_file path for prepared data.
split : str
CSV prepration for train/valid/test
fs : int
Sampling rate. Defaults to 16000.
"""
clean_fullpaths = []
noise_fullpaths = []
noisy_fullpaths = []
language = []
lang = "de"
clean_f1_path = extract_files(
os.path.join(data_folder, split), type="clean"
)
noise_f1_path = extract_files(
os.path.join(data_folder, split), type="noise"
)
noisy_f1_path = extract_files(
os.path.join(data_folder, split), type="noisy"
)
language.extend([lang] * len(clean_f1_path))
clean_fullpaths.extend(clean_f1_path)
noise_fullpaths.extend(noise_f1_path)
noisy_fullpaths.extend(noisy_f1_path)
# Write CSV for train and dev
msg = "Writing " + split + " csv files"
logger.info(msg)
write2csv(
language,
clean_fullpaths,
noise_fullpaths,
noisy_fullpaths,
csv_file,
fs,
)
def write2csv(
language,
clean_fullpaths,
noise_fullpaths,
noisy_fullpaths,
csv_file,
fs=16000,
):
"""
Write data to CSV file in an appropriate format.
Arguments
---------
language : str
Language of audio file
clean_fullpaths : str
Path to clean audio files of a split in the train/valid-set
noise_fullpaths : str
Path to noise audio files of a split in the train/valid-set
noisy_fullpaths : str
Path to noisy audio files of a split in the train/valid-set
csv_file : str
Save csv_file path for prepared data.
fs : int
Sampling rate. Defaults to 16000.
"""
csv_columns = [
"ID",
"language",
"duration",
"clean_wav",
"clean_wav_format",
"clean_wav_opts",
"noise_wav",
"noise_wav_format",
"noise_wav_opts",
"noisy_wav",
"noisy_wav_format",
"noisy_wav_opts",
]
# Retrieve duration of just one signal. It is assumed
# that all files have the same duration in MS-DNS dataset.
total_duration = 0
with open(csv_file, "w") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for i, (lang, clean_fp, noise_fp, noisy_fp) in enumerate(
tqdm(
zip(language, clean_fullpaths, noise_fullpaths, noisy_fullpaths)
)
):
signal = read_audio(clean_fp)
duration = signal.shape[0] / fs
total_duration += duration
row = {
"ID": i,
"language": lang,
"duration": duration,
"clean_wav": clean_fp,
"clean_wav_format": "wav",
"clean_wav_opts": None,
"noise_wav": noise_fp,
"noise_wav_format": "wav",
"noise_wav_opts": None,
"noisy_wav": noisy_fp,
"noisy_wav_format": "wav",
"noisy_wav_opts": None,
}
writer.writerow(row)
# Final prints
msg = "%s successfully created!" % (csv_file)
logger.info(msg)
msg = "Number of samples: %s " % (str(len(clean_fullpaths)))
logger.info(msg)
msg = "Total duration: %s Hours" % (str(round(total_duration / 3600, 2)))
logger.info(msg)
def check_RescueSpeech_data_folders(data_folder):
"""
Check if the data folder actually contains the RescueSpeech dataset.
If not, raises an error.
Arguments
---------
data_folder : str
Path to folder containing data.
Raises
------
FileNotFoundError
If data folder doesn't contain RescueSpeech dataset.
"""
# Checking clips
if not os.path.exists(data_folder):
err_msg = (
"the folder %s does not exist (it is expected in "
"the RescueSpeech dataset)" % (data_folder)
)
raise FileNotFoundError(err_msg)
def unicode_normalisation(text):
"""
Normalizes the Unicode representation of a given text.
Arguments
---------
text : str
The text to be normalized.
Returns
-------
str
The normalized text.
"""
try:
text = unicode(text, "utf-8")
except NameError: # unicode is a default on python 3
pass
return str(text)
def data_cleaning(words):
"""
Perform data cleaning
Arguments
---------
words : str
Text that needs to be cleaned
Returns
-------
str
Cleaned data
"""
# this replacement helps preserve the case of ß
# (and helps retain solitary occurrences of SS)
# since python's upper() converts ß to SS.
words = words.replace("ß", "0000ß0000")
words = re.sub("[^’'A-Za-z0-9öÖäÄüÜß]+", " ", words).upper()
words = words.replace("'", " ")
words = words.replace("’", " ")
words = words.replace(
"0000SS0000", "ß"
) # replace 0000SS0000 back to ß as its initial presence in the corpus
return words
def strip_accents(text):
"""
Strips accents from a given text string.
Arguments
---------
text : str
The text from which accents are to be stripped.
Returns
-------
str
The text with accents stripped.
"""
text = (
unicodedata.normalize("NFD", text)
.encode("ascii", "ignore")
.decode("utf-8")
)
return str(text)
def extract_files(datapath, type=None):
"""
Given a dir-path, it extracts full path of all wav files
and sorts them.
Arguments
---------
datapath : str
Path to synthesized SAR data
type : str
Type of split: clean, noisy, noise.
Returns
-------
list
Sorted list of all wav files found in the given path.
"""
if type:
path = os.path.join(datapath, type)
files = glob.glob(path + "/*.wav")
# Sort all files based on the suffixed file_id (ascending order)
files.sort(key=lambda f: int(f.split("fileid_")[-1].strip(".wav")))
else:
# Sort all files by name
files = sorted(glob.glob(datapath + "/*.wav"))
return files