-
Notifications
You must be signed in to change notification settings - Fork 1
/
beta-VAE.py
726 lines (562 loc) · 22.8 KB
/
beta-VAE.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from skimage import io, transform
import numpy as np
from PIL import Image
from models import Rescale, betaVAE, betaVAEdSprite, betaVAEXYS, Bernoulli
from datasetXYS import load_dataset_XYS
def test_mnist():
import os
import torchvision
from torchvision import datasets, transforms
size = 64
batch_size = 128
dataset = datasets.MNIST(root='./data',
train=True,
#transform=transforms.ToTensor(),
transform=transforms.Compose([
Rescale( (size,size) ),
transforms.ToTensor()]),
download=True)
# Data loader
data_loader = torch.utils.data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=True)
data_iter = iter(data_loader)
iter_per_epoch = len(data_loader)
# Model :
z_dim = 12
img_dim = size
img_depth=1
conv_dim = 32
use_cuda = True#False
net_depth = 3
beta = 5e0
betavae = betaVAE(beta=beta,net_depth=net_depth,z_dim=z_dim,img_dim=img_dim,img_depth=img_depth,conv_dim=conv_dim, use_cuda=use_cuda)
print(betavae)
# Optim :
lr = 1e-4
optimizer = torch.optim.Adam( betavae.parameters(), lr=lr)
# Debug :
# fixed inputs for debugging
fixed_z = Variable(torch.randn(100, z_dim))
if use_cuda :
fixed_z = fixed_z.cuda()
fixed_x, _ = next(data_iter)
path = 'test--mnist-beta{}-layers{}-z{}-conv{}-lr{}'.format(beta,net_depth,z_dim,conv_dim,lr)
if not os.path.exists( './beta-data/{}/'.format(path) ) :
os.mkdir('./beta-data/{}/'.format(path))
if not os.path.exists( './beta-data/{}/gen_images/'.format(path) ) :
os.mkdir('./beta-data/{}/gen_images/'.format(path))
fixed_x = fixed_x.view( (-1, img_depth, img_dim, img_dim) )
torchvision.utils.save_image(fixed_x.cpu(), './beta-data/{}/real_images.png'.format(path))
fixed_x = Variable(fixed_x.view(fixed_x.size(0), img_depth, img_dim, img_dim))
if use_cuda :
fixed_x = fixed_x.cuda()
out = torch.zeros((1,1))
# variations over the latent variable :
sigma_mean = torch.ones((z_dim))
mu_mean = torch.zeros((z_dim))
for epoch in range(50):
# Save the reconstructed images
reconst_images, _, _ = betavae(fixed_x)
reconst_images = reconst_images.view(-1, img_depth, img_dim, img_dim)
torchvision.utils.save_image(reconst_images.data.cpu(),'./beta-data/{}/reconst_images_{}.png'.format(path,(epoch+1)) )
# Save generated variable images :
nbr_steps = 8
mu_mean /= batch_size
sigma_mean /= batch_size
gen_images = torch.ones( (8, img_depth, img_dim, img_dim) )
for latent in range(z_dim) :
#var_z0 = torch.stack( [mu_mean]*nbr_steps, dim=0)
var_z0 = torch.zeros(nbr_steps, z_dim)
val = mu_mean[latent]-sigma_mean[latent]
step = 2.0*sigma_mean[latent]/nbr_steps
print(latent,mu_mean[latent],step)
for i in range(nbr_steps) :
var_z0[i] = mu_mean
var_z0[i][latent] = val
val += step
var_z0 = Variable(var_z0)
if use_cuda :
var_z0 = var_z0.cuda()
gen_images_latent = betavae.decoder(var_z0)
gen_images_latent = gen_images_latent.view(-1, img_depth, img_dim, img_dim).cpu().data
gen_images = torch.cat( [gen_images,gen_images_latent], dim=0)
#torchvision.utils.save_image(gen_images.data.cpu(),'./beta-data/{}/gen_images/dim{}/{}.png'.format(path,latent,(epoch+1)) )
torchvision.utils.save_image(gen_images,'./beta-data/{}/gen_images/{}.png'.format(path,(epoch+1)) )
mu_mean = 0.0
sigma_mean = 0.0
for i, (images, _) in enumerate(data_loader):
images = Variable( (images.view(-1,1,img_dim, img_dim) ) )
if use_cuda :
images = images.cuda()
out, mu, log_var = betavae(images)
mu_mean += torch.mean(mu.data,dim=0)
sigma_mean += torch.mean( torch.sqrt( torch.exp(log_var.data) ), dim=0 )
# Compute :
#reconstruction loss :
reconst_loss = F.binary_cross_entropy(out, images, size_average=False)
#reconst_loss = torch.mean( (out.view(-1) - images.view(-1))**2 )
# expected log likelyhood :
expected_log_lik = torch.mean( Bernoulli( out.view((-1)) ).log_prob( images.view((-1)) ) )
#expected_log_lik = torch.mean( Bernoulli( out ).log_prob( images ) )
# kl divergence :
#kl_divergence = 0.5 * torch.mean( torch.sum( (mu**2 + torch.exp(log_var) - log_var -1), dim=1) )
kl_divergence = 0.5 * torch.sum( (mu**2 + torch.exp(log_var) - log_var -1) )
# ELBO :
elbo = expected_log_lik - betavae.beta * kl_divergence
# TOTAL LOSS :
total_loss = reconst_loss + betavae.beta*kl_divergence
#total_loss = reconst_loss
#total_loss = -elbo
# Backprop + Optimize :
optimizer.zero_grad()
total_loss.backward()
optimizer.step()
if i % 100 == 0:
print ("Epoch[%d/%d], Step [%d/%d], Total Loss: %.4f, "
"Reconst Loss: %.4f, KL Div: %.7f, E[ |~| p(x|theta)]: %.7f "
%(epoch+1, 50, i+1, iter_per_epoch, total_loss.data[0],
reconst_loss.data[0], kl_divergence.data[0],expected_log_lik.exp().data[0]) )
def test_dSprite():
import os
import matplotlib.pyplot as plt
import torchvision
from torchvision import datasets, transforms
from datasets import dSpriteDataset
size = 64
batch_size = 256
root = './dsprites-dataset/dsprites_ndarray_co1sh3sc6or40x32y32_64x64.npz'
dataset = dSpriteDataset(root=root,
transform=transforms.Compose([
Rescale( (size,size) ),
transforms.ToTensor()])
)
# Data loader
data_loader = torch.utils.data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=True)
data_iter = iter(data_loader)
iter_per_epoch = len(data_loader)
# Model :
frompath = True
z_dim = 10
img_dim = size
img_depth=1
conv_dim = 64
use_cuda = True#False
net_depth = 3
beta = 5e0
betavae = betaVAEdSprite(beta=beta,net_depth=net_depth,z_dim=z_dim,img_dim=img_dim,img_depth=img_depth,conv_dim=conv_dim, use_cuda=use_cuda)
'''
# Model :
z_dim = 10
img_dim = size
img_depth=1
conv_dim = 16
use_cuda = True#False
net_depth = 3
beta = 100e0
betavae = betaVAE(beta=beta,net_depth=net_depth,z_dim=z_dim,img_dim=img_dim,img_depth=img_depth,conv_dim=conv_dim, use_cuda=use_cuda)
'''
print(betavae)
# Optim :
lr = 1e-5
optimizer = torch.optim.Adam( betavae.parameters(), lr=lr)
#optimizer = torch.optim.Adagrad( betavae.parameters(), lr=lr)
#lr = 1e-3
# Debug :
# fixed inputs for debugging
fixed_z = Variable(torch.randn(45, z_dim))
if use_cuda :
fixed_z = fixed_z.cuda()
fixed_x, _ = next(data_iter)
#path = 'dSprite--beta{}-layers{}-z{}-conv{}-lr{}'.format(beta,net_depth,z_dim,conv_dim,lr)
path = 'testAblation--dSprite--beta{}-layers{}-z{}-conv{}'.format(beta,net_depth,z_dim,conv_dim)
if not os.path.exists( './beta-data/{}/'.format(path) ) :
os.mkdir('./beta-data/{}/'.format(path))
if not os.path.exists( './beta-data/{}/gen_images/'.format(path) ) :
os.mkdir('./beta-data/{}/gen_images/'.format(path))
if not os.path.exists( './beta-data/{}/reconst_images/'.format(path) ) :
os.mkdir('./beta-data/{}/reconst_images/'.format(path))
fixed_x = fixed_x.view( (-1, img_depth, img_dim, img_dim) )
torchvision.utils.save_image(255*fixed_x.cpu(), './beta-data/{}/real_images.png'.format(path))
# effect of each latent var :
nbr_steps = 8
var_x = fixed_x[0, :, :, :]
var_x = torch.cat( nbr_steps*[var_x], dim=0 ).unsqueeze(1)
fixed_x = Variable(fixed_x.view(fixed_x.size(0), img_depth, img_dim, img_dim))
var_x = Variable(var_x)
if use_cuda :
fixed_x = fixed_x.cuda()
var_x = var_x.cuda()
out = torch.zeros((1,1))
# variations over the latent variable :
sigma_mean = torch.ones((z_dim))
mu_mean = torch.zeros((z_dim))
best_loss = None
best_model_wts = betavae.state_dict()
SAVE_PATH = './beta-data/{}'.format(path)
if frompath :
try :
betavae.load_state_dict( torch.load( os.path.join(SAVE_PATH,'weights')) )
print('NET LOADING : OK.')
except Exception as e :
print('EXCEPTION : NET LOADING : {}'.format(e) )
for epoch in range(50):
# Save generated variable images :
var_z = betavae.encoder(var_x)
mu_z, log_var_z = torch.chunk(var_z, 2, dim=1 )
mu_mean /= batch_size
sigma_mean /= batch_size
gen_images = None
for latent in range(z_dim) :
#var_z0 = torch.stack( [mu_mean]*nbr_steps, dim=0)
#var_z0 = torch.zeros(nbr_steps, z_dim)
var_z0 = mu_z.cpu().data
val = mu_mean[latent]-sigma_mean[latent]
step = 2.0*sigma_mean[latent]/nbr_steps
print(latent,mu_mean[latent],step)
for i in range(nbr_steps) :
var_z0[i] = mu_mean
var_z0[i][latent] = val
val += step
var_z0 = Variable(var_z0)
if use_cuda :
var_z0 = var_z0.cuda()
gen_images_latent = betavae.decoder(var_z0)
gen_images_latent = gen_images_latent.view(-1, img_depth, img_dim, img_dim).cpu().data
if gen_images is None :
gen_images = gen_images_latent
else :
gen_images = torch.cat( [gen_images,gen_images_latent], dim=0)
#torchvision.utils.save_image(gen_images.data.cpu(),'./beta-data/{}/gen_images/dim{}/{}.png'.format(path,latent,(epoch+1)) )
torchvision.utils.save_image(255.0*gen_images,'./beta-data/{}/gen_images/{}.png'.format(path,(epoch)) )
mu_mean = 0.0
sigma_mean = 0.0
epoch_loss = 0.0
for i, (images, _) in enumerate(data_loader):
# Save the reconstructed images
if i % 100 == 0 :
reconst_images, _, _ = betavae(fixed_x)
reconst_images = reconst_images.view(-1, img_depth, img_dim, img_dim).cpu().data
orimg = fixed_x.cpu().data.view(-1, img_depth, img_dim, img_dim)
ri = torch.cat( [orimg, reconst_images], dim=2)
torchvision.utils.save_image(255*ri,'./beta-data/{}/reconst_images/{}.png'.format(path,(epoch)) )
images = Variable( (images.view(-1,1,img_dim, img_dim) ) )
if use_cuda :
images = images.cuda()
out, mu, log_var = betavae(images)
mu_mean += torch.mean(mu.data,dim=0)
sigma_mean += torch.mean( torch.sqrt( torch.exp(log_var.data) ), dim=0 )
# Compute :
#reconstruction loss :
reconst_loss = F.binary_cross_entropy( out, images, size_average=False)
#reconst_loss = nn.MultiLabelSoftMarginLoss()(input=out_logits, target=images)
#reconst_loss = F.binary_cross_entropy_with_logits( input=out, target=images, size_average=False)
#reconst_loss = F.binary_cross_entropy( Bernoulli(out).sample(), images, size_average=False)
#reconst_loss = torch.mean( (out.view(-1) - images.view(-1))**2 )
# expected log likelyhood :
expected_log_lik = torch.mean( Bernoulli( out.view((-1)) ).log_prob( images.view((-1)) ) )
#expected_log_lik = torch.mean( Bernoulli( out ).log_prob( images ) )
# kl divergence :
kl_divergence = 0.5 * torch.mean( torch.sum( (mu**2 + torch.exp(log_var) - log_var -1), dim=1) )
#kl_divergence = 0.5 * torch.sum( (mu**2 + torch.exp(log_var) - log_var -1) )
# ELBO :
elbo = expected_log_lik - betavae.beta * kl_divergence
# TOTAL LOSS :
total_loss = reconst_loss + betavae.beta*kl_divergence
#total_loss = reconst_loss
#total_loss = -elbo
# Backprop + Optimize :
optimizer.zero_grad()
total_loss.backward()
optimizer.step()
del images
epoch_loss += total_loss.cpu().data[0]
if i % 100 == 0:
print ("Epoch[%d/%d], Step [%d/%d], Total Loss: %.4f, "
"Reconst Loss: %.4f, KL Div: %.7f, E[ |~| p(x|theta)]: %.7f "
%(epoch+1, 50, i+1, iter_per_epoch, total_loss.data[0],
reconst_loss.data[0], kl_divergence.data[0],expected_log_lik.exp().data[0]) )
if best_loss is None :
#first validation : let us set the initialization but not save it :
best_loss = epoch_loss
best_model_wts = betavae.state_dict()
# save best model weights :
torch.save( best_model_wts, os.path.join(SAVE_PATH,'weights') )
print('Model saved at : {}'.format(os.path.join(SAVE_PATH,'weights')) )
elif epoch_loss < best_loss:
best_loss = epoch_loss
best_model_wts = betavae.state_dict()
# save best model weights :
torch.save( best_model_wts, os.path.join(SAVE_PATH,'weights') )
print('Model saved at : {}'.format(os.path.join(SAVE_PATH,'weights')) )
def test_XYS(offset=0):
import os
import matplotlib.pyplot as plt
import torchvision
from torchvision import datasets, transforms
from models import Bernoulli
size = 256
batch_size = 16#32
dataset = load_dataset_XYS(img_dim=size)
# Data loader
data_loader = torch.utils.data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=True)
data_iter = iter(data_loader)
iter_per_epoch = len(data_loader)
# Model :
frompath = True
'''
z_dim = 10
img_dim = size
img_depth=1
conv_dim = 64
use_cuda = True#False
net_depth = 3
beta = 1e0
betavae = betaVAEdSprite(beta=beta,net_depth=net_depth,z_dim=z_dim,img_dim=img_dim,img_depth=img_depth,conv_dim=conv_dim, use_cuda=use_cuda)
'''
# Model :
z_dim = 4
img_dim = size
img_depth=3
conv_dim = 32
use_cuda = True#False
net_depth = 5
beta = 5000e0
betavae = betaVAEXYS(beta=beta,net_depth=net_depth,z_dim=z_dim,img_dim=img_dim,img_depth=img_depth,conv_dim=conv_dim, use_cuda=use_cuda)
print(betavae)
# Optim :
lr = 1e-5
optimizer = torch.optim.Adam( betavae.parameters(), lr=lr)
#optimizer = torch.optim.Adagrad( betavae.parameters(), lr=lr)
#lr = 1e-3
# Debug :
# fixed inputs for debugging
fixed_z = Variable(torch.randn(45, z_dim))
if use_cuda :
fixed_z = fixed_z.cuda()
sample = next(data_iter)
fixed_x, _ = sample['image'], sample['landmarks']
path = 'test--XYS--img{}-lr{}-beta{}-layers{}-z{}-conv{}'.format(img_dim,lr,beta,net_depth,z_dim,conv_dim)
if not os.path.exists( './beta-data/{}/'.format(path) ) :
os.mkdir('./beta-data/{}/'.format(path))
if not os.path.exists( './beta-data/{}/gen_images/'.format(path) ) :
os.mkdir('./beta-data/{}/gen_images/'.format(path))
if not os.path.exists( './beta-data/{}/reconst_images/'.format(path) ) :
os.mkdir('./beta-data/{}/reconst_images/'.format(path))
fixed_x = fixed_x.view( (-1, img_depth, img_dim, img_dim) )
torchvision.utils.save_image(fixed_x.cpu(), './beta-data/{}/real_images.png'.format(path))
fixed_x = Variable(fixed_x.view(fixed_x.size(0), img_depth, img_dim, img_dim)).float()
if use_cuda :
fixed_x = fixed_x.cuda()
out = torch.zeros((1,1))
# variations over the latent variable :
sigma_mean = torch.ones((z_dim))
mu_mean = torch.zeros((z_dim))
best_loss = None
best_model_wts = betavae.state_dict()
SAVE_PATH = './beta-data/{}'.format(path)
if frompath :
try :
betavae.load_state_dict( torch.load( os.path.join(SAVE_PATH,'weights')) )
print('NET LOADING : OK.')
except Exception as e :
print('EXCEPTION : NET LOADING : {}'.format(e) )
for epoch in range(50):
# Save generated variable images :
nbr_steps = 8
mu_mean /= batch_size
sigma_mean /= batch_size
gen_images = torch.ones( (8, img_depth, img_dim, img_dim) )
for latent in range(z_dim) :
#var_z0 = torch.stack( [mu_mean]*nbr_steps, dim=0)
var_z0 = torch.zeros(nbr_steps, z_dim)
val = mu_mean[latent]-sigma_mean[latent]
step = 2.0*sigma_mean[latent]/nbr_steps
print(latent,mu_mean[latent],step)
for i in range(nbr_steps) :
var_z0[i] = mu_mean
var_z0[i][latent] = val
val += step
var_z0 = Variable(var_z0)
if use_cuda :
var_z0 = var_z0.cuda()
gen_images_latent = betavae.decoder(var_z0)
gen_images_latent = gen_images_latent.view(-1, img_depth, img_dim, img_dim).cpu().data
gen_images = torch.cat( [gen_images,gen_images_latent], dim=0)
#torchvision.utils.save_image(gen_images.data.cpu(),'./beta-data/{}/gen_images/dim{}/{}.png'.format(path,latent,(epoch+1)) )
torchvision.utils.save_image(gen_images,'./beta-data/{}/gen_images/{}.png'.format(path,(epoch+offset+1)) )
mu_mean = 0.0
sigma_mean = 0.0
epoch_loss = 0.0
for i, sample in enumerate(data_loader):
images = sample['image']
# Save the reconstructed images
if i % 100 == 0 :
reconst_images, _, _ = betavae(fixed_x)
reconst_images = reconst_images.view(-1, img_depth, img_dim, img_dim).cpu().data
orimg = fixed_x.cpu().data.view(-1, img_depth, img_dim, img_dim)
ri = torch.cat( [orimg, reconst_images], dim=2)
torchvision.utils.save_image(ri,'./beta-data/{}/reconst_images/{}.png'.format(path,(epoch+offset+1) ) )
images = Variable( (images.view(-1, img_depth,img_dim, img_dim) ) ).float()
if use_cuda :
images = images.cuda()
out, mu, log_var = betavae(images)
mu_mean += torch.mean(mu.data,dim=0)
sigma_mean += torch.mean( torch.sqrt( torch.exp(log_var.data) ), dim=0 )
# Compute :
#reconstruction loss :
reconst_loss = F.binary_cross_entropy( out, images, size_average=False)
#reconst_loss = nn.MultiLabelSoftMarginLoss()(input=out_logits, target=images)
#reconst_loss = F.binary_cross_entropy_with_logits( input=out, target=images, size_average=False)
#reconst_loss = F.binary_cross_entropy( Bernoulli(out).sample(), images, size_average=False)
#reconst_loss = torch.mean( (out.view(-1) - images.view(-1))**2 )
# expected log likelyhood :
expected_log_lik = torch.mean( Bernoulli( out.view((-1)) ).log_prob( images.view((-1)) ) )
#expected_log_lik = torch.mean( Bernoulli( out ).log_prob( images ) )
# kl divergence :
kl_divergence = 0.5 * torch.mean( torch.sum( (mu**2 + torch.exp(log_var) - log_var -1), dim=1) )
#kl_divergence = 0.5 * torch.sum( (mu**2 + torch.exp(log_var) - log_var -1) )
# ELBO :
elbo = expected_log_lik - betavae.beta * kl_divergence
# TOTAL LOSS :
total_loss = reconst_loss + betavae.beta*kl_divergence
#total_loss = reconst_loss
#total_loss = -elbo
# Backprop + Optimize :
optimizer.zero_grad()
total_loss.backward()
optimizer.step()
del images
epoch_loss += total_loss.cpu().data[0]
if i % 100 == 0:
print ("Epoch[%d/%d], Step [%d/%d], Total Loss: %.4f, "
"Reconst Loss: %.4f, KL Div: %.7f, E[ |~| p(x|theta)]: %.7f "
%(epoch+1, 50, i+1, iter_per_epoch, total_loss.data[0],
reconst_loss.data[0], kl_divergence.data[0],expected_log_lik.exp().data[0]) )
if best_loss is None :
#first validation : let us set the initialization but not save it :
best_loss = epoch_loss
best_model_wts = betavae.state_dict()
# save best model weights :
torch.save( best_model_wts, os.path.join(SAVE_PATH,'weights') )
print('Model saved at : {}'.format(os.path.join(SAVE_PATH,'weights')) )
elif epoch_loss < best_loss:
best_loss = epoch_loss
best_model_wts = betavae.state_dict()
# save best model weights :
torch.save( best_model_wts, os.path.join(SAVE_PATH,'weights') )
print('Model saved at : {}'.format(os.path.join(SAVE_PATH,'weights')) )
def queryXYS():
import os
import matplotlib.pyplot as plt
import torchvision
from torchvision import datasets, transforms
from models import Bernoulli
size = 256
batch_size = 16#32
dataset = load_dataset_XYS(img_dim=size)
# Data loader
data_loader = torch.utils.data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=True)
data_iter = iter(data_loader)
iter_per_epoch = len(data_loader)
# Model :
frompath = True
'''
z_dim = 10
img_dim = size
img_depth=1
conv_dim = 64
use_cuda = True#False
net_depth = 3
beta = 1e0
betavae = betaVAEdSprite(beta=beta,net_depth=net_depth,z_dim=z_dim,img_dim=img_dim,img_depth=img_depth,conv_dim=conv_dim, use_cuda=use_cuda)
'''
# Model :
z_dim = 4#10
img_dim = size
img_depth=3
conv_dim = 32
use_cuda = True#False
net_depth = 5
beta = 5000e0
betavae = betaVAEXYS(beta=beta,net_depth=net_depth,z_dim=z_dim,img_dim=img_dim,img_depth=img_depth,conv_dim=conv_dim, use_cuda=use_cuda)
print(betavae)
lr=1e-5
# Debug :
# fixed inputs for debugging
fixed_z = Variable(torch.randn(45, z_dim))
if use_cuda :
fixed_z = fixed_z.cuda()
sample = next(data_iter)
fixed_x, _ = sample['image'], sample['landmarks']
path = 'test--XYS--img{}-lr{}-beta{}-layers{}-z{}-conv{}'.format(img_dim,lr,beta,net_depth,z_dim,conv_dim)
if not os.path.exists( './beta-data/{}/'.format(path) ) :
os.mkdir('./beta-data/{}/'.format(path))
if not os.path.exists( './beta-data/{}/gen_images/'.format(path) ) :
os.mkdir('./beta-data/{}/gen_images/'.format(path))
if not os.path.exists( './beta-data/{}/reconst_images/'.format(path) ) :
os.mkdir('./beta-data/{}/reconst_images/'.format(path))
fixed_x = fixed_x.view( (-1, img_depth, img_dim, img_dim) )
torchvision.utils.save_image(fixed_x.cpu(), './beta-data/{}/real_images_query.png'.format(path))
fixed_x = Variable(fixed_x.view(fixed_x.size(0), img_depth, img_dim, img_dim)).float()
if use_cuda :
fixed_x = fixed_x.cuda()
out = torch.zeros((1,1))
# variations over the latent variable :
sigma_mean = 3.0*torch.ones((z_dim))
mu_mean = torch.zeros((z_dim))
SAVE_PATH = './beta-data/{}'.format(path)
if frompath :
try :
betavae.load_state_dict( torch.load( os.path.join(SAVE_PATH,'weights')) )
print('NET LOADING : OK.')
except Exception as e :
print('EXCEPTION : NET LOADING : {}'.format(e) )
# Save generated variable images :
nbr_steps = 8
gen_images = torch.ones( (8, img_depth, img_dim, img_dim) )
for latent in range(z_dim) :
#var_z0 = torch.stack( [mu_mean]*nbr_steps, dim=0)
var_z0 = torch.zeros(nbr_steps, z_dim)
val = mu_mean[latent]-sigma_mean[latent]
step = 2.0*sigma_mean[latent]/nbr_steps
print(latent,mu_mean[latent]-sigma_mean[latent],mu_mean[latent],mu_mean[latent]+sigma_mean[latent])
for i in range(nbr_steps) :
var_z0[i] = mu_mean
var_z0[i][latent] = val
val += step
var_z0 = Variable(var_z0)
if use_cuda :
var_z0 = var_z0.cuda()
gen_images_latent = betavae.decoder(var_z0)
gen_images_latent = gen_images_latent.view(-1, img_depth, img_dim, img_dim).cpu().data
gen_images = torch.cat( [gen_images,gen_images_latent], dim=0)
#torchvision.utils.save_image(gen_images.data.cpu(),'./beta-data/{}/gen_images/dim{}/{}.png'.format(path,latent,(epoch+1)) )
torchvision.utils.save_image(gen_images,'./beta-data/{}/gen_images/query.png'.format(path) )
reconst_images, _, _ = betavae(fixed_x)
reconst_images = reconst_images.view(-1, img_depth, img_dim, img_dim).cpu().data
orimg = fixed_x.cpu().data.view(-1, img_depth, img_dim, img_dim)
ri = torch.cat( [orimg, reconst_images], dim=2)
torchvision.utils.save_image(ri,'./beta-data/{}/reconst_images/query.png'.format(path ) )
if __name__ == '__main__' :
import argparse
parser = argparse.ArgumentParser(description='beta-VAE')
parser.add_argument('--train',action='store_true',default=False)
parser.add_argument('--offset', type=int, default=0)
args = parser.parse_args()
if args.train :
#test_mnist()
#test_dSprite()
test_XYS(offset=args.offset)
else :
queryXYS()