forked from cics-nd/pde-surrogate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_codec_max_likelihood.py
235 lines (209 loc) · 11.3 KB
/
train_codec_max_likelihood.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
"""Data-driven surrogates, trained with maximum likelihood estimation.
Use the same network as physics-constrained surrogate, but requires output data.
"""
import torch
import torch.optim as optim
import torch.nn.functional as F
from models.codec import DenseED
from utils.load import load_data
from utils.misc import mkdirs, to_numpy
from utils.plot import save_stats, plot_prediction_det
from utils.practices import OneCycleScheduler, adjust_learning_rate, find_lr
import time
import argparse
import random
from pprint import pprint
import json
import sys
import matplotlib.pyplot as plt
plt.switch_backend('agg')
class Parser(argparse.ArgumentParser):
def __init__(self):
super(Parser, self).__init__(description='Learning data-driven surrogata with MLE')
self.add_argument('--exp-name', type=str, default='codec/max_likelihood', help='experiment name')
self.add_argument('--exp-dir', type=str, default="./experiments", help='directory to save experiments')
# codec
self.add_argument('--blocks', type=list, default=[6, 8, 6], help='list of number of layers in each dense block')
self.add_argument('--growth-rate', type=int, default=16, help='number of output feature maps of each conv layer within each dense block')
self.add_argument('--init-features', type=int, default=48, help='number of initial features after the first conv layer')
self.add_argument('--drop-rate', type=float, default=0., help='dropout rate')
self.add_argument('--upsample', type=str, default='nearest', choices=['nearest', 'bilinear'])
# data
self.add_argument('--data-dir', type=str, default="./datasets", help='directory to dataset')
self.add_argument('--data', type=str, default='grf_kle512', choices=['grf_kle512', 'channelized'])
self.add_argument('--ntrain', type=int, default=4096, help="number of training data")
self.add_argument('--ntest', type=int, default=512, help="number of test data")
self.add_argument('--imsize', type=int, default=64)
# training
self.add_argument('--run', type=int, default=1, help='run instance')
self.add_argument('--epochs', type=int, default=200, help='number of epochs to train')
self.add_argument('--lr', type=float, default=1e-3, help='learning rate')
self.add_argument('--lr-div', type=float, default=2., help='lr div factor to get the initial lr')
self.add_argument('--lr-pct', type=float, default=0.3, help='percentage of epochs to reach the (max) lr')
self.add_argument('--weight-decay', type=float, default=0., help="weight decay")
# self.add_argument('--weight-bound', type=float, default=10, help="weight for boundary loss")
self.add_argument('--batch-size', type=int, default=32, help='input batch size for training')
self.add_argument('--test-batch-size', type=int, default=64, help='input batch size for testing')
self.add_argument('--seed', type=int, default=1, help='manual seed used in Tensor')
self.add_argument('--cuda', type=int, default=1, choices=[0, 1, 2, 3], help='cuda number')
# logging
self.add_argument('--debug', action='store_true', default=False, help='debug or verbose')
self.add_argument('--ckpt-epoch', type=int, default=None, help='which epoch of checkpoints to be loaded in post mode')
self.add_argument('--ckpt-freq', type=int, default=50, help='how many epochs to wait before saving model')
self.add_argument('--log-freq', type=int, default=1, help='how many epochs to wait before logging training status')
self.add_argument('--plot-freq', type=int, default=50, help='how many epochs to wait before plotting test output')
self.add_argument('--plot-fn', type=str, default='imshow', choices=['contourf', 'imshow'], help='plotting method')
def parse(self):
args = self.parse_args()
hparams = f'{args.data}_ntrain{args.ntrain}_run{args.run}_bs{args.batch_size}_lr{args.lr}_epochs{args.epochs}'
if args.debug:
hparams = 'debug/' + hparams
args.run_dir = args.exp_dir + '/' + args.exp_name + '/' + hparams
args.ckpt_dir = args.run_dir + '/checkpoints'
mkdirs(args.run_dir, args.ckpt_dir)
assert args.ntrain % args.batch_size == 0 and \
args.ntest % args.test_batch_size == 0
if args.seed is None:
args.seed = random.randint(1, 10000)
print("Random Seed: ", args.seed)
random.seed(args.seed)
torch.manual_seed(args.seed)
print('Arguments:')
pprint(vars(args))
with open(args.run_dir + "/args.txt", 'w') as args_file:
json.dump(vars(args), args_file, indent=4)
return args
if __name__ == '__main__':
args = Parser().parse()
device = torch.device(f"cuda:{args.cuda}" if torch.cuda.is_available() else "cpu")
args.train_dir = args.run_dir + '/training'
args.pred_dir = args.train_dir + '/predictions'
mkdirs(args.train_dir, args.pred_dir)
model = DenseED(in_channels=1, out_channels=3,
imsize=args.imsize,
blocks=args.blocks,
growth_rate=args.growth_rate,
init_features=args.init_features,
drop_rate=args.drop_rate,
out_activation=None,
upsample=args.upsample).to(device)
if args.debug:
print(model)
# if start from ckpt
if args.ckpt_epoch is not None:
ckpt_file = args.run_dir + f'/checkpoints/model_epoch{args.ckpt_epoch}.pth'
model.load_state_dict(torch.load(ckpt_file, map_location='cpu'))
print(f'Loaded ckpt: {ckpt_file}')
print(f'training from epoch {args.ckpt_epoch + 1} to {args.epochs}')
# load data
if args.data == 'grf_kle512':
train_hdf5_file = args.data_dir + \
f'/{args.imsize}x{args.imsize}/kle512_lhs10000_train.hdf5'
test_hdf5_file = args.data_dir + \
f'/{args.imsize}x{args.imsize}/kle512_lhs1000_val.hdf5'
ntrain_total, ntest_total = 10000, 1000
elif args.data == 'channelized':
train_hdf5_file = args.data_dir + \
f'/{args.imsize}x{args.imsize}/channel_ng64_n4096_train.hdf5'
test_hdf5_file = args.data_dir + \
f'/{args.imsize}x{args.imsize}/channel_ng64_n512_test.hdf5'
ntrain_total, ntest_total = 4096, 512
assert args.ntrain <= ntrain_total, f"Only {args.ntrain_total} data "\
f"available in {args.data} dataset, but needs {args.ntrain} training data."
assert args.ntest <= ntest_total, f"Only {args.ntest_total} data "\
f"available in {args.data} dataset, but needs {args.ntest} test data."
train_loader, _ = load_data(train_hdf5_file, args.ntrain, args.batch_size,
only_input=False, return_stats=False)
test_loader, test_stats = load_data(test_hdf5_file, args.ntest,
args.test_batch_size, only_input=False, return_stats=True)
y_test_variation = test_stats['y_variation']
print(f'Test output variation per channel: {y_test_variation}')
optimizer = optim.Adam(model.parameters(), lr=args.lr,
weight_decay=args.weight_decay)
scheduler = OneCycleScheduler(lr_max=args.lr, div_factor=args.lr_div,
pct_start=args.lr_pct)
n_out_pixels = test_loader.dataset[0][1].numel()
print(f'# out pixels: {n_out_pixels}')
logger = {}
logger['loss_train'] = []
logger['loss_test'] = []
logger['r2_test'] = []
logger['nrmse_test'] = []
def test(epoch):
model.eval()
loss_test = 0.
relative_l2, err2 = [], []
for batch_idx, (input, target) in enumerate(test_loader):
input, target = input.to(device), target.to(device)
output = model(input)
loss = F.mse_loss(output, target)
loss_test += loss.item()
# sum over H, W --> (B, C)
err2_sum = torch.sum((output - target) ** 2, [-1, -2])
relative_l2.append(torch.sqrt(err2_sum / (target ** 2).sum([-1, -2])))
err2.append(err2_sum)
# plot predictions
if (epoch % args.plot_freq == 0 or epoch == args.epochs) and \
batch_idx == len(test_loader) - 1:
n_samples = 6 if epoch == args.epochs else 2
idx = torch.randperm(input.size(0))[:n_samples]
samples_output = output.data.cpu()[idx].numpy()
samples_target = target.data.cpu()[idx].numpy()
for i in range(n_samples):
print('epoch {}: plotting prediction {}'.format(epoch, i))
plot_prediction_det(args.pred_dir, samples_target[i],
samples_output[i], epoch, i, plot_fn=args.plot_fn)
loss_test /= (batch_idx + 1)
relative_l2 = to_numpy(torch.cat(relative_l2, 0).mean(0))
r2_score = 1 - to_numpy(torch.cat(err2, 0).sum(0)) / y_test_variation
print(f"Epoch: {epoch}, test r2-score: {r2_score}, relative-l2: {relative_l2}")
if epoch % args.log_freq == 0:
logger['loss_test'].append(loss_test)
logger['r2_test'].append(r2_score)
logger['nrmse_test'].append(relative_l2)
print('Start training........................................................')
start_epoch = 1 if args.ckpt_epoch is None else args.ckpt_epoch + 1
tic = time.time()
total_steps = args.epochs * len(train_loader)
print(f'total steps: {total_steps}')
for epoch in range(start_epoch, args.epochs + 1):
model.train()
# if epoch == 30:
# print('begin finding lr')
# logs, losses = find_lr(model, train_loader, optimizer, loss_fn,
# args.weight_bound, init_value=1e-8, final_value=10., beta=0.98)
# plt.plot(logs[10:-5],losses[10:-5])
# plt.savefig(args.train_dir + '/find_lr.png')
# sys.exit(0)
loss_train, mse = 0., 0.
for batch_idx, (input, target) in enumerate(train_loader, start=1):
input, target = input.to(device), target.to(device)
model.zero_grad()
output = model(input)
loss = F.mse_loss(output, target)
loss.backward()
# lr scheduling
step = (epoch - 1) * len(train_loader) + batch_idx
pct = step / total_steps
lr = scheduler.step(pct)
adjust_learning_rate(optimizer, lr)
optimizer.step()
loss_train += loss.item()
loss_train /= batch_idx
print(f'Epoch {epoch}, lr {lr:.6f}')
print(f'Epoch {epoch}: training loss: {loss_train:.6f}')
if epoch % args.log_freq == 0:
logger['loss_train'].append(loss_train)
if epoch % args.ckpt_freq == 0:
torch.save(model.state_dict(), args.ckpt_dir + "/model_epoch{}.pth".format(epoch))
with torch.no_grad():
test(epoch)
tic2 = time.time()
print(f'Finished training {args.epochs} epochs with {args.ntrain} data '\
f'using {(tic2 - tic) / 60:.2f} mins')
metrics = ['loss_train', 'loss_test', 'nrmse_test', 'r2_test']
save_stats(args.train_dir, logger, *metrics)
args.training_time = tic2 - tic
args.n_params, args.n_layers = model.model_size
with open(args.run_dir + "/args.txt", 'w') as args_file:
json.dump(vars(args), args_file, indent=4)