-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_script.py
213 lines (180 loc) · 6.71 KB
/
run_script.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
import os
from datetime import datetime
from typing import Any
import torch
from hydra.utils import instantiate
from hydra.errors import InstantiationException
from lightning import Trainer
from lightning.pytorch.callbacks import (
EarlyStopping,
LearningRateMonitor,
ModelCheckpoint,
)
from torchvision.transforms import Resize
from omegaconf.errors import ConfigAttributeError
from lightning.pytorch.loggers import CSVLogger, WandbLogger # noqa: F401
from omegaconf import OmegaConf
def create_experiment_dir(config: dict[str, Any]) -> str:
"""Create experiment directory.
Args:
config: config file
Returns:
config with updated save_dir
"""
os.makedirs(config["experiment"]["exp_dir"], exist_ok=True)
exp_dir_name = (
f"{config['experiment']['experiment_name']}"
f"_{config['uq_method']['_target_'].split('.')[-1]}"
f"_{datetime.now().strftime('%m-%d-%Y_%H-%M-%S-%f')}"
)
config["experiment"]["experiment_name"] = exp_dir_name
exp_dir_path = os.path.join(config["experiment"]["exp_dir"], exp_dir_name)
os.makedirs(exp_dir_path)
config["experiment"]["save_dir"] = exp_dir_path
config["trainer"]["default_root_dir"] = exp_dir_path
return config
def generate_trainer(config: dict[str, Any]) -> Trainer:
"""Generate a pytorch lightning trainer."""
loggers = [
CSVLogger(config["experiment"]["save_dir"], name="csv_logs"),
WandbLogger(
name=config["experiment"]["experiment_name"],
save_dir=config["experiment"]["save_dir"],
project=config["wandb"]["project"],
entity=config["wandb"]["entity"],
resume="allow",
mode="offline",
),
]
mode = "min"
checkpoint_callback = ModelCheckpoint(
dirpath=config["experiment"]["save_dir"],
save_top_k=1,
monitor="val_loss",
mode=mode,
every_n_epochs=1,
)
lr_monitor_callback = LearningRateMonitor(logging_interval="step")
if "SWAG" in config.uq_method["_target_"]:
callbacks = None
else:
callbacks = [checkpoint_callback, lr_monitor_callback]
return instantiate(
config.trainer,
default_root_dir=config["experiment"]["save_dir"],
callbacks=callbacks,
logger=loggers,
)
post_hoc_methods = [
"SWAG",
"Laplace",
"ConformalQR",
"CARD",
"DeepEnsemble",
"TempScaling",
"RAPS",
"TTA",
]
if __name__ == "__main__":
torch.set_float32_matmul_precision("medium")
command_line_conf = OmegaConf.from_cli()
model_conf = OmegaConf.load(command_line_conf.model_config)
data_conf = OmegaConf.load(command_line_conf.data_config)
trainer_conf = OmegaConf.load(command_line_conf.trainer_config)
full_config = OmegaConf.merge(data_conf, trainer_conf, model_conf)
full_config = create_experiment_dir(full_config)
datamodule = instantiate(full_config.datamodule)
datamodule.setup("fit")
datamodule.aug.data_keys = ["input"]
# store predictions for training and test set
target_mean = datamodule.target_mean
target_std = datamodule.target_std
# Also store predictions for training
def collate(batch: list[dict[str, torch.Tensor]]):
"""Collate fn to include augmentations."""
inputs = torch.stack([item["input"] for item in batch])
targets = torch.stack([item["target"] for item in batch])
if datamodule.task == "regression":
new_batch = {
"input": inputs,
"target": (targets[..., -1:].float() - target_mean) / target_std,
}
else:
new_batch = {
"input": inputs,
"target": targets.squeeze().long(),
}
keys = batch[0].keys()
for key in keys:
if key not in ["input", "target"]:
new_batch[key] = [x[key] for x in batch]
return new_batch
calib_loader = datamodule.calibration_dataloader()
calib_loader.collate_fn = collate
trainer = generate_trainer(full_config)
if any(method in full_config.uq_method._target_ for method in post_hoc_methods):
# post hoc methods just load a checkpoint
if (
"SWAG" in full_config.uq_method["_target_"]
or "CARD" in full_config.uq_method["_target_"]
):
model = instantiate(full_config.uq_method)
trainer.fit(model, datamodule=datamodule)
elif "Laplace" in full_config.uq_method["_target_"]:
model = instantiate(full_config.uq_method)
elif "DeepEnsemble" in full_config.uq_method["_target_"]:
ensemble_members = [
{
"base_model": instantiate(full_config.ensemble_members),
"ckpt_path": path,
}
for path in full_config.uq_method.ensemble_members
]
model = instantiate(
full_config.uq_method, ensemble_members=ensemble_members
)
elif (
"ConformalQR" in full_config.uq_method["_target_"]
or "RAPS" in full_config.uq_method["_target_"]
):
datamodule.setup("fit")
model = instantiate(full_config.uq_method)
trainer.validate(model, dataloaders=calib_loader)
else:
model = instantiate(full_config.uq_method)
trainer.validate(model, datamodule=datamodule)
model.pred_file_name = "preds_test.csv"
trainer.test(model, datamodule=datamodule)
else:
model = instantiate(full_config.uq_method)
trainer.fit(model, datamodule)
model.pred_file_name = "preds_test.csv"
trainer.test(ckpt_path="best", datamodule=datamodule)
# store predictions for training and test set
target_mean = datamodule.target_mean
target_std = datamodule.target_std
# train dataset results
model.pred_file_name = "preds_train.csv"
datamodule.setup("fit")
train_loader = datamodule.train_dataloader()
train_loader.shuffle = False
train_loader.collate_fn = collate
try:
trainer.test(ckpt_path="best", dataloaders=train_loader)
except:
trainer.test(model, dataloaders=train_loader)
# val dataset results
model.pred_file_name = "preds_val.csv"
val_loader = datamodule.val_dataloader()
val_loader.shuffle = False
val_loader.collate_fn = collate
try:
trainer.test(ckpt_path="best", dataloaders=val_loader)
except:
trainer.test(model, dataloaders=val_loader)
# save configuration file
with open(
os.path.join(full_config["experiment"]["save_dir"], "config.yaml"), "w"
) as f:
OmegaConf.save(config=full_config, f=f)
print("FINISHED EXPERIMENT", flush=True)