Repository navigation
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·91 lines (77 loc) · 2.86 KB
/
Copy pathrun.py
File metadata and controls
executable file
·91 lines (77 loc) · 2.86 KB
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
import json
import os
import random
import sys
from pathlib import Path
import numpy as np
import torch
from src.config import Config, get_option_fallback
from src.log import get_logger
from src.path import PathHandler
from src.slack import slack_is_available, slack_message
from src.trainer import Trainer
logger = get_logger(__name__, "tmp.log")
def seed_everything(seed: int = 42):
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
def check_prev_report(config_dict, projectdir):
config_name, exp_name = config_dict["config_name"], config_dict["exp_name"]
reportdir = projectdir / "output" / config_name / exp_name / "report"
checkpointdir = projectdir / "output" / config_name / exp_name / "checkpoints"
if not reportdir.exists() or not checkpointdir.exists():
return None
report_path = sorted(reportdir.glob("*/*.json"))[-1]
checkpoint_path = sorted(checkpointdir.glob("*/fall_final.model"))[-1]
return report_path, checkpoint_path
def run(configpath: Path):
project_path = os.path.dirname(os.path.realpath(__file__))
ph = PathHandler(project_path)
projectdir = ph.projectdir
codedir = Path(project_path)
with open(configpath, "r") as f:
cfg = json.load(f)
with open("config/fallback.json", "r") as f:
default_cfg = json.load(f)
default_cfg["config_name"] = configpath.parent.name
config_dict = get_option_fallback(cfg, fallback=default_cfg)
config_dict["exp_name"] = configpath.stem
if not config_dict["overwrite"] and check_prev_report(config_dict, projectdir):
report_path, checkpoint_path = check_prev_report(config_dict, projectdir)
with open(report_path, "r") as f:
report_dict = json.load(f)
config_dict = report_dict["config"]
config_dict["load_model"] = str(checkpoint_path)
config = Config(config_dict, ph=ph)
if not slack_is_available():
logger.warning("Slack message is not available.")
logger.info("Starting Experiment: {}".format(config.exp_name))
seed_everything()
trainer = Trainer(config)
if config.load_model:
trainer.evaluate_model()
trainer.evaluate_model_enwrapped()
logger.info("All evaluations done!")
return
try:
trainer.cv()
except KeyboardInterrupt as e:
print(e)
except Exception as e:
slack_message("Exception: {}".format(e))
raise e
finally:
trainer.dump_report()
logger.info("All experiments done!")
slack_message(
"All experiments done for {}.\nBest: ```{}```".format(
configpath.stem, str(trainer.report.as_dict()["best"])
)
)
if __name__ == "__main__":
config = Path(sys.argv[1])
assert config.exists(), config
run(config)