-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
166 lines (121 loc) · 5.16 KB
/
train.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
import os
from os.path import join
import time
import torch
import torch.nn as nn
import numpy as np
from tqdm import tqdm
import data
import model
from evaluation import checkpoint_figures
def _check_gradients_per_block(inn):
print('===' * 10)
for i, mod in enumerate(inn.module_list):
grad_norm_list = []
for p in mod.parameters():
if p.grad is not None:
grad_norm_list.append(1000. * torch.norm(p.grad.data**2).item())
if grad_norm_list:
mean = '{:.5f}'.format(sum(grad_norm_list) / len(grad_norm_list))
else:
mean = '--'
print('{:>4d} {:>10s}'.format(i, mean))
print('===' * 10)
def train(args):
##########################
# Relevant config values #
##########################
log_interval = 1 #print losses every epoch
checkpoint_interval = eval(args['checkpoints']['checkpoint_interval'])
checkpoint_overwrite = eval(args['checkpoints']['checkpoint_overwrite'])
checkpoint_on_error = eval(args['checkpoints']['checkpoint_on_error'])
figures_interval = eval(args['checkpoints']['figures_interval'])
figures_overwrite = eval(args['checkpoints']['figures_overwrite'])
no_progress_bar = not eval(args['checkpoints']['epoch_progress_bar'])
N_epochs = eval(args['training']['N_epochs'])
output_dir = args['checkpoints']['output_dir']
n_gpus = eval(args['training']['parallel_GPUs'])
checkpoint_resume = args['checkpoints']['resume_checkpoint']
cond_net_resume = args['checkpoints']['resume_cond_net']
checkpoints_dir = join(output_dir, 'checkpoints')
figures_dir = join(output_dir, 'figures')
os.makedirs(checkpoints_dir, exist_ok=True)
os.makedirs(figures_dir, exist_ok=True)
#######################################
# Construct and load network and data #
#######################################
cinn = model.CINN(args)
cinn.train()
cinn.cuda()
if checkpoint_resume:
cinn.load(checkpoint_resume)
if cond_net_resume:
cinn.load_cond_net(cond_net_resume)
if n_gpus > 1:
cinn_parallel = nn.DataParallel(cinn, list(range(n_gpus)))
else:
cinn_parallel = cinn
scheduler = torch.optim.lr_scheduler.MultiStepLR(cinn.optimizer, gamma=0.1,
milestones=eval(args['training']['milestones_lr_decay']))
dataset = data.dataset(args)
val_x = dataset.val_x.cuda()
val_y = dataset.val_y.cuda()
x_std, y_std = [], []
x_mean, y_mean = [], []
with torch.no_grad():
for x, y in tqdm(dataset.train_loader):
x_std.append(torch.std(x, dim=(0,2,3)).numpy())
y_std.append(torch.std(y, dim=(0,2,3)).numpy())
x_mean.append(torch.mean(x, dim=(0,2,3)).numpy())
y_mean.append(torch.mean(y, dim=(0,2,3)).numpy())
break
print(np.mean(x_std, axis=0))
print(np.mean(x_mean, axis=0))
print(np.mean(y_std, axis=0))
print(np.mean(y_mean, axis=0))
####################
# Logging business #
####################
logfile = open(join(output_dir, 'losses.dat'), 'w')
def log_write(string):
logfile.write(string + '\n')
logfile.flush()
print(string, flush=True)
log_header = '{:>8s}{:>10s}{:>12s}{:>12s}'.format('Epoch', 'Time (m)', 'NLL train', 'NLL val')
log_fmt = '{:>8d}{:>10.1f}{:>12.5f}{:>12.5f}'
log_write(log_header)
if figures_interval > 0:
checkpoint_figures(join(figures_dir, 'init.pdf'), cinn, dataset, args)
t_start = time.time()
####################
# V Training V #
####################
for epoch in range(N_epochs):
progress_bar = tqdm(total=dataset.epoch_length, ascii=True, ncols=100, leave=False,
disable=True)#no_progress_bar)
loss_per_batch = []
for i, (x, y) in enumerate(dataset.train_loader):
x, y = x.cuda(), y.cuda()
nll = cinn_parallel(x, y).mean()
nll.backward()
# _check_gradients_per_block(cinn.inn)
loss_per_batch.append(nll.item())
print('{:03d}/445 {:.6f}'.format(i, loss_per_batch[-1]), end='\r')
cinn.optimizer.step()
cinn.optimizer.zero_grad()
progress_bar.update()
# from here: end of epoch
scheduler.step()
progress_bar.close()
if (epoch + 1) % log_interval == 0:
with torch.no_grad():
time_delta = (time.time() - t_start) / 60.
train_loss = np.mean(loss_per_batch)
val_loss = cinn_parallel(val_x, val_y).mean()
log_write(log_fmt.format(epoch + 1, time_delta, train_loss, val_loss))
if figures_interval > 0 and (epoch + 1) % figures_interval == 0:
checkpoint_figures(join(figures_dir, 'epoch_{:05d}.pdf'.format(epoch + 1)), cinn, dataset, args)
if checkpoint_interval > 0 and (epoch + 1) % checkpoint_interval == 0:
cinn.save(join(checkpoints_dir, 'checkpoint_{:05d}.pt'.format(epoch + 1)))
logfile.close()
cinn.save(join(output_dir, 'checkpoint_end.pt'))