-
Notifications
You must be signed in to change notification settings - Fork 48
/
solver.py
426 lines (356 loc) · 17.5 KB
/
solver.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
"""solver.py"""
import os
import visdom
from tqdm import tqdm
import torch
import torch.optim as optim
import torch.nn.functional as F
from torchvision.utils import make_grid, save_image
from utils import DataGather, mkdirs, grid2gif
from ops import recon_loss, kl_divergence, permute_dims
from model import FactorVAE1, FactorVAE2, Discriminator
from dataset import return_data
class Solver(object):
def __init__(self, args):
# Misc
use_cuda = args.cuda and torch.cuda.is_available()
self.device = 'cuda' if use_cuda else 'cpu'
self.name = args.name
self.max_iter = int(args.max_iter)
self.print_iter = args.print_iter
self.global_iter = 0
self.pbar = tqdm(total=self.max_iter)
# Data
self.dset_dir = args.dset_dir
self.dataset = args.dataset
self.batch_size = args.batch_size
self.data_loader = return_data(args)
# Networks & Optimizers
self.z_dim = args.z_dim
self.gamma = args.gamma
self.lr_VAE = args.lr_VAE
self.beta1_VAE = args.beta1_VAE
self.beta2_VAE = args.beta2_VAE
self.lr_D = args.lr_D
self.beta1_D = args.beta1_D
self.beta2_D = args.beta2_D
if args.dataset == 'dsprites':
self.VAE = FactorVAE1(self.z_dim).to(self.device)
self.nc = 1
else:
self.VAE = FactorVAE2(self.z_dim).to(self.device)
self.nc = 3
self.optim_VAE = optim.Adam(self.VAE.parameters(), lr=self.lr_VAE,
betas=(self.beta1_VAE, self.beta2_VAE))
self.D = Discriminator(self.z_dim).to(self.device)
self.optim_D = optim.Adam(self.D.parameters(), lr=self.lr_D,
betas=(self.beta1_D, self.beta2_D))
self.nets = [self.VAE, self.D]
# Visdom
self.viz_on = args.viz_on
self.win_id = dict(D_z='win_D_z', recon='win_recon', kld='win_kld', acc='win_acc')
self.line_gather = DataGather('iter', 'soft_D_z', 'soft_D_z_pperm', 'recon', 'kld', 'acc')
self.image_gather = DataGather('true', 'recon')
if self.viz_on:
self.viz_port = args.viz_port
self.viz = visdom.Visdom(port=self.viz_port)
self.viz_ll_iter = args.viz_ll_iter
self.viz_la_iter = args.viz_la_iter
self.viz_ra_iter = args.viz_ra_iter
self.viz_ta_iter = args.viz_ta_iter
if not self.viz.win_exists(env=self.name+'/lines', win=self.win_id['D_z']):
self.viz_init()
# Checkpoint
self.ckpt_dir = os.path.join(args.ckpt_dir, args.name)
self.ckpt_save_iter = args.ckpt_save_iter
mkdirs(self.ckpt_dir)
if args.ckpt_load:
self.load_checkpoint(args.ckpt_load)
# Output(latent traverse GIF)
self.output_dir = os.path.join(args.output_dir, args.name)
self.output_save = args.output_save
mkdirs(self.output_dir)
def train(self):
self.net_mode(train=True)
ones = torch.ones(self.batch_size, dtype=torch.long, device=self.device)
zeros = torch.zeros(self.batch_size, dtype=torch.long, device=self.device)
out = False
while not out:
for x_true1, x_true2 in self.data_loader:
self.global_iter += 1
self.pbar.update(1)
x_true1 = x_true1.to(self.device)
x_recon, mu, logvar, z = self.VAE(x_true1)
vae_recon_loss = recon_loss(x_true1, x_recon)
vae_kld = kl_divergence(mu, logvar)
D_z = self.D(z)
vae_tc_loss = (D_z[:, :1] - D_z[:, 1:]).mean()
vae_loss = vae_recon_loss + vae_kld + self.gamma*vae_tc_loss
self.optim_VAE.zero_grad()
vae_loss.backward(retain_graph=True)
self.optim_VAE.step()
x_true2 = x_true2.to(self.device)
z_prime = self.VAE(x_true2, no_dec=True)
z_pperm = permute_dims(z_prime).detach()
D_z_pperm = self.D(z_pperm)
D_tc_loss = 0.5*(F.cross_entropy(D_z, zeros) + F.cross_entropy(D_z_pperm, ones))
self.optim_D.zero_grad()
D_tc_loss.backward()
self.optim_D.step()
if self.global_iter%self.print_iter == 0:
self.pbar.write('[{}] vae_recon_loss:{:.3f} vae_kld:{:.3f} vae_tc_loss:{:.3f} D_tc_loss:{:.3f}'.format(
self.global_iter, vae_recon_loss.item(), vae_kld.item(), vae_tc_loss.item(), D_tc_loss.item()))
if self.global_iter%self.ckpt_save_iter == 0:
self.save_checkpoint(self.global_iter)
if self.viz_on and (self.global_iter%self.viz_ll_iter == 0):
soft_D_z = F.softmax(D_z, 1)[:, :1].detach()
soft_D_z_pperm = F.softmax(D_z_pperm, 1)[:, :1].detach()
D_acc = ((soft_D_z >= 0.5).sum() + (soft_D_z_pperm < 0.5).sum()).float()
D_acc /= 2*self.batch_size
self.line_gather.insert(iter=self.global_iter,
soft_D_z=soft_D_z.mean().item(),
soft_D_z_pperm=soft_D_z_pperm.mean().item(),
recon=vae_recon_loss.item(),
kld=vae_kld.item(),
acc=D_acc.item())
if self.viz_on and (self.global_iter%self.viz_la_iter == 0):
self.visualize_line()
self.line_gather.flush()
if self.viz_on and (self.global_iter%self.viz_ra_iter == 0):
self.image_gather.insert(true=x_true1.data.cpu(),
recon=F.sigmoid(x_recon).data.cpu())
self.visualize_recon()
self.image_gather.flush()
if self.viz_on and (self.global_iter%self.viz_ta_iter == 0):
if self.dataset.lower() == '3dchairs':
self.visualize_traverse(limit=2, inter=0.5)
else:
self.visualize_traverse(limit=3, inter=2/3)
if self.global_iter >= self.max_iter:
out = True
break
self.pbar.write("[Training Finished]")
self.pbar.close()
def visualize_recon(self):
data = self.image_gather.data
true_image = data['true'][0]
recon_image = data['recon'][0]
true_image = make_grid(true_image)
recon_image = make_grid(recon_image)
sample = torch.stack([true_image, recon_image], dim=0)
self.viz.images(sample, env=self.name+'/recon_image',
opts=dict(title=str(self.global_iter)))
def visualize_line(self):
data = self.line_gather.data
iters = torch.Tensor(data['iter'])
recon = torch.Tensor(data['recon'])
kld = torch.Tensor(data['kld'])
D_acc = torch.Tensor(data['acc'])
soft_D_z = torch.Tensor(data['soft_D_z'])
soft_D_z_pperm = torch.Tensor(data['soft_D_z_pperm'])
soft_D_zs = torch.stack([soft_D_z, soft_D_z_pperm], -1)
self.viz.line(X=iters,
Y=soft_D_zs,
env=self.name+'/lines',
win=self.win_id['D_z'],
update='append',
opts=dict(
xlabel='iteration',
ylabel='D(.)',
legend=['D(z)', 'D(z_perm)']))
self.viz.line(X=iters,
Y=recon,
env=self.name+'/lines',
win=self.win_id['recon'],
update='append',
opts=dict(
xlabel='iteration',
ylabel='reconstruction loss',))
self.viz.line(X=iters,
Y=D_acc,
env=self.name+'/lines',
win=self.win_id['acc'],
update='append',
opts=dict(
xlabel='iteration',
ylabel='discriminator accuracy',))
self.viz.line(X=iters,
Y=kld,
env=self.name+'/lines',
win=self.win_id['kld'],
update='append',
opts=dict(
xlabel='iteration',
ylabel='kl divergence',))
def visualize_traverse(self, limit=3, inter=2/3, loc=-1):
self.net_mode(train=False)
decoder = self.VAE.decode
encoder = self.VAE.encode
interpolation = torch.arange(-limit, limit+0.1, inter)
random_img = self.data_loader.dataset.__getitem__(0)[1]
random_img = random_img.to(self.device).unsqueeze(0)
random_img_z = encoder(random_img)[:, :self.z_dim]
if self.dataset.lower() == 'dsprites':
fixed_idx1 = 87040 # square
fixed_idx2 = 332800 # ellipse
fixed_idx3 = 578560 # heart
fixed_img1 = self.data_loader.dataset.__getitem__(fixed_idx1)[0]
fixed_img1 = fixed_img1.to(self.device).unsqueeze(0)
fixed_img_z1 = encoder(fixed_img1)[:, :self.z_dim]
fixed_img2 = self.data_loader.dataset.__getitem__(fixed_idx2)[0]
fixed_img2 = fixed_img2.to(self.device).unsqueeze(0)
fixed_img_z2 = encoder(fixed_img2)[:, :self.z_dim]
fixed_img3 = self.data_loader.dataset.__getitem__(fixed_idx3)[0]
fixed_img3 = fixed_img3.to(self.device).unsqueeze(0)
fixed_img_z3 = encoder(fixed_img3)[:, :self.z_dim]
Z = {'fixed_square':fixed_img_z1, 'fixed_ellipse':fixed_img_z2,
'fixed_heart':fixed_img_z3, 'random_img':random_img_z}
elif self.dataset.lower() == 'celeba':
fixed_idx1 = 191281 # 'CelebA/img_align_celeba/191282.jpg'
fixed_idx2 = 143307 # 'CelebA/img_align_celeba/143308.jpg'
fixed_idx3 = 101535 # 'CelebA/img_align_celeba/101536.jpg'
fixed_idx4 = 70059 # 'CelebA/img_align_celeba/070060.jpg'
fixed_img1 = self.data_loader.dataset.__getitem__(fixed_idx1)[0]
fixed_img1 = fixed_img1.to(self.device).unsqueeze(0)
fixed_img_z1 = encoder(fixed_img1)[:, :self.z_dim]
fixed_img2 = self.data_loader.dataset.__getitem__(fixed_idx2)[0]
fixed_img2 = fixed_img2.to(self.device).unsqueeze(0)
fixed_img_z2 = encoder(fixed_img2)[:, :self.z_dim]
fixed_img3 = self.data_loader.dataset.__getitem__(fixed_idx3)[0]
fixed_img3 = fixed_img3.to(self.device).unsqueeze(0)
fixed_img_z3 = encoder(fixed_img3)[:, :self.z_dim]
fixed_img4 = self.data_loader.dataset.__getitem__(fixed_idx4)[0]
fixed_img4 = fixed_img4.to(self.device).unsqueeze(0)
fixed_img_z4 = encoder(fixed_img4)[:, :self.z_dim]
Z = {'fixed_1':fixed_img_z1, 'fixed_2':fixed_img_z2,
'fixed_3':fixed_img_z3, 'fixed_4':fixed_img_z4,
'random':random_img_z}
elif self.dataset.lower() == '3dchairs':
fixed_idx1 = 40919 # 3DChairs/images/4682_image_052_p030_t232_r096.png
fixed_idx2 = 5172 # 3DChairs/images/14657_image_020_p020_t232_r096.png
fixed_idx3 = 22330 # 3DChairs/images/30099_image_052_p030_t232_r096.png
fixed_img1 = self.data_loader.dataset.__getitem__(fixed_idx1)[0]
fixed_img1 = fixed_img1.to(self.device).unsqueeze(0)
fixed_img_z1 = encoder(fixed_img1)[:, :self.z_dim]
fixed_img2 = self.data_loader.dataset.__getitem__(fixed_idx2)[0]
fixed_img2 = fixed_img2.to(self.device).unsqueeze(0)
fixed_img_z2 = encoder(fixed_img2)[:, :self.z_dim]
fixed_img3 = self.data_loader.dataset.__getitem__(fixed_idx3)[0]
fixed_img3 = fixed_img3.to(self.device).unsqueeze(0)
fixed_img_z3 = encoder(fixed_img3)[:, :self.z_dim]
Z = {'fixed_1':fixed_img_z1, 'fixed_2':fixed_img_z2,
'fixed_3':fixed_img_z3, 'random':random_img_z}
else:
fixed_idx = 0
fixed_img = self.data_loader.dataset.__getitem__(fixed_idx)[0]
fixed_img = fixed_img.to(self.device).unsqueeze(0)
fixed_img_z = encoder(fixed_img)[:, :self.z_dim]
random_z = torch.rand(1, self.z_dim, 1, 1, device=self.device)
Z = {'fixed_img':fixed_img_z, 'random_img':random_img_z, 'random_z':random_z}
gifs = []
for key in Z:
z_ori = Z[key]
samples = []
for row in range(self.z_dim):
if loc != -1 and row != loc:
continue
z = z_ori.clone()
for val in interpolation:
z[:, row] = val
sample = F.sigmoid(decoder(z)).data
samples.append(sample)
gifs.append(sample)
samples = torch.cat(samples, dim=0).cpu()
title = '{}_latent_traversal(iter:{})'.format(key, self.global_iter)
self.viz.images(samples, env=self.name+'/traverse',
opts=dict(title=title), nrow=len(interpolation))
if self.output_save:
output_dir = os.path.join(self.output_dir, str(self.global_iter))
mkdirs(output_dir)
gifs = torch.cat(gifs)
gifs = gifs.view(len(Z), self.z_dim, len(interpolation), self.nc, 64, 64).transpose(1, 2)
for i, key in enumerate(Z.keys()):
for j, val in enumerate(interpolation):
save_image(tensor=gifs[i][j].cpu(),
filename=os.path.join(output_dir, '{}_{}.jpg'.format(key, j)),
nrow=self.z_dim, pad_value=1)
grid2gif(str(os.path.join(output_dir, key+'*.jpg')),
str(os.path.join(output_dir, key+'.gif')), delay=10)
self.net_mode(train=True)
def viz_init(self):
zero_init = torch.zeros([1])
self.viz.line(X=zero_init,
Y=torch.stack([zero_init, zero_init], -1),
env=self.name+'/lines',
win=self.win_id['D_z'],
opts=dict(
xlabel='iteration',
ylabel='D(.)',
legend=['D(z)', 'D(z_perm)']))
self.viz.line(X=zero_init,
Y=zero_init,
env=self.name+'/lines',
win=self.win_id['recon'],
opts=dict(
xlabel='iteration',
ylabel='reconstruction loss',))
self.viz.line(X=zero_init,
Y=zero_init,
env=self.name+'/lines',
win=self.win_id['acc'],
opts=dict(
xlabel='iteration',
ylabel='discriminator accuracy',))
self.viz.line(X=zero_init,
Y=zero_init,
env=self.name+'/lines',
win=self.win_id['kld'],
opts=dict(
xlabel='iteration',
ylabel='kl divergence',))
def net_mode(self, train):
if not isinstance(train, bool):
raise ValueError('Only bool type is supported. True|False')
for net in self.nets:
if train:
net.train()
else:
net.eval()
def save_checkpoint(self, ckptname='last', verbose=True):
model_states = {'D':self.D.state_dict(),
'VAE':self.VAE.state_dict()}
optim_states = {'optim_D':self.optim_D.state_dict(),
'optim_VAE':self.optim_VAE.state_dict()}
states = {'iter':self.global_iter,
'model_states':model_states,
'optim_states':optim_states}
filepath = os.path.join(self.ckpt_dir, str(ckptname))
with open(filepath, 'wb+') as f:
torch.save(states, f)
if verbose:
self.pbar.write("=> saved checkpoint '{}' (iter {})".format(filepath, self.global_iter))
def load_checkpoint(self, ckptname='last', verbose=True):
if ckptname == 'last':
ckpts = os.listdir(self.ckpt_dir)
if not ckpts:
if verbose:
self.pbar.write("=> no checkpoint found")
return
ckpts = [int(ckpt) for ckpt in ckpts]
ckpts.sort(reverse=True)
ckptname = str(ckpts[0])
filepath = os.path.join(self.ckpt_dir, ckptname)
if os.path.isfile(filepath):
with open(filepath, 'rb') as f:
checkpoint = torch.load(f)
self.global_iter = checkpoint['iter']
self.VAE.load_state_dict(checkpoint['model_states']['VAE'])
self.D.load_state_dict(checkpoint['model_states']['D'])
self.optim_VAE.load_state_dict(checkpoint['optim_states']['optim_VAE'])
self.optim_D.load_state_dict(checkpoint['optim_states']['optim_D'])
self.pbar.update(self.global_iter)
if verbose:
self.pbar.write("=> loaded checkpoint '{} (iter {})'".format(filepath, self.global_iter))
else:
if verbose:
self.pbar.write("=> no checkpoint found at '{}'".format(filepath))