-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
310 lines (273 loc) · 13.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
import darts.models.forecasting.torch_forecasting_model
import pyrootutils
import pytorch_lightning.loggers.mlflow
root = pyrootutils.setup_root(__file__, indicator=".project-root", pythonpath=True)
import os
from pathlib import Path
from typing import List, Optional, Tuple
import hydra
import pytorch_lightning as pl
from omegaconf import DictConfig, OmegaConf, open_dict
import src.datamodules.components.timeseries_datamodule
import src.eval
import src.models.utils
import src.predict
import src.utils.plotting
from src import utils
from src.utils.utils import call_function_with_data
log = utils.get_pylogger(__name__)
@utils.task_wrapper
def train(cfg: DictConfig) -> Tuple[dict, dict]:
"""Trains the model. Can additionally evaluate on a testset, using best weights obtained during
training.
This method is wrapped in optional @task_wrapper decorator which applies extra utilities
before and after the call.
Args:
cfg (DictConfig): Configuration composed by Hydra.
Returns:
Tuple[dict, dict]: Dict with metrics and dict with all instantiated objects.
"""
if cfg.get("use_inputs", {}) is not None and len(cfg.get("use_inputs", {})) > 0:
num_active_covariate_inputs = 0
for cov_type in ["past", "future"]:
cov_num_active = OmegaConf.select(
cfg, f"datamodule.data_variables.{cov_type}_covariates", default=[]
)
if cov_num_active is not None:
num_active_covariate_inputs += len(cov_num_active)
if num_active_covariate_inputs == 0:
# all (covariate) inputs are disabled
if src.models.utils.is_regression_model(cfg.model):
# if model uses target as input, we just disable the covariates
if OmegaConf.select(cfg, "model.lags", default=None) is not None:
for cov_type in ["past", "future"]:
if f"lags_{cov_type}_covariates" in cfg.model:
cfg.model[f"lags_{cov_type}_covariates"] = None
else: # else we prune the trial as model has no inputs
return {
cfg.get("optimized_metric"): 10.0
}, {} # TODO: should be nan but that fails somehow. For now, setting to high number
elif src.models.utils.is_torch_model(cfg.model):
if (
OmegaConf.select(cfg, "model._target_", default="")
== "darts.models.forecasting.rnn_model.RNNModel"
and OmegaConf.select(cfg, "model.model.path", default="")
== "src.models.rnn_no_target.NoTargetRNNModule"
):
return {
cfg.get("optimized_metric"): 10.0
}, {} # TODO: should be nan but that fails somehow. For now, setting to high number
# TODO: same for TCN
else:
raise NotImplementedError
try:
OmegaConf.resolve(cfg)
resolved_cfg_path = Path(cfg.paths.output_dir) / ".hydra" / "resolved_config.yaml"
if not resolved_cfg_path.parent.exists():
os.makedirs(resolved_cfg_path.parent, exist_ok=True)
OmegaConf.save(cfg, resolved_cfg_path)
log.info(f"Saved resolved config to: {resolved_cfg_path}")
except Exception as e:
log.exception(
"Could not save resolved config. Continuing without saving resolved version."
)
# set seed for random number generators in pytorch, numpy and python.random
if cfg.get("seed") is not None: # TODO: separate seed for non-torch models?
pl.seed_everything(cfg.seed, workers=True)
if (
OmegaConf.select(cfg, "model._target_") == "darts.models.forecasting.xgboost.XGBModel"
and OmegaConf.select(cfg, "model.random_state") is None
):
with open_dict(cfg):
cfg.model.random_state = cfg.seed
time_metrics = {}
metric_dict = {}
object_dict = src.utils.instantiate_objects(cfg)
model = object_dict["model"]
datamodule = object_dict["datamodule"]
trainer = object_dict["trainer"]
callbacks = object_dict["callbacks"]
logger = object_dict["logger"]
fit_kwargs = dict(cfg.get("fit", {}))
if trainer is not None:
fit_kwargs["trainer"] = trainer
# TODO: support for fit_from_dataset
# TODO: dynamically decide what arguments to pass such as trainer
datamodule.setup("fit")
datamodule.save_state(os.path.join(cfg.paths.output_dir, "datamodule"))
if cfg.get("lr_tuner"):
if not src.models.utils.is_torch_model(model):
log.info(
"lr_tuner was set in config, but model is not a torch model. Skipping lr_tuner."
)
else:
log.info("Using lr_tuner to set initial learning rate!")
if trainer is not None:
# Perhaps no point in passing custom trainer?
original_max_epochs = trainer.max_epochs
trainer.fit_loop.max_epochs = None
lr_find_results = call_function_with_data(
model.lr_find,
datamodule,
main_split="train",
model=model,
trainer=trainer,
**cfg.lr_tuner.get("lr_find", {}),
)
if trainer is not None:
trainer.fit_loop.max_epochs = original_max_epochs
if lr_find_results is None:
log.warning("lr_tuner returned None. Is trainer set to fast_dev_run?")
else:
if cfg.lr_tuner.get("plot"):
fig = lr_find_results.plot(suggest=True)
presenters, presenter_kwargs = src.utils.get_presenters_and_kwargs(
None,
os.path.join(cfg.paths.output_dir, "plots"),
"lr_find_results",
logger,
trainer,
)
src.utils.plotting.multiple_present_figure(
fig, presenters, presenter_kwargs=presenter_kwargs
)
lr_suggestion = lr_find_results.suggestion(**cfg.lr_tuner.get("suggestion", {}))
if lr_suggestion is None:
if cfg.lr_tuner.error_on_fail:
raise RuntimeError("Could not find suitable learning rate")
log.warning(
"lr_find did not find a suitable learning rate. Not changing learning_rate"
)
else:
log.info(f"lr_find set learning rate to {lr_find_results.suggestion():.2E}")
with open_dict(cfg): # TODO: update saved config as well?
cfg.model.optimizer_kwargs.lr = lr_find_results.suggestion()
model = hydra.utils.instantiate(cfg.model, _convert_="partial")
object_dict["model"] = model
object_dict["lr_tuner"] = lr_find_results
if cfg.get("train"):
# Ensure model is properly saved when using customer trainer object
if trainer is not None: # maybe also only if is torchmodel?
assert isinstance(
model, darts.models.forecasting.torch_forecasting_model.TorchForecastingModel
), "Pytorch lightning trainer should only be used with TorchForecastingModels"
if any(
isinstance(cb, pl.callbacks.model_checkpoint.ModelCheckpoint)
for cb in trainer.callbacks
):
src.models.utils.ensure_torch_model_saving(model, cfg.paths.output_dir)
if any(
isinstance(cb, pl.callbacks.progress.RichProgressBar) for cb in trainer.callbacks
):
fit_kwargs["verbose"] = False
if cfg.get("ckpt_path", None) is not None:
assert os.path.exists(cfg.ckpt_path), "Provided checkpoint file could not be found"
model.load_ckpt_path = cfg.ckpt_path
if cfg.get(
"plot_datasets"
): # TODO: can rewrite to use multiple presenters and only call datamodule.plot_data once
# requires either changing plot_data to use new multiple presenters function, or using presenter=None
# and then calling multiple_present after
presenters = logger if len(logger) > 0 else ["savefig"]
for presenter in presenters:
if src.utils.plotting.is_supported_presenter(presenter):
if isinstance(presenter, pytorch_lightning.loggers.TensorBoardLogger):
dataset_plot_kwargs = dict(global_step=0)
elif isinstance(presenter, pytorch_lightning.loggers.MLFlowLogger):
dataset_plot_kwargs = dict(fname="datasets")
else:
dataset_plot_kwargs = dict(
fname=os.path.join(cfg.paths.output_dir, "plots", "datasets")
)
datamodule.plot_data(
presenter=presenter,
separate_components=cfg.plot_datasets.get("separate_components", False),
**dataset_plot_kwargs,
)
log.info("Starting training!")
# execution time is measured through callbacks for pytorch code
with src.utils.time_block(
enabled=cfg.get("measure_execution_time") and trainer is None,
metric_dict=time_metrics,
log_file=(
os.path.join(cfg.paths.output_dir, "train_exec_time.log")
if logger is None
else None
),
):
call_function_with_data(
model.fit, datamodule, main_split="train", model=model, **fit_kwargs
)
metric_dict.update({f"train_{k}": v for k, v in time_metrics.items()})
src.models.utils.save_model(model, cfg.paths.output_dir)
if trainer is not None:
metric_dict.update({k: v.numpy().item() for k, v in trainer.callback_metrics.items()})
if cfg.get("validate", False):
if datamodule.has_split_data("val"):
with open_dict(cfg):
cfg.eval.split = "val"
val_metric_dict, val_object_dict = src.eval.run(
cfg, datamodule, model, logger=logger, trainer=trainer
)
object_dict.update({f"val_{k}": v for k, v in val_object_dict.items()})
metric_dict.update(val_metric_dict)
else:
log.info(
"Validate argument was true but datamodule has no validation data. Skipping validation!"
)
if cfg.get("test", False):
if datamodule.has_split_data("test"):
log.info("Starting testing!")
ckpt_path = None
if trainer is not None:
ckpt_path = trainer.checkpoint_callback.best_model_path
if ckpt_path == "":
log.warning("Best ckpt not found! Using current weights for testing...")
ckpt_path = None
else:
log.info(f"Best ckpt path: {ckpt_path}")
with open_dict(cfg):
cfg.eval.split = "test"
test_metrics, test_object_dict = src.eval.run(
cfg, datamodule, model, logger=logger, trainer=trainer, ckpt_path=ckpt_path
)
object_dict.update({f"test_{k}": v for k, v in test_object_dict.items()})
metric_dict.update(test_metrics)
else:
log.info("Test argument was true but datamodule has no test data. Skipping testing!")
if cfg.get("predict"):
try:
predict_metrics, predictions, predict_figs = src.predict.run(
cfg, datamodule, model, logger
)
metric_dict.update(predict_metrics)
object_dict["predictions"] = predictions
object_dict["predict_figs"] = predict_figs
except NotImplementedError as e:
log.warning(f"Prediction failed: {e}")
# TODO: possibly also do this in task_wrapper if the script crashes somehow?
if logger: # Have to do this after call to model.fit, as model is initialized during fit call.
log.info("Logging hyperparameters!")
utils.log_hyperparameters(object_dict)
if trainer is None:
for lg in logger:
lg.finalize("success")
return metric_dict, object_dict
@hydra.main(version_base="1.3", config_path="../configs", config_name="train.yaml")
def main(cfg: DictConfig) -> Optional[float]:
"""Main entry point for training.
:param cfg: DictConfig configuration composed by Hydra.
:return: Optional[float] with optimized metric value.
"""
# train the model
metric_dict, _ = train(cfg)
# safely retrieve metric value for hydra-based hyperparameter optimization
metric_value = utils.get_metric_value(
metric_dict=metric_dict, metric_name=cfg.get("optimized_metric")
)
# return optimized metric
return metric_value
if __name__ == "__main__":
utils.enable_eval_resolver()
utils.enable_powerset_resolver()
main()