-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPattern_Generator.py
551 lines (469 loc) · 20.6 KB
/
Pattern_Generator.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
import numpy as np
import yaml, os, time, pickle, librosa, re, argparse
from concurrent.futures import ThreadPoolExecutor as PE
from collections import deque
from threading import Thread
from random import shuffle
from tqdm import tqdm
from Audio import Audio_Prep, Mel_Generate
from yin import pitch_calc
from Arg_Parser import Recursive_Parse
hp = Recursive_Parse(yaml.load(
open('Hyper_Parameters.yaml', encoding='utf-8'),
Loader=yaml.Loader
))
using_Extension = [x.upper() for x in ['.wav', '.m4a', '.flac']]
regex_Checker = re.compile('[A-Z,.?!\'\-\s]+')
top_DB_Dict = {'LJ': 60, 'BC2013': 60, 'VCTK': 15, 'VC1': 23, 'VC1T': 23, 'VC2': 23, 'Libri': 23, 'CMUA': 60} # VC1 and Libri is from 'https://github.com/CorentinJ/Real-Time-Voice-Cloning'
def Text_Filtering(text):
remove_Letter_List = ['(', ')', '\"', '[', ']', ':', ';']
replace_List = [(' ', ' '), (' ,', ','), ('\' ', '\'')]
text = text.upper().strip()
for filter in remove_Letter_List:
text= text.replace(filter, '')
for filter, replace_STR in replace_List:
text= text.replace(filter, replace_STR)
text= text.strip()
if len(regex_Checker.findall(text)) != 1:
return None
elif text.startswith('\''):
return None
else:
return regex_Checker.findall(text)[0]
def Pitch_Generate(audio):
pitch = pitch_calc(
sig= audio,
sr= hp.Sound.Sample_Rate,
w_len= hp.Sound.Frame_Length,
w_step= hp.Sound.Frame_Shift,
f0_min= hp.Sound.Pitch_Min,
f0_max= hp.Sound.Pitch_Max,
confidence_threshold= hp.Sound.Confidence_Threshold,
gaussian_smoothing_sigma = hp.Sound.Gaussian_Smoothing_Sigma
)
return (pitch - np.min(pitch)) / (np.max(pitch) - np.min(pitch) + 1e-7)
def Pattern_Generate(path, top_db= 60):
audio = Audio_Prep(path, hp.Sound.Sample_Rate, top_db)
mel = Mel_Generate(
audio= audio,
sample_rate= hp.Sound.Sample_Rate,
num_frequency= hp.Sound.Spectrogram_Dim,
num_mel= hp.Sound.Mel_Dim,
window_length= hp.Sound.Frame_Length,
hop_length= hp.Sound.Frame_Shift,
mel_fmin= hp.Sound.Mel_F_Min,
mel_fmax= hp.Sound.Mel_F_Max,
max_abs_value= hp.Sound.Max_Abs_Mel
)
pitch = Pitch_Generate(audio)
return audio, mel, pitch
def Pattern_File_Generate(path, speaker_ID, speaker, dataset, text= None, tag='', eval= False):
pattern_Path = hp.Train.Eval_Pattern.Path if eval else hp.Train.Train_Pattern.Path
file = '{}.{}{}.PICKLE'.format(
speaker if dataset in speaker else '{}.{}'.format(dataset, speaker),
'{}.'.format(tag) if tag != '' else '',
os.path.splitext(os.path.basename(path))[0]
).upper()
file = os.path.join(pattern_Path, dataset, speaker, file).replace("\\", "/")
if os.path.exists(file):
return
try:
audio, mel, pitch = Pattern_Generate(path, top_DB_Dict[dataset])
assert mel.shape[0] == pitch.shape[0], 'Mel_shape != Pitch_shape {} != {}'.format(mel.shape, pitch.shape)
new_Pattern_Dict = {
'Audio': audio.astype(np.float32),
'Mel': mel.astype(np.float32),
'Pitch': pitch.astype(np.float32),
'Speaker_ID': speaker_ID,
'Speaker': speaker,
'Dataset': dataset,
}
if not text is None:
new_Pattern_Dict['Text'] = text
except Exception as e:
print('Error: {} in {}'.format(e, path))
return
os.makedirs(os.path.join(pattern_Path, dataset, speaker).replace('\\', '/'), exist_ok= True)
with open(os.path.join(pattern_Path, dataset, file).replace("\\", "/"), 'wb') as f:
pickle.dump(new_Pattern_Dict, f, protocol=4)
def LJ_Info_Load(path, use_text= False):
paths = []
for root, _, files in os.walk(path):
for file in files:
file = os.path.join(root, file).replace('\\', '/')
if not os.path.splitext(file)[1].upper() in using_Extension:
continue
paths.append(file)
text_Dict = {}
if use_text:
for line in open(os.path.join(path, 'metadata.csv').replace('\\', '/'), 'r', encoding= 'utf-8').readlines():
file, _, text = line.strip().split('|')
text = Text_Filtering(text)
if text is None:
continue
text_Dict[os.path.join(path, 'wavs', '{}.wav'.format(file)).replace('\\', '/')] = text
paths = list(text_Dict.keys())
speaker_Dict = {
path: 'LJ'
for path in paths
}
print('LJ info generated: {}'.format(len(paths)))
return paths, text_Dict, speaker_Dict
def BC2013_Info_Load(path, use_text= False):
paths = []
for root, _, files in os.walk(path):
for file in files:
file = os.path.join(root, file).replace('\\', '/')
if not os.path.splitext(file)[1].upper() in using_Extension:
continue
paths.append(file)
text_Dict = {}
if use_text:
for path in paths:
text = Text_Filtering(open(path.replace('wav', 'txt'), 'r').readlines()[0].strip())
if not text is None:
text_Dict[path] = text
paths = list(text_Dict.keys())
speaker_Dict = {
path: 'BC2013'
for path in paths
}
print('BC2013 info generated: {}'.format(len(paths)))
return paths, text_Dict, speaker_Dict
def CMUA_Info_Load(path, use_text= False):
paths = []
for root, _, files in os.walk(path):
for file in files:
file = os.path.join(root, file).replace('\\', '/')
if not os.path.splitext(file)[1].upper() in using_Extension:
continue
paths.append(file)
text_Dict = {}
if use_text:
for root, _, _ in os.walk(path):
if not os.path.exists(os.path.join(root, 'txt.done.data')):
continue
for line in open(os.path.join(root, 'txt.done.data'), 'r').readlines():
file, text, _ = line.strip().split('"')
file = file.strip().split(' ')[1]
path = os.path.join(root.replace('etc', 'wav'), '{}.wav'.format(file)).replace('\\', '/')
text = Text_Filtering(text)
if not text is None:
text_Dict[path] = text
paths = list(text_Dict.keys())
speaker_Dict = {
path: 'CMUA.{}'.format(path.split('/')[-3].split('_')[2].upper())
for path in paths
}
print('CMUA info generated: {}'.format(len(paths)))
return paths, text_Dict, speaker_Dict
def VCTK_Info_Load(path, use_text= False):
path = os.path.join(path, 'wav48').replace('\\', '/')
try:
with open(os.path.join(path, 'VCTK.NonOutlier.txt').replace('\\', '/'), 'r') as f:
vctk_Non_Outlier_List = [x.strip() for x in f.readlines()]
except:
vctk_Non_Outlier_List = None
paths = []
for root, _, files in os.walk(path):
for file in files:
if not vctk_Non_Outlier_List is None and not file in vctk_Non_Outlier_List:
continue
file = os.path.join(root, file).replace('\\', '/')
if not os.path.splitext(file)[1].upper() in using_Extension:
continue
paths.append(file)
text_Dict = {}
if use_text:
for path in paths:
if 'p315'.upper() in path.upper(): #Officially, 'p315' text is lost in VCTK dataset.
continue
text = Text_Filtering(open(path.replace('wav48', 'txt').replace('wav', 'txt'), 'r').readlines()[0])
if not text is None:
text_Dict[path] = text
paths = list(text_Dict.keys())
speaker_Dict = {
path: 'VCTK.{}'.format(path.split('/')[-2].upper())
for path in paths
}
print('VCTK info generated: {}'.format(len(paths)))
return paths, text_Dict, speaker_Dict
def Libri_Info_Load(path, use_text= False):
paths = []
for root, _, files in os.walk(path):
for file in files:
file = os.path.join(root, file).replace('\\', '/')
if not os.path.splitext(file)[1].upper() in using_Extension:
continue
paths.append(file)
text_Dict = {}
if use_text:
for path in paths:
text = Text_Filtering(open('{}.normalized.txt'.format(os.path.splitext(path)[0]), 'r', encoding= 'utf-8').readlines()[0])
if not text is None:
text_Dict[path] = text
paths = list(text_Dict.keys())
speaker_Dict = {
path: 'Libri.{:04d}'.format(int(path.split('/')[-3].upper()))
for path in paths
}
print('Libri info generated: {}'.format(len(paths)))
return paths, text_Dict, speaker_Dict
def VC1_Info_Load(path, use_text= False):
if use_text:
raise ValueError('VC1 does not support the text.')
paths = []
for root, _, files in os.walk(path):
for file in files:
file = os.path.join(root, file).replace('\\', '/')
if not os.path.splitext(file)[1].upper() in using_Extension:
continue
paths.append(file)
speaker_Dict = {
path: 'VC1.{}'.format(path.split('/')[-3].upper())
for path in paths
}
tag_Dict = {
path: path.split('/')[-2].upper()
for path in paths
}
print('VC1 info generated: {}'.format(len(paths)))
return paths, speaker_Dict, tag_Dict
def VC2_Info_Load(path, use_text= False):
if use_text:
raise ValueError('VC2 does not support the text.')
paths = []
for root, _, files in os.walk(path):
for file in files:
wav_File_Path = os.path.join(root, file).replace('\\', '/')
if not os.path.splitext(wav_File_Path)[1].upper() in using_Extension:
continue
paths.append(wav_File_Path)
speaker_Dict = {
path: 'VC2.{}'.format(path.split('/')[-3].upper())
for path in paths
}
tag_Dict = {
path: path.split('/')[-2].upper()
for path in paths
}
print('VC2 info generated: {}'.format(len(paths)))
return paths, speaker_Dict, tag_Dict
def VC1T_Info_Load(path, use_text= False):
if use_text:
raise ValueError('VC1-Test does not support the text.')
paths = []
for root, _, files in os.walk(path):
for file in files:
file = os.path.join(root, file).replace('\\', '/')
if not os.path.splitext(file)[1].upper() in using_Extension:
continue
paths.append(file)
speaker_Dict = {
path: 'VC1T.{}'.format(path.split('/')[-3].upper())
for path in paths
}
tag_Dict = {
path: path.split('/')[-2].upper()
for path in paths
}
print('VC1T info generated: {}'.format(len(paths)))
return paths, speaker_Dict, tag_Dict
def Speaker_Index_Dict_Generate(speaker_Dict):
return {
speaker: index
for index, speaker in enumerate(sorted(set(speaker_Dict.values())))
}
def Split_Eval(paths, eval_ratio= 0.001, min_Eval= 1):
shuffle(paths)
index = max(int(len(paths) * eval_ratio), min_Eval)
return paths[index:], paths[:index]
def Metadata_Generate(eval= False, use_text= False):
pattern_Path = hp.Train.Eval_Pattern.Path if eval else hp.Train.Train_Pattern.Path
metadata_File = hp.Train.Eval_Pattern.Metadata_File if eval else hp.Train.Train_Pattern.Metadata_File
new_Metadata_Dict = {
'Spectrogram_Dim': hp.Sound.Spectrogram_Dim,
'Mel_Dim': hp.Sound.Mel_Dim,
'Frame_Shift': hp.Sound.Frame_Shift,
'Frame_Length': hp.Sound.Frame_Length,
'Sample_Rate': hp.Sound.Sample_Rate,
'Max_Abs_Mel': hp.Sound.Max_Abs_Mel,
'File_List': [],
'Audio_Length_Dict': {},
'Mel_Length_Dict': {},
'Pitch_Length_Dict': {},
'Speaker_ID_Dict': {},
'Speaker_Dict': {},
'Dataset_Dict': {},
'File_List_by_Speaker_Dict': {},
}
if use_text:
new_Metadata_Dict['Text_Length_Dict'] = {}
files_TQDM = tqdm(
total= sum([len(files) for root, _, files in os.walk(pattern_Path)]),
desc= 'Eval_Pattern' if eval else 'Train_Pattern'
)
for root, _, files in os.walk(pattern_Path):
for file in files:
with open(os.path.join(root, file).replace("\\", "/"), "rb") as f:
pattern_Dict = pickle.load(f)
file = os.path.join(root, file).replace("\\", "/").replace(pattern_Path, '').lstrip('/')
try:
if not all([
key in ('Audio', 'Mel', 'Pitch', 'Speaker_ID', 'Speaker', 'Dataset', 'Text' if use_text else '')
for key in pattern_Dict.keys()
]):
continue
new_Metadata_Dict['Audio_Length_Dict'][file] = pattern_Dict['Audio'].shape[0]
new_Metadata_Dict['Mel_Length_Dict'][file] = pattern_Dict['Mel'].shape[0]
new_Metadata_Dict['Pitch_Length_Dict'][file] = pattern_Dict['Pitch'].shape[0]
new_Metadata_Dict['Speaker_ID_Dict'][file] = pattern_Dict['Speaker_ID']
new_Metadata_Dict['Speaker_Dict'][file] = pattern_Dict['Speaker']
new_Metadata_Dict['Dataset_Dict'][file] = pattern_Dict['Dataset']
new_Metadata_Dict['File_List'].append(file)
if not pattern_Dict['Speaker'] in new_Metadata_Dict['File_List_by_Speaker_Dict'].keys():
new_Metadata_Dict['File_List_by_Speaker_Dict'][pattern_Dict['Speaker']] = []
new_Metadata_Dict['File_List_by_Speaker_Dict'][pattern_Dict['Speaker']].append(file)
if use_text:
new_Metadata_Dict['Text_Length_Dict'][file] = len(pattern_Dict['Text'])
except:
print('File \'{}\' is not correct pattern file. This file is ignored.'.format(file))
files_TQDM.update(1)
with open(os.path.join(pattern_Path, metadata_File.upper()).replace("\\", "/"), 'wb') as f:
pickle.dump(new_Metadata_Dict, f, protocol= 4)
print('Metadata generate done.')
def Token_Dict_Generate(text_Dict):
tokens = set()
for text in text_Dict.values():
tokens = tokens.union(set(text))
os.makedirs(os.path.dirname(hp.Token_Path), exist_ok= True)
#I don't use yaml.dump in this case to sort clearly.
yaml.dump(
{token: index for index, token in enumerate(['<S>', '<E>'] + sorted(tokens))},
open(hp.Token_Path, 'w')
)
if __name__ == '__main__':
argParser = argparse.ArgumentParser()
argParser.add_argument("-lj", "--lj_path", required=False)
argParser.add_argument("-bc2013", "--bc2013_path", required=False)
argParser.add_argument("-cmua", "--cmua_path", required=False)
argParser.add_argument("-vctk", "--vctk_path", required=False)
argParser.add_argument("-libri", "--libri_path", required=False)
argParser.add_argument("-vc1", "--vc1_path", required=False)
argParser.add_argument("-vc2", "--vc2_path", required=False)
argParser.add_argument("-vc1t", "--vc1_test_path", required=False)
argParser.add_argument("-text", "--use_text", action= 'store_true')
argParser.add_argument("-evalr", "--eval_ratio", default= 0.001, type= float)
argParser.add_argument("-evalm", "--eval_min", default= 1, type= int)
argParser.add_argument("-mw", "--max_worker", default= 10, required=False, type= int)
args = argParser.parse_args()
paths = []
text_Dict = {}
speaker_Dict = {}
dataset_Dict = {}
tag_Dict = {}
if not args.lj_path is None:
lj_Paths, lj_Text_Dict, lj_Speaker_Dict = LJ_Info_Load(path= args.lj_path, use_text= args.use_text)
paths.extend(lj_Paths)
text_Dict.update(lj_Text_Dict)
speaker_Dict.update(lj_Speaker_Dict)
dataset_Dict.update({path: 'LJ' for path in lj_Paths})
tag_Dict.update({path: '' for path in lj_Paths})
if not args.bc2013_path is None:
bc2013_Paths, bc2013_Text_Dict, bc2013_Speaker_Dict = BC2013_Info_Load(path= args.bc2013_path, use_text= args.use_text)
paths.extend(bc2013_Paths)
text_Dict.update(bc2013_Text_Dict)
speaker_Dict.update(bc2013_Speaker_Dict)
dataset_Dict.update({path: 'BC2013' for path in bc2013_Paths})
tag_Dict.update({path: '' for path in bc2013_Paths})
if not args.cmua_path is None:
cmua_Paths, cuma_Text_Dict, cmua_Speaker_Dict = CMUA_Info_Load(path= args.cmua_path, use_text= args.use_text)
paths.extend(cmua_Paths)
text_Dict.update(cuma_Text_Dict)
speaker_Dict.update(cmua_Speaker_Dict)
dataset_Dict.update({path: 'CMUA' for path in cmua_Paths})
tag_Dict.update({path: '' for path in cmua_Paths})
if not args.vctk_path is None:
vctk_Paths, vctk_Text_Dict, vctk_Speaker_Dict = VCTK_Info_Load(path= args.vctk_path, use_text= args.use_text)
paths.extend(vctk_Paths)
text_Dict.update(vctk_Text_Dict)
speaker_Dict.update(vctk_Speaker_Dict)
dataset_Dict.update({path: 'VCTK' for path in vctk_Paths})
tag_Dict.update({path: '' for path in vctk_Paths})
if not args.libri_path is None:
libri_Paths, libri_Text_Dict, libri_Speaker_Dict = Libri_Info_Load(path= args.libri_path, use_text= args.use_text)
paths.extend(libri_Paths)
text_Dict.update(libri_Text_Dict)
speaker_Dict.update(libri_Speaker_Dict)
dataset_Dict.update({path: 'Libri' for path in libri_Paths})
tag_Dict.update({path: '' for path in libri_Paths})
if not args.vc1_path is None:
vc1_Paths, vc1_Speaker_Dict, vc1_Tag_Dict = VC1_Info_Load(path= args.vc1_path, use_text= args.use_text)
paths.extend(vc1_Paths)
speaker_Dict.update(vc1_Speaker_Dict)
dataset_Dict.update({path: 'VC1' for path in vc1_Paths})
tag_Dict.update(vc1_Tag_Dict)
if not args.vc2_path is None:
vc2_Paths, vc2_Speaker_Dict, vc2_Tag_Dict = VC2_Info_Load(path= args.vc2_path, use_text= args.use_text)
paths.extend(vc2_Paths)
speaker_Dict.update(vc2_Speaker_Dict)
dataset_Dict.update({path: 'VC2' for path in vc2_Paths})
tag_Dict.update(vc2_Tag_Dict)
if not args.vc1_test_path is None:
vc1t_Paths, vc1t_Speaker_Dict, vc1t_Tag_Dict = VC1T_Info_Load(path= args.vc1_test_path, use_text= args.use_text)
paths.extend(vc1t_Paths)
speaker_Dict.update(vc1t_Speaker_Dict)
dataset_Dict.update({path: 'VC1T' for path in vc1t_Paths})
tag_Dict.update(vc1t_Tag_Dict)
if len(paths) == 0:
raise ValueError('Total info count must be bigger than 0.')
if args.use_text:
Token_Dict_Generate(text_Dict)
speaker_Index_Dict = Speaker_Index_Dict_Generate(speaker_Dict)
train_Paths, eval_Paths = Split_Eval(paths, args.eval_ratio)
with PE(max_workers = args.max_worker) as pe:
for _ in tqdm(
pe.map(
lambda params: Pattern_File_Generate(*params),
[
(
path,
speaker_Index_Dict[speaker_Dict[path]],
speaker_Dict[path],
dataset_Dict[path],
text_Dict[path] if args.use_text else None,
tag_Dict[path],
False
)
for path in train_Paths
]
),
total= len(train_Paths)
):
pass
for _ in tqdm(
pe.map(
lambda params: Pattern_File_Generate(*params),
[
(
path,
speaker_Index_Dict[speaker_Dict[path]],
speaker_Dict[path],
dataset_Dict[path],
text_Dict[path] if args.use_text else None,
tag_Dict[path],
True
)
for path in eval_Paths
]
),
total= len(eval_Paths)
):
pass
Metadata_Generate(use_text= args.use_text)
Metadata_Generate(eval= True, use_text= args.use_text)
# python Pattern_Generator.py -lj "D:\Pattern\ENG\LJSpeech" -bc2013 "D:\Pattern\ENG\BC2013" -cmua "D:\Pattern\ENG\CMUA" -vctk "D:\Pattern\ENG\VCTK" -libri "D:\Pattern\ENG\LibriTTS"
# python Pattern_Generator.py -lj "D:\Pattern\ENG\LJSpeech" -vctk "D:\Pattern\ENG\VCTK" -libri "D:\Pattern\ENG\LibriTTS" -text
# python Pattern_Generator.py -lj "D:\Pattern\ENG\LJSpeech" -text
# python Pattern_Generator.py -lj /home/heejo/data/Eng/LJSpeech-1.1 -text
# python Pattern_Generator.py -vc2 "D:\Pattern\ENG\VC2" -mw 1