-
Notifications
You must be signed in to change notification settings - Fork 1
/
manipulate.py
executable file
·198 lines (177 loc) · 6.03 KB
/
manipulate.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
import argparse
import json
import os
import pickle
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import torch
from tqdm import trange
from wgan import get_manipulate_z, get_random_z
from wavegan import WaveGANDiscriminator, WaveGANGenerator, WaveGANQ
def save_samples(epoch_samples, epoch, output_dir, fs=16000):
import matplotlib.pyplot as plt
import numpy as np
import soundfile as sf
"""
Save output samples to disk
"""
sample_dir = output_dir
sample_dir.mkdir(parents=True, exist_ok=True)
for idx, samp in enumerate(epoch_samples):
output_path = sample_dir / f"{epoch}_{idx + 1:02d}.wav"
print(output_path)
samp = samp[0]
samp = (samp - np.mean(samp)) / np.abs(samp).max()
plt.figure()
plt.plot(samp)
plt.savefig(Path(sample_dir) / f"{epoch}_{idx + 1:02d}.png")
plt.close()
sf.write(output_path, samp, fs)
def parse_arguments():
def str_to_bool(flag: str):
return {"true": True, "false": False}[flag.lower()]
"""
Get command line arguments
"""
parser = argparse.ArgumentParser(
description='Analyze a fiwGAN on a given latent code c')
parser.add_argument('--model-size',
dest='model_size',
type=int,
default=64,
help='Model size parameter used in WaveGAN')
parser.add_argument(
'-ppfl',
'--post-proc-filt-len',
dest='post_proc_filt_len',
type=int,
default=512,
help=
'Length of post processing filter used by generator. Set to 0 to disable.'
)
parser.add_argument('--ngpus',
dest='ngpus',
type=int,
default=1,
help='Number of GPUs to use for training')
parser.add_argument('--latent-dim',
dest='latent_dim',
type=int,
default=100,
help='Size of latent dimension used by generator')
parser.add_argument('--verbose',
dest='verbose',
default=False,
action='store_true')
parser.add_argument(
'--output_dir',
type=str,
help='Path to directory where model files will be output',
)
parser.add_argument(
'--num_categ',
dest='num_categ',
type=int,
default=3,
help='Number of categorical variables',
)
parser.add_argument(
'--model_path',
dest='model_path',
type=str,
help="the path of the model",
)
parser.add_argument(
'--random_range',
dest='random_range',
type=int,
help="latent variable range",
)
parser.add_argument(
'--num_epochs',
dest='num_epochs',
type=int,
default=100,
help='Number of epochs',
)
parser.add_argument('--job_id', type=str)
parser.add_argument('--alter_axis', type=str)
parser.add_argument('--alter_range', type=str)
parser.add_argument('--filter_range', type=str)
parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument('--initialize_mode', type=str, default="uniform")
parser.add_argument('--control_non_alter_vals',
type=str_to_bool,
default=True)
parser.add_argument('--fix_laten_vals_accross_alter_vals',
type=str_to_bool,
default=True)
args = parser.parse_args()
return vars(args)
if __name__ == '__main__':
args = parse_arguments()
with open(Path(args["output_dir"]) / f"params.json", 'w') as fout:
json.dump(args, fout)
latent_dim = args['latent_dim']
ngpus = args['ngpus']
model_size = args['model_size']
Q_num_categ = args['num_categ']
model_path = Path(args['model_path'])
random_range = args['random_range']
output_dir = Path(args['output_dir'])
num_epochs = args['num_epochs']
print(args['alter_axis'])
alter_axis = [int(x) for x in args['alter_axis'].split(",")]
filter_start, filter_end = [
float(x) for x in args["filter_range"].split(",")
]
alter_start, alter_end, interval = [
float(x) for x in args["alter_range"].split(",")
]
alter_vals = np.arange(alter_start, alter_end, interval)
print(alter_vals)
use_cuda = ngpus >= 1
#load model
model_gen = WaveGANGenerator(
model_size=model_size,
ngpus=ngpus,
latent_dim=latent_dim,
post_proc_filt_len=args['post_proc_filt_len'],
upsample=True,
verbose=args["verbose"],
)
model_gen.load_state_dict(torch.load(model_path / "Gen.pkl"))
batch_size = args["batch_size"]
# batch_size=1
batch_step = 0
(output_dir / "latent_v").mkdir(parents=True, exist_ok=True)
for noise_v, altered_vals, i, total_batches in get_manipulate_z(
Q_num_categ,
batch_size,
latent_dim,
alter_axis=alter_axis,
alter_vals=alter_vals,
use_cuda=use_cuda,
random_range=random_range,
initialize_mode=args["initialize_mode"],
control_non_alter_vals=args["control_non_alter_vals"],
fix_laten_vals_accross_alter_vals=args[
"fix_laten_vals_accross_alter_vals"],
):
print(f"\rPrediction batch {i}/{total_batches}", end="")
latent_v = noise_v.cpu().data.numpy()
with open(
output_dir / "latent_v" / f"{batch_step:02d}.pickle",
'wb',
) as fout:
pickle.dump(latent_v, fout)
if use_cuda:
noise_v = noise_v.cuda()
# Generate outputs for fixed latent samples
samp_output = model_gen.forward(noise_v)
if use_cuda:
samp_output = samp_output.cpu()
samples = samp_output.data.numpy()
save_samples(samples, f"{batch_step:02d}", output_dir / "Audio")
batch_step += 1