forked from kratzert/ealstm_regional_modeling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
686 lines (565 loc) · 22.7 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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
"""
This file is part of the accompanying code to our manuscript:
Kratzert, F., Klotz, D., Shalev, G., Klambauer, G., Hochreiter, S., Nearing, G., "Benchmarking
a Catchment-Aware Long Short-Term Memory Network (LSTM) for Large-Scale Hydrological Modeling".
submitted to Hydrol. Earth Syst. Sci. Discussions (2019)
You should have received a copy of the Apache-2.0 license along with the code. If not,
see <https://opensource.org/licenses/Apache-2.0>
"""
import argparse
import json
import pickle
import random
import sys
from collections import defaultdict
from datetime import datetime
from pathlib import Path, PosixPath
from typing import Dict, List, Tuple
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from tqdm import tqdm
from papercode.datasets import CamelsH5, CamelsTXT
from papercode.datautils import (add_camels_attributes, load_attributes,
rescale_features)
from papercode.ealstm import EALSTM
from papercode.lstm import LSTM
from papercode.metrics import calc_nse
from papercode.nseloss import NSELoss
from papercode.utils import create_h5_files, get_basin_list
###########
# Globals #
###########
# fixed settings for all experiments
GLOBAL_SETTINGS = {
'batch_size': 256,
'clip_norm': True,
'clip_value': 1,
'dropout': 0.4,
'epochs': 30,
'hidden_size': 256,
'initial_forget_gate_bias': 5,
'log_interval': 50,
'learning_rate': 1e-3,
'seq_length': 270,
'train_start': pd.to_datetime('01101999', format='%d%m%Y'),
'train_end': pd.to_datetime('30092008', format='%d%m%Y'),
'val_start': pd.to_datetime('01101989', format='%d%m%Y'),
'val_end': pd.to_datetime('30091999', format='%d%m%Y')
}
# check if GPU is available
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
###############
# Prepare run #
###############
def get_args() -> Dict:
"""Parse input arguments
Returns
-------
dict
Dictionary containing the run config.
"""
parser = argparse.ArgumentParser()
parser.add_argument('mode', choices=["train", "evaluate", "eval_robustness"])
parser.add_argument('--camels_root', type=str, help="Root directory of CAMELS data set")
parser.add_argument('--seed', type=int, required=False, help="Random seed")
parser.add_argument('--run_dir', type=str, help="For evaluation mode. Path to run directory.")
parser.add_argument('--cache_data',
type=bool,
default=False,
help="If True, loads all data into memory")
parser.add_argument('--num_workers',
type=int,
default=12,
help="Number of parallel threads for data loading")
parser.add_argument('--no_static',
type=bool,
default=False,
help="If True, trains LSTM without static features")
parser.add_argument('--concat_static',
type=bool,
default=False,
help="If True, train LSTM with static feats concatenated at each time step")
parser.add_argument('--use_mse',
type=bool,
default=False,
help="If True, uses mean squared error as loss function.")
cfg = vars(parser.parse_args())
# Validation checks
if (cfg["mode"] == "train") and (cfg["seed"] is None):
# generate random seed for this run
cfg["seed"] = int(np.random.uniform(low=0, high=1e6))
if (cfg["mode"] in ["evaluate", "eval_robustness"]) and (cfg["run_dir"] is None):
raise ValueError("In evaluation mode a run directory (--run_dir) has to be specified")
# combine global settings with user config
cfg.update(GLOBAL_SETTINGS)
if cfg["mode"] == "train":
# print config to terminal
for key, val in cfg.items():
print(f"{key}: {val}")
# convert path to PosixPath object
cfg["camels_root"] = Path(cfg["camels_root"])
if cfg["run_dir"] is not None:
cfg["run_dir"] = Path(cfg["run_dir"])
return cfg
def _setup_run(cfg: Dict) -> Dict:
"""Create folder structure for this run
Parameters
----------
cfg : dict
Dictionary containing the run config
Returns
-------
dict
Dictionary containing the updated run config
"""
now = datetime.now()
day = f"{now.day}".zfill(2)
month = f"{now.month}".zfill(2)
hour = f"{now.hour}".zfill(2)
minute = f"{now.minute}".zfill(2)
run_name = f'run_{day}{month}_{hour}{minute}_seed{cfg["seed"]}'
cfg['run_dir'] = Path(__file__).absolute().parent / "runs" / run_name
if not cfg["run_dir"].is_dir():
cfg["train_dir"] = cfg["run_dir"] / 'data' / 'train'
cfg["train_dir"].mkdir(parents=True)
cfg["val_dir"] = cfg["run_dir"] / 'data' / 'val'
cfg["val_dir"].mkdir(parents=True)
else:
raise RuntimeError(f"There is already a folder at {cfg['run_dir']}")
# dump a copy of cfg to run directory
with (cfg["run_dir"] / 'cfg.json').open('w') as fp:
temp_cfg = {}
for key, val in cfg.items():
if isinstance(val, PosixPath):
temp_cfg[key] = str(val)
elif isinstance(val, pd.Timestamp):
temp_cfg[key] = val.strftime(format="%d%m%Y")
else:
temp_cfg[key] = val
json.dump(temp_cfg, fp, sort_keys=True, indent=4)
return cfg
def _prepare_data(cfg: Dict, basins: List) -> Dict:
"""Preprocess training data.
Parameters
----------
cfg : dict
Dictionary containing the run config
basins : List
List containing the 8-digit USGS gauge id
Returns
-------
dict
Dictionary containing the updated run config
"""
# create database file containing the static basin attributes
cfg["db_path"] = str(cfg["run_dir"] / "attributes.db")
add_camels_attributes(cfg["camels_root"], db_path=cfg["db_path"])
# create .h5 files for train and validation data
cfg["train_file"] = cfg["train_dir"] / 'train_data.h5'
create_h5_files(camels_root=cfg["camels_root"],
out_file=cfg["train_file"],
basins=basins,
dates=[cfg["train_start"], cfg["train_end"]],
with_basin_str=True,
seq_length=cfg["seq_length"])
return cfg
################
# Define Model #
################
class Model(nn.Module):
"""Wrapper class that connects LSTM/EA-LSTM with fully connceted layer"""
def __init__(self,
input_size_dyn: int,
input_size_stat: int,
hidden_size: int,
initial_forget_bias: int = 5,
dropout: float = 0.0,
concat_static: bool = False,
no_static: bool = False):
"""Initialize model.
Parameters
----------
input_size_dyn: int
Number of dynamic input features.
input_size_stat: int
Number of static input features (used in the EA-LSTM input gate).
hidden_size: int
Number of LSTM cells/hidden units.
initial_forget_bias: int
Value of the initial forget gate bias. (default: 5)
dropout: float
Dropout probability in range(0,1). (default: 0.0)
concat_static: bool
If True, uses standard LSTM otherwise uses EA-LSTM
no_static: bool
If True, runs standard LSTM
"""
super(Model, self).__init__()
self.input_size_dyn = input_size_dyn
self.input_size_stat = input_size_stat
self.hidden_size = hidden_size
self.initial_forget_bias = initial_forget_bias
self.dropout_rate = dropout
self.concat_static = concat_static
self.no_static = no_static
if self.concat_static or self.no_static:
self.lstm = LSTM(input_size=input_size_dyn,
hidden_size=hidden_size,
initial_forget_bias=initial_forget_bias)
else:
self.lstm = EALSTM(input_size_dyn=input_size_dyn,
input_size_stat=input_size_stat,
hidden_size=hidden_size,
initial_forget_bias=initial_forget_bias)
self.dropout = nn.Dropout(p=dropout)
self.fc = nn.Linear(hidden_size, 1)
def forward(self, x_d: torch.Tensor, x_s: torch.Tensor = None) \
-> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""Run forward pass through the model.
Parameters
----------
x_d : torch.Tensor
Tensor containing the dynamic input features of shape [batch, seq_length, n_features]
x_s : torch.Tensor, optional
Tensor containing the static catchment characteristics, by default None
Returns
-------
out : torch.Tensor
Tensor containing the network predictions
h_n : torch.Tensor
Tensor containing the hidden states of each time step
c_n : torch,Tensor
Tensor containing the cell states of each time step
"""
if self.concat_static or self.no_static:
h_n, c_n = self.lstm(x_d)
else:
h_n, c_n = self.lstm(x_d, x_s)
last_h = self.dropout(h_n[:, -1, :])
out = self.fc(last_h)
return out, h_n, c_n
###########################
# Train or evaluate model #
###########################
def train(cfg):
"""Train model.
Parameters
----------
cfg : Dict
Dictionary containing the run config
"""
# fix random seeds
random.seed(cfg["seed"])
np.random.seed(cfg["seed"])
torch.cuda.manual_seed(cfg["seed"])
torch.manual_seed(cfg["seed"])
basins = get_basin_list()
# create folder structure for this run
cfg = _setup_run(cfg)
# prepare data for training
cfg = _prepare_data(cfg=cfg, basins=basins)
# prepare PyTorch DataLoader
ds = CamelsH5(h5_file=cfg["train_file"],
basins=basins,
db_path=cfg["db_path"],
concat_static=cfg["concat_static"],
cache=cfg["cache_data"],
no_static=cfg["no_static"])
loader = DataLoader(ds,
batch_size=cfg["batch_size"],
shuffle=True,
num_workers=cfg["num_workers"])
# create model and optimizer
input_size_stat = 0 if cfg["no_static"] else 27
input_size_dyn = 5 if (cfg["no_static"] or not cfg["concat_static"]) else 32
model = Model(input_size_dyn=input_size_dyn,
input_size_stat=input_size_stat,
hidden_size=cfg["hidden_size"],
initial_forget_bias=cfg["initial_forget_gate_bias"],
dropout=cfg["dropout"],
concat_static=cfg["concat_static"],
no_static=cfg["no_static"]).to(DEVICE)
optimizer = torch.optim.Adam(model.parameters(), lr=cfg["learning_rate"])
# define loss function
if cfg["use_mse"]:
loss_func = nn.MSELoss()
else:
loss_func = NSELoss()
# reduce learning rates after each 10 epochs
learning_rates = {11: 5e-4, 21: 1e-4}
for epoch in range(1, cfg["epochs"] + 1):
# set new learning rate
if epoch in learning_rates.keys():
for param_group in optimizer.param_groups:
param_group["lr"] = learning_rates[epoch]
train_epoch(model, optimizer, loss_func, loader, cfg, epoch, cfg["use_mse"])
model_path = cfg["run_dir"] / f"model_epoch{epoch}.pt"
torch.save(model.state_dict(), str(model_path))
def train_epoch(model: nn.Module, optimizer: torch.optim.Optimizer, loss_func: nn.Module,
loader: DataLoader, cfg: Dict, epoch: int, use_mse: bool):
"""Train model for a single epoch.
Parameters
----------
model : nn.Module
The PyTorch model to train
optimizer : torch.optim.Optimizer
Optimizer used for weight updating
loss_func : nn.Module
The loss function, implemented as a PyTorch Module
loader : DataLoader
PyTorch DataLoader containing the training data in batches.
cfg : Dict
Dictionary containing the run config
epoch : int
Current Number of epoch
use_mse : bool
If True, loss_func is nn.MSELoss(), else NSELoss() which expects addtional std of discharge
vector
"""
model.train()
# process bar handle
pbar = tqdm(loader, file=sys.stdout)
pbar.set_description(f'# Epoch {epoch}')
# Iterate in batches over training set
for data in pbar:
# delete old gradients
optimizer.zero_grad()
# forward pass through LSTM
if len(data) == 3:
x, y, q_stds = data
x, y, q_stds = x.to(DEVICE), y.to(DEVICE), q_stds.to(DEVICE)
predictions = model(x)[0]
# forward pass through EALSTM
elif len(data) == 4:
x_d, x_s, y, q_stds = data
x_d, x_s, y = x_d.to(DEVICE), x_s.to(DEVICE), y.to(DEVICE)
predictions = model(x_d, x_s[:, 0, :])[0]
# MSELoss
if use_mse:
loss = loss_func(predictions, y)
# NSELoss needs std of each basin for each sample
else:
q_stds = q_stds.to(DEVICE)
loss = loss_func(predictions, y, q_stds)
# calculate gradients
loss.backward()
if cfg["clip_norm"]:
torch.nn.utils.clip_grad_norm_(model.parameters(), cfg["clip_value"])
# perform parameter update
optimizer.step()
pbar.set_postfix_str(f"Loss: {loss.item():5f}")
def evaluate(user_cfg: Dict):
"""Train model for a single epoch.
Parameters
----------
user_cfg : Dict
Dictionary containing the user entered evaluation config
"""
with open(user_cfg["run_dir"] / 'cfg.json', 'r') as fp:
run_cfg = json.load(fp)
basins = get_basin_list()
# get attribute means/stds
db_path = str(user_cfg["run_dir"] / "attributes.db")
attributes = load_attributes(db_path=db_path,
basins=basins,
drop_lat_lon=True)
means = attributes.mean()
stds = attributes.std()
# create model
input_size_stat = 0 if run_cfg["no_static"] else 27
input_size_dyn = 5 if (run_cfg["no_static"] or not run_cfg["concat_static"]) else 32
model = Model(input_size_dyn=input_size_dyn,
input_size_stat=input_size_stat,
hidden_size=run_cfg["hidden_size"],
dropout=run_cfg["dropout"],
concat_static=run_cfg["concat_static"],
no_static=run_cfg["no_static"]).to(DEVICE)
# load trained model
weight_file = user_cfg["run_dir"] / 'model_epoch30.pt'
model.load_state_dict(torch.load(weight_file, map_location=DEVICE))
date_range = pd.date_range(start=GLOBAL_SETTINGS["val_start"], end=GLOBAL_SETTINGS["val_end"])
results = {}
for basin in tqdm(basins):
ds_test = CamelsTXT(camels_root=user_cfg["camels_root"],
basin=basin,
dates=[GLOBAL_SETTINGS["val_start"], GLOBAL_SETTINGS["val_end"]],
is_train=False,
seq_length=run_cfg["seq_length"],
with_attributes=True,
attribute_means=means,
attribute_stds=stds,
concat_static=run_cfg["concat_static"],
db_path=db_path)
loader = DataLoader(ds_test, batch_size=1024, shuffle=False, num_workers=4)
preds, obs = evaluate_basin(model, loader)
df = pd.DataFrame(data={'qobs': obs.flatten(), 'qsim': preds.flatten()}, index=date_range)
results[basin] = df
_store_results(user_cfg, run_cfg, results)
def evaluate_basin(model: nn.Module, loader: DataLoader) -> Tuple[np.ndarray, np.ndarray]:
"""Evaluate model on a single basin
Parameters
----------
model : nn.Module
The PyTorch model to train
loader : DataLoader
PyTorch DataLoader containing the basin data in batches.
Returns
-------
preds : np.ndarray
Array containing the (rescaled) network prediction for the entire data period
obs : np.ndarray
Array containing the observed discharge for the entire data period
"""
model.eval()
preds, obs = None, None
with torch.no_grad():
for data in loader:
if len(data) == 2:
x, y = data
x, y = x.to(DEVICE), y.to(DEVICE)
p = model(x)[0]
elif len(data) == 3:
x_d, x_s, y = data
x_d, x_s, y = x_d.to(DEVICE), x_s.to(DEVICE), y.to(DEVICE)
p = model(x_d, x_s[:, 0, :])[0]
if preds is None:
preds = p.detach().cpu()
obs = y.detach().cpu()
else:
preds = torch.cat((preds, p.detach().cpu()), 0)
obs = torch.cat((obs, y.detach().cpu()), 0)
preds = rescale_features(preds.numpy(), variable='output')
obs = obs.numpy()
# set discharges < 0 to zero
preds[preds < 0] = 0
return preds, obs
def eval_robustness(user_cfg: Dict):
"""Evaluate model robustness of EA-LSTM
In this experiment, gaussian noise with increasing scale is added to the static features to
evaluate the model robustness against pertubations of the static catchment characteristics.
For each scale, 50 noise vectors are drawn.
Parameters
----------
user_cfg : Dict
Dictionary containing the user entered evaluation config
Raises
------
NotImplementedError
If the run_dir specified points not to a EA-LSTM model folder.
"""
random.seed(user_cfg["seed"])
np.random.seed(user_cfg["seed"])
# fixed settings for this analysis
n_repetitions = 50
scales = [0.1 * i for i in range(11)]
with open(user_cfg["run_dir"] / 'cfg.json', 'r') as fp:
run_cfg = json.load(fp)
if run_cfg["concat_static"] or run_cfg["no_static"]:
raise NotImplementedError("This function is only implemented for EA-LSTM models")
basins = get_basin_list()
# get attribute means/stds
db_path = str(user_cfg["run_dir"] / "attributes.db")
attributes = load_attributes(db_path=db_path,
basins=basins,
drop_lat_lon=True)
means = attributes.mean()
stds = attributes.std()
# initialize Model
model = Model(input_size_dyn=5,
input_size_stat=27,
hidden_size=run_cfg["hidden_size"],
dropout=run_cfg["dropout"]).to(DEVICE)
weight_file = user_cfg["run_dir"] / "model_epoch30.pt"
model.load_state_dict(torch.load(weight_file, map_location=DEVICE))
overall_results = {}
# process bar handle
pbar = tqdm(basins, file=sys.stdout)
for basin in pbar:
ds_test = CamelsTXT(camels_root=user_cfg["camels_root"],
basin=basin,
dates=[GLOBAL_SETTINGS["val_start"], GLOBAL_SETTINGS["val_end"]],
is_train=False,
with_attributes=True,
attribute_means=means,
attribute_stds=stds,
db_path=db_path)
loader = DataLoader(ds_test, batch_size=len(ds_test), shuffle=False, num_workers=0)
basin_results = defaultdict(list)
step = 1
for scale in scales:
for _ in range(1 if scale == 0.0 else n_repetitions):
noise = np.random.normal(loc=0, scale=scale, size=27).astype(np.float32)
noise = torch.from_numpy(noise).to(DEVICE)
nse = eval_with_added_noise(model, loader, noise)
basin_results[scale].append(nse)
pbar.set_postfix_str(f"Basin progress: {step}/{(len(scales)-1)*n_repetitions+1}")
step += 1
overall_results[basin] = basin_results
out_file = (Path(__file__).absolute().parent /
f'results/{user_cfg["run_dir"].name}_model_robustness.p')
if not out_file.parent.is_dir():
out_file.parent.mkdir(parents=True)
with out_file.open("wb") as fp:
pickle.dump(overall_results, fp)
def eval_with_added_noise(model: torch.nn.Module, loader: DataLoader, noise: torch.Tensor) -> float:
"""Evaluate model on a single basin with added noise
Parameters
----------
model : nn.Module
The PyTorch model to train
loader : DataLoader
PyTorch DataLoader containing the basin data in batches.
noise : torch.Tensor
Tensor containing the noise for this evaluation run.
Returns
-------
float
Nash-Sutcliff-Efficiency of the simulations with added noise.
"""
model.eval()
preds, obs = None, None
with torch.no_grad():
for x_d, x_s, y in loader:
x_d, x_s, y = x_d.to(DEVICE), x_s.to(DEVICE), y.to(DEVICE)
batch_noise = noise.repeat(*x_s.size()[:2], 1)
x_s = x_s.add(batch_noise)
y_hat = model(x_d, x_s[:, 0, :])[0]
if preds is None:
preds = y_hat.detach().cpu()
obs = y.detach().cpu()
else:
preds = torch.cat((preds, y_hat.detach().cpu()), 0)
obs = torch.cat((obs, y.detach().cpu()), 0)
obs = obs.numpy()
preds = rescale_features(preds.numpy(), variable='output')
# set discharges < 0 to zero
preds[preds < 0] = 0
nse = calc_nse(obs[obs >= 0], preds[obs >= 0])
return nse
def _store_results(user_cfg: Dict, run_cfg: Dict, results: pd.DataFrame):
"""Store results in a pickle file.
Parameters
----------
user_cfg : Dict
Dictionary containing the user entered evaluation config
run_cfg : Dict
Dictionary containing the run config loaded from the cfg.json file
results : pd.DataFrame
DataFrame containing the observed and predicted discharge.
"""
if run_cfg["no_static"]:
file_name = user_cfg["run_dir"] / f"lstm_no_static_seed{run_cfg['seed']}.p"
else:
if run_cfg["concat_static"]:
file_name = user_cfg["run_dir"] / f"lstm_seed{run_cfg['seed']}.p"
else:
file_name = user_cfg["run_dir"] / f"ealstm_seed{run_cfg['seed']}.p"
with (file_name).open('wb') as fp:
pickle.dump(results, fp)
print(f"Sucessfully store results at {file_name}")
if __name__ == "__main__":
config = get_args()
globals()[config["mode"]](config)