-
Notifications
You must be signed in to change notification settings - Fork 1
/
manipulate_two_z.py
executable file
·216 lines (196 loc) · 6.8 KB
/
manipulate_two_z.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
from wgan import get_random_z, get_manipulate_z
import argparse
from wavegan import WaveGANDiscriminator, WaveGANGenerator, WaveGANQ
import os
from pathlib import Path
import torch
from tqdm import trange
import pickle
import numpy as np
import matplotlib.pyplot as plt
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}.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():
"""
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)
args = parser.parse_args()
return vars(args)
if __name__ == '__main__':
args = parse_arguments()
latent_dim = args['latent_dim']
ngpus = args['ngpus']
model_size = args['model_size']
model_dir = os.path.join(args['output_dir'], args["job_id"])
args['model_dir'] = Path(model_dir)
model_dir = args['model_dir']
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 = len(alter_vals)
#Starting: analyze the model
for axis in alter_axis:
for epoch in trange(num_epochs):
noise_v, alter_vals = get_manipulate_z(
Q_num_categ,
batch_size,
latent_dim,
alter_axis=axis,
alter_vals=alter_vals,
use_cuda=use_cuda,
random_range=random_range,
)
latent_v = noise_v.cpu().data.numpy()
print(latent_v)
(model_dir / "latent_v").mkdir(parents=True, exist_ok=True)
with open(
model_dir / "latent_v" / f"{axis}_{epoch}.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()
if model_dir:
save_samples(samples, f"{axis}_{epoch}", model_dir / "Audio")
ax = plt.figure(figsize=(5, 5)).add_subplot(projection='3d')
#ax.invert_yaxis()
ax.view_init(30, 30)
start, end = filter_start * len(samples[0][0]), filter_end * len(
samples[0][0])
print(start, end)
start, end = int(start), int(end)
sec_per_sample = 1.0 / 16000
x = np.arange(0, sec_per_sample * len(samples[0, 0, start:end]),
sec_per_sample)
print(x.shape, samples[:, :, start:end].shape)
print()
for i in range(batch_size):
print(f"Plot {i}: {alter_vals[i]}")
ax.plot(
x,
samples[i, 0, start:end],
zs=alter_vals[i],
zdir="x",
label=f"{i}",
)
ax.set_xlabel(f'L {axis}')
ax.set_xlim(xmin=alter_start, xmax=alter_end)
ax.set_ylabel('Time')
ax.set_zlabel('Amplitude')
print('ax.azim {}'.format(ax.azim))
print('ax.elev {}'.format(ax.elev))
plt.savefig(model_dir / "Audio" / f"{axis}_{epoch}.png", dpi=600)
plt.close()
with open(model_dir / "Audio" / f"{axis}_{epoch}.txt",
'w') as fout:
np.savetxt(fname=fout, X=alter_vals)