-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
517 lines (456 loc) · 15.5 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
"""
Primary training and evaluation script. Run ``python3 train.py -h`` to see available
options.
"""
import os
from argparse import Namespace
import random
from datetime import datetime
from glob import glob
import shutil
import importlib
import plac
import time
import signal
import sys
import numpy as np
import torch
import pytorch_lightning as pl
# from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_lightning import _logger as log
from pytorch_lightning.loggers import WandbLogger
import wandb
# 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
def str2num(s):
"""
Converts parameter strings to appropriate types.
:param s: Parameter value
:type s: str
:return: Appropriately converted value
:rtype: Varying
"""
if isinstance(s, bool):
return s
s = str(s)
if "," in s:
return [str2num(i) for i in s.split(",")]
if "." in s or "e-" in s:
try:
return float(s)
except:
pass
elif s.isdigit():
return int(s)
elif s.lower() == "inf":
return float("inf")
elif s.lower() == "none":
return None
else:
if s.lower() == "true":
return True
elif s.lower() == "false":
return False
return s
def get_hparams(
#
# U-Net config
init_features: ("Architecture complexity [int]", "option") = 16,
in_days: ("Number of input days [int]", "option") = 2,
out_days: ("Number of output days [int]", "option") = 1,
#
# General
epochs: ("Number of training epochs [int]", "option") = 100,
learning_rate: ("Maximum learning rate [float]", "option") = 1e-3,
batch_size: ("Batch size of the input [int]", "option") = 1,
split: ("Test split fraction [float]", "option") = 0.2,
use_16bit: ("Use 16-bit precision for training (train only)", "option") = True,
gpus: ("Number of GPUs to use [int]", "option") = 1,
optim: (
"Learning rate optimizer: one_cycle or cosine (train only) [str]",
"option",
) = "one_cycle",
dry_run: ("Use small amount of data for sanity check [Bool]", "option") = False,
find_lr: (
"Automatically search for an ideal learning rate [Bool]",
"option",
) = False,
search_bs: (
"Scale the batch dynamically for full GPU usage [Bool]",
"option",
) = False,
case_study: (
"The case-study region to use for inference: australia, california, portugal,"
" siberia, chile, uk [Bool/str]",
"option",
) = False,
clip_output: (
"Limit the inference to the datapoints within supplied range (e.g. 0.5,60) "
"[Bool/list]",
"option",
) = False,
boxcox: (
"Apply boxcox transformation with specified lambda while training and the "
"inverse boxcox transformation during the inference. [Bool/float]",
"option",
) = 0.1182,
binned: (
"Show the extended metrics for supplied comma separated binned FWI value range "
"(e.g. 0,15,70) [Bool/list]",
"option",
) = "0,5.2,11.2,21.3,38.0,50",
round_to_zero: (
"Round off the target values below the specified threshold to zero "
"[Bool/float]",
"option",
) = False,
isolate_frp: (
"Exclude the isolated datapoints with FRP > 0 [Bool]",
"option",
) = False,
date_range: (
"Filter the data with specified date range in YYYY-MM-DD format. E.g. "
"2019-04-01,2019-05-01 "
"[Bool/str]",
"option",
) = False,
cb_loss: (
"Use Class-Balanced loss with the supplied beta parameter [Bool/float]",
"option",
) = False,
chronological_split: (
"Do chronological train-test split in the specified ratio [Bool/float]",
"option",
) = False,
undersample: (
"Undersample the datapoints with smaller than specified FWI [Bool/float]",
"option",
) = False,
#
# Run specific
model: (
"Model to use: unet, unet_downsampled, unet_snipped, unet_tapered,"
" unet_interpolated [str]",
"option",
) = "unet_tapered",
out: (
"Output data for training: fwi_reanalysis or gfas_frp [str]",
"option",
) = "fwi_reanalysis",
benchmark: (
"Benchmark the FWI-Forecast data against FWI-Reanalysis [Bool]",
"option",
) = False,
smos_input: ("Use soil-moisture input data [Bool]", "option") = "False",
forecast_dir: (
"Directory containing the forecast data. Alternatively set $FORECAST_DIR [str]",
"option",
) = os.environ.get("FORECAST_DIR"),
forcings_dir: (
"Directory containing the forcings data Alternatively set $FORCINGS_DIR [str]",
"option",
) = os.environ.get("FORCINGS_DIR"),
smos_dir: (
"Directory containing the soil-moisture data Alternatively set $SMOS_DIR [str]",
"option",
) = os.environ.get("SMOS_DIR"),
reanalysis_dir: (
"Directory containing the reanalysis data. Alternatively set $REANALYSIS_DIR. "
"[str]",
"option",
) = os.environ.get("REANALYSIS_DIR"),
frp_dir: (
"Directory containing the FRP data. Alternatively set $FRP_DIR. [str]",
"option",
) = os.environ.get("FRP_DIR"),
mask: (
"File containing the mask stored as the numpy array [str]",
"option",
) = "src/dataloader/mask/reanalysis_mask.npy",
comment: ("Used for logging [str]", "option") = False,
checkpoint_file: (
"Path to the test model checkpoint [Bool/str]",
"option",
) = False,
):
"""
Process and print the dictionary of project wide arguments.
:return: Dictionary containing configuration options.
:rtype: dict
"""
d = {k: str2num(v) for k, v in locals().items()}
for k, v in d.items():
log.info(f" |{k.replace('_', '-'):>20} -> {str(v):<20}")
return d
if __name__ == "__main__":
"""
Script entrypoint.
"""
# Converting dictionary to namespace
hparams = Namespace(**plac.call(get_hparams, eager=False))
# ---------------------
# RUN TRAINING
# ---------------------
main(hparams)