-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_pino.py
executable file
·238 lines (212 loc) · 9 KB
/
train_pino.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
from datetime import datetime
import os
import yaml
import random
from argparse import ArgumentParser
import math
import torch
from tqdm import tqdm
import numpy as np
from torch.optim import Adam
from torch.utils.data import DataLoader
from libs.models.pino_models import PINObserver2d
from libs.pino_utils.losses import LpLoss, PINO_loss3d, get_forcing
from libs.envs.diff_control_env import Channelflow_PINO_loss
from libs.pino_utils.datasets import MultipleReynoldsKFaDataset, sample_data
from libs.pino_utils.utils import save_ckpt, count_params, dict2str
try:
import wandb
except ImportError:
wandb = None
@torch.no_grad()
def eval_ns(model, val_loader, criterion, device):
model.eval()
val_err = []
for u, a, re in val_loader:
u, a, re = u.to(device), a.to(device), re.to(device)
out = model(a, re)
val_loss = criterion(out, u)
val_err.append(val_loss.item())
N = len(val_loader)
avg_err = np.mean(val_err)
std_err = np.std(val_err, ddof=1) / np.sqrt(N)
return avg_err, std_err
def train_ns(model,
train_u_loader, # training data
val_loader, # validation data
optimizer,
scheduler,
device, config, args):
start_iter = config['train']['start_iter']
t_duration = config['data']['t_duration']
save_step = config['train']['save_step']
eval_step = config['train']['eval_step']
ic_weight = config['train']['ic_loss']
f_weight = config['train']['f_loss']
xy_weight = config['train']['xy_loss']
# set up directory
base_dir = os.path.join('exp', config['log']['logdir'])
ckpt_dir = os.path.join(base_dir, 'ckpts')
os.makedirs(ckpt_dir, exist_ok=True)
# loss fn
lploss = LpLoss(size_average=True)
S = config['data']['pde_res'][0]
forcing = get_forcing(S).to(device)
# set up wandb
if wandb and args.log:
run = wandb.init(project=config['log']['project'],
entity=config['log']['entity'],
group=config['log']['group'],
config=config, reinit=True,
settings=wandb.Settings(start_method='fork'))
pbar = range(start_iter, config['train']['num_iter'])
if args.tqdm:
pbar = tqdm(pbar, dynamic_ncols=True, smoothing=0.2)
u_loader = sample_data(train_u_loader)
for e in pbar:
log_dict = {}
optimizer.zero_grad()
u, a_in, re = next(u_loader)
u = u.to(device)
a_in = a_in.to(device) # 1, 128, 128, 65, 4
re = re.to(device)
# data loss
if xy_weight > 0:
out = model(a_in, re)
data_loss = lploss(out, u)
else:
data_loss = torch.zeros(1, device=device)
if f_weight != 0.0:
# pde loss
# a = next(a_loader)
a = a_in.to(device)
re = re.to(device)
out = model(a, re)
u0 = a[:, :, :, 0, -1]
v = 1 / re
loss_ic, loss_f = Channelflow_PINO_loss(out, u0, forcing, v, t_duration)
# loss_ic, loss_f = PINO_loss3d(out, u0, forcing, v, t_duration)
log_dict['IC'] = loss_ic.item()
log_dict['PDE'] = loss_f.item()
else:
loss_ic = loss_f = 0.0
loss = data_loss * xy_weight + loss_f * f_weight + loss_ic * ic_weight
loss.backward()
optimizer.step()
scheduler.step()
log_dict['train loss'] = loss.item()
log_dict['data'] = data_loss.item()
if e % eval_step == 0:
eval_err, std_err = eval_ns(model, val_loader, lploss, device)
log_dict['val error'] = eval_err
if args.tqdm:
logstr = dict2str(log_dict)
pbar.set_description(
(
logstr
)
)
if wandb and args.log:
wandb.log(log_dict)
if e % save_step == 0 and e > 0:
ckpt_path = os.path.join(ckpt_dir, f'model-{e}.pt')
save_ckpt(ckpt_path, model, optimizer, scheduler)
if e == 101: # log for short epoch validation
eval_err, std_err = eval_ns(model, val_loader, lploss, device)
print(f"iter: {e}, data loss: {data_loss.item()}, train loss: {loss.item()}, eval loss: {eval_err}.\n")
# clean up wandb
if wandb and args.log:
run.finish()
def subprocess(args):
with open(args.config, 'r') as f:
config = yaml.load(f, yaml.FullLoader)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# set random seed
config['seed'] = args.seed
seed = args.seed
torch.manual_seed(seed)
random.seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
# create model
model = PINObserver2d(modes1=config['model']['modes1'],
modes2=config['model']['modes2'],
modes3=config['model']['modes3'],
fc_dim=config['model']['fc_dim'],
layers=config['model']['layers'],
act=config['model']['act'],
pad_ratio=config['model']['pad_ratio']).to(device)
num_params = count_params(model)
config['num_params'] = num_params
print(f'Number of parameters: {num_params}')
# Load from checkpoint
if args.ckpt:
ckpt_path = args.ckpt
ckpt = torch.load(ckpt_path)
model.load_state_dict(ckpt['model'])
print('Weights loaded from %s' % ckpt_path)
if args.test:
batchsize = config['test']['batchsize']
testset = KFDataset(paths=config['data']['paths'],
raw_res=config['data']['raw_res'],
data_res=config['test']['data_res'],
pde_res=config['test']['data_res'],
n_samples=config['data']['n_test_samples'],
offset=config['data']['testoffset'],
t_duration=config['data']['t_duration'])
testloader = DataLoader(testset, batch_size=batchsize, num_workers=4)
criterion = LpLoss()
test_err, std_err = eval_ns(model, testloader, criterion, device)
print(f'Averaged test relative L2 error: {test_err}; Standard error: {std_err}')
else:
# training set
batchsize = config['train']['batchsize']
u_set = MultipleReynoldsKFaDataset(paths=config['data']['train_paths'],
raw_res=config['data']['raw_res'],
data_res=config['data']['data_res'],
pde_res=config['data']['data_res'],
n_samples=config['data']['n_data_samples'],
offset=config['data']['offset'],
t_duration=config['data']['t_duration'])
u_loader = DataLoader(u_set, batch_size=batchsize, num_workers=4, shuffle=True)
# val set
valset = MultipleReynoldsKFaDataset(paths=config['data']['test_paths'],
raw_res=config['data']['raw_res'],
data_res=config['test']['data_res'],
pde_res=config['test']['data_res'],
n_samples=config['data']['n_test_samples'],
offset=config['data']['testoffset'],
t_duration=config['data']['t_duration'])
val_loader = DataLoader(valset, batch_size=batchsize, num_workers=4)
print(f'Test set num: {len(valset)}; IC set size: {len(u_set)}')
optimizer = Adam(model.parameters(), lr=config['train']['base_lr'])
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer,
milestones=config['train']['milestones'],
gamma=config['train']['scheduler_gamma'])
if args.ckpt:
ckpt = torch.load(ckpt_path)
optimizer.load_state_dict(ckpt['optim'])
scheduler.load_state_dict(ckpt['scheduler'])
config['train']['start_iter'] = scheduler.last_epoch
import pdb; pdb.set_trace()
train_ns(model,
u_loader,
val_loader,
optimizer, scheduler,
device,
config, args)
print('Done!')
if __name__ == '__main__':
torch.backends.cudnn.benchmark = True
# parse options
parser = ArgumentParser(description='Basic paser')
parser.add_argument('--config', type=str, help='Path to the configuration file')
parser.add_argument('--log', action='store_true', help='Turn on the wandb')
parser.add_argument('--seed', type=int, default=0)
parser.add_argument('--ckpt', type=str, default=None)
parser.add_argument('--test', action='store_true', help='Test')
parser.add_argument('--tqdm', action='store_true', help='Turn on the tqdm')
args = parser.parse_args()
if args.seed is None:
args.seed = random.randint(0, 100000)
subprocess(args)