-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
256 lines (226 loc) · 9.97 KB
/
main.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
from YParams import YParams
import wandb
import argparse
import torch
import numpy as np
from data_utils.data_loaders import *
from data_utils.data_utils import MaskerNonuniformMesh, get_meshes
from layers.variable_encoding import *
from models.get_models import *
from train.trainer import trainer
from utils import *
from models.model_helpers import count_parameters
from test.evaluations import missing_variable_testing
import random
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--exp", nargs="?", default="FSI", type=str)
parser.add_argument("--config", nargs="?", default="base_config", type=str)
parser.add_argument("--ntrain", nargs="?", default=None, type=int)
parser.add_argument("--epochs", nargs="?", default=None, type=int)
parser.add_argument("--random_seed", nargs="?", default=42, type=int)
parser.add_argument("--scheduler_step", nargs="?", default=None, type=int)
parser.add_argument("--batch_size", nargs="?", default=None, type=int)
parsed_args = parser.parse_args()
if parsed_args.exp == "FSI":
config_file = './config/ssl_ns_elastic.yaml'
elif parsed_args.exp == "RB":
config_file = './config/RB_config.yaml'
else:
raise ValueError("Unknown experiment type")
config = parsed_args.config
print("Loading config", config)
params = YParams(config_file, config, print_params=True)
if parsed_args.ntrain is not None:
params.ntrain = parsed_args.ntrain
print("Overriding ntrain to", params.ntrain)
if parsed_args.random_seed is not None:
params.random_seed = parsed_args.random_seed
print("Overriding random seed to", params.random_seed)
if parsed_args.epochs is not None:
params.epochs = parsed_args.epochs
print("Overriding epochs to", params.epochs)
if parsed_args.scheduler_step is not None:
params.scheduler_step = parsed_args.scheduler_step
print("Overriding scheduler step to", params.scheduler_step)
if parsed_args.batch_size is not None:
params.batch_size = parsed_args.batch_size
print("Overriding batch size to", params.batch_size)
torch.manual_seed(params.random_seed)
random.seed(params.random_seed)
np.random.seed(params.random_seed)
params.config = config
# Set up WandB logging
params.wandb_name = config
params.wandb_group = params.nettype
if params.wandb_log:
wandb.login(key=get_wandb_api_key())
wandb.init(
config=params,
name=params.wandb_name,
group=params.wandb_group,
project=params.wandb_project,
entity=params.wandb_entity)
# stage of training: reconstructive (Self supervised traning)
# or predictive (Supervised training either finetuning or from scratch)
if params.pretrain_ssl:
stage = StageEnum.RECONSTRUCTIVE
else:
stage = StageEnum.PREDICTIVE
variable_encoder = None
token_expander = None
if params.nettype == 'transformer':
if params.grid_type == 'uniform':
encoder, decoder, contrastive, predictor = get_ssl_models_codano(
params)
input_mesh = None
else:
encoder, decoder, contrastive, predictor = get_ssl_models_codano_gino(
params)
if params.use_variable_encoding:
variable_encoder = get_variable_encoder(params)
token_expander = TokenExpansion(sum([params.equation_dict[i] for i in params.equation_dict.keys(
)]), params.n_encoding_channels, params.n_static_channels, params.grid_type == 'uniform')
print("Parameters Encoder", count_parameters(encoder), "x10^6")
print("Parameters Decoder", count_parameters(decoder), "x10^6")
print("Parameters Perdictor", count_parameters(predictor), "x10^6")
if params.wandb_log:
wandb.log(
{'Encoder #parameters': count_parameters(encoder)}, commit=True)
wandb.log(
{'Decoder #parameters': count_parameters(decoder)}, commit=True)
wandb.log(
{'Predictor #parameters': count_parameters(predictor)}, commit=True)
model = SSLWrapper(
params,
encoder,
decoder,
contrastive,
predictor,
stage=stage)
if params.grid_type != 'uniform':
print("Setting the Grid")
mesh = get_mesh(params)
input_mesh = torch.from_numpy(mesh).type(torch.float).cuda()
model.set_initial_mesh(input_mesh)
elif params.nettype in ['simple', 'gnn', 'deeponet', 'vit', 'unet', 'fno']:
model = get_baseline_model(params)
print("Parameters Model", count_parameters(model), "x10^6")
wandb.log({'Model #parameters': count_parameters(model)}, commit=True)
input_mesh = None
print("PDE list", *list(params.equation_dict.keys()))
if parsed_args.exp == 'FSI':
# loading Fluid Stucture Interaction dataset
dataset = NsElasticDataset(
params.data_location,
equation=list(params.equation_dict.keys()),
mesh_location=params.input_mesh_location,
params=params)
train, test = dataset.get_dataloader(params.mu_list, params.dt, ntrain=params.get(
'ntrain'), ntest=params.get('ntest'), sample_per_inlet=params.sample_per_inlet)
elif parsed_args.exp == 'RB':
# loading Rayleigh-Benard dataset
train, test = get_RB_dataloader(params)
if getattr(params, 'evaluate_only', False):
# setting satge to predictive for evaluation
# load model weights
stage = StageEnum.PREDICTIVE
model.load_state_dict(torch.load(params.model_path), strict=False)
if params.nettype == 'transformer' and params.use_variable_encoding:
if "NS" in params.equation_dict.keys():
print("Loading NS variable encoder")
variable_encoder.load_encoder(
"NS", params.NS_variable_encoder_path)
if "ES" in params.equation_dict.keys() and params.ES_variable_encoder_path is not None:
print("Loading ES variable encoder")
variable_encoder.load_encoder(
"ES", params.ES_variable_encoder_path)
if "T" in params.equation_dict.keys() and params.T_variable_encoder_path is not None:
print("Loading T variable encoder")
variable_encoder.load_encoder(
"T", params.T_variable_encoder_path)
if params.freeze_encoder:
variable_encoder.freeze("T")
elif params.training_stage == 'fine_tune':
# load only encooder and vaariable encoder weights (VSPE)
print(f"Loading Pretrained weights from {params.pretrain_weight}")
model.encoder.load_state_dict(torch.load(
params.pretrain_weight), strict=True)
if params.use_variable_encoding:
print(
f"Loading Pretrained weights from {params.NS_variable_encoder_path}")
if "NS" in params.equation_dict.keys():
print("Loading NS variable encoder")
variable_encoder.load_encoder(
"NS", params.NS_variable_encoder_path)
if params.freeze_encoder:
variable_encoder.freeze("NS")
if "ES" in params.equation_dict.keys() and params.ES_variable_encoder_path is not None:
print("Loading ES variable encoder")
variable_encoder.load_encoder(
"ES", params.ES_variable_encoder_path)
if params.freeze_encoder:
variable_encoder.freeze("ES")
if "T" in params.equation_dict.keys() and params.T_variable_encoder_path is not None:
print("Loading T variable encoder")
variable_encoder.load_encoder(
"T", params.T_variable_encoder_path)
if params.freeze_encoder:
variable_encoder.freeze("T")
# Move model and encoders to GPU
model = model.cuda()
if variable_encoder is not None:
variable_encoder.cuda()
if token_expander is not None:
token_expander.cuda()
if not getattr(params, 'evaluate_only', False):
# Train the model
trainer(
model,
train,
test,
params,
wandb_log=params.wandb_log,
log_test_interval=params.wandb_log_test_interval,
stage=stage,
variable_encoder=variable_encoder,
token_expander=token_expander,
initial_mesh=input_mesh)
if getattr(params, 'missing_var_test', False):
# evaluate on missing variables and partially observed
# variabless
grid_non, grid_uni = get_meshes(
params, params.grid_size)
test_augmenter = MaskerNonuniformMesh(
grid_non_uni=grid_non.clone().detach(),
gird_uni=grid_uni.clone().detach(),
radius=params.masking_radius,
drop_type=params.drop_type,
drop_pix=params.drop_pix_val,
channel_aug_rate=params.channel_per_val,
channel_drop_rate=params.channel_drop_per_val,
verbose=True)
missing_variable_testing(
model,
test,
test_augmenter,
'sl',
params,
variable_encoder=variable_encoder,
token_expander=token_expander,
initial_mesh=input_mesh)
else:
# Evaluate the model on the
# unaugmentted the test set
missing_variable_testing(
model,
test,
augmenter=None,
normalizer=None,
stage=stage,
params=params,
variable_encoder=variable_encoder,
token_expander=token_expander,
initial_mesh=input_mesh)
if params.wandb_log:
wandb.finish()