-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
222 lines (203 loc) · 10.5 KB
/
main.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
from pytorch_lightning.profiler import AdvancedProfiler
from os.path import exists
from src.optimization.ModelOptimization import *
from src.utils.ModelValidation import *
import torch
import sys
import json
import logging
from src.utils import util
import argparse
from src.utils.util import save_config, save_path, set_default_trainer_args, \
retrieve_model_checkpoint, get_tb_logdir_version, check_config, setup_logger, get_model_folder
MODEL_DIR = "./model"
CONFIG_DIR = "./config"
CONFIG_VALIDATION = "./config_requirements.json"
def choose_data_module(config, device):
if hasattr(config.dataset_config, "data_module"):
if config.dataset_config.data_module == "PSD":
data_module = PSDDataModule(config, device)
elif config.dataset_config.data_module == "graph":
data_module = GraphDataModule(config, device)
else:
raise IOError("Unknown data module {}".format(config.dataset_config.data_module))
elif hasattr(config.net_config, "net_class") and config.net_config.net_class.startswith("Graph"):
data_module = GraphDataModule(config, device)
else:
data_module = PSDDataModule(config, device)
return data_module
def main():
parser = argparse.ArgumentParser()
parser.add_argument("config", help="relative path of config file to use (in config folder)")
parser.add_argument("--name", "-n",
help="Set the experiment name for this run. Overrides exp_name specified in the run_config.",
type=str)
parser.add_argument("--load_best", "-lb", action="store_true",
help="finds the best checkpoint matching the model and experiment names, loads it and resumes training.")
parser.add_argument("--load_checkpoint", "-l", type=str,
help="Set the path to the checkpoint you'd like to resume training on.")
parser.add_argument("--restore_training", "-r", action="store_true",
help="Restores the training state in addition to model weights when loading a checkpoint. Does nothing if no checkpoints are loaded.")
parser.add_argument("--test", "-t", action="store_true", help="Run test on model after training.")
parser.add_argument("--verbosity", "-v",
help="Set the verbosity for this run.",
type=int, default=0)
parser.add_argument("--logfile", "-lf",
help="Set the filename or path to the filename for the program log this run."
" Set --verbosity to control the amount of information logged.",
type=str)
parser.add_argument("--validate", "-va", action="store_true",
help="If set, will validate the input algorithm before running")
parser.add_argument("--optimize_config", "-oc", type=str,
help="Set the path to the optuna optimization config file.")
parser.add_argument("--config_validation", "-cv", type=str,
help="Set the path to the config validation file.")
parser.add_argument("--num_threads", "-nt", type=int, help="number of threads to use")
parser.add_argument(
"--pruning",
"-p",
action="store_true",
help="Activate the pruning feature. `MedianPruner` stops unpromising "
"trials at the early stages of training.",
)
non_trainer_args = ["config", "load_checkpoint", "load_best", "name",
"restore_training", "verbosity",
"logfile", "config_validation", "optimize_config",
"pruning", "validate", "test", "num_threads"]
parser = Trainer.add_argparse_args(parser)
args = parser.parse_args()
run_test = False
if args.test:
run_test = True
verbosity = args.verbosity
config_file = args.config
config_file = check_config(config_file, CONFIG_DIR)
# if args.config_validation:
# valid_file = args.config_validation
# else:
# valid_file = CONFIG_VALIDATION
# read config
with open(config_file) as json_data_file:
config = json.load(json_data_file)
# validate config
# if not os.path.exists(valid_file):
# print("WARNING: Could not find config validation file. Search path is set to {}".format(CONFIG_VALIDATION))
# else:
# ValidateUtility.validate_config(config, valid_file)
# convert dict to object recursively for easy call
config = util.DictionaryUtility.to_object(config)
if not hasattr(config, "system_config"): raise IOError("Config file must contain system_config")
if not hasattr(config, "dataset_config"): raise IOError("Config file must contain dataset_config")
if not hasattr(config.dataset_config, "paths"): raise IOError("Dataset config must contain paths list")
model_name, model_folder = get_model_folder(config)
if hasattr(config, "run_config"):
if not hasattr(config.run_config, "exp_name"):
counter = 1
exp_name = "experiment_{0}".format(counter)
while exists(save_path(model_folder, model_name, exp_name)):
counter += 1
exp_name = "experiment_{0}".format(counter)
config.run_config.exp_name = exp_name
if args.name:
config.run_config.exp_name = args.name
main_logger = setup_logger(args)
logging.debug('Command line arguments: %s' % str(sys.argv))
logging.info('=======================================================')
logging.info('Using system from %s' % config_file)
logging.info('=======================================================')
if args.num_threads:
torch.set_num_threads(args.num_threads)
if args.validate:
ModelValidation.validate(config)
if args.auto_lr_find:
setattr(args, "auto_lr_find", True)
if args.optimize_config or hasattr(config, "optuna_config"):
set_pruning = args.pruning
opt_config = args.optimize_config
trainer_args = vars(args)
for non_trainer_arg in non_trainer_args:
del trainer_args[non_trainer_arg]
if opt_config:
logging.info('Running optimization routine using optuna config file: %s' % str(opt_config))
opt_config = check_config(opt_config, CONFIG_DIR)
with open(opt_config) as f:
opt_config = json.load(f)
opt_config = util.DictionaryUtility.to_object(opt_config)
m = ModelOptimization(opt_config, config, model_folder, trainer_args)
else:
logging.info('Running optimization routine using optuna_config')
m = ModelOptimization(config.optuna_config, config, model_folder, trainer_args)
m.run_study(pruning=set_pruning)
else:
tb_folder = join(model_folder, "runs")
if not os.path.exists(tb_folder):
os.mkdir(tb_folder)
exp_folder = join(tb_folder, config.run_config.exp_name)
if not os.path.exists(exp_folder):
os.mkdir(exp_folder)
load_checkpoint = None
if args.load_best:
load_checkpoint = retrieve_model_checkpoint(exp_folder, model_name, config.run_config.exp_name)
if args.load_checkpoint:
load_checkpoint = args.load_checkpoint
if args.restore_training and load_checkpoint:
vnum = get_tb_logdir_version(load_checkpoint)
if vnum:
logger = TensorBoardLogger(tb_folder, name=config.run_config.exp_name, version=vnum,
default_hp_metric=False)
main_logger.info("Utilizing existing log directory {}".format(logger.log_dir))
else:
logger = TensorBoardLogger(tb_folder, name=config.run_config.exp_name, default_hp_metric=False)
else:
logger = TensorBoardLogger(tb_folder, name=config.run_config.exp_name, default_hp_metric=False)
log_folder = logger.log_dir
if not os.path.exists(log_folder):
os.makedirs(log_folder, exist_ok=True)
write_run_info(log_folder)
psd_callbacks = PSDCallbacks(config)
trainer_args = vars(args)
for non_trainer_arg in non_trainer_args:
if non_trainer_arg == "restore_training" and load_checkpoint:
main_logger.info("Training is set to resume from model checkpoint {}".format(load_checkpoint))
trainer_args["resume_from_checkpoint"] = load_checkpoint
del trainer_args[non_trainer_arg]
trainer_args = psd_callbacks.set_args(trainer_args)
checkpoint_callback = \
ModelCheckpoint(
dirpath=log_folder,
filename='{epoch}-{val_loss:.2f}',
monitor="val_loss")
if trainer_args["profiler"] or verbosity >= 5:
if verbosity >= 5:
profiler = AdvancedProfiler(output_filename=join(log_folder, "profile_results.txt"))
else:
profiler = SimpleProfiler(output_filename=join(log_folder, "profile_results.txt"))
trainer_args["profiler"] = profiler
trainer_args["logger"] = logger
trainer_args["default_root_dir"] = model_folder
set_default_trainer_args(trainer_args, config)
save_config(config, log_folder, config.run_config.exp_name, "config")
# save_config(DictionaryUtility.to_object(trainer_args), log_folder,
# config.run_config.exp_name, "train_args")
modules = ModuleUtility(config.run_config.imports)
if load_checkpoint:
main_logger.info("Loading model checkpoint {}".format(load_checkpoint))
runner = modules.retrieve_class(config.run_config.run_class).load_from_checkpoint(load_checkpoint,
config=config)
else:
runner = modules.retrieve_class(config.run_config.run_class)(config)
data_module = choose_data_module(config, runner.device)
psd_callbacks.add_callback(checkpoint_callback)
trainer = Trainer(**trainer_args, callbacks=psd_callbacks.callbacks)
if "auto_lr_find" in trainer_args.keys():
if trainer_args["auto_lr_find"]:
lr_finder = trainer.tuner.lr_find(runner, datamodule=data_module)
new_lr = lr_finder.suggestion()
print("Setting learning rate to suggested learning rate: {}".format(new_lr))
runner.lr = new_lr
# trainer.tune(runner, datamodule=data_module)
trainer.fit(runner, datamodule=data_module)
if run_test:
trainer.test()
if __name__ == '__main__':
main()