-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdata_load.py
390 lines (333 loc) · 13.9 KB
/
data_load.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
# -*- coding: utf-8 -*-
'''
modified from
https://www.github.com/kyubyong/tacotron
'''
from __future__ import print_function
from hyperparams import Hyperparams as hp
import numpy as np
import tensorflow as tf
from utils import *
import re
import os
import json
import unicodedata
from bopomofo import to_bopomofo
from pypinyin import pinyin, Style
def load_vocab():
# word base
if hp.input_mode == "word":
transcript = os.path.join(hp.prepro_path, 'transcript_training.txt')
lines = open(transcript, "r").readlines()
text_dict = dict()
remove_key = list()
count = 0
char2idx = dict()
idx2char = dict()
for line in lines:
text = ''.join(line.split()[1:])
english_check = re.search('[a-zA-Z]', text)
if english_check:
continue
for word in text:
if word not in text_dict:
text_dict[word] = 0
text_dict[word] += 1
for key in text_dict:
if text_dict[key] <= 5:
remove_key.append(key)
for key in remove_key:
del text_dict[key]
for idx, char in enumerate(text_dict):
char2idx[char] = idx+1
idx2char[idx+1] = char
#char2idx = {char:idx for idx, char in enumerate(text_dict)}
#idx2char = {idx:char for idx, char in enumerate(text_dict)}
char2idx['P'] = 0
idx2char[0] = 'P'
length = len(char2idx)
char2idx['oov'] = length
char2idx['E'] = length+1
idx2char[length] = 'oov'
idx2char[length+1] = 'E'
# bopomofo base
elif hp.input_mode == "bopomofo" or hp.input_mode == "pinyin":
char2idx = {char: idx for idx, char in enumerate(hp.vocab)}
idx2char = {idx: char for idx, char in enumerate(hp.vocab)}
# phoneme base
elif hp.input_mode == "phoneme":
char2idx = json.load(open("./phoneme_preprocess/phone2idx.json", "r"))
idx2char = json.load(open("./phoneme_preprocess/idx2phone.json", "r"))
#syllable base
else:
transcript = os.path.join(hp.prepro_path, 'transcript_training.txt')
lines = open(transcript, "r").readlines()
syl_dict = dict()
remove_key = list()
count = 0
char2idx = dict()
idx2char = dict()
for line in lines:
text = ''.join(line.split()[1:])
english_check = re.search('[a-zA-Z]', text)
if english_check:
continue
if hp.input_mode == "syllable":
# bopomofo syllable
if hp.withtone:
text = to_bopomofo(text)
else:
text = to_bopomofo(text, tones=False)
else:
# pinyin syllable
text_pinyin = pinyin(text, style=Style.TONE3)
text = ""
for t in text_pinyin:
if hp.withtone:
text = text + " " + t[0]
else:
tmp = ''.join([i for i in t[0] if not i.isdigit()])
text = text + " " + tmp
text = text.split()
for syl in text:
if syl not in syl_dict:
syl_dict[syl] = 0
syl_dict[syl] += 1
for key in syl_dict:
if syl_dict[key] <= 5:
remove_key.append(key)
for key in remove_key:
del syl_dict[key]
char2idx = {char:idx for idx, char in enumerate(syl_dict)}
idx2char = {idx:char for idx, char in enumerate(syl_dict)}
"""
for idx, char in enumerate(syl_dict):
char2idx[char] = idx+1
idx2char[idx+1] = char
char2idx['P'] = 0
idx2char[0] = 'P'
"""
length = len(char2idx)
char2idx['oov'] = length
char2idx['E'] = length+1
idx2char[length] = 'oov'
idx2char[length+1] = 'E'
return char2idx, idx2char
def text_normalize(text):
text = ''.join(char for char in unicodedata.normalize('NFD', text)
if unicodedata.category(char) != 'Mn') # Strip accents
text = text.lower()
text = re.sub("[^{}]".format(hp.vocab), " ", text)
text = re.sub("[ ]+", " ", text)
return text
def load_data(mode="train"):
# Load vocabulary
char2idx, idx2char = load_vocab()
if hp.input_mode == "phoneme":
word2phonelist = json.load(open("./phoneme_preprocess/word2phonelist.json", "r"))
if mode in ("train", "eval", "evaluate"):
# Parse
fpaths, text_lengths, texts = [], [], []
transcript = os.path.join(hp.prepro_path, 'transcript_training.txt')
if mode=="evaluate":
transcript = os.path.join(hp.prepro_path, 'transcript_testing.txt')
lines = open(transcript, 'r').readlines()
total_hours = 0
for line in lines:
check_text = "".join(line.split()[1:])
english_check = re.search('[a-zA-Z]', check_text)
if english_check:
continue
#print(line)
fname, text = line.strip().split()
#text = text_normalize(text)
if len(text) > hp.max_len:
continue
# bopomofo base
if hp.input_mode == "pinyin" or hp.input_mode == "pinyin_syl":
text_pinyin = pinyin(text, style=Style.TONE3)
text = ""
for t in text_pinyin:
if hp.withtone:
if hp.input_mode == "pinyin":
text = text + t[0]
else:
text = text + " " + t[0]
else:
tmp = ''.join([i for i in t[0] if not i.isdigit()])
if hp.input_mode == "pinyin":
text = text + tmp
else:
text = text + " " + t[0]
elif hp.input_mode == "phoneme":
t_tmp = ""
for t in text:
if t not in word2phonelist:
t_tmp = t_tmp + " " + "oov"
else:
phonelist = word2phonelist[t]
t_tmp = t_tmp + " " + " ".join(phonelist)
text = t_tmp
elif hp.input_mode != "word":
if hp.withtone:
text = to_bopomofo(text)
else:
text = to_bopomofo(text, tones=False)
if hp.input_mode != "pinyin" and hp.input_mode != "pinyin_syl" and hp.input_mode != "phoneme":
text = text.replace("er", u"\u3126")
text = text.replace("an", u"\u3122")
text = text.replace("jue", u"\u3110\u3129\u311d")
text = text.replace("xue", u"\u3112\u3129\u311d")
text = text.replace("aE", u"\u311a")
english_check = re.search('[a-zA-Z]', text)
if english_check:
continue
if hp.input_mode == "phoneme":
text = text + " S"
else:
text = text + " E"
temp = []
if hp.input_mode != "syllable" and hp.input_mode != "pinyin_syl" and hp.input_mode != "phoneme":
text = "".join(text.split());
else:
text = text.split()
### check illegal words
illegal_word = False
for char in text:
if char not in char2idx:
if hp.input_mode == "word" or hp.input_mode == "syllable" or hp.input_mode == "pinyin_syl" or hp.input_mode == "phoneme":
# word base or syllable base
temp.append(char2idx['oov'])
elif hp.input_mode == "bopomofo" or hp.input_mode == "pinyin":
# bopomofo base
illegal_word = True
break
else:
temp.append(char2idx[char])
if illegal_word:
continue
#text = [char2idx[char] for char in text]
text = temp
text_lengths.append(len(text))
if mode=="evaluate":
texts.append(np.array(text, np.int32))
else:
texts.append(np.array(text, np.int32).tostring())
fpath = os.path.join(hp.data, fname + ".wav")
fpaths.append(fpath)
return fpaths, text_lengths, texts
else:
lines = open(hp.test_data, 'r').readlines()
sents = []
for line in lines:
if hp.input_mode == "pinyin" or hp.input_mode == "pinyin_syl":
# pinyin base
text_pinyin = pinyin(line, style=Style.TONE3)
line = ""
for t in text_pinyin:
if hp.withtone:
if hp.input_mode == "pinyin":
line = line + t[0]
else:
line = line + " " + t[0]
else:
tmp = ''.join([i for i in t[0] if not i.isdigit()])
if hp.input_mode == "pinyin":
line = line + tmp
else:
line = line + " " + t[0]
elif hp.input_mode == "phoneme":
t_tmp = ""
for t in line:
if t not in word2phonelist:
t_tmp = t_tmp + " " + "oov"
else:
phonelist = word2phonelist[t]
t_tmp = t_tmp + " " + " ".join(phonelist)
line = t_tmp
elif hp.input_mode != "word":
# bopomofo base
if hp.withtone:
line = to_bopomofo(line.strip())
else:
line = to_bopomofo(line.strip(), tones=False)
if hp.input_mode != "pinyin" and hp.input_mode != "pinyin_syl" and hp.input_mode != "phoneme":
english_check = re.search('[a-zA-Z]', line)
if english_check:
continue
if hp.input_mode == "syllable" or hp.input_mode == "pinyin_syl":
line = line + " E"
line = line.split()
elif hp.input_mode == "phoneme":
line = line + " S"
line = line.split()
else:
line = ''.join(line.split()) + "E"
sents.append(line)
lengths = [len(sent) for sent in sents]
maxlen = sorted(lengths, reverse=True)[0]
texts = np.zeros((len(sents), maxlen), np.int32)
if hp.input_mode == "word" or hp.input_mode == "syllable" or hp.input_mode == "pinyin_syl" or hp.input_mode == "phoneme":
# word base
for i, sent in enumerate(sents):
for j, char in enumerate(sent):
if char in char2idx:
texts[i, j] = char2idx[char]
else:
texts[i, j] = char2idx['oov']
elif hp.input_mode == "bopomofo" or hp.input_mode == "pinyin":
# bopomofo base
for i, sent in enumerate(sents):
texts[i, :len(sent)] = [char2idx[char] for char in sent]
return texts
"""
else:
# Parse
lines = codecs.open(hp.test_data, 'r', 'utf-8').readlines()[1:]
sents = [text_normalize(line.split(" ", 1)[-1]).strip() + "E" for line in lines] # text normalization, E: EOS
lengths = [len(sent) for sent in sents]
maxlen = sorted(lengths, reverse=True)[0]
texts = np.zeros((len(sents), maxlen), np.int32)
for i, sent in enumerate(sents):
texts[i, :len(sent)] = [char2idx[char] for char in sent]
return texts
"""
def get_batch():
"""Loads training data and put them in queues"""
with tf.device('/cpu:0'):
# Load data
fpaths, text_lengths, texts = load_data() # list
maxlen, minlen = max(text_lengths), min(text_lengths)
# Calc total batch count
num_batch = len(fpaths) // hp.batch_size
fpaths = tf.convert_to_tensor(fpaths)
text_lengths = tf.convert_to_tensor(text_lengths)
texts = tf.convert_to_tensor(texts)
# Create Queues
fpath, text_length, text = tf.train.slice_input_producer([fpaths, text_lengths, texts], shuffle=True)
# Parse
text = tf.decode_raw(text, tf.int32) # (None,)
if hp.prepro:
def _load_spectrograms(fpath):
fname = os.path.basename(fpath.decode())
mel = hp.prepro_path + "/mels/{}".format(fname.replace("wav", "npy"))
mag = hp.prepro_path + "/mags/{}".format(fname.replace("wav", "npy"))
return fname, np.load(mel), np.load(mag)
fname, mel, mag = tf.py_func(_load_spectrograms, [fpath], [tf.string, tf.float32, tf.float32])
else:
fname, mel, mag = tf.py_func(load_spectrograms, [fpath], [tf.string, tf.float32, tf.float32]) # (None, n_mels)
# Add shape information
fname.set_shape(())
text.set_shape((None,))
mel.set_shape((None, hp.n_mels*hp.r))
mag.set_shape((None, hp.n_fft//2+1))
# Batching
_, (texts, mels, mags, fnames) = tf.contrib.training.bucket_by_sequence_length(
input_length=text_length,
tensors=[text, mel, mag, fname],
batch_size=hp.batch_size,
bucket_boundaries=[i for i in range(minlen + 1, maxlen - 1, 20)],
num_threads=4,
capacity=hp.batch_size * 4,
dynamic_pad=True)
return texts, mels, mags, fnames, num_batch