-
Notifications
You must be signed in to change notification settings - Fork 11
/
train.py
348 lines (294 loc) · 10.1 KB
/
train.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
Primary training and evaluation script. Run ``python3 train.py -h`` to see available
options.
"""
from datetime import datetime
from glob import glob
import os
import random
import shutil
import importlib
import plac
import time
import signal
import sys
import wandb
import numpy as np
import torch
import pytorch_lightning as pl
from config import get_hparams, create_config
# from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_lightning.loggers import WandbLogger
# Setting seeds to ensure reproducibility. Setting CUDA to deterministic mode slows down
# the training.
SEED = 2334
torch.manual_seed(SEED)
# torch.backends.cudnn.deterministic = True
if torch.cuda.is_available():
torch.cuda.manual_seed_all(SEED)
np.random.seed(SEED)
random.seed(SEED)
def main(hparams):
"""
Main training routine specific for this project
:param hparams: Namespace containing configuration values
:type hparams: Namespace
"""
# ------------------------
# 1 INIT MODEL
# ------------------------
# Prepare model and link it with the data
model = get_model(hparams)
# Categorize logging
name = hparams.model + "-" + hparams.out
# Callback to save checkpoint of best performing model
checkpoint_callback = pl.callbacks.model_checkpoint.ModelCheckpoint(
filepath=f"src/model/checkpoints/{name}/",
monitor="val_loss",
verbose=True,
save_top_k=1,
save_weights_only=False,
mode="min",
period=1,
prefix="-".join(
[
str(x)
for x in (
name,
hparams.in_days,
hparams.out_days,
datetime.now().strftime("-%m/%d-%H:%M"),
)
]
),
)
# ------------------------
# LOGGING SETUP
# ------------------------
# Enable logging only during training
if not hparams.dry_run:
# tb_logger = TensorBoardLogger(save_dir="logs/tb_logs/", name=name)
# tb_logger.experiment.add_graph(model, model.data[0][0].unsqueeze(0))
wandb_logger = WandbLogger(
name=hparams.comment if hparams.comment else time.ctime(),
project=name,
save_dir="logs",
)
# if not hparams.test:
# wandb_logger.watch(model, log="all", log_freq=100)
wandb_logger.log_hyperparams(model.hparams)
for file in [
i
for s in [
glob(x) for x in ["src/*.py", "src/dataloader/*.py", "src/model/*.py"]
]
for i in s
]:
shutil.copy(file, wandb.run.dir)
# ------------------------
# INIT TRAINER
# ------------------------
trainer = pl.Trainer(
auto_lr_find=False,
# progress_bar_refresh_rate=0,
# Profiling the code to find bottlenecks
# profiler=pl.profiler.AdvancedProfiler('profile'),
max_epochs=hparams.epochs if not hparams.dry_run else 1,
# CUDA trick to speed up training after the first epoch
benchmark=True,
deterministic=False,
# Sanity checks
# fast_dev_run=False,
# overfit_pct=0.01,
gpus=hparams.gpus,
precision=16 if hparams.use_16bit and hparams.gpus else 32,
# Alternative method for 16-bit training
# amp_level="O2",
logger=None if hparams.dry_run else [wandb_logger], # , tb_logger],
checkpoint_callback=None if hparams.dry_run else checkpoint_callback,
# Using maximum GPU memory. NB: Learning rate should be adjusted according to
# the batch size
# auto_scale_batch_size='binsearch',
)
# ------------------------
# LR FINDER
# ------------------------
if hparams.find_lr:
# Run learning rate finder
lr_finder = trainer.lr_find(model)
# Results can be found in
lr_finder.results
# Plot with
fig = lr_finder.plot(suggest=True)
fig.show()
# Pick point based on plot, or get suggestion
new_lr = lr_finder.suggestion()
# update hparams of the model
model.hparams.learning_rate = new_lr
# ------------------------
# BATCH SIZE SEARCH
# ------------------------
if hparams.search_bs:
# Invoke the batch size search using a sophisticated algorithm.
new_batch_size = trainer.scale_batch_size(
model, mode="binary", steps_per_trial=50, init_val=1, max_trials=10
)
# Override old batch size
model.hparams.batch_size = new_batch_size
# ------------------------
# 3 START TRAINING
# ------------------------
# Interrupt training anytime and continue to test
signal.signal(signal.SIGINT or 255, trainer.test)
trainer.fit(model)
results = trainer.test()
return results
def set_hparams(hparams):
"""
Add constant parameter values based on passed arguments.
:param hparams: Parameters
:type hparams: Namespace
:return: Modified parameters
:rtype: Namespace
"""
# Check for CUDA availability if gpus > 0 requested
if hparams.gpus and not torch.cuda.is_available():
hparams.gpus = 0
if hparams.benchmark:
# Use empty model while benchmarking with fwi-forecast
hparams.model = "base_model"
hparams.out = "fwi_reanalysis"
hparams.eval = True
hparams.gpus = 0
if hparams.case_study:
case_studies = importlib.import_module("data.consts.case_study").case_studies
hparams.case_study_dates = case_studies[hparams.case_study]
hparams.mask = f"src/dataloader/mask/{hparams.case_study}_mask.npy"
from data.consts.forcing_stats import (
FORCING_STD_TP,
FORCING_STD_T2,
FORCING_STD_WSPEED,
FORCING_STD_RH,
FORCING_MEAN_WSPEED,
FORCING_MEAN_TP,
FORCING_MEAN_T2,
FORCING_MEAN_RH,
)
hparams.inp_mean = {
"wspeed": FORCING_MEAN_WSPEED,
"tp": FORCING_MEAN_TP,
"t2": FORCING_MEAN_T2,
"rh": FORCING_MEAN_RH,
}
hparams.inp_std = {
"wspeed": FORCING_STD_WSPEED,
"tp": FORCING_STD_TP,
"t2": FORCING_STD_T2,
"rh": FORCING_STD_RH,
}
if hparams.smos_input:
from data.consts.soil_moisture_stats import (
SOIL_MOISTURE_MEAN,
SOIL_MOISTURE_STD,
)
hparams.smos_mean = SOIL_MOISTURE_MEAN
hparams.smos_std = SOIL_MOISTURE_STD
if hparams.out == "fwi_reanalysis":
from data.consts.fwi_reanalysis_stats import (
REANALYSIS_FWI_MEAN,
REANALYSIS_FWI_MAD,
REANALYSIS_FWI_VAR,
)
hparams.out_mean, hparams.out_mad, hparams.out_var = (
REANALYSIS_FWI_MEAN,
REANALYSIS_FWI_MAD,
REANALYSIS_FWI_VAR,
)
elif hparams.out == "gfas_frp":
from data.consts.frp_stats import (
FRP_MEAN,
FRP_MAD,
FRP_VAR,
BOX_COX_FRP_MEAN,
BOX_COX_FRP_MAD,
BOX_COX_FRP_VAR,
BOX_COX_LAMBDA,
)
hparams.out_mean, hparams.out_mad, hparams.out_var = (
BOX_COX_FRP_MEAN if hparams.boxcox else FRP_VAR,
BOX_COX_FRP_MAD if hparams.boxcox else FRP_MAD,
BOX_COX_FRP_VAR if hparams.boxcox else FRP_MEAN,
)
if hparams.boxcox and not (type(hparams.boxcox) == type(bool)):
hparams.boxcox = BOX_COX_LAMBDA
if hparams.cb_loss and hparams.out == "fwi_reanalysis":
from data.consts.reanalysis_freq import bin_centers, freq
# Not allow zero frequency for numerical stability
freq[freq == 0] = 1
hparams.bin_centers = bin_centers
hparams.loss_factors = (1 - hparams.cb_loss) / (1 - hparams.cb_loss ** freq)
hparams.loss_factors = (
hparams.loss_factors
/ hparams.loss_factors.sum()
* hparams.loss_factors.size
)
assert (
hparams.bin_centers.shape == hparams.loss_factors.shape
), "The number of bin-centers for corresponding frequencies must be the same"
return hparams
def get_model(hparams):
"""
Prepare model and the data.
:param hparams: Holds configuration values.
:type hparams: Namespace
:raises ImportError: The requested model and prediction data must be compatible.
:return: Model with the linked data.
:rtype: Model
"""
sys.path += ["../", "."]
# Update hparams with the constants
set_hparams(hparams)
if hparams.model in ["base_model"]:
Model = importlib.import_module(f"model.{hparams.model}").BaseModel
if hparams.out == "fwi_reanalysis":
ModelDataset = importlib.import_module(
f"dataloader.{hparams.out}"
).ModelDataset
ModelDataset.BenchmarkDataset = importlib.import_module(
"dataloader.fwi_forecast"
).ModelDataset
else:
Model = importlib.import_module(f"model.{hparams.model}").Model
if hparams.model in ["unet"]:
if hparams.out == "fwi_forecast":
ModelDataset = importlib.import_module(
f"dataloader.{hparams.out}"
).ModelDataset
elif hparams.model in [
"unet_downsampled",
"unet_snipped",
"unet_tapered",
]:
if hparams.out == "fwi_reanalysis":
ModelDataset = importlib.import_module(
f"dataloader.{hparams.out}"
).ModelDataset
elif hparams.model in ["unet_interpolated"]:
if hparams.out == "gfas_frp":
ModelDataset = importlib.import_module(
f"dataloader.{hparams.out}"
).ModelDataset
else:
raise ImportError(f"{hparams.model} and {hparams.out} combination invalid.")
model = Model(hparams).to("cuda" if hparams.gpus else "cpu")
model.prepare_data(ModelDataset)
return model
if __name__ == "__main__":
"""
Script entrypoint.
"""
hparams=create_config()
# ---------------------
# RUN TRAINING
# ---------------------
main(hparams)