-
Notifications
You must be signed in to change notification settings - Fork 4
/
training_offline.py
154 lines (123 loc) · 6.54 KB
/
training_offline.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
'''Implements a generic training loop.
'''
import torch
import utils
from torch.utils.tensorboard import SummaryWriter
from tqdm.autonotebook import tqdm
import time
import numpy as np
import os, dataio
import shutil
import diff_operators
def func(ckpt_path):
# print(ckpt_path)
dic = torch.load(ckpt_path[0])
return dic
def train(model, train_dataloader, epochs, lr, steps_til_summary, epochs_til_checkpoint, model_dir, loss_fn,
summary_fn, val_dataloader=None, double_precision=False, clip_grad=False, use_lbfgs=False, loss_schedules=None, overwrite=False, sz=128):
optim = torch.optim.AdamW(lr=lr, params=filter(lambda p: p.requires_grad, model.parameters()), amsgrad=True)
# optim = torch.optim.Adam(lr=lr, params=filter(lambda p: p.requires_grad, model.parameters()))
# optim = torch.optim.Adam(lr=lr, params=model.parameters())
# copy settings from Raissi et al. (2019) and here
# https://github.com/maziarraissi/PINNs
if use_lbfgs:
optim = torch.optim.LBFGS(lr=lr, params=model.parameters(), max_iter=50000, max_eval=50000,
history_size=50, line_search_fn='strong_wolfe')
if os.path.exists(model_dir):
if overwrite:
shutil.rmtree(model_dir)
else:
val = input("The model directory %s exists. Overwrite? (y/n)"%model_dir)
if val == 'y':
shutil.rmtree(model_dir)
os.makedirs(model_dir)
summaries_dir = os.path.join(model_dir, 'summaries')
utils.cond_mkdir(summaries_dir)
checkpoints_dir = os.path.join(model_dir, 'checkpoints')
utils.cond_mkdir(checkpoints_dir)
writer = SummaryWriter(summaries_dir)
total_steps = 0
with tqdm(total=len(train_dataloader) * epochs) as pbar:
train_losses = []
for epoch in range(epochs):
if not epoch % epochs_til_checkpoint and epoch:
torch.save(model.state_dict(),
os.path.join(checkpoints_dir, 'model_epoch_%04d.pth' % epoch))
np.savetxt(os.path.join(checkpoints_dir, 'train_losses_epoch_%04d.txt' % epoch),
np.array(train_losses))
for step, (model_input, gt) in enumerate(train_dataloader):
start_time = time.time()
model_input = {key: value.cuda() for key, value in model_input.items() if key != 'ckpt'}
gt = {key: value.cuda() for key, value in gt.items()}
if double_precision:
model_input = {key: value.double() for key, value in model_input.items()}
gt = {key: value.double() for key, value in gt.items()}
if use_lbfgs:
def closure():
optim.zero_grad()
model_output = model(model_input)
losses = loss_fn(model_output, gt)
train_loss = 0.
for loss_name, loss in losses.items():
train_loss += loss.mean()
train_loss.backward()
return train_loss
optim.step(closure)
model_output = model(model_input)
losses = loss_fn(model_output, gt)
train_loss = 0.
for loss_name, loss in losses.items():
single_loss = loss.mean()
if loss_schedules is not None and loss_name in loss_schedules:
writer.add_scalar(loss_name + "_weight", loss_schedules[loss_name](total_steps), total_steps)
single_loss *= loss_schedules[loss_name](total_steps)
writer.add_scalar(loss_name, single_loss, total_steps)
train_loss += single_loss
train_losses.append(train_loss.item())
writer.add_scalar("total_train_loss", train_loss, total_steps)
if not total_steps % steps_til_summary:
torch.save(model.state_dict(),
os.path.join(checkpoints_dir, 'model_current.pth'))
if not use_lbfgs:
optim.zero_grad()
train_loss.backward()
if clip_grad:
if isinstance(clip_grad, bool):
torch.nn.utils.clip_grad_value_(model.parameters(), clip_value=1.)
else:
torch.nn.utils.clip_grad_value_(model.parameters(), clip_value=1.)
optim.step()
pbar.update(1)
if not total_steps % steps_til_summary:
tqdm.write("Epoch %d, Total loss %0.6f, iteration time %0.6f" % (epoch, train_loss, time.time() - start_time))
if val_dataloader is not None:
print("Running validation set...")
model.eval()
val_losses = []
val_out = []
gt_ = []
for (model_input, gt) in tqdm(val_dataloader):
model_input = {key: value.cuda() for key, value in model_input.items() if key != 'ckpt'}
gt = {key: value.cuda() for key, value in gt.items()}
model_output = model(model_input)
val_out.append(model_output['new_img'].detach())
gt_.append(gt['img'].detach())
val_loss = loss_fn(model_output, gt)
val_losses.append(val_loss)
val_out = torch.cat(val_out, 0)
gt_ = torch.cat(gt_, 0)
writer.add_image("out_img", dataio.rescale_img(val_out.view(sz, sz, 3), mode='clamp'), total_steps, dataformats='HWC')
writer.add_image("gt_img", dataio.rescale_img(gt_.view(sz, sz, 3), mode='clamp'), total_steps, dataformats='HWC')
model.train()
total_steps += 1
torch.save(model.state_dict(),
os.path.join(checkpoints_dir, 'model_final.pth'))
np.savetxt(os.path.join(checkpoints_dir, 'train_losses_final.txt'),
np.array(train_losses))
class LinearDecaySchedule():
def __init__(self, start_val, final_val, num_steps):
self.start_val = start_val
self.final_val = final_val
self.num_steps = num_steps
def __call__(self, iter):
return self.start_val + (self.final_val - self.start_val) * min(iter / self.num_steps, 1.)