-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinference_2019.py
More file actions
325 lines (294 loc) · 12.7 KB
/
Copy pathinference_2019.py
File metadata and controls
325 lines (294 loc) · 12.7 KB
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
# coding: utf-8
"""
Inference Rep from trained Autoencoder.
usage: inference_2019.py [options] <scp_dir> <feat> <checkpoint> <dst_dir>
options:
--hparams=<parmas> Hyper parameters [default: ].
--preset=<json> Path of preset parameters (json).
-h, --help Show help message.
"""
from docopt import docopt
import json
import sys
import os
from os.path import dirname, join, basename, splitext
import torch
import numpy as np
from nnmnkwii import preprocessing as P
from tqdm import tqdm
import librosa
from wavenet_vocoder.util import is_mulaw_quantize, is_mulaw, is_raw, is_scalar_input
import audio
from hparams import hparams, hparams_debug_string
from autoencoders.autoencoder import Model as AE ,Model2 as AE2 ,Model4 as AE4
from autoencoders.cat_ae_model import Model as CatAE
from wavenet_vocoder import WaveNet
from autoencoders.wavenet_ae_model import AE as wvae, INAE, INAE1
from autoencoders.wavenet_ae_model import VQVAE, CatWavAE
#from train import build_model as build_wavenet_model
#from new_ae_train import build_model as build_autoencoder_model
torch.set_num_threads(4)
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
def build_catae_model():
if is_mulaw_quantize(hparams.input_type):
if hparams.out_channels != hparams.quantize_channels:
raise RuntimeError(
"out_channels must equal to quantize_chennels if input_type is 'mulaw-quantize'")
if hparams.upsample_conditional_features and hparams.cin_channels < 0:
s = "Upsample conv layers were specified while local conditioning disabled. "
s += "Notice that upsample conv layers will never be used."
warn(s)
upsample_params = hparams.upsample_params
upsample_params["cin_channels"] = hparams.cin_channels
upsample_params["cin_pad"] = hparams.cin_pad
wavenet = WaveNet(
out_channels=hparams.out_channels,
layers=hparams.layers,
stacks=hparams.stacks,
residual_channels=hparams.residual_channels,
gate_channels=hparams.gate_channels,
skip_out_channels=hparams.skip_out_channels,
cin_channels=hparams.cin_channels,
gin_channels=hparams.gin_channels,
n_speakers=hparams.n_speakers,
dropout=hparams.dropout,
kernel_size=hparams.kernel_size,
cin_pad=hparams.cin_pad,
upsample_conditional_features=hparams.upsample_conditional_features,
upsample_params=upsample_params,
scalar_input=is_scalar_input(hparams.input_type),
output_distribution=hparams.output_distribution,
use_speaker_embedding=True,
)
model = CatWavAE(wavenet=wavenet,c_in=39,hid= hparams.cin_channels, tau = 0.1, k = hparams.K, frame_rate = hparams.frame_rate, hard = hparams.hard, slices = hparams.num_slices)
return model
def build_inae_model():
if is_mulaw_quantize(hparams.input_type):
if hparams.out_channels != hparams.quantize_channels:
raise RuntimeError(
"out_channels must equal to quantize_chennels if input_type is 'mulaw-quantize'")
if hparams.upsample_conditional_features and hparams.cin_channels < 0:
s = "Upsample conv layers were specified while local conditioning disabled. "
s += "Notice that upsample conv layers will never be used."
warn(s)
upsample_params = hparams.upsample_params
upsample_params["cin_channels"] = hparams.cin_channels
upsample_params["cin_pad"] = hparams.cin_pad
wavenet = WaveNet(
out_channels=hparams.out_channels,
layers=hparams.layers,
stacks=hparams.stacks,
residual_channels=hparams.residual_channels,
gate_channels=hparams.gate_channels,
skip_out_channels=hparams.skip_out_channels,
cin_channels=hparams.cin_channels,
gin_channels=hparams.gin_channels,
n_speakers=hparams.n_speakers,
dropout=hparams.dropout,
kernel_size=hparams.kernel_size,
cin_pad=hparams.cin_pad,
upsample_conditional_features=hparams.upsample_conditional_features,
upsample_params=upsample_params,
scalar_input=is_scalar_input(hparams.input_type),
output_distribution=hparams.output_distribution,
use_speaker_embedding=True,
)
if hparams.name == 'inae':
model = INAE(wavenet = wavenet, c_in = 39, hid = 64, frame_rate = hparams.frame_rate, adain = hparams.adain)
elif hparams.name == 'inae1':
model = INAE1(wavenet = wavenet, c_in = 39, hid = 64, frame_rate = hparams.frame_rate, adain = hparams.adain)
return model
def build_vqvae_model():
if is_mulaw_quantize(hparams.input_type):
if hparams.out_channels != hparams.quantize_channels:
raise RuntimeError(
"out_channels must equal to quantize_chennels if input_type is 'mulaw-quantize'")
if hparams.upsample_conditional_features and hparams.cin_channels < 0:
s = "Upsample conv layers were specified while local conditioning disabled. "
s += "Notice that upsample conv layers will never be used."
warn(s)
upsample_params = hparams.upsample_params
upsample_params["cin_channels"] = hparams.cin_channels
upsample_params["cin_pad"] = hparams.cin_pad
wavenet = WaveNet(
out_channels=hparams.out_channels,
layers=hparams.layers,
stacks=hparams.stacks,
residual_channels=hparams.residual_channels,
gate_channels=hparams.gate_channels,
skip_out_channels=hparams.skip_out_channels,
cin_channels=hparams.cin_channels,
gin_channels=hparams.gin_channels,
n_speakers=hparams.n_speakers,
dropout=hparams.dropout,
kernel_size=hparams.kernel_size,
cin_pad=hparams.cin_pad,
upsample_conditional_features=hparams.upsample_conditional_features,
upsample_params=upsample_params,
scalar_input=is_scalar_input(hparams.input_type),
output_distribution=hparams.output_distribution,
use_speaker_embedding=True,
)
if hparams.use_K1 and hparams.K1 != hparams.K:
K1 = hparams.K1
else:
K1 = None
if hparams.post_conv:
hid = 64
else:
hid = hparams.cin_channels
model = VQVAE(wavenet=wavenet,c_in=39,hid=hid, frame_rate = hparams.frame_rate,
use_time_jitter = hparams.time_jitter, K = hparams.K, ema = hparams.ema,
sliced = hparams.sliced, ins_norm = hparams.ins_norm, post_conv = hparams.post_conv, adain = hparams.adain,
dropout = hparams.vq_drop, drop_dim = hparams.drop_dim, K1 = K1, num_slices = hparams.num_slices )
return model
def build_autoencoder_model():
model = eval(hparams.name)(c_in = hparams.cin_channels,hid=64)
return model
def build_wvae_model():
if is_mulaw_quantize(hparams.input_type):
if hparams.out_channels != hparams.quantize_channels:
raise RuntimeError(
"out_channels must equal to quantize_chennels if input_type is 'mulaw-quantize'")
if hparams.upsample_conditional_features and hparams.cin_channels < 0:
s = "Upsample conv layers were specified while local conditioning disabled. "
s += "Notice that upsample conv layers will never be used."
warn(s)
upsample_params = hparams.upsample_params
upsample_params["cin_channels"] = hparams.cin_channels
upsample_params["cin_pad"] = hparams.cin_pad
wavenet = WaveNet(
out_channels=hparams.out_channels,
layers=hparams.layers,
stacks=hparams.stacks,
residual_channels=hparams.residual_channels,
gate_channels=hparams.gate_channels,
skip_out_channels=hparams.skip_out_channels,
cin_channels=hparams.cin_channels,
gin_channels=hparams.gin_channels,
n_speakers=hparams.n_speakers,
dropout=hparams.dropout,
kernel_size=hparams.kernel_size,
cin_pad=hparams.cin_pad,
upsample_conditional_features=hparams.upsample_conditional_features,
upsample_params=upsample_params,
scalar_input=is_scalar_input(hparams.input_type),
output_distribution=hparams.output_distribution,
use_speaker_embedding=True,
)
model = wvae(wavenet=wavenet,c_in=39,hid=64, frame_rate = hparams.frame_rate)
return model
def build_wavenet_model():
if is_mulaw_quantize(hparams.input_type):
if hparams.out_channels != hparams.quantize_channels:
raise RuntimeError(
"out_channels must equal to quantize_chennels if input_type is 'mulaw-quantize'")
if hparams.upsample_conditional_features and hparams.cin_channels < 0:
s = "Upsample conv layers were specified while local conditioning disabled. "
s += "Notice that upsample conv layers will never be used."
warn(s)
upsample_params = hparams.upsample_params
upsample_params["cin_channels"] = hparams.cin_channels
upsample_params["cin_pad"] = hparams.cin_pad
model = WaveNet(
out_channels=hparams.out_channels,
layers=hparams.layers,
stacks=hparams.stacks,
residual_channels=hparams.residual_channels,
gate_channels=hparams.gate_channels,
skip_out_channels=hparams.skip_out_channels,
cin_channels=hparams.cin_channels,
gin_channels=hparams.gin_channels,
n_speakers=hparams.n_speakers,
dropout=hparams.dropout,
kernel_size=hparams.kernel_size,
cin_pad=hparams.cin_pad,
upsample_conditional_features=hparams.upsample_conditional_features,
upsample_params=upsample_params,
scalar_input=is_scalar_input(hparams.input_type),
output_distribution=hparams.output_distribution,
use_speaker_embedding=True,
)
return model
def _process_utterance(base_dir, f, model, dst_dir):
#feat_path = base_dir + feat + '.norm.npy'
feat_path = base_dir + f + '.npy'
if not os.path.exists(feat_path):
raise Exception
dirs = base_dir.split('/')
assert len(dirs) == 6
lan = dirs[-4]
fnm = dirs[-2]
out_dir = dst_dir + f'2019/{lan}/test/{fnm}.txt'
feat = np.load(feat_path)
print(f"feat shape {feat.shape}",flush=True)
n_feat = feat.shape[1]
n_frames = feat.shape[0]
feat = feat.T
feat_tensor = torch.from_numpy(np.array([feat]))
seg_len = hparams.max_time_steps // hparams.hop_size
feat_tensor = feat_tensor.to(device)
rep_tensor = model.encode(feat_tensor)
rep = rep_tensor.data.cpu().numpy()[0].transpose()
out = rep
print(f"{out.shape}: {out_dir} ",flush=True)
out_dir_dirname = os.path.dirname(out_dir)
os.makedirs(out_dir_dirname,exist_ok=True)
np.savetxt(out_dir, out,fmt='%.6f')
if __name__ == "__main__":
args = docopt(__doc__)
print("Command line args:\n", args)
checkpoint_path = args["<checkpoint>"]
dst_dir = args["<dst_dir>"]
os.makedirs(dst_dir, exist_ok=True)
scp_dir = args['<scp_dir>']
feat = args['<feat>']
#length = int(args["--length"])
#initial_value = args["--initial-value"]
#initial_value = None if initial_value is None else float(initial_value)
#conditional_path = args["--conditional"]
#file_name_suffix = args["--file-name-suffix"]
#output_html = args["--output-html"]
#speaker_id = args["--speaker-id"]
#speaker_id = None if speaker_id is None else int(speaker_id)
preset = args["--preset"]
# Load preset if specified
if preset is not None:
with open(preset) as f:
hparams.parse_json(f.read())
# Override hyper parameters
hparams.parse(args["--hparams"])
print(hparams_debug_string())
#assert hparams.name == "wavenet_vocoder"
# Load conditional features
#if conditional_path is not None:
# c = np.load(conditional_path)
# if c.shape[1] != hparams.num_mels:
# c = np.swapaxes(c, 0, 1)
#else:
# c = None
#from train import build_model
# Model
if hparams.name == 'wvae':
model = build_wvae_model()
elif hparams.name == 'vqvae':
model = build_vqvae_model()
elif hparams.name == 'inae' or hparams.name == 'inae1':
model = build_inae_model()
elif hparams.name == 'catae':
model = build_catae_model()
else:
model = build_autoencoder_model()#.to(device)
if use_cuda:
checkpoint = torch.load(checkpoint_path)
else:
checkpoint = torch.load(checkpoint_path, map_location=lambda storage, loc: storage)
model.load_state_dict(checkpoint["state_dict"])
model = model.to(device)
print("Load checkpoint from {}".format(checkpoint_path),flush=True)
model.eval()
scp_f = open(scp_dir)
file_list = json.load(scp_f)
for _,base_dir in file_list:
_process_utterance(base_dir,feat,model,dst_dir )