-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_experiment.py
145 lines (130 loc) · 5.1 KB
/
run_experiment.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
import argparse
import os
import sys
import numpy as np
import pytorch_lightning as pl
from pytorch_lightning import loggers as pl_loggers
import torch
import pathlib
from utils import setup_data_and_model_from_args, get_callbacks
import constants
np.random.seed(constants.SEED)
torch.manual_seed(constants.SEED)
pl.seed_everything(constants.SEED, workers=True)
NUM_AVAIL_CPUS = len(os.sched_getaffinity(0))
NUM_AVAIL_GPUS = torch.cuda.device_count()
if NUM_AVAIL_GPUS:
ACCELERATOR = "gpu"
else:
ACCELERATOR = None
DEFAULT_NUM_WORKERS = NUM_AVAIL_CPUS
DEFAULT_NUM_WORKERS = NUM_AVAIL_CPUS // NUM_AVAIL_GPUS if NUM_AVAIL_GPUS else DEFAULT_NUM_WORKERS
def _setup_parser():
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, help="JSON config file")
parser.add_argument("--num_epochs", type=int,
default=100, help="number of epochs")
parser.add_argument(
"--load_checkpoint", type=str, default=None, help="If passed, loads a model from the provided path."
)
parser.add_argument(
"--optimizer", type=str, default=constants.OPTIMIZER, help="Optimizer"
)
parser.add_argument(
"--wandb",
action="store_true",
default=False,
help="If passed, logs experiment results to Weights & Biases. Otherwise logs only to local Tensorboard.",
)
parser.add_argument(
"--lr", type=float, default=constants.LR, help="Learning Rate"
)
parser.add_argument("--loss", type=str,
default=constants.LOSS, choices=[
"MSELoss",
"L1Loss",
"SSIMLoss",
], help="Loss Function")
# Schedulers parameters
parser.add_argument("--one_cycle_max_lr", type=float, default=None)
parser.add_argument("--one_cycle_total_steps", type=int, default=constants.ONE_CYCLE_TOTAL_STEPS)
# Callbacks
parser.add_argument("--use_es", action="store_true",
help="use early stopping or not")
parser.add_argument("--n_checkpoints", type=int,
default=constants.N_CHECKPOINTS, help="number of checkpoints")
parser.add_argument("--patience", type=int,
default=constants.PATIENCE, help="patience for early stopping/checkpointing")
parser.add_argument("--mode", type=str,
default="max", choices=[
"min",
"max"
], help="mode for early stopping/checkpointing")
parser.add_argument("--monitor", type=str,
default="val_PCK", choices=[
"val_loss",
"train_loss",
"val_PCK",
], help="monitor for early stopping/checkpointing")
parser.add_argument("--exp_name", type=str, help="experiment name")
parser.add_argument("--accelerator", type=str, default=ACCELERATOR, help="accelerator")
parser.add_argument("--devices", type=int, default=None, help="number of devices")
parser.add_argument(
"--num_workers",
type=int,
default=DEFAULT_NUM_WORKERS,
help="Number of workers for dataloaders"
)
parser.add_argument(
"--pin_memory",
type=str,
default=constants.PINMEMORY,
help="pin memory for dataloader")
parser.add_argument(
"--project_name",
type=str,
default="CUDALAB",
help="W and b proj name")
return parser
def _run_experiment(args):
data_module, lit_model, args = setup_data_and_model_from_args(args)
callbacks = get_callbacks(args)
logdir = f'{constants.WORKING_DIR}/{constants.LOG_DIR}/' \
+ f'{args["config"]["data"]["dataset"]}/' \
+ f'{args["config"]["model"]["name"]}/' \
+ f'{args["exp_name"]}'
# logger = pl_loggers.TensorBoardLogger(save_dir=logdir)
if args["wandb"]:
pathlib.Path(logdir).mkdir(parents=True, exist_ok=True)
logger = pl_loggers.WandbLogger(
project=args["project_name"],
name=args["exp_name"],
# log_model="all",
save_dir=logdir,
job_type="train",
log_model=False,
)
# logger.watch(lit_model, log_freq=max(100, constants.LOG_STEPS))
logger.log_hyperparams(args["config"]["model"])
else:
logger = pl_loggers.TensorBoardLogger(save_dir=logdir)
trainer = pl.Trainer(
deterministic=True,
accelerator=args["accelerator"],
devices=args["devices"] if args["devices"] else constants.DEVICES,
max_epochs=args["num_epochs"],
callbacks=callbacks,
logger=logger,
log_every_n_steps=constants.LOG_STEPS,
)
trainer.fit(lit_model, data_module, ckpt_path=args["load_checkpoint"])
best_model_path = callbacks[0].best_model_path
print("best_model_path", best_model_path)
print("args:", args)
def main():
parser = _setup_parser()
args = parser.parse_args()
args = vars(args)
_run_experiment(args)
if __name__ == "__main__":
main()