-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_glow.py
542 lines (437 loc) · 15.1 KB
/
train_glow.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
"""
train.py from glow/ submodule
adapted for datasets used in this project
"""
import argparse
import os
import json
import shutil
import random
from itertools import islice
import torch
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data as data
from ignite.contrib.handlers import ProgressBar
from ignite.engine import Engine, Events
from ignite.handlers import ModelCheckpoint, Timer
from ignite.metrics import RunningAverage, Loss
from glow.datasets import get_CIFAR10, get_SVHN, get_CIFAR64
from glow.model import Glow
from data import get_dataset, get_transform
from torchvision import transforms
from glow.datasets import preprocess, postprocess
from torchvision.utils import make_grid
import matplotlib
matplotlib.use("Agg")
import matplotlib.pylab as plt
class SimpleDataset:
def __init__(self, x, transform):
self.x = x
self.transform = transform
def __getitem__(self,i):
img = self.transform(self.x[i])
return img, 0
def __len__(self):
return len(self.x)
def check_manual_seed(seed):
seed = seed or random.randint(1, 10000)
random.seed(seed)
torch.manual_seed(seed)
print("Using seed: {seed}".format(seed=seed))
def generate_from_noise(model, batch_size, device, clamp=False, guard_nans=True):
assert not clamp
_, c2, h, w = model.prior_h.shape
c = c2 // 2
zshape = (batch_size, c, h, w)
randz = torch.randn(zshape).to(device)
randz = torch.autograd.Variable(randz, requires_grad=True)
if clamp:
randz = torch.clamp(randz,-5,5)
images = model(z= randz, y_onehot=None, temperature=1, reverse=True)
if guard_nans:
images[(images!=images)] = 0
return images
def check_dataset(dataset, dataroot, augment, download):
if dataset == "cifar64":
# cifar64 = get_CIFAR64(augment, dataroot, download)
# input_size, num_classes, train_dataset, test_dataset = cifar64
train_data = get_dataset('cifar-fs-train-train', args.dataroot)
test_data = get_dataset('cifar-fs-train-test', args.dataroot)
transform = transforms.Compose([transforms.ToTensor(), preprocess])
train_dataset = SimpleDataset(train_data['x'], transform)
test_dataset = SimpleDataset(test_data['x'], transform)
input_size = (32,32,3)
num_classes = 64
if dataset == "cifar10":
cifar10 = get_CIFAR10(augment, dataroot, download)
input_size, num_classes, train_dataset, test_dataset = cifar10
if dataset == "svhn":
svhn = get_SVHN(augment, dataroot, download)
input_size, num_classes, train_dataset, test_dataset = svhn
if dataset == "miniimagenet":
train_data = get_dataset('miniimagenet-train-train', args.dataroot)
test_data = get_dataset('miniimagenet-train-test', args.dataroot)
transform = transforms.Compose([transforms.ToPILImage(), transforms.Resize((32,32)), transforms.ToTensor(), preprocess])
train_dataset = SimpleDataset(train_data['x'], transform)
test_dataset = SimpleDataset(test_data['x'], transform)
input_size = (32,32,3)
num_classes = 64
return input_size, num_classes, train_dataset, test_dataset
def compute_loss(nll, reduction="mean"):
if reduction == "mean":
losses = {"nll": torch.mean(nll)}
elif reduction == "none":
losses = {"nll": nll}
losses["total_loss"] = losses["nll"]
return losses
def compute_loss_y(nll, y_logits, y_weight, y, multi_class, reduction="mean"):
if reduction == "mean":
losses = {"nll": torch.mean(nll)}
elif reduction == "none":
losses = {"nll": nll}
if multi_class:
y_logits = torch.sigmoid(y_logits)
loss_classes = F.binary_cross_entropy_with_logits(
y_logits, y, reduction=reduction
)
else:
loss_classes = F.cross_entropy(
y_logits, torch.argmax(y, dim=1), reduction=reduction
)
losses["loss_classes"] = loss_classes
losses["total_loss"] = losses["nll"] + y_weight * loss_classes
return losses
def main(
dataset,
dataroot,
download,
augment,
batch_size,
eval_batch_size,
epochs,
saved_model,
seed,
hidden_channels,
K,
L,
actnorm_scale,
flow_permutation,
flow_coupling,
LU_decomposed,
learn_top,
y_condition,
y_weight,
max_grad_clip,
max_grad_norm,
lr,
n_workers,
cuda,
n_init_batches,
output_dir,
saved_optimizer,
warmup,
):
device = "cpu" if (not torch.cuda.is_available() or not cuda) else "cuda:0"
check_manual_seed(seed)
ds = check_dataset(dataset, dataroot, augment, download)
image_shape, num_classes, train_dataset, test_dataset = ds
# Note: unsupported for now
multi_class = False
train_loader = data.DataLoader(
train_dataset,
batch_size=batch_size,
shuffle=True,
num_workers=n_workers,
drop_last=True,
)
test_loader = data.DataLoader(
test_dataset,
batch_size=eval_batch_size,
shuffle=False,
num_workers=n_workers,
drop_last=False,
)
model = Glow(
image_shape,
hidden_channels,
K,
L,
actnorm_scale,
flow_permutation,
flow_coupling,
LU_decomposed,
num_classes,
learn_top,
y_condition,
)
model = model.to(device)
optimizer = optim.Adamax(model.parameters(), lr=lr, weight_decay=5e-5)
lr_lambda = lambda epoch: min(1.0, (epoch + 1) / warmup) # noqa
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lr_lambda)
def step(engine, batch):
model.train()
optimizer.zero_grad()
x, y = batch
x = x.to(device)
if y_condition:
y = y.to(device)
z, nll, y_logits = model(x, y)
losses = compute_loss_y(nll, y_logits, y_weight, y, multi_class)
else:
z, nll, y_logits = model(x, None)
losses = compute_loss(nll)
losses["total_loss"].backward()
if max_grad_clip > 0:
torch.nn.utils.clip_grad_value_(model.parameters(), max_grad_clip)
if max_grad_norm > 0:
torch.nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm)
optimizer.step()
return losses
def eval_step(engine, batch):
model.eval()
x, y = batch
x = x.to(device)
with torch.no_grad():
if y_condition:
y = y.to(device)
z, nll, y_logits = model(x, y)
losses = compute_loss_y(
nll, y_logits, y_weight, y, multi_class, reduction="none"
)
else:
z, nll, y_logits = model(x, None)
losses = compute_loss(nll, reduction="none")
return losses
trainer = Engine(step)
checkpoint_handler = ModelCheckpoint(
output_dir, "glow", save_interval=1, n_saved=2, require_empty=False
)
trainer.add_event_handler(
Events.EPOCH_COMPLETED,
checkpoint_handler,
{"model": model, "optimizer": optimizer},
)
monitoring_metrics = ["total_loss"]
RunningAverage(output_transform=lambda x: x["total_loss"]).attach(
trainer, "total_loss"
)
evaluator = Engine(eval_step)
# Note: replace by https://github.com/pytorch/ignite/pull/524 when released
Loss(
lambda x, y: torch.mean(x),
output_transform=lambda x: (
x["total_loss"],
torch.empty(x["total_loss"].shape[0]),
),
).attach(evaluator, "total_loss")
if y_condition:
monitoring_metrics.extend(["nll"])
RunningAverage(output_transform=lambda x: x["nll"]).attach(trainer, "nll")
# Note: replace by https://github.com/pytorch/ignite/pull/524 when released
Loss(
lambda x, y: torch.mean(x),
output_transform=lambda x: (x["nll"], torch.empty(x["nll"].shape[0])),
).attach(evaluator, "nll")
pbar = ProgressBar()
pbar.attach(trainer, metric_names=monitoring_metrics)
# load pre-trained model if given
if saved_model:
model.load_state_dict(torch.load(saved_model))
model.set_actnorm_init()
if saved_optimizer:
optimizer.load_state_dict(torch.load(saved_optimizer))
file_name, ext = os.path.splitext(saved_model)
resume_epoch = int(file_name.split("_")[-1])
@trainer.on(Events.STARTED)
def resume_training(engine):
engine.state.epoch = resume_epoch
engine.state.iteration = resume_epoch * len(engine.state.dataloader)
@trainer.on(Events.STARTED)
def init(engine):
model.train()
init_batches = []
init_targets = []
with torch.no_grad():
for batch, target in islice(train_loader, None, n_init_batches):
init_batches.append(batch)
init_targets.append(target)
init_batches = torch.cat(init_batches).to(device)
assert init_batches.shape[0] == n_init_batches * batch_size
if y_condition:
init_targets = torch.cat(init_targets).to(device)
else:
init_targets = None
model(init_batches, init_targets)
@trainer.on(Events.EPOCH_COMPLETED)
def evaluate(engine):
evaluator.run(test_loader)
scheduler.step()
metrics = evaluator.state.metrics
losses = ", ".join([f"{key}: {value:.2f}" for key, value in metrics.items()])
print(f"Validation Results - Epoch: {engine.state.epoch} {losses}")
# Viz
real = test_loader.__iter__().__next__()[0]
with torch.no_grad():
fake = generate_from_noise(model,25, device)
fig, axs = plt.subplots(1,2,figsize=(8,5))
plt.subplot(axs[0])
plt.imshow(make_grid((postprocess(real.cpu())[:25]), nrow=5).permute(1,2,0))
plt.title('data')
plt.subplot(axs[1])
plt.imshow(make_grid((postprocess(fake.cpu())[:25]), nrow=5).permute(1,2,0))
plt.title('sample')
plt.axis('off')
plt.savefig(os.path.join(output_dir, f'samples_{engine.state.epoch}.jpeg'), bbox_inches='tight')
timer = Timer(average=True)
timer.attach(
trainer,
start=Events.EPOCH_STARTED,
resume=Events.ITERATION_STARTED,
pause=Events.ITERATION_COMPLETED,
step=Events.ITERATION_COMPLETED,
)
@trainer.on(Events.EPOCH_COMPLETED)
def print_times(engine):
pbar.log_message(
f"Epoch {engine.state.epoch} done. Time per batch: {timer.value():.3f}[s]"
)
timer.reset()
trainer.run(train_loader, epochs)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
type=str,
default="cifar10",
choices=["cifar10","cifar64", "svhn", "miniimagenet"],
help="Type of the dataset to be used.",
)
parser.add_argument("--dataroot", type=str, default="./", help="path to dataset")
parser.add_argument("--download", action="store_true", help="downloads dataset")
parser.add_argument(
"--no_augment",
action="store_false",
dest="augment",
help="Augment training data",
)
parser.add_argument(
"--hidden_channels", type=int, default=512, help="Number of hidden channels"
)
parser.add_argument("--K", type=int, default=32, help="Number of layers per block")
parser.add_argument("--L", type=int, default=3, help="Number of blocks")
parser.add_argument(
"--actnorm_scale", type=float, default=1.0, help="Act norm scale"
)
parser.add_argument(
"--flow_permutation",
type=str,
default="invconv",
choices=["invconv", "shuffle", "reverse"],
help="Type of flow permutation",
)
parser.add_argument(
"--flow_coupling",
type=str,
default="affine",
choices=["additive", "affine"],
help="Type of flow coupling",
)
parser.add_argument(
"--no_LU_decomposed",
action="store_false",
dest="LU_decomposed",
help="Train with LU decomposed 1x1 convs",
)
parser.add_argument(
"--no_learn_top",
action="store_false",
help="Do not train top layer (prior)",
dest="learn_top",
)
parser.add_argument(
"--y_condition", action="store_true", help="Train using class condition"
)
parser.add_argument(
"--y_weight", type=float, default=0.01, help="Weight for class condition loss"
)
parser.add_argument(
"--max_grad_clip",
type=float,
default=0,
help="Max gradient value (clip above - for off)",
)
parser.add_argument(
"--max_grad_norm",
type=float,
default=0,
help="Max norm of gradient (clip above - 0 for off)",
)
parser.add_argument(
"--n_workers", type=int, default=6, help="number of data loading workers"
)
parser.add_argument(
"--batch_size", type=int, default=64, help="batch size used during training"
)
parser.add_argument(
"--eval_batch_size",
type=int,
default=512,
help="batch size used during evaluation",
)
parser.add_argument(
"--epochs", type=int, default=250, help="number of epochs to train for"
)
parser.add_argument("--lr", type=float, default=5e-4, help="Learning rate")
parser.add_argument(
"--warmup",
type=float,
default=5,
help="Use this number of epochs to warmup learning rate linearly from zero to learning rate", # noqa
)
parser.add_argument(
"--n_init_batches",
type=int,
default=8,
help="Number of batches to use for Act Norm initialisation",
)
parser.add_argument(
"--no_cuda", action="store_false", dest="cuda", help="Disables cuda"
)
parser.add_argument(
"--output_dir",
default="output/",
help="Directory to output logs and model checkpoints",
)
parser.add_argument(
"--fresh", action="store_true", help="Remove output directory before starting"
)
parser.add_argument(
"--saved_model",
default="",
help="Path to model to load for continuing training",
)
parser.add_argument(
"--saved_optimizer",
default="",
help="Path to optimizer to load for continuing training",
)
parser.add_argument("--seed", type=int, default=0, help="manual seed")
args = parser.parse_args()
try:
os.makedirs(args.output_dir)
except FileExistsError:
if args.fresh:
shutil.rmtree(args.output_dir)
os.makedirs(args.output_dir)
if (not os.path.isdir(args.output_dir)) or (
len(os.listdir(args.output_dir)) > 0
):
raise FileExistsError(
"Please provide a path to a non-existing or empty directory. Alternatively, pass the --fresh flag." # noqa
)
kwargs = vars(args)
del kwargs["fresh"]
with open(os.path.join(args.output_dir, "hparams.json"), "w") as fp:
json.dump(kwargs, fp, sort_keys=True, indent=4)
main(**kwargs)