forked from ciaua/unagan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.py
545 lines (427 loc) · 15.3 KB
/
generate.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
import argparse
from pathlib import Path
import yaml
import wandb
import sys
import shutil
import numpy as np
import soundfile as sf
from collections import OrderedDict
from argparse import Namespace
import src.training_manager as manager
import torch
import torch.nn as nn
import torch.nn.functional as F
from datetime import datetime
import download_weights
# melgan
import melgan_models
# hifigan
import hifi_models
def read_yaml(fp):
with open(fp) as file:
# return yaml.load(file)
return yaml.load(file, Loader=yaml.Loader)
class RCBlock(nn.Module):
def __init__(self, feat_dim, ks, dilation, num_groups):
super().__init__()
# ks = 3 # kernel size
ksm1 = ks - 1
mfd = feat_dim
di = dilation
self.num_groups = num_groups
self.relu = nn.LeakyReLU()
self.rec = nn.GRU(mfd, mfd, num_layers=1, batch_first=True, bidirectional=True)
self.conv = nn.Conv1d(
mfd, mfd, ks, 1, ksm1 * di // 2, dilation=di, groups=num_groups
)
self.gn = nn.GroupNorm(num_groups, mfd)
def init_hidden(self, batch_size, hidden_size):
num_layers = 1
num_directions = 2
hidden = torch.zeros(num_layers * num_directions, batch_size, hidden_size)
hidden.normal_(0, 1)
return hidden
def forward(self, x):
bs, mfd, nf = x.size()
hidden = self.init_hidden(bs, mfd).to(x.device)
r = x.transpose(1, 2)
r, _ = self.rec(r, hidden)
r = r.transpose(1, 2).view(bs, 2, mfd, nf).sum(1)
c = self.relu(self.gn(self.conv(r)))
x = x + r + c
return x
class BodyGBlock(nn.Module):
def __init__(self, input_dim, output_dim, middle_dim, num_groups):
super().__init__()
ks = 3 # filter size
mfd = middle_dim
self.input_dim = input_dim
self.output_dim = output_dim
self.mfd = mfd
self.num_groups = num_groups
# ### Main body ###
block = [
nn.Conv1d(input_dim, mfd, 3, 1, 1),
nn.GroupNorm(num_groups, mfd),
nn.LeakyReLU(),
RCBlock(mfd, ks, dilation=1, num_groups=num_groups),
nn.Conv1d(mfd, output_dim, 3, 1, 1),
]
self.block = nn.Sequential(*block)
def forward(self, x):
# ### Main ###
x = self.block(x)
return x
class HierarchicalGenerator(nn.Module):
def __init__(self, feat_dim, z_dim, z_scale_factors):
super().__init__()
# ks = 3 # filter size
mfd = 512
num_groups = 4
self.num_groups = num_groups
self.mfd = mfd
self.feat_dim = feat_dim
self.z_dim = z_dim
self.z_scale_factors = z_scale_factors
# ### Main body ###
self.block0 = BodyGBlock(z_dim, mfd, mfd, num_groups)
self.head0 = nn.Conv1d(mfd, feat_dim, 3, 1, 1)
blocks = []
heads = []
for scale_factor in z_scale_factors:
block = BodyGBlock(mfd, mfd, mfd, num_groups)
blocks.append(block)
head = nn.Conv1d(mfd, feat_dim, 3, 1, 1)
heads.append(head)
self.blocks = nn.ModuleList(blocks)
self.heads = nn.ModuleList(heads)
# ### Head ###
# self.head = nn.Conv1d(mfd, feat_dim, 3, 1, 1)
def forward(self, z):
# SBlock0
z_scale_factors = self.z_scale_factors
# nf = min(z.size(2), cond_.size(2))
# zc = torch.cat([z[:, :, :nf], cond_[:, :, :nf]], dim=1)
x_body = self.block0(z)
x_head = self.head0(x_body)
# print(len(self.blocks))
for ii, (block, head, scale_factor) in enumerate(
zip(self.blocks, self.heads, z_scale_factors)
):
x_body = F.interpolate(x_body, scale_factor=scale_factor, mode="nearest")
x_head = F.interpolate(x_head, scale_factor=scale_factor, mode="nearest")
# print(total_scale_factor, x.shape, cond_.shape)
# nf = min(x.size(2), cond_.size(2))
# c = torch.cat([x[:, :, :nf], cond_[:, :, :nf]], dim=1)
x_body = x_body + block(x_body)
x_head = x_head + head(x_body)
# Head
# shape=(bs, feat_dim, nf)
# x = torch.sigmoid(self.head(x))
# x = torch.sigmoid(x)
return x_head
class NonHierarchicalGenerator(nn.Module):
def __init__(self, feat_dim, z_dim):
super().__init__()
ks = 3 # filter size
mfd = 512
num_groups = 4
self.num_groups = num_groups
self.mfd = mfd
self.feat_dim = feat_dim
self.z_dim = z_dim
# ### Main body ###
blocks = [
nn.Conv1d(z_dim, mfd, 3, 1, 1),
nn.GroupNorm(num_groups, mfd),
nn.LeakyReLU(),
RCBlock(mfd, ks, dilation=2, num_groups=num_groups),
RCBlock(mfd, ks, dilation=4, num_groups=num_groups),
]
self.body = nn.Sequential(*blocks)
# ### All heads ###
self.head = nn.Conv1d(mfd, feat_dim, 3, 1, 1)
def forward(self, z):
# Body
x = self.body(z)
# Head
# shape=(bs, feat_dim, nf)
x = self.head(x)
return x
def main(
output_folder=None,
duration=10,
num_samples=5,
gid=1,
seed=123,
melgan_run_id=None,
unagan_run_id=None,
hifigan_run_id=None,
wandb_code=None,
):
if wandb_code:
wandb.login(key=wandb_code, relogin=True)
if melgan_run_id and hifigan_run_id:
raise Exception("Can only set one of [melgan_run_id, hifigan_run_id], not both")
if not unagan_run_id:
raise Exception("unagan_run_id should not be empty")
download_weights.main(
model_dir=Path("models/custom"),
melgan_run_id=melgan_run_id,
unagan_run_id=unagan_run_id,
hifigan_run_id=hifigan_run_id,
)
# ### Data type ###
# assert data_type in ["singing", "speech", "piano", "violin"]
# ### Architecture type ###
# if data_type == "singing":
# assert arch_type in ["nh", "h", "hc"]
# elif data_type == "speech":
# assert arch_type in ["h", "hc"]
# elif data_type == "piano":
# assert arch_type in ["hc"]
# elif data_type == "violin":
# assert arch_type in ["hc"]
# if arch_type == "nh":
# arch_type = "nonhierarchical"
# elif arch_type == "h":
# arch_type = "hierarchical"
# elif arch_type == "hc":
# arch_type = "hierarchical_with_cycle"
data_type = "custom"
arch_type = "hierarchical_with_cycle"
# ### Model type ###
model_type = f"{data_type}.{arch_type}"
# ### Model info ###
if output_folder is None:
output_folder = Path("generated_samples") / model_type
output_folder = Path(output_folder)
output_folder.mkdir(parents=True, exist_ok=True)
# also save to all_generated_audio_dir is the folder exists,
# but do not save if in unagan training (both run id None)
if not unagan_run_id:
all_generated_audio_dir = None
else:
try:
all_generated_audio_dir = Path(
"/content/drive/My Drive/PUBLICATIONS/The Replicant/AUDIO DATABASE/UNAGAN OUTPUT/AUDIOS/"
)
all_generated_audio_dir.mkdir(parents=True, exist_ok=True)
print(
"generated audio files will also saved to:",
str(all_generated_audio_dir),
)
except:
all_generated_audio_dir = None
print(
"the path '",
str(all_generated_audio_dir),
"' not exists. Only save audio files to:",
str(output_folder),
)
api = wandb.Api()
previous_run = api.run(f"demiurge/unagan/{unagan_run_id}")
unagan_config = Namespace(**previous_run.config)
################# unagan config parameters ##################
z_dim = unagan_config.z_dim
z_scale_factors = unagan_config.z_scale_factors
z_total_scale_factor = np.prod(z_scale_factors)
feat_dim = unagan_config.feat_dim
##################
param_fp = f"models/{data_type}/params.generator.{arch_type}.pt"
mean_fp = f"models/{data_type}/mean.mel.npy"
std_fp = f"models/{data_type}/std.mel.npy"
mean = torch.from_numpy(np.load(mean_fp)).float().view(1, feat_dim, 1)
std = torch.from_numpy(np.load(std_fp)).float().view(1, feat_dim, 1)
if gid >= 0:
mean = mean.cuda(gid)
std = std.cuda(gid)
###############################################################
### Vocoder info ###
### MELGAN ###
if melgan_run_id:
api = wandb.Api()
previous_run = api.run(f"demiurge/melgan/{melgan_run_id}")
melgan_config = Namespace(**previous_run.config)
################# melgan config parameters ##################
# melgan only parameters
n_mel_channels = 80
ngf = 32
n_residual_layers = 3
# also applied to unagan generate
sampling_rate = 44100
hop_length = 256
if n_mel_channels != feat_dim:
print(
f"Warning!!! melgan n_mel_channels {n_mel_channels} != unagan feat_dim {feat_dim}"
)
if hasattr(melgan_config, "hop_length"):
hop_length = melgan_config.hop_length
if hasattr(melgan_config, "sampling_rate"):
sampling_rate = melgan_config.sampling_rate
if hasattr(melgan_config, "n_mel_channels"):
n_mel_channels = melgan_config.n_mel_channels
if hasattr(melgan_config, "ngf"):
ngf = melgan_config.ngf
if hasattr(melgan_config, "n_residual_layers"):
n_residual_layers = melgan_config.n_residual_layers
########################
# ### Vocoder Model ###
vocoder_model_dir = Path("models") / data_type / "vocoder"
if data_type == "speech":
vocoder_name = "OriginalGenerator"
else:
vocoder_name = "GRUGenerator"
MelGAN = getattr(melgan_models, vocoder_name)
vocoder = MelGAN(n_mel_channels, ngf, n_residual_layers)
vocoder.eval()
vocoder_param_fp = vocoder_model_dir / "params.pt"
vocoder_state_dict = torch.load(vocoder_param_fp)
try:
vocoder.load_state_dict(vocoder_state_dict)
except RuntimeError as e:
print(e)
print("Fixing model by removing .module prefix")
vocoder_state_dict = OrderedDict(
(k.split(".", 1)[1], v) for k, v in vocoder_state_dict.items()
)
vocoder.load_state_dict(vocoder_state_dict)
if gid >= 0:
vocoder = vocoder.cuda(gid)
### HIFI-GAN ###
if hifigan_run_id:
api = wandb.Api()
previous_run = api.run(f"demiurge/hifi-gan/{hifigan_run_id}")
hifigan_config = Namespace(**previous_run.config)
# parameters applied to unagan generate
sampling_rate = 44100
hop_length = 256
if hasattr(hifigan_config, "hop_size"):
hop_length = hifigan_config.hop_size
if hasattr(hifigan_config, "sampling_rate"):
sampling_rate = hifigan_config.sampling_rate
vocoder_model_dir = Path("models") / data_type / "vocoder"
vocoder = hifi_models.Generator(hifigan_config)
vocoder.eval()
vocoder_state_dict = torch.load(vocoder_model_dir / "g")
vocoder.load_state_dict(vocoder_state_dict["generator"])
if gid >= 0:
vocoder = vocoder.cuda(gid)
###################################################################
# ### Generator ###
if arch_type == "nonhierarchical":
generator = NonHierarchicalGenerator(feat_dim, z_dim)
elif arch_type.startswith("hierarchical"):
generator = HierarchicalGenerator(feat_dim, z_dim, z_scale_factors)
generator.eval()
for p in generator.parameters():
p.requires_grad = False
manager.load_model(param_fp, generator, device_id="cpu")
if gid >= 0:
generator = generator.cuda(gid)
# ### Process ###
torch.manual_seed(seed)
# information for filename
filename_base = datetime.utcnow().strftime("%Y-%m-%d_%H-%M")
if melgan_run_id:
filename_base += "_mel-" + melgan_run_id
if unagan_run_id:
filename_base += "_una-" + unagan_run_id
if hifigan_run_id:
filename_base += "_hifi-" + hifigan_run_id
num_frames = int(np.ceil(duration * (sampling_rate / hop_length)))
audio_array = []
for ii in range(num_samples):
out_fp_wav = Path(output_folder) / f"{filename_base}_sample{ii}.wav"
print(f"Generating {out_fp_wav}")
if arch_type == "nonhierarchical":
z = torch.zeros((1, z_dim, num_frames)).normal_(0, 1).float()
elif arch_type.startswith("hierarchical"):
z = (
torch.zeros((1, z_dim, int(np.ceil(num_frames / z_total_scale_factor))))
.normal_(0, 1)
.float()
)
if gid >= 0:
z = z.cuda(gid)
with torch.set_grad_enabled(False):
with torch.cuda.device(gid):
# Generator
melspec_voc = generator(z)
melspec_voc = (melspec_voc * std) + mean
# Vocoder
audio = vocoder(melspec_voc)
audio = audio.squeeze().cpu().numpy()
# keep generated audio as array to log to wandb
if not unagan_run_id:
audio_array.append(audio)
else:
# Save to wav
sf.write(out_fp_wav, audio, sampling_rate)
audio_array.append(out_fp_wav)
# Save also to all_generated_audio_dir
if all_generated_audio_dir:
out2_fp_wav = (
Path(all_generated_audio_dir) / f"{filename_base}_sample{ii}.wav"
)
sf.write(out2_fp_wav, audio, sampling_rate)
return audio_array, sampling_rate
def parse_argument():
parser = argparse.ArgumentParser(
description="Uncondtional Singing Voice Generation"
)
parser.add_argument(
"--data_type",
"-d",
dest="data_type",
default="custom",
help='Data type. Options: "custom"(Default)|"speech"|"piano"|"violin"',
)
parser.add_argument(
"--arch_type",
"-a",
dest="arch_type",
default="hc",
help='Architecture type. Options: \
"nh" for non-hierarchical, available to singing|\
"h" for hierarchical, available to singing and speech|\
"hc" (Default) for hierarchical with cycle, available to all',
)
parser.add_argument(
"--output_folder",
"-o",
dest="output_folder",
default=None,
help="Output folder",
)
parser.add_argument(
"--duration",
dest="duration",
default=10,
help="Sample duration (second)",
type=float,
)
parser.add_argument(
"--num_samples",
"-ns",
dest="num_samples",
default=5,
help="Number of samples to be generated",
type=int,
)
parser.add_argument(
"--gid",
dest="gid",
default=-1,
type=int,
help="GPU id. Default: -1 for using cpu",
)
parser.add_argument(
"--seed", dest="seed", default=123, help="Random seed. Default: 123"
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_argument()
main(**vars(args))