-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.py
223 lines (200 loc) · 9.3 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 18 17:59:23 2020
@author: rfablet
"""
import os
import numpy as np
import pytorch_lightning as pl
import torch
from omegaconf import OmegaConf
from pytorch_lightning.callbacks import ModelCheckpoint
class FourDVarNetRunner:
def __init__(self, dataloading="old", config=None):
from lit_model import LitModel
from lit_model_sst import LitModelWithSST
from lit_model_stochastic import LitModelStochastic
from new_dataloading import FourDVarNetDataModule
from old_dataloading import LegacyDataLoading
self.filename_chkpt = 'modelSLAInterpGF-Exp3-{epoch:02d}-{val_loss:.2f}'
if config is None:
import config
else:
import importlib
print('loading config')
config = importlib.import_module("config_" + str(config))
self.callbacks = []
self.logger = True
self.cfg = OmegaConf.create(config.params)
print(OmegaConf.to_yaml(self.cfg))
dataloading = config.params['dataloading']
dim_range = config.dim_range
slice_win = config.slice_win
strides = config.strides
time_period = config.time_period
if dataloading == "old":
datamodule = LegacyDataLoading(self.cfg)
else:
datamodule = FourDVarNetDataModule(
slice_win=slice_win,
dim_range=dim_range,
strides=strides,
train_slices=config.time_period['train_slices'],
test_slices=config.time_period['test_slices'],
val_slices=config.time_period['val_slices'],
resize_factor=config.params['resize_factor'],
**config.params['files_cfg']
)
datamodule.setup()
self.dataloaders = {
'train': datamodule.train_dataloader(),
'val': datamodule.val_dataloader(),
'test': datamodule.test_dataloader(),
}
if dataloading == "old":
self.var_Tr = datamodule.var_Tr
self.var_Tt = datamodule.var_Tt
self.var_Val = datamodule.var_Val
self.mean_Tr = datamodule.mean_Tr
self.mean_Tt = datamodule.mean_Tt
self.mean_Val = datamodule.mean_Val
self.min_lon, self.max_lon, self.min_lat, self.max_lat = -65, -55, 33, 43
self.ds_size_time = 20
self.ds_size_lon = 1
self.ds_size_lat = 1
else:
self.setup(datamodule=datamodule)
if config.params['stochastic'] == False:
self.lit_cls = LitModelWithSST if dataloading == "with_sst" else LitModel
else:
self.lit_cls = LitModelStochastic
self.time = config.time
def setup(self, datamodule):
self.mean_Tr = datamodule.norm_stats[0]
self.mean_Tt = datamodule.norm_stats[0]
self.mean_Val = datamodule.norm_stats[0]
self.var_Tr = datamodule.norm_stats[1] ** 2
self.var_Tt = datamodule.norm_stats[1] ** 2
self.var_Val = datamodule.norm_stats[1] ** 2
self.min_lon = datamodule.dim_range['lon'].start
self.max_lon = datamodule.dim_range['lon'].stop
self.min_lat = datamodule.dim_range['lat'].start
self.max_lat = datamodule.dim_range['lat'].stop
self.ds_size_time = datamodule.ds_size['time']
self.ds_size_lon = datamodule.ds_size['lon']
self.ds_size_lat = datamodule.ds_size['lat']
self.dX = int((datamodule.slice_win['lon']-datamodule.strides['lon'])/2)
self.dY = int((datamodule.slice_win['lat']-datamodule.strides['lat'])/2)
self.swX = datamodule.slice_win['lon']
self.swY = datamodule.slice_win['lat']
self.lon, self.lat = datamodule.coordXY()
w_ = np.zeros(self.cfg.dT)
w_[int(self.cfg.dT / 2)] = 1.
self.wLoss = torch.Tensor(w_)
def run(self, ckpt_path=None, dataloader="test", **trainer_kwargs):
"""
Train and test model and run the test suite
:param ckpt_path: (Optional) Checkpoint from which to resume
:param dataloader: Dataloader on which to run the test Checkpoint from which to resume
:param trainer_kwargs: (Optional)
"""
mod, trainer = self.train(ckpt_path, **trainer_kwargs)
self.test(dataloader=dataloader, _mod=mod, _trainer=trainer)
def _get_model(self, ckpt_path=None):
"""
Load model from ckpt_path or instantiate new model
:param ckpt_path: (Optional) Checkpoint path to load
:return: lightning module
"""
print('get_model: ', ckpt_path)
if ckpt_path:
mod = self.lit_cls.load_from_checkpoint(ckpt_path, hparam=self.cfg, w_loss=self.wLoss, strict=False,
mean_Tr=self.mean_Tr, mean_Tt=self.mean_Tt, mean_Val=self.mean_Val,
var_Tr=self.var_Tr, var_Tt=self.var_Tt, var_Val=self.var_Val,
min_lon=self.min_lon, max_lon=self.max_lon,
min_lat=self.min_lat, max_lat=self.max_lat,
ds_size_time=self.ds_size_time,
ds_size_lon=self.ds_size_lon,
ds_size_lat=self.ds_size_lat,
time=self.time,
dX = self.dX, dY = self.dY,
swX = self.swX, swY = self.swY,
coord_ext = {'lon_ext': self.lon, 'lat_ext': self.lat}
)
else:
mod = self.lit_cls(hparam=self.cfg, w_loss=self.wLoss,
mean_Tr=self.mean_Tr, mean_Tt=self.mean_Tt, mean_Val=self.mean_Val,
var_Tr=self.var_Tr, var_Tt=self.var_Tt, var_Val=self.var_Val,
min_lon=self.min_lon, max_lon=self.max_lon,
min_lat=self.min_lat, max_lat=self.max_lat,
ds_size_time=self.ds_size_time,
ds_size_lon=self.ds_size_lon,
ds_size_lat=self.ds_size_lat,
time=self.time,
dX = self.dX, dY = self.dY,
swX = self.swX, swY = self.swY,
coord_ext = {'lon_ext': self.lon, 'lat_ext': self.lat}
)
return mod
def train(self, ckpt_path=None, **trainer_kwargs):
"""
Train a model
:param ckpt_path: (Optional) Checkpoint from which to resume
:param trainer_kwargs: (Optional) Trainer arguments
:return:
"""
mod = self._get_model(ckpt_path=ckpt_path)
checkpoint_callback = ModelCheckpoint(monitor='val_loss',
filename=self.filename_chkpt,
save_top_k=1,
mode='min')
from pytorch_lightning.callbacks import LearningRateMonitor
lr_monitor = LearningRateMonitor(logging_interval='step')
num_nodes = int(os.environ.get('SLURM_JOB_NUM_NODES', 1))
num_gpus = torch.cuda.device_count()
accelerator = "ddp" if (num_gpus * num_nodes) > 1 else None
trainer = pl.Trainer(logger=self.logger, num_nodes=num_nodes, gpus=num_gpus, accelerator=accelerator, auto_select_gpus=(num_gpus * num_nodes) > 0,
callbacks=[checkpoint_callback, lr_monitor] + self.callbacks, **trainer_kwargs)
trainer.fit(mod, self.dataloaders['train'], self.dataloaders['val'])
return mod, trainer
def test(self, ckpt_path=None, dataloader="test", _mod=None, _trainer=None, **trainer_kwargs):
"""
Test a model
:param ckpt_path: (Optional) Checkpoint from which to resume
:param dataloader: Dataloader on which to run the test Checkpoint from which to resume
:param trainer_kwargs: (Optional)
"""
mod = _mod or self._get_model(ckpt_path=ckpt_path)
trainer = pl.Trainer(num_nodes=1, gpus=1, accelerator=None, **trainer_kwargs)
trainer.test(mod, test_dataloaders=self.dataloaders[dataloader])
return mod
def profile(self):
"""
Run the profiling
:return:
"""
from pytorch_lightning.profiler import PyTorchProfiler
profiler = PyTorchProfiler(
"results/profile_report",
schedule=torch.profiler.schedule(
wait=1,
warmup=1,
active=1),
activities=[
torch.profiler.ProfilerActivity.CPU,
torch.profiler.ProfilerActivity.CUDA,
],
on_trace_ready=torch.profiler.tensorboard_trace_handler('./tb_profile'),
record_shapes=True,
profile_memory=True,
)
self.train(
**{
'profiler': profiler,
'max_epochs': 1,
}
)
if __name__ == '__main__':
import fire
fire.Fire(FourDVarNetRunner)