-
Notifications
You must be signed in to change notification settings - Fork 1
/
predict_model.py
64 lines (50 loc) · 2.01 KB
/
predict_model.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
"""This script will run nowcasting prediction for the L-CNN model implementation
"""
import argparse
from pathlib import Path
import torch
import pytorch_lightning as pl
from pytorch_lightning.loggers import WandbLogger
from utils import load_config, setup_logging
from utils import LagrangianHDF5Writer
from models import LCNN
from datamodules import LagrangianSHMUDataModule
def run(checkpointpath, configpath, predict_list) -> None:
confpath = Path("config") / configpath
dsconf = load_config(confpath / "lagrangian_datasets.yaml")
outputconf = load_config(confpath / "output.yaml")
modelconf = load_config(confpath / "lcnn.yaml")
setup_logging(outputconf.logging)
datamodel = LagrangianSHMUDataModule(
dsconf, modelconf.train_params, predict_list=predict_list
)
model = LCNN(modelconf).load_from_checkpoint(checkpointpath, config=modelconf, map_location=torch.device('cpu'))
output_writer = LagrangianHDF5Writer(**modelconf.prediction_output)
logger = WandbLogger(save_dir=f"checkpoints/{modelconf.train_params.savefile}/wandb/predictions", project=modelconf.train_params.savefile, log_model=True)
trainer = pl.Trainer(
profiler="pytorch",
logger=logger,
devices=modelconf.train_params.gpus,
callbacks=[output_writer],
)
# Predictions are written in HDF5 file
trainer.predict(model, datamodel, return_predictions=False)
if __name__ == "__main__":
argparser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
argparser.add_argument("checkpoint", type=str, help="Path to checkpoint file")
argparser.add_argument(
"config",
type=str,
help="Configuration folder path",
)
argparser.add_argument(
"-l",
"--list",
type=str,
default="predict",
help="Name of predicted list (replaces {split} in dataset settings).",
)
args = argparser.parse_args()
run(args.checkpoint, args.config, args.list)