-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
983 lines (890 loc) · 44.3 KB
/
main.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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
from __future__ import print_function
import argparse
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from torch.autograd import Variable
import os
import models.dcgan as dcgan
import models.mlp as mlp
from models import gan
from fileUtil import FileUtil
from kde import cal_logprob
from kde import fit_kde
from kde import convert_to_ndarrays
import numpy as np
import math
import time
import logging
import csv
import traceback
def train(opt, log_file_path):
if opt.experiment is None:
opt.experiment = 'samples'
os.system('mkdir {0}'.format(opt.experiment))
elif not os.path.exists(opt.experiment):
os.system('mkdir {0}'.format(opt.experiment))
logger = logging.getLogger()
for hdlr in logger.handlers[:]: # remove all old handlers
logger.removeHandler(hdlr)
logger.setLevel(logging.INFO)
# create a file handler
handler = logging.FileHandler(log_file_path)
handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
logger.info(opt)
#opt.manualSeed = random.randint(1, 10000) # fix seed
logger.info("Random Seed: %s " % opt.manualSeed)
random.seed(opt.manualSeed)
torch.manual_seed(opt.manualSeed)
if opt.cuda:
torch.cuda.manual_seed_all(opt.manualSeed)
cudnn.benchmark = True
nc = int(opt.nc)
if torch.cuda.is_available() and not opt.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
def eval_with_KDE(generator, original_test_set, random_test_noise):
num_insts = random_test_noise.size()[0]
num_batches = int(math.ceil(num_insts / float(opt.batchSize)))
instances = []
for i in range(num_batches):
fake_test_set = generator(
Variable(random_test_noise[i * opt.batchSize: (i + 1) * opt.batchSize], volatile=True))
instances.extend([vec.flatten() for vec in fake_test_set.data.cpu().numpy()])
flattened_data = np.stack(instances)
# print('input data for KDE is of shape {0} '.format(flattened_data.shape))
kde = fit_kde(flattened_data, bandwidth=opt.bandwidth)
mean_logp = cal_logprob(kde, original_test_set)
return mean_logp
def xavier_init(param):
size = param.data.size()
in_dim = size[0]
xavier_stddev = 1. / np.sqrt(in_dim / 2.)
param.data = torch.randn(*size) * xavier_stddev
sample_validation_without_replacement = True
val_set = []
if opt.dataset == 'lsun':
opt.normalizeImages = True
#3x256x341
if opt.normalizeImages:
transform_op = transforms.Compose([
transforms.Scale(opt.imageSize),
transforms.CenterCrop(opt.imageSize),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
else:
transform_op = transforms.Compose([
transforms.Scale(opt.imageSize),
transforms.CenterCrop(opt.imageSize),
transforms.ToTensor(),
])
dataset = dset.LSUN(db_path=opt.dataroot, classes=['{0}_train'.format(opt.subset)],
transform=transform_op)
if opt.task == 'hyper':
dataset = [dataset[i] for i in range(10000, 40000)]
test_dataset = dset.LSUN(db_path=opt.dataroot, classes=['{0}_val'.format(opt.subset)],
transform=transform_op)
val_set = convert_to_ndarrays([dataset[i] for i in range(0, 1000)])
if sample_validation_without_replacement:
dataset = [dataset[i] for i in range(1001, len(dataset))]
nc = 3
opt.bandwidth = 0.335981828628
size_test_noise = 3000
size_val_noise = 3000
elif opt.dataset == 'cifar10':
opt.normalizeImages = True
dataset = dset.CIFAR10(root=opt.dataroot, download=True,
transform=transforms.Compose([
transforms.Scale(opt.imageSize),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
test_dataset = dset.CIFAR10(root=opt.dataroot, download=True, train=False,
transform=transforms.Compose([
transforms.Scale(opt.imageSize),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
print('number of images in test set %s ' % len(test_dataset))
val_set = convert_to_ndarrays([test_dataset[i] for i in range(0, 1000)])
test_dataset = [test_dataset[i] for i in range(1001, len(test_dataset))]
if sample_validation_without_replacement:
dataset = [dataset[i] for i in range(1001, len(dataset))]
nc = 3
size_test_noise = 6000
size_val_noise = 6000
if opt.imageSize == 32:
opt.bandwidth = 0.263665089873
else:
opt.bandwidth = 0.335981828628
elif opt.dataset == 'mnist':
if opt.normalizeImages:
img_transform = transforms.Compose([
transforms.Scale(opt.imageSize),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
else:
img_transform = transforms.Compose([
transforms.Scale(opt.imageSize),
transforms.ToTensor(),
])
dataset = dset.MNIST(root=opt.dataroot, download=True,
transform=img_transform)
test_dataset = dset.MNIST(root=opt.dataroot, download=True, train=False,
transform=img_transform)
nc = 1
if opt.imageSize == 32:
opt.bandwidth = 0.1
else:
opt.bandwidth = 0.12742749857
val_set = convert_to_ndarrays([dataset[i] for i in range(50000, 51000)])
if sample_validation_without_replacement:
dataset = [dataset[i] for i in range(0, len(dataset)) if i < 50000 or i > 51000]
size_test_noise = 16000
size_val_noise = 5000
# if opt.A in ['mlp']:
# logger.info('Apply experimental setting of F-GAN on MNIST.')
# opt.nz = 100
# opt.ndf = 240
# opt.ngf = 1200
# opt.init_z = 'uniform_one'
# opt.adam = True
# opt.lrD = 0.0002
# opt.lrG = 0.0002
# opt.beta1 = 0.5
# opt.batchSize = 4096
# opt.init_w = 'uniform'
# opt.last_layer = 'sigmoid'
# opt.clamp_lower = 0
# opt.clamp_upper = 0
assert dataset
assert test_dataset
assert len(val_set) > 0
dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,
shuffle=True, num_workers=int(opt.workers))
ngpu = 1
if opt.gpu_id < 0:
ngpu = int(opt.ngpu)
nz = int(opt.nz)
ngf = int(opt.ngf)
ndf = int(opt.ndf)
n_extra_layers = int(opt.n_extra_layers)
# Load KDE model, if available
def init_z(tensor):
if opt.init_z == 'uniform_one':
tensor.uniform_(-1, 1)
elif opt.init_z == 'uniform_zero_one':
tensor.uniform_(0, 1)
else:
tensor.normal_(0, 1)
return tensor
test_noise = None
val_noise = None
if opt.bandwidth != 0:
test_noise = init_z(torch.FloatTensor(size_test_noise, nz, 1, 1))
val_noise = init_z(torch.FloatTensor(size_val_noise, nz, 1, 1))
test_set = convert_to_ndarrays(test_dataset)
if opt.cuda:
test_noise = test_noise.cuda()
val_noise = val_noise.cuda()
# custom weights initialization called on netG and netD
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
# initialize generator
if opt.A == 'wmlp':
netG = mlp.MLP_G(opt.imageSize, nz, nc, ngf, ngpu, hidden_activation=opt.H, mu=opt.mu, last_layer=opt.last_layer)
if opt.init_w == 'xavier':
[xavier_init(param) for param in netG.parameters()]
else:
[param.data.uniform_(-0.05, 0.05) for param in netG.parameters()]
elif opt.A == 'mlp':
netG = gan.GAN_G(opt.imageSize, nz, nc, ngf, ngpu, hidden_activation=opt.H, mu=opt.mu, last_layer=opt.last_layer)
if opt.init_w == 'xavier':
[xavier_init(param) for param in netG.parameters()]
else:
[param.data.uniform_(-0.05, 0.05) for param in netG.parameters()]
else:
if opt.noBN:
netG = dcgan.DCGAN_G_nobn(opt.imageSize, nz, nc, ngf, ngpu, n_extra_layers)
else:
last_layer = 'sigmoid'
if opt.normalizeImages:
last_layer = 'tanh'
netG = dcgan.DCGAN_G(opt.imageSize, nz, nc, ngf, ngpu, n_extra_layers, hidden_activation=opt.H, mu=opt.mu, last_layer=last_layer)
netG.apply(weights_init)
if opt.netG != '': # load checkpoint if needed
netG.load_state_dict(torch.load(opt.netG))
logger.info(netG)
# Initialize critic
if opt.A == 'wmlp':
netD = mlp.MLP_D(opt.imageSize, nz, nc, ndf, ngpu)
if opt.init_w == 'xavier':
[xavier_init(param) for param in netG.parameters()]
else:
[param.data.uniform_(-0.005, 0.005) for param in netG.parameters()]
elif opt.A == 'mlp':
netD = gan.GAN_D(opt.imageSize, nz, nc, ngf, ngpu,hidden_activation = opt.c_activation, last_layer=opt.critic_last_layer, alpha=opt.alpha)
if opt.init_w == 'xavier':
[xavier_init(param) for param in netG.parameters()]
else:
[param.data.uniform_(-0.005, 0.005) for param in netG.parameters()]
else:
netD = dcgan.DCGAN_D(opt.imageSize, nz, nc, ndf, ngpu, n_extra_layers, last_layer=opt.critic_last_layer)
netD.apply(weights_init)
if opt.netD != '':
netD.load_state_dict(torch.load(opt.netD))
logger.info(netD)
input = torch.FloatTensor(opt.batchSize, nc, opt.imageSize, opt.imageSize)
noise = torch.FloatTensor(opt.batchSize, nz, 1, 1)
num_images = 24
fixed_noise = init_z(torch.FloatTensor(num_images, nz, 1, 1))
one = torch.FloatTensor([1])
mone = one * -1
# for GAN
label = torch.FloatTensor(opt.batchSize)
real_label = 1
fake_label = 0
criterion = nn.BCELoss()
if opt.cuda:
netD.cuda()
netG.cuda()
input = input.cuda()
criterion.cuda()
label = label.cuda()
one, mone = one.cuda(), mone.cuda()
noise, fixed_noise = noise.cuda(), fixed_noise.cuda()
# setup optimizer
if opt.adam:
logger.info("Use ADAM")
optimizerD = optim.Adam(netD.parameters(), lr=opt.lrD, betas=(opt.beta1, 0.999), weight_decay=opt.weightDecay)
optimizerG = optim.Adam(netG.parameters(), lr=opt.lrG, betas=(opt.beta1, 0.999), weight_decay=opt.weightDecay)
else:
logger.info("Use RMSprop")
optimizerD = optim.RMSprop(netD.parameters(), lr=opt.lrD)
optimizerG = optim.RMSprop(netG.parameters(), lr=opt.lrG)
def sample_image_compute_density(start_epoch, end_epoch):
with open(os.path.join(opt.kde_result_dir, 'kde_results.csv'), 'w') as kde_file:
best_logprob = -10000000
for epoch in range(start_epoch, end_epoch):
netG_model = '{0}/netG_epoch_{1}.pth'.format(opt.experiment, epoch)
if os.path.exists(netG_model):
netG.load_state_dict(torch.load(netG_model))
if opt.cuda:
netG.cuda()
fake = netG(Variable(fixed_noise, volatile=True))
if opt.normalizeImages:
fake.data = fake.data.mul(0.5).add(0.5)
vutils.save_image(fake.data,
'{0}/fake_samples_epoch_{1}.png'.format(opt.kde_result_dir, epoch))
logprob_mean = eval_with_KDE(netG, test_set, test_noise)
kde_file.write("{0}\t{1}".format(epoch, logprob_mean))
best_logprob = max(best_logprob, logprob_mean)
return best_logprob
best_logprob = 0
label = Variable(label)
noisev = Variable(noise)
def eval_gan(netDiscriminator, netGenerator):
gen_iterations = 0
train_best_netG_model = ''
train_best_logprob = 0
num_epochs_img = max(3, opt.niter / 5)
if opt.dataset == 'lsun':
num_epochs_img = 1
start_time = time.time()
for epoch in range(opt.niter):
loss_D = 0
loss_G = 0
for i, data in enumerate(dataloader, 0):
############################
# (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
###########################
# train with real
for p in netDiscriminator.parameters(): # reset requires_grad
p.requires_grad = True # they are set to False below in netG update
for p in netGenerator.parameters(): # disable grad of generator
p.requires_grad = False
if opt.clamp_upper > 0:
# clamp parameters to a cube
for p in netDiscriminator.parameters():
p.data.clamp_(opt.clamp_lower, opt.clamp_upper)
netDiscriminator.zero_grad()
real_cpu, _ = data
batch_size = real_cpu.size(0)
if opt.cuda:
real_cpu = real_cpu.cuda()
input.resize_as_(real_cpu).copy_(real_cpu)
inputv = Variable(input)
inputv.data.resize_(real_cpu.size()).copy_(real_cpu)
label.data.resize_(batch_size).fill_(real_label)
output = netDiscriminator(inputv)
#errD_real = criterion(output, label)
errD_real = torch.mean(torch.neg(torch.log(output)))
errD_real.backward()
# train with fake
noisev.data.resize_(batch_size, nz, 1, 1)
init_z(noisev.data) # totally freeze netG
fake = netGenerator(noisev)
label.data.fill_(fake_label)
output = netDiscriminator(fake.detach())
errD_fake = criterion(output, label)
#errD_fake = torch.mean(torch.neg(torch.log(1 - torch.exp(torch.log(output)))))
errD_fake.backward()
errD = errD_real + errD_fake
loss_D += errD.data.sum()
optimizerD.step()
############################
# (2) Update G network: maximize log(D(G(z)))
###########################
for p in netDiscriminator.parameters():
p.requires_grad = False # to avoid computation
for p in netGenerator.parameters(): # reset requires_grad
p.requires_grad = True
netGenerator.zero_grad()
label.data.fill_(real_label) # fake labels are real for generator cost
init_z(noisev.data) # totally freeze netG
fake = netGenerator(noisev)
fake_output = netDiscriminator(fake)
errG = criterion(fake_output, label)
#errG = torch.mean(torch.neg(torch.log(fake_output)))
errG.backward()
loss_G += errG.data.sum()
optimizerG.step()
gen_iterations += 1
if epoch % num_epochs_img == 0:
if opt.normalizeImages:
real_cpu = real_cpu.mul(0.5).add(0.5)
vutils.save_image(real_cpu[0:num_images], '{0}/real_samples.png'.format(opt.experiment))
fake = netGenerator(Variable(fixed_noise, volatile=True))
if opt.normalizeImages:
fake.data = fake.data.mul(0.5).add(0.5)
vutils.save_image(fake.data,
'{0}/fake_samples_{1}_epoch_{2}.png'.format(opt.experiment, gen_iterations, epoch))
# do checkpointing
end_time = time.time()
logger.info('[%d/%d] : Loss_D: %f Loss_G: %f, running time: %f'
% (epoch, opt.niter,
loss_D, loss_G, (end_time - start_time)))
torch.save(netGenerator.state_dict(), '{0}/netG_epoch_{1}.pth'.format(opt.experiment, epoch))
torch.save(netDiscriminator.state_dict(), '{0}/netD_epoch_{1}.pth'.format(opt.experiment, epoch))
val_logprob_normalized = eval_with_KDE(netGenerator, val_set, val_noise)
if train_best_netG_model == '' or val_logprob_normalized > train_best_logprob:
train_best_logprob = val_logprob_normalized
train_best_netG_model = '{0}/netG_epoch_{1}.pth'.format(opt.experiment, epoch)
logger.info(
'The current best model is epoch {0} with mean log probability {1}.'.format(
epoch, val_logprob_normalized))
return train_best_netG_model, train_best_logprob
def eval(netDiscriminator, netGenerator):
gen_iterations = 0
train_best_netG_model = ''
train_best_logprob = 0
num_epochs_img = max(3, opt.niter/5)
if opt.dataset == 'lsun':
num_epochs_img = 1
start_time = time.time()
for epoch in range(opt.niter):
data_iter = iter(dataloader)
i = 0
loss_D = 0
loss_G = 0
while i < len(dataloader):
############################
# (1) Update D network
###########################
for p in netDiscriminator.parameters(): # reset requires_grad
p.requires_grad = True # they are set to False below in netG update
for p in netGenerator.parameters(): # disable grad of generator
p.requires_grad = False
Diters = 1
if opt.D == 'wgan':
# train the discriminator Diters times
if opt.wganheuristics and (gen_iterations < 25 or gen_iterations % 500 == 0):
Diters = 100
else:
Diters = opt.Diters
j = 0
while j < Diters and i < len(dataloader):
j += 1
if opt.clamp_upper > 0:
# clamp parameters to a cube
for p in netDiscriminator.parameters():
p.data.clamp_(opt.clamp_lower, opt.clamp_upper)
data = data_iter.next()
i += 1
# train with real
real_cpu, _ = data
batch_size = real_cpu.size(0)
netDiscriminator.zero_grad()
if opt.cuda:
real_cpu = real_cpu.cuda()
input.resize_as_(real_cpu).copy_(real_cpu)
inputv = Variable(input)
errD_real = netDiscriminator(inputv)
if opt.D == 'fgan':
errD_real = torch.neg(torch.log(1 + torch.exp(torch.neg(errD_real))))
# train with fake
init_z(noise.resize_(batch_size, nz, 1, 1))
noisev = Variable(noise) # totally freeze netG
fake = Variable(netGenerator(noisev).data)
errD_fake = netDiscriminator(fake)
if opt.D == 'fgan':
errD_fake = torch.log(1/(1 + torch.exp(torch.neg(errD_fake))))
errD_fake = torch.neg(torch.log(1 - torch.exp(errD_fake)))
elif opt.D == 'kl':
errD_fake = torch.exp(errD_fake - 1)
loss_discriminator = torch.mean(errD_fake - errD_real)
loss_discriminator.backward(one)
optimizerD.step()
#loss_D += errD_fake.data + errD_real.data
loss_D += loss_discriminator.data
############################
# (2) Update G network
###########################
for p in netDiscriminator.parameters():
p.requires_grad = False # to avoid computation
for p in netGenerator.parameters(): # reset requires_grad
p.requires_grad = True
netGenerator.zero_grad()
# in case our last batch was the tail batch of the dataloader,
# make sure we feed a full batch of noise
init_z(noise.resize_(batch_size, nz, 1, 1))
noisev = Variable(noise, volatile=False)
fake = netGenerator(noisev)
errG = netDiscriminator(fake)
if opt.D == 'fgan':
errG = torch.neg(torch.log(1 + torch.exp(torch.neg(errG)))) # sigmoid
errG = torch.neg(torch.mean(errG))
errG.backward(one)
optimizerG.step()
loss_G += errG.data
gen_iterations += 1
# print('[%d/%d][%d/%d][%d] Loss_D: %f Loss_G: %f Loss_D_real: %f Loss_D_fake %f'
# % (epoch, opt.niter, i, len(dataloader), gen_iterations,
# errD.data[0], errG.data[0], errD_real.data[0], errD_fake.data[0]))
if epoch % num_epochs_img == 0:
if opt.normalizeImages:
real_cpu = real_cpu.mul(0.5).add(0.5)
vutils.save_image(real_cpu[0:num_images], '{0}/real_samples.png'.format(opt.experiment))
fake = netGenerator(Variable(fixed_noise, volatile=True))
if opt.normalizeImages:
fake.data = fake.data.mul(0.5).add(0.5)
vutils.save_image(fake.data,
'{0}/fake_samples_{1}_epoch_{2}.png'.format(opt.experiment, gen_iterations, epoch))
# do checkpointing
end_time = time.time()
logger.info('[%d/%d] : Loss_D: %f Loss_G: %f, running time: %f'
% (epoch, opt.niter,
loss_D[0], loss_G[0], (end_time - start_time)))
torch.save(netGenerator.state_dict(), '{0}/netG_epoch_{1}.pth'.format(opt.experiment, epoch))
torch.save(netDiscriminator.state_dict(), '{0}/netD_epoch_{1}.pth'.format(opt.experiment, epoch))
val_logprob_normalized = eval_with_KDE(netGenerator, val_set, val_noise)
if train_best_netG_model == '' or val_logprob_normalized > train_best_logprob:
train_best_logprob = val_logprob_normalized
train_best_netG_model = '{0}/netG_epoch_{1}.pth'.format(opt.experiment, epoch)
logger.info(
'The current best model is epoch {0} with mean log probability {1}.'.format(
epoch, val_logprob_normalized))
return train_best_netG_model, train_best_logprob
if opt.task == 'eval_kde':
return sample_image_compute_density(opt.start, opt.end)
else:
if opt.kdeEpoch == 0:
if opt.D == 'gan':
best_netG_model, best_logprob = eval_gan(netD, netG)
else:
best_netG_model, best_logprob = eval(netD, netG)
else:
best_netG_model = '{0}/netG_epoch_{1}.pth'.format(opt.experiment, opt.kdeEpoch)
print('Load model from epoch %d for KDE evaluation.' % opt.kdeEpoch)
logger.info('Load the best model from {0} with log probability {1} .'.format(best_netG_model, best_logprob))
netG.load_state_dict(torch.load(best_netG_model))
if opt.cuda:
netG.cuda()
fake = netG(Variable(fixed_noise, volatile=True))
if opt.normalizeImages:
fake.data = fake.data.mul(0.5).add(0.5)
vutils.save_image(fake.data,
'{0}/best_fake_samples.png'.format(opt.experiment))
logprob_mean = eval_with_KDE(netG, test_set, test_noise)
logger.info("On the test set, mean log probablity is %s " % logprob_mean)
return logprob_mean
def search_hyperparams(opt):
learn_rates = [0.0002, 0.0001, 0.00005, 0.00001]
hidden_units = [opt.imageSize, opt.imageSize * 2, opt.imageSize * 8, opt.imageSize * 32, 1200]
clamping_bounds = [0, 0.1, 0.01, 0.001]
batch_size = [64, 4096]
init_weights = ['uniform', 'xavier']
init_noise = ['uniform_one', 'uniform_zero_one', 'gaussian']
original_exp = opt.experiment
error_out = open(os.path.join(original_exp, '{0}_{1}_{2}_{3}_hyperparams.error'.format(opt.dataset, opt.D, opt.A, opt.H)), 'w')
with open(os.path.join(original_exp, '{0}_{1}_{2}_{3}_hyperparams.log'.format(opt.dataset, opt.D, opt.A, opt.H)), 'w') as out:
max_logprob = 0
best_config = ''
if opt.adam:
for lr in learn_rates:
opt.lrD = lr
opt.lrG = lr
opt.experiment = os.path.join(original_exp,
'{0}_{1}_{2}_{3}_{4}'.format(opt.dataset, opt.D, opt.A, opt.H, lr))
if not os.path.exists(opt.experiment):
os.makedirs(opt.experiment)
try:
logprob = train(opt=opt, log_file_path=os.path.join(opt.experiment,
'{0}_{1}_{2}_{3}_evaluation.log'.format(
opt.dataset, opt.D, opt.A, opt.H)))
config = '{0}\t{1}\n'.format(lr, logprob)
if max_logprob == 0 or logprob > max_logprob:
max_logprob = logprob
best_config = config
out.write(config)
out.flush()
except Exception as e:
print(e)
error_out.write('learning rate {0} with error {1}'.format(lr, e))
print(best_config)
elif opt.A == 'dcgan' and opt.D != 'wgan':
for lr in learn_rates:
opt.lrD = lr
opt.lrG = lr
for c in clamping_bounds:
opt.clamp_lower = -c
opt.clamp_upper = c
opt.experiment = os.path.join(original_exp, '{0}_{1}_{2}_{3}_{4}_{5}'.format(opt.dataset, opt.D, opt.A, opt.H, lr, c))
if not os.path.exists(opt.experiment):
os.makedirs(opt.experiment)
try:
logprob = train(opt=opt, log_file_path=os.path.join(opt.experiment, '{0}_{1}_{2}_{3}_evaluation.log'.format(opt.dataset, opt.D, opt.A, opt.H)))
config = '{0}\t{1}\t{2}\n'.format(lr, c, logprob)
if max_logprob == 0 or logprob > max_logprob:
max_logprob = logprob
best_config = config
out.write(config)
out.flush()
except Exception as e:
print(e)
error_out.write('config {0} {1} with error {2}'.format(lr, c, e))
print(best_config)
elif opt.D == 'wgan':
opt.clamp_lower = -0.01
opt.clamp_upper = 0.01
for lr in learn_rates:
opt.lrD = lr
opt.lrG = lr
opt.experiment = os.path.join(original_exp, '{0}_{1}_{2}_{3}_{4}'.format(opt.dataset, opt.D, opt.A, opt.H, lr))
if not os.path.exists(opt.experiment):
os.makedirs(opt.experiment)
try:
logprob = train(opt=opt, log_file_path=os.path.join(opt.experiment, '{0}_{1}_{2}_{3}_evaluation.log'.format(opt.dataset, opt.D, opt.A, opt.H)))
config = '{0}\t{1}\n'.format(lr, logprob)
if max_logprob == 0 or logprob > max_logprob:
max_logprob = logprob
best_config = config
out.write(config)
out.flush()
except Exception as e:
print(e)
error_out.write('learning rate {0} with error {1}'.format(lr, e))
print(best_config)
else:
for lr in learn_rates:
opt.lrD = lr
opt.lrG = lr
for h in hidden_units:
opt.ngf = h
opt.ndf = h
for c in clamping_bounds:
opt.clamp_lower = -c
opt.clamp_upper = c
opt.experiment = os.path.join(original_exp,
'{0}_{1}_{2}_{3}_{4}_{5}_{6}'.format(opt.dataset, opt.D, opt.A,
opt.H, lr, h, c))
if not os.path.exists(opt.experiment):
os.makedirs(opt.experiment)
try:
logprob = train(opt=opt, log_file_path=os.path.join(opt.experiment, '{0}_{1}_{2}_{3}_evaluation.log'.format(opt.dataset, opt.D, opt.A, opt.H)))
config = '{0}\t{1}\t{2}\t{3}\n'.format(lr, h, c, logprob)
if max_logprob == 0 or logprob > max_logprob:
max_logprob = logprob
best_config = config
out.write(config)
out.flush()
except Exception as e:
print(e)
error_out.write('config {0} {1} {2} with error {3}'.format(lr, h, c, e))
print(best_config)
out.write('best configuration : {0}'.format(best_config))
error_out.close()
def search_lsun_hyperparams(opt):
learn_rates = [0.0001, 0.00005, 0.00001]
hidden_units = [opt.imageSize, 1024]
clamping_bounds = [0, 0.1, 0.01, 0.001]
original_exp = opt.experiment
error_out = open(os.path.join(original_exp, '{0}_{1}_{2}_{3}_hyperparams.error'.format(opt.dataset, opt.D, opt.A, opt.H)), 'w')
with open(os.path.join(original_exp, '{0}_{1}_{2}_{3}_hyperparams.log'.format(opt.dataset, opt.D, opt.A, opt.H)), 'w') as out:
max_logprob = 0
best_config = ''
if opt.A == 'dcgan':
for lr in learn_rates:
opt.lrD = lr
opt.lrG = lr
for c in clamping_bounds:
opt.clamp_lower = -c
opt.clamp_upper = c
opt.experiment = os.path.join(original_exp, '{0}_{1}_{2}_{3}_{4}_{5}'.format(opt.dataset, opt.D, opt.A, opt.H, lr, c))
if not os.path.exists(opt.experiment):
os.makedirs(opt.experiment)
try:
logprob = train(opt=opt, log_file_path=os.path.join(opt.experiment, '{0}_{1}_{2}_{3}_evaluation.log'.format(opt.dataset, opt.D, opt.A, opt.H)))
config = '{0}\t{1}\t{2}\n'.format(lr, c, logprob)
if max_logprob == 0 or logprob > max_logprob:
max_logprob = logprob
best_config = config
out.write(config)
out.flush()
except Exception as e:
print(e)
error_out.write('config {0} {1} with error {2}'.format(lr, c, e))
print(best_config)
else:
for lr in learn_rates:
opt.lrD = lr
opt.lrG = lr
for h in hidden_units:
opt.ngf = h
opt.ndf = h
for c in clamping_bounds:
opt.clamp_lower = -c
opt.clamp_upper = c
opt.experiment = os.path.join(original_exp,
'{0}_{1}_{2}_{3}_{4}_{5}_{6}'.format(opt.dataset, opt.D, opt.A,
opt.H, lr, h, c))
if not os.path.exists(opt.experiment):
os.makedirs(opt.experiment)
try:
logprob = train(opt=opt, log_file_path=os.path.join(opt.experiment, '{0}_{1}_{2}_{3}_evaluation.log'.format(opt.dataset, opt.D, opt.A, opt.H)))
config = '{0}\t{1}\t{2}\t{3}\n'.format(lr, h, c, logprob)
if max_logprob == 0 or logprob > max_logprob:
max_logprob = logprob
best_config = config
out.write(config)
out.flush()
except Exception as e:
print(e)
error_out.write('config {0} {1} {2} with error {3}'.format(lr, h, c, e))
print(best_config)
out.write('best configuration : {0}'.format(best_config))
error_out.close()
def read(file_path):
mus = set()
if os.path.exists(file_path):
with open(file_path, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
if len(row[0]) > 0 and len(row) > 1:
mus.add(float(row[0]))
return mus
def search_mu(opt, start = 0, end = 11):
mus = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
original_exp = opt.experiment
file_name = '{0}_{1}_{2}_{3}_{4}_{5}_search_mu.log'.format(opt.dataset, opt.D, opt.A, opt.H, opt.manualSeed, opt.critic_last_layer)
csv_file = os.path.join(opt.experiment, file_name)
mu_set = read(csv_file)
with open(csv_file, 'a') as out:
max_logprob = 0
best_config = ''
for i in range(start, end):
mu = mus[i]
if mu not in mu_set:
opt.mu = mu
opt.H = 'murelu'
if mu == 1 :
opt.H = 'relu'
try:
opt.experiment = os.path.join(original_exp, '{0}_{1}_{2}_{3}_{4}_{5}_{6}'.format(opt.dataset, opt.D, opt.A, opt.H, mu, opt.manualSeed, opt.critic_last_layer))
if not os.path.exists(opt.experiment):
os.makedirs(opt.experiment)
logprob = train(opt=opt, log_file_path=os.path.join(opt.experiment, '{0}_{1}_{2}_{3}_{4}_{5}_{6}_matsushita_mu.log'.format(opt.dataset, opt.D, opt.A, opt.H, mu, opt.manualSeed, opt.critic_last_layer)))
config = '{0},{1}\n'.format(mu, logprob)
if max_logprob == 0 or logprob > max_logprob:
max_logprob = logprob
best_config = config
out.write(config)
out.flush()
print('best %s ' % best_config)
except Exception as e:
traceback.print_exc()
#out.write('best configuration : {0}'.format(best_config))
def experiments_randseeds(opt, start = 0, end = 5):
random_seeds = [1, 101, 512, 1001, 10001]
original_exp = opt.experiment
file_name = '{0}_{1}_{2}_{3}_{4}_experiments.csv'.format(opt.dataset, opt.D, opt.A, opt.H, opt.critic_last_layer)
csv_file = os.path.join(opt.experiment, file_name)
with open(csv_file, 'a') as out:
max_logprob = 0
best_config = ''
for i in range(start, end):
rand_seed = random_seeds[i]
opt.manualSeed = rand_seed
try:
opt.experiment = os.path.join(original_exp, '{0}_{1}_{2}_{3}_{4}_{5}'.format(opt.dataset, opt.D, opt.A, opt.H, opt.manualSeed, opt.critic_last_layer))
if not os.path.exists(opt.experiment):
os.makedirs(opt.experiment)
logprob = train(opt=opt, log_file_path=os.path.join(opt.experiment, '{0}_{1}_{2}_{3}_{4}_{5}_experiments.log'.format(opt.dataset, opt.D, opt.A, opt.H, opt.manualSeed, opt.critic_last_layer)))
config = '{0},{1}\n'.format(rand_seed, logprob)
if max_logprob == 0 or logprob > max_logprob:
max_logprob = logprob
best_config = config
out.write(config)
out.flush()
print('best %s ' % best_config)
except:
traceback.print_exc()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', required=True, help='cifar10 | lsun | imagenet | folder | lfw | mnist')
parser.add_argument('--subset', help='tower | bedroom')
parser.add_argument('--dataroot', required=True, help='path to dataset')
parser.add_argument('--workers', type=int, help='number of data loading workers', default=2)
parser.add_argument('--batchSize', type=int, default=64, help='input batch size')
parser.add_argument('--imageSize', type=int, default=64, help='the height / width of the input image to network')
parser.add_argument('--gpu_id', type=int, default=-1, help='GPU id')
parser.add_argument('--nc', type=int, default=3, help='input image channels')
parser.add_argument('--nz', type=int, default=100, help='size of the latent z vector')
parser.add_argument('--ngf', type=int, default=64)
parser.add_argument('--ndf', type=int, default=64)
parser.add_argument('--niter', type=int, default=25, help='number of epochs to train for')
parser.add_argument('--lrD', type=float, default=0.0001, help='learning rate for Critic, default=0.0001') # 0.00005
parser.add_argument('--lrG', type=float, default=0.0001, help='learning rate for Generator, default=0.0001') # 0.00005
parser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5')
parser.add_argument('--alpha', type=float, default=1, help='alpha for elu. default=1')
parser.add_argument('--cuda' , action='store_true', help='enables cuda')
parser.add_argument('--ngpu' , type=int, default=1, help='number of GPUs to use')
parser.add_argument('--netG', default='', help="path to netG (to continue training)")
parser.add_argument('--netD', default='', help="path to netD (to continue training)")
parser.add_argument('--clamp_lower', type=float, default=-0.01)
parser.add_argument('--clamp_upper', type=float, default=0.01)
parser.add_argument('--Diters', type=int, default=5, help='number of D iters per each G iter')
parser.add_argument('--noBN', action='store_true', help='use batchnorm or not (only for DCGAN)')
parser.add_argument('--D', default='kl', help='kl | gan | wgan')
parser.add_argument('--A', default='wmlp', help='architecture : dcgan | wmlp | mlp ')
parser.add_argument('--H', default='relu', help='activation function in hidden layers : relu | murelu | elu | ls | sp')
parser.add_argument('--c_activation', default='none', help='activation function in the hidden layers of critic: relu | elu')
parser.add_argument('--n_extra_layers', type=int, default=0, help='Number of extra layers on gen and disc')
parser.add_argument('--experiment', default=None, help='Where to store samples and models')
parser.add_argument('--adam', action='store_true', help='Whether to use adam (default is rmsprop)')
parser.add_argument('--bandwidth', type=float, default=0, help='optimal bandwidth for KDE, default=0')
parser.add_argument('--kdeEpoch', type=int, default=0, help='The epoch of the model that is loaded for KDE evaluation.')
parser.add_argument('--weightDecay', type=float, default=0)
parser.add_argument('--task', default='train', help='rand')
parser.add_argument('--init_z', default='gaussian', help='uniform_one | uniform_zero_one | gaussian')
parser.add_argument('--init_w', default='xavier', help='xavier | uniform')
parser.add_argument('--normalizeImages', type=bool, default=False)
parser.add_argument('--last_layer', default='sigmoid', help='none | sigmoid | tanh')
parser.add_argument('--critic_last_layer', default='none', help='none | sigmoid | tanh | matsu')
parser.add_argument('--mu', type=float, default=0, help='mu for matsushita')
parser.add_argument('--manualSeed', type=int, default=512, help='random seed')
parser.add_argument('--wganheuristics', type=bool, default=False)
parser.add_argument('--kde_result_dir', default='', help='folder which stores the images and KDE results created by the generator')
parser.add_argument('--start', type=int, default=0,
help='The starting epoch of the model that is loaded for KDE evaluation.')
parser.add_argument('--end', type=int, default=99,
help='The last epoch of the model that is loaded for KDE evaluation.')
opt = parser.parse_args()
# mnist
opt.cuda = True
if opt.dataset == 'mnist':
opt.imageSize = 32
opt.last_layer = 'sigmoid'
opt.niter = 100
elif opt.dataset == 'lsun':
opt.imageSize = 64
opt.last_layer = 'tanh'
if opt.A == 'mlp':
if opt.D == 'gan':
opt.lrD = 0.0002
opt.lrG = 0.0002
opt.c_activation = 'elu'
opt.init_w = 'xavier'
opt.init_z = 'uniform_zero_one'
opt.batchSize = 64
opt.clamp_lower = 0
opt.clamp_upper = 0
opt.adam = True
opt.ndf = 1024
opt.ngf = 1024
if opt.critic_last_layer == 'none':
opt.critic_last_layer = 'sigmoid'
elif opt.D == 'wgan':
opt.lrD = 0.0002
opt.lrG = 0.0002
opt.c_activation = 'elu'
opt.init_w = 'xavier'
opt.init_z = 'uniform_zero_one'
opt.batchSize = 64
opt.clamp_lower = -0.01
opt.clamp_upper = 0.01
opt.adam = False
opt.ndf = 1024
opt.ngf = 1024
elif opt.A == 'dcgan':
if opt.D == 'gan':
opt.lrD = 0.0002
opt.lrG = 0.0002
opt.init_z = 'gaussian'
opt.batchSize = 64
opt.clamp_lower = 0
opt.clamp_upper = 0
opt.adam = True
if opt.critic_last_layer == 'none':
opt.critic_last_layer = 'sigmoid'
elif opt.D == 'wgan':
opt.lrD = 0.0002
opt.lrG = 0.0002
opt.init_z = 'gaussian'
opt.batchSize = 64
opt.clamp_lower = -0.01
opt.clamp_upper = 0.01
opt.adam = False
if opt.cuda:
if opt.gpu_id >= 0:
os.environ['CUDA_VISIBLE_DEVICES'] = str(opt.gpu_id)
if opt.task == 'train':
train(opt=opt, log_file_path=os.path.join(opt.experiment, '{0}_{1}_{2}_{3}_{4}_{5}.log'.format(opt.D, opt.A, opt.H, opt.c_activation, opt.manualSeed, opt.critic_last_layer)))
elif opt.task == 'eval_kde':
print('max log prob is %s ' % train(opt=opt, log_file_path=os.path.join(opt.kde_result_dir, '{0}_{1}_{2}.log'.format(opt.D, opt.A, opt.H))))
elif opt.task == 'mu':
search_mu(opt=opt)
elif opt.task == 'murange':
search_mu(opt=opt, start = opt.start, end = opt.end)
elif opt.task == 'rand':
experiments_randseeds(opt=opt, start=opt.start, end=opt.end)
else:
assert opt.task == 'hyper'
if opt.dataset == 'lsun':
search_lsun_hyperparams(opt=opt)
else:
search_hyperparams(opt=opt)
if __name__ == '__main__':
main()