forked from NVIDIA/flowtron
/
inference_fp32.py
183 lines (147 loc) · 6.53 KB
/
inference_fp32.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
###############################################################################
#
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###############################################################################
import matplotlib
matplotlib.use("Agg")
import matplotlib.pylab as plt
import pyprof
import os
import argparse
import json
import sys
import numpy as np
import torch
import time
from flowtron import Flowtron
from torch.utils.data import DataLoader
from data import Data
from train import update_params
sys.path.insert(0, "tacotron2")
sys.path.insert(0, "tacotron2/waveglow")
from glow import WaveGlow
from scipy.io.wavfile import write
#pyprof.init() ########### prof.
def infer(flowtron_path, waveglow_path, text, speaker_id, n_frames, sigma,
seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
# load waveglow
waveglow = torch.load(waveglow_path)['model'].cuda().eval()
waveglow.cuda().half()
for k in waveglow.convinv:
k.float()
waveglow.eval()
# load flowtron
model = Flowtron(**model_config).cuda()
cpt_dict = torch.load(flowtron_path )
if 'model' in cpt_dict:
dummy_dict = cpt_dict['model'].state_dict()
else:
dummy_dict = cpt_dict['state_dict']
model.load_state_dict(dummy_dict)
model.eval()
print("Loaded checkpoint '{}')" .format(flowtron_path))
ignore_keys = ['training_files', 'validation_files']
trainset = Data(
data_config['training_files'],
**dict((k, v) for k, v in data_config.items() if k not in ignore_keys))
tic_prep = time.time()
str_text = text
num_char = len(str_text)
num_word = len(str_text.split() )
speaker_vecs = trainset.get_speaker_id(speaker_id).cuda()
text = trainset.get_text(text).cuda()
speaker_vecs = speaker_vecs[None]
text = text[None]
toc_prep = time.time()
############## warm up ########### to measure exact flowtron inference time
with torch.no_grad():
tic_warmup = time.time()
residual = torch.cuda.FloatTensor(1, 80, n_frames).normal_() * sigma
mels, attentions = model.infer(residual, speaker_vecs, text)
toc_warmup = time.time()
tic_flowtron = time.time()
with torch.no_grad() :#,torch.autograd.profiler.emit_nvtx(): ########### prof.
tic_residual = time.time()
residual = torch.cuda.FloatTensor(1, 80, n_frames).normal_() * sigma
toc_residual = time.time()
# profiler.start() ########### prof.
mels, attentions = model.infer(residual, speaker_vecs, text)
# profiler.stop() ########### prof.
toc_flowtron = time.time()
for k in range(len(attentions)):
attention = torch.cat(attentions[k]).cpu().numpy()
fig, axes = plt.subplots(1, 2, figsize=(16, 4))
axes[0].imshow(mels[0].cpu().numpy(), origin='bottom', aspect='auto')
axes[1].imshow(attention[:, 0].transpose(), origin='bottom', aspect='auto')
fig.savefig('sid{}_sigma{}_attnlayer{}.png'.format(speaker_id, sigma, k))
plt.close("all")
tic_waveglow = time.time()
audio = waveglow.infer(mels.half(), sigma=0.8).float()
toc_waveglow = time.time()
audio = audio.cpu().numpy()[0]
# normalize audio for now
audio = audio / np.abs(audio).max()
len_audio = len(audio)
dur_audio = len_audio / 22050
num_frames = int(len_audio / 256)
dur_prep = toc_prep - tic_prep
dur_residual = toc_residual - tic_residual
dur_flowtron_in = toc_flowtron - toc_residual
dur_warmup = toc_warmup - tic_warmup
dur_flowtron_out = toc_flowtron - tic_residual
dur_waveglow = toc_waveglow - tic_waveglow
dur_total = dur_prep + dur_flowtron_out + dur_waveglow
RTF = dur_audio / dur_total
str_text = "\n text : " + str_text
str_num = "\n text {:d} char {:d} words ".format(num_char, num_word )
str_audio = "\n generated audio : {:2.3f} samples {:2.3f} sec with {:d} mel frames ".format( len_audio, dur_audio, num_frames )
str_perf = "\n total time {:2.3f} = text prep {:2.3f} + flowtron{:2.3f} + wg {:2.3f} ".format( dur_total, dur_prep, dur_flowtron_out, dur_waveglow )
str_flow ="\n total flowtron {:2.3f} = residual cal {:2.3f} + flowtron {:2.3f} " .format(dur_flowtron_out, dur_residual, dur_flowtron_in )
str_rtf = "\n RTF is {:2.3f} x with warm up {:2.3f} ".format(RTF, dur_warmup )
print(str_text, str_num, str_audio, str_perf, str_flow, str_rtf )
write("sid{}_sigma{}.wav".format(speaker_id, sigma),
data_config['sampling_rate'], audio)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str,
help='JSON file for configuration')
parser.add_argument('-p', '--params', nargs='+', default=[])
parser.add_argument('-f', '--flowtron_path',
help='Path to flowtron state dict', type=str)
parser.add_argument('-w', '--waveglow_path',
help='Path to waveglow state dict', type=str)
parser.add_argument('-t', '--text', help='Text to synthesize', type=str)
parser.add_argument('-i', '--id', help='Speaker id', type=int)
parser.add_argument('-n', '--n_frames', help='Number of frames',
default=400, type=int)
parser.add_argument('-o', "--output_dir", default="results/")
parser.add_argument("-s", "--sigma", default=0.5, type=float)
parser.add_argument("--seed", default=1234, type=int)
args = parser.parse_args()
# Parse configs. Globals nicer in this case
with open(args.config) as f:
data = f.read()
global config
config = json.loads(data)
update_params(config, args.params)
data_config = config["data_config"]
global model_config
model_config = config["model_config"]
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = False
infer(args.flowtron_path, args.waveglow_path, args.text, args.id,
args.n_frames, args.sigma, args.seed)