-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.py
executable file
·406 lines (303 loc) · 12.8 KB
/
image.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
import sys
import os
import time
import importlib
import argparse
import numpy as np
import torch
import torch.utils.data
from torch import optim
from modules import ResNetEncoderV2, BNResNetEncoderV2, PixelCNNDecoderV2
from modules import VAE
from logger import Logger
from utils import calc_mi
clip_grad = 5.0
decay_epoch = 20
lr_decay = 0.5
max_decay = 5
def init_config():
parser = argparse.ArgumentParser(description='VAE mode collapse study')
# model hyperparameters
parser.add_argument('--dataset', default='omniglot', type=str, help='dataset to use')
# optimization parameters
parser.add_argument('--nsamples', type=int, default=1, help='number of samples for training')
parser.add_argument('--iw_nsamples', type=int, default=500,
help='number of samples to compute importance weighted estimate')
# select mode
parser.add_argument('--eval', action='store_true', default=False, help='compute iw nll')
parser.add_argument('--load_path', type=str, default='')
# annealing paramters
parser.add_argument('--warm_up', type=int, default=10)
parser.add_argument('--kl_start', type=float, default=1.0)
# these are for slurm purpose to save model
parser.add_argument('--jobid', type=int, default=0, help='slurm job id')
parser.add_argument('--taskid', type=int, default=0, help='slurm task id')
parser.add_argument('--device', type=str, default="cpu")
parser.add_argument('--delta_rate', type=float, default=1.0,
help=" coontrol the minization of the variation of latent variables")
parser.add_argument('--gamma', type=float, default=0.5) # BN-VAE
parser.add_argument("--reset_dec", action="store_true", default=False)
parser.add_argument("--nz_new", type=int, default=32) # myGaussianLSTMencoder
parser.add_argument('--p_drop', type=float, default=0.2) # p \in [0, 1]
args = parser.parse_args()
if 'cuda' in args.device:
args.cuda = True
else:
args.cuda = False
load_str = "_load" if args.load_path != "" else ""
save_dir = "models/%s%s/" % (args.dataset, load_str)
if args.warm_up > 0 and args.kl_start < 1.0:
cw_str = '_warm%d' % args.warm_up
else:
cw_str = ''
hkl_str = 'KL%.2f' % args.kl_start
drop_str = '_drop%.2f' % args.p_drop if args.p_drop != 0 else ''
seed_set = [783435, 101, 202, 303, 404, 505, 606, 707, 808, 909]
args.seed = seed_set[args.taskid]
if args.gamma > 0:
gamma_str = '_gamma%.2f' % (args.gamma)
else:
gamma_str = ''
id_ = "%s_%s%s%s%s_dr%.2f_nz%d%s_%d_%d_%d" % \
(args.dataset, hkl_str,
cw_str, load_str, gamma_str, args.delta_rate,
args.nz_new,drop_str,
args.jobid, args.taskid, args.seed)
save_dir += id_
if not os.path.exists(save_dir):
os.makedirs(save_dir)
save_path = os.path.join(save_dir, 'model.pt')
args.save_path = save_path
print("save path", args.save_path)
args.log_path = os.path.join(save_dir, "log.txt")
print("log path", args.log_path)
# load config file into args
config_file = "config.config_%s" % args.dataset
params = importlib.import_module(config_file).params
args = argparse.Namespace(**vars(args), **params)
if args.nz != args.nz_new:
args.nz = args.nz_new
print('args.nz', args.nz)
if 'label' in params:
args.label = params['label']
else:
args.label = False
args.kl_weight = 1
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
torch.backends.cudnn.deterministic = True
return args
def test(model, test_loader, mode, args):
report_kl_loss = report_kl_t_loss = report_rec_loss = 0
report_num_examples = 0
mutual_info = []
for datum in test_loader:
batch_data, _ = datum
batch_data = batch_data.to(args.device)
batch_size = batch_data.size(0)
report_num_examples += batch_size
loss, loss_rc, loss_kl = model.loss(batch_data, 1.0, args, training=False)
loss_kl_t = model.KL(batch_data, args)
assert (not loss_rc.requires_grad)
loss_rc = loss_rc.sum()
loss_kl = loss_kl.sum()
loss_kl_t = loss_kl_t.sum()
report_rec_loss += loss_rc.item()
report_kl_loss += loss_kl.item()
report_kl_t_loss += loss_kl_t.item()
mutual_info = calc_mi(model, test_loader, device=args.device)
test_loss = (report_rec_loss + report_kl_loss) / report_num_examples
nll = (report_kl_t_loss + report_rec_loss) / report_num_examples
kl = report_kl_loss / report_num_examples
kl_t = report_kl_t_loss / report_num_examples
print('%s --- avg_loss: %.4f, kl: %.4f, mi: %.4f, recon: %.4f, nll: %.4f' % \
(mode, test_loss, report_kl_t_loss / report_num_examples, mutual_info,
report_rec_loss / report_num_examples, nll))
sys.stdout.flush()
return test_loss, nll, kl_t ##返回真实的kl_t 不是训练中的kl
def calc_au(model, test_loader, delta=0.01):
"""compute the number of active units
"""
means = []
for datum in test_loader:
batch_data, _ = datum
batch_data = batch_data.to(args.device)
mean, _ = model.encode_stats(batch_data)
means.append(mean)
means = torch.cat(means, dim=0)
au_mean = means.mean(0, keepdim=True)
# (batch_size, nz)
au_var = means - au_mean
ns = au_var.size(0)
au_var = (au_var ** 2).sum(dim=0) / (ns - 1)
return (au_var >= delta).sum().item(), au_var
def calc_iwnll(model, test_loader, args):
report_nll_loss = 0
report_num_examples = 0
for id_, datum in enumerate(test_loader):
batch_data, _ = datum
batch_data = batch_data.to(args.device)
batch_size = batch_data.size(0)
report_num_examples += batch_size
if id_ % (round(len(test_loader) / 10)) == 0:
print('iw nll computing %d0%%' % (id_ / (round(len(test_loader) / 10))))
sys.stdout.flush()
loss = model.nll_iw(batch_data, nsamples=args.iw_nsamples)
report_nll_loss += loss.sum().item()
nll = report_nll_loss / report_num_examples
print('iw nll: %.4f' % nll)
sys.stdout.flush()
return nll
def main(args):
if args.cuda:
print('using cuda')
print(args)
args.device = torch.device(args.device)
device = args.device
opt_dict = {"not_improved": 0, "lr": 0.001, "best_loss": 1e4}
all_data = torch.load(args.data_file)
x_train, x_val, x_test = all_data
if args.dataset == 'omniglot':
x_train = x_train.to(device)
x_val = x_val.to(device)
x_test = x_test.to(device)
y_size = 1
y_train = x_train.new_zeros(x_train.size(0), y_size)
y_val = x_train.new_zeros(x_val.size(0), y_size)
y_test = x_train.new_zeros(x_test.size(0), y_size)
print(torch.__version__)
train_data = torch.utils.data.TensorDataset(x_train, y_train)
val_data = torch.utils.data.TensorDataset(x_val, y_val)
test_data = torch.utils.data.TensorDataset(x_test, y_test)
train_loader = torch.utils.data.DataLoader(train_data, batch_size=args.batch_size, shuffle=True)
val_loader = torch.utils.data.DataLoader(val_data, batch_size=args.batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=args.batch_size, shuffle=True)
print('Train data: %d batches' % len(train_loader))
print('Val data: %d batches' % len(val_loader))
print('Test data: %d batches' % len(test_loader))
sys.stdout.flush()
log_niter = len(train_loader) // 5
if args.gamma > 0:
encoder = BNResNetEncoderV2(args)
else:
encoder = ResNetEncoderV2(args)
decoder = PixelCNNDecoderV2(args)
vae = VAE(encoder, decoder, args).to(device)
if args.eval:
print('begin evaluation')
test_loader = torch.utils.data.DataLoader(test_data, batch_size=50, shuffle=True)
vae.load_state_dict(torch.load(args.load_path))
vae.eval()
with torch.no_grad():
test(vae, test_loader, "TEST", args)
au, au_var = calc_au(vae, test_loader)
print("%d active units" % au)
# print(au_var)
calc_iwnll(vae, test_loader, args)
return
enc_optimizer = optim.Adam(vae.encoder.parameters(), lr=0.001)
dec_optimizer = optim.Adam(vae.decoder.parameters(), lr=0.001)
opt_dict['lr'] = 0.001
iter_ = 0
best_loss = 1e4
decay_cnt = 0
vae.train()
start = time.time()
kl_weight = args.kl_start
anneal_rate = (1.0 - args.kl_start) / (args.warm_up * len(train_loader))
for epoch in range(args.epochs):
report_kl_loss = report_rec_loss = 0
report_num_examples = 0
for datum in train_loader:
batch_data, _ = datum
batch_data = batch_data.to(device)
if args.dataset != 'fashion-mnist':
batch_data = torch.bernoulli(batch_data)
batch_size = batch_data.size(0)
report_num_examples += batch_size
# kl_weight = 1.0
kl_weight = min(1.0, kl_weight + anneal_rate)
args.kl_weight = kl_weight
enc_optimizer.zero_grad()
dec_optimizer.zero_grad()
loss, loss_rc, loss_kl = vae.loss(batch_data, kl_weight, args)
loss = loss.mean(dim=-1)
loss.backward()
torch.nn.utils.clip_grad_norm_(vae.parameters(), clip_grad)
loss_rc = loss_rc.sum()
loss_kl = loss_kl.sum()
enc_optimizer.step()
dec_optimizer.step()
report_rec_loss += loss_rc.item()
report_kl_loss += loss_kl.item()
if iter_ % log_niter == 0:
train_loss = (report_rec_loss + report_kl_loss) / report_num_examples
if epoch == 0:
vae.eval()
with torch.no_grad():
mi = calc_mi(vae, val_loader, device=device)
au, _ = calc_au(vae, val_loader)
vae.train()
print('epoch: %d, iter: %d, avg_loss: %.4f, kl: %.4f, mi: %.4f, recon: %.4f,' \
'au %d, time elapsed %.2fs' %
(epoch, iter_, train_loss, report_kl_loss / report_num_examples, mi,
report_rec_loss / report_num_examples, au, time.time() - start))
else:
print('epoch: %d, iter: %d, avg_loss: %.4f, kl: %.4f, recon: %.4f,' \
'time elapsed %.2fs' %
(epoch, iter_, train_loss, report_kl_loss / report_num_examples,
report_rec_loss / report_num_examples, time.time() - start))
sys.stdout.flush()
report_rec_loss = report_kl_loss = 0
report_num_examples = 0
iter_ += 1
print('kl weight %.4f' % args.kl_weight)
print('epoch: %d, VAL' % epoch)
vae.eval()
with torch.no_grad():
loss, nll, kl = test(vae, val_loader, "VAL", args)
au, au_var = calc_au(vae, val_loader)
print("%d active units" % au)
# print(au_var)
if loss < best_loss:
print('update best loss')
best_loss = loss
torch.save(vae.state_dict(), args.save_path)
if loss > best_loss:
opt_dict["not_improved"] += 1
if opt_dict["not_improved"] >= decay_epoch:
opt_dict["best_loss"] = loss
opt_dict["not_improved"] = 0
opt_dict["lr"] = opt_dict["lr"] * lr_decay
vae.load_state_dict(torch.load(args.save_path))
decay_cnt += 1
print('new lr: %f' % opt_dict["lr"])
enc_optimizer = optim.Adam(vae.encoder.parameters(), lr=opt_dict["lr"])
dec_optimizer = optim.Adam(vae.decoder.parameters(), lr=opt_dict["lr"])
else:
opt_dict["not_improved"] = 0
opt_dict["best_loss"] = loss
if decay_cnt == max_decay:
break
if epoch % args.test_nepoch == 0:
with torch.no_grad():
loss, nll, kl = test(vae, test_loader, "TEST", args)
vae.train()
# compute importance weighted estimate of log p(x)
vae.load_state_dict(torch.load(args.save_path))
vae.eval()
with torch.no_grad():
loss, nll, kl = test(vae, test_loader, "TEST", args)
au, au_var = calc_au(vae, test_loader)
print("%d active units" % au)
# print(au_var)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=50, shuffle=True)
with torch.no_grad():
calc_iwnll(vae, test_loader, args)
if __name__ == '__main__':
args = init_config()
if not args.eval:
sys.stdout = Logger(args.log_path)
main(args)