-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_traffic_benchmark.py
247 lines (210 loc) · 8.9 KB
/
run_traffic_benchmark.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
from typing import Optional
import torch
from omegaconf import DictConfig
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.loggers import Logger, TensorBoardLogger, WandbLogger
from tsl import logger
from tsl.data import SpatioTemporalDataModule, SpatioTemporalDataset
from tsl.data.preprocessing import StandardScaler
from tsl.datasets import MetrLA, PemsBay
from tsl.datasets.pems_benchmarks import PeMS03, PeMS04, PeMS07, PeMS08
from tsl.engines import Predictor
from tsl.experiment import Experiment, NeptuneLogger
from tsl.metrics import numpy as numpy_metrics
from tsl.metrics import torch as torch_metrics
from tsl.nn import models
from tsl.utils.casting import torch_to_numpy
def get_model_class(model_str):
# Spatiotemporal Models ###################################################
if model_str == 'dcrnn':
model = models.DCRNNModel # (Li et al., ICLR 2018)
elif model_str == 'gwnet':
model = models.GraphWaveNetModel # (Wu et al., IJCAI 2019)
elif model_str == 'evolvegcn':
model = models.EvolveGCNModel # (Pereja et al., AAAI 2020)
elif model_str == 'agcrn':
model = models.AGCRNModel # (Bai et al., NeurIPS 2020)
elif model_str == 'grugcn':
model = models.GRUGCNModel # (Guo et al., ICML 2022)
elif model_str == 'gatedgn':
model = models.GatedGraphNetworkModel # (Satorras et al., 2022)
elif model_str == 'stcn':
model = models.STCNModel
elif model_str == 'transformer':
model = models.TransformerModel
elif model_str == 'nexusqn':
model = models.NexuSQNModel
# Temporal Models #########################################################
elif model_str == 'ar':
model = models.ARModel
elif model_str == 'var':
model = models.VARModel
elif model_str == 'rnn':
model = models.RNNModel
elif model_str == 'fcrnn':
model = models.FCRNNModel
elif model_str == 'tcn':
model = models.TCNModel
else:
raise NotImplementedError(f'Model "{model_str}" not available.')
return model
def get_dataset(dataset_name):
if dataset_name == 'la':
dataset = MetrLA(impute_zeros=True) # From Li et al. (ICLR 2018)
elif dataset_name == 'bay':
dataset = PemsBay() # From Li et al. (ICLR 2018)
elif dataset_name == 'pems3':
dataset = PeMS03() # From Guo et al. (2021)
elif dataset_name == 'pems4':
dataset = PeMS04() # From Guo et al. (2021)
elif dataset_name == 'pems7':
dataset = PeMS07() # From Guo et al. (2021)
elif dataset_name == 'pems8':
dataset = PeMS08() # From Guo et al. (2021)
else:
raise ValueError(f"Dataset {dataset_name} not available.")
return dataset
def get_logger(cfg: DictConfig, exp: Experiment) -> Optional[Logger]:
if cfg.logger is None:
return None
assert 'backend' in cfg.logger, \
"cfg.logger must have a 'backend' attribute."
if cfg.logger.backend == 'wandb':
exp_logger = WandbLogger(name=cfg.run.name,
save_dir=cfg.run.dir,
offline=cfg.logger.offline,
project=cfg.logger.project,
config=exp.get_config_dict(),
tags=cfg.tags)
elif cfg.logger.backend == 'neptune':
exp_logger = NeptuneLogger(project_name=cfg.neptune.project,
experiment_name=cfg.run.name,
save_dir=cfg.run.dir,
tags=cfg.tags,
params=exp.get_config_dict(),
debug=cfg.neptune.offline)
elif cfg.logger.backend == 'tensorboard':
exp_name = f'{cfg.run.name}_{"_".join(cfg.tags)}'
exp_logger = TensorBoardLogger(save_dir=cfg.run.dir, name=exp_name)
else:
raise ValueError(f"Logger {cfg.logger.backend} not available.")
return exp_logger
def run_traffic(cfg: DictConfig):
########################################
# data module #
########################################
dataset = get_dataset(cfg.dataset.name)
# encode time of the day and use it as exogenous variable
covariates = {'u': dataset.datetime_encoded('day').values}
# get adjacency matrix
adj = dataset.get_connectivity(**cfg.dataset.connectivity)
torch_dataset = SpatioTemporalDataset(
target=dataset.dataframe(),
mask=dataset.mask,
connectivity=adj,
covariates=covariates,
horizon=cfg.horizon,
window=cfg.window,
stride=cfg.stride,
)
transform = {'target': StandardScaler(axis=(0, 1))} # axis: time&space
dm = SpatioTemporalDataModule(
dataset=torch_dataset,
scalers=transform,
splitter=dataset.get_splitter(**cfg.dataset.splitting),
batch_size=cfg.batch_size,
workers=cfg.workers,
)
dm.setup()
########################################
# predictor #
########################################
model_cls = get_model_class(cfg.model.name)
model_kwargs = dict(n_nodes=torch_dataset.n_nodes,
input_size=torch_dataset.n_channels,
output_size=torch_dataset.n_channels,
horizon=torch_dataset.horizon,
exog_size=torch_dataset.input_map.u.shape[-1])
model_cls.filter_model_args_(model_kwargs)
model_kwargs.update(cfg.model.hparams)
loss_fn = torch_metrics.MaskedMAE(compute_on_step=True)
log_metrics = {
'mae': torch_metrics.MaskedMAE(),
'mse': torch_metrics.MaskedMSE(),
'mape': torch_metrics.MaskedMAPE(),
'mae_at_15': torch_metrics.MaskedMAE(at=2), # 3rd is 15 min
'mae_at_30': torch_metrics.MaskedMAE(at=5), # 6th is 30 min
'mae_at_60': torch_metrics.MaskedMAE(at=11), # 12th is 1 h
}
if cfg.lr_scheduler is not None:
scheduler_class = getattr(torch.optim.lr_scheduler,
cfg.lr_scheduler.name)
scheduler_kwargs = dict(cfg.lr_scheduler.hparams)
else:
scheduler_class = scheduler_kwargs = None
# setup predictor
predictor = Predictor(
model_class=model_cls,
model_kwargs=model_kwargs,
optim_class=getattr(torch.optim, cfg.optimizer.name),
optim_kwargs=dict(cfg.optimizer.hparams),
loss_fn=loss_fn,
metrics=log_metrics,
scheduler_class=scheduler_class,
scheduler_kwargs=scheduler_kwargs,
scale_target=cfg.scale_target,
)
########################################
# training #
########################################
early_stop_callback = EarlyStopping(monitor='val_mae',
patience=cfg.patience,
mode='min')
checkpoint_callback = ModelCheckpoint(
dirpath=cfg.run.dir,
save_top_k=1,
monitor='val_mae',
mode='min',
)
exp_logger = get_logger(cfg, exp)
trainer = Trainer(
max_epochs=cfg.epochs,
default_root_dir=cfg.run.dir,
logger=exp_logger,
accelerator='gpu' if torch.cuda.is_available() else 'cpu',
devices=1,
gradient_clip_val=cfg.grad_clip_val,
callbacks=[early_stop_callback, checkpoint_callback],
)
trainer.fit(predictor, datamodule=dm)
########################################
# testing #
########################################
predictor.load_model(checkpoint_callback.best_model_path)
predictor.freeze()
trainer.test(predictor, datamodule=dm)
output = trainer.predict(predictor, dataloaders=dm.test_dataloader())
output = predictor.collate_prediction_outputs(output)
output = torch_to_numpy(output)
y_hat, y_true, mask = (output['y_hat'], output['y'],
output.get('mask', None))
res = dict(test_mae=numpy_metrics.mae(y_hat, y_true, mask),
test_rmse=numpy_metrics.rmse(y_hat, y_true, mask),
test_mape=numpy_metrics.mape(y_hat, y_true, mask))
output = trainer.predict(predictor, dataloaders=dm.val_dataloader())
output = predictor.collate_prediction_outputs(output)
output = torch_to_numpy(output)
y_hat, y_true, mask = (output['y_hat'], output['y'],
output.get('mask', None))
res.update(
dict(val_mae=numpy_metrics.mae(y_hat, y_true, mask),
val_rmse=numpy_metrics.rmse(y_hat, y_true, mask),
val_mape=numpy_metrics.mape(y_hat, y_true, mask)))
return res
if __name__ == '__main__':
exp = Experiment(run_fn=run_traffic,
config_path='Config/traffic',
config_name='default')
res = exp.run()
logger.info(res)