-
Notifications
You must be signed in to change notification settings - Fork 0
/
re_predict.py
90 lines (61 loc) · 2.47 KB
/
re_predict.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
import torch
import lightning.pytorch as pl
from datamodule import SeqDataModule
from test_predict import save_predict
from trainer import LitModel, TrainingConfig
from utils import set_global_seed
from lightning.pytorch.callbacks import ModelCheckpoint
from pathlib import Path
import glob
import argparse
parser = argparse.ArgumentParser()
general = parser.add_argument_group('general args',
'general_argumens')
general.add_argument("--model_dir",
type=str,
required=True)
general.add_argument("--data_path",
type=str,
required=True)
general.add_argument("--outdir",
type=str,
required=True)
general.add_argument("--device",
type=int,
default=0)
general.add_argument("--num_workers",
type=int,
default=8)
general.add_argument("--fraction",
type=float,
default=1.0)
args = parser.parse_args()
model_dir = Path(args.model_dir)
train_cfg = TrainingConfig.from_json(model_dir / "config.json")
train_cfg.data_path = args.data_path
outdir = Path(args.outdir)
outdir.mkdir(parents=True, exist_ok=True)
torch.set_float32_matmul_precision('medium') # type: ignore
for test_fold in range(1, 11):
for val_fold in range(1, 11):
if test_fold == val_fold:
continue
data = SeqDataModule(val_fold=val_fold,
test_fold=test_fold,
cfg=train_cfg)
dump_dir = model_dir / f"model_{val_fold}_{test_fold}"
trainer = pl.Trainer(accelerator='gpu',
devices=[train_cfg.device],
precision='16-mixed')
models = glob.glob(str(dump_dir / "lightning_logs" / "version_0" / "checkpoints" / "pearson*") )
save_dir = outdir / f"model_{val_fold}_{test_fold}"
save_dir.mkdir(parents=True, exist_ok=True)
assert len(models) == 1
model_path = models[0]
model = LitModel.load_from_checkpoint(model_path,
tr_cfg=train_cfg)
df_pred = save_predict(trainer,
model,
data,
save_dir=save_dir,
pref="new_format")