forked from SeanPLeary/dc_tts-transfer-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynthesize.py
executable file
·125 lines (101 loc) · 3.73 KB
/
synthesize.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
By kyubyong park. kbpark.linguist@gmail.com.
https://www.github.com/kyubyong/dc_tts
Modified by sean leary. learysean1@hotmail.com
https://github.com/SeanPLeary/dc_tts-transfer-learning
Modified
https://github.com/kwmkwm/dc_tts-phonetic-transfer-learning
'''
from __future__ import print_function
import os
from hyperparams import Hyperparams as hp
import numpy as np
import tensorflow as tf
from train_transfer import Graph
from utils import *
from data_load import load_data
from scipy.io.wavfile import write
from tqdm import tqdm
import argparse
import sys
def synthesize(logdir, txtfile, outdir):
# Load data
L = load_data("_",mode="synthesize", txtfile=txtfile)
# Load graph
g = Graph(mode="synthesize")
print("Graph loaded")
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Restore parameters
var_list = tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES, 'Text2Mel')
saver1 = tf.train.Saver(var_list=var_list)
saver1.restore(sess, tf.train.latest_checkpoint(logdir + "-1"))
print("Text2Mel Restored!")
var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'SSRN') + \
tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, 'gs')
saver2 = tf.train.Saver(var_list=var_list)
saver2.restore(sess, tf.train.latest_checkpoint(logdir + "-2"))
print("SSRN Restored!")
# Feed Forward
# mel
Y = np.zeros((len(L), hp.max_T, hp.n_mels), np.float32)
prev_max_attentions = np.zeros((len(L),), np.int32)
for j in tqdm(range(hp.max_T)):
_gs, _Y, _max_attentions, _alignments = \
sess.run([g.global_step, g.Y, g.max_attentions, g.alignments],
{g.L: L,
g.mels: Y,
g.prev_max_attentions: prev_max_attentions})
Y[:, j, :] = _Y[:, j, :]
prev_max_attentions = _max_attentions[:, j]
# Get magnitude
Z = sess.run(g.Z, {g.Y: Y})
# Generate wav files
if not os.path.exists(outdir):
os.makedirs(outdir)
for i, mag in enumerate(Z):
print("Working on file", i)
wav = spectrogram2wav(mag)
write(outdir + "/" + os.path.basename(txtfile) +
".{}.wav".format(i), hp.sr, wav)
def get_arguments():
parser = argparse.ArgumentParser(description='DC_TTS synthesizer')
parser.add_argument('--voice', type=str, required=False,
help='Directory containing output/logdir subdirectories')
parser.add_argument('--text', type=str, required=False, help='TXT file')
parser.add_argument('--outdir', type=str, required=False,
help='Directory to output wav files')
arguments = parser.parse_args()
return arguments
if __name__ == '__main__':
args = get_arguments()
if args.voice:
if not os.path.exists(args.voice):
print('Directory %s not found. Exiting.' % args.voice)
sys.exit()
else:
voicedir = args.voice
else:
voicedir = hp.restoredir
logdir = voicedir + hp.logdir
if args.text:
if not os.path.exists(args.text):
print('File %s not found. Exiting.' % args.text)
sys.exit()
else:
txtfile = args.text
else:
txtfile = hp.test_data
if args.outdir:
if not os.path.exists(args.outdir):
print('Directory %s not found. Exiting.' % args.outdir)
sys.exit()
else:
outdir = args.outdir
else:
outdir = voicedir + hp.sampledir
synthesize(logdir, txtfile, outdir)
print("Done")