-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
80 lines (60 loc) · 2.3 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
"""Main entry point for the retinal RL project."""
import os
import sys
import warnings
import hydra
import torch
from hydra.utils import instantiate
from omegaconf import DictConfig, OmegaConf
from runner.frameworks.classification.classification_framework import (
ClassificationFramework,
)
from runner.frameworks.framework_interface import TrainingFramework
from runner.frameworks.rl.sf_framework import SFFramework
from runner.sweep import launch_sweep
from runner.util import create_brain, delete_results
# Load the eval resolver for OmegaConf
OmegaConf.register_new_resolver("eval", eval)
# Hydra entry point
@hydra.main(config_path="config/base", config_name="config", version_base=None)
def _program(cfg: DictConfig):
# TODO: Instead of doing checks of the config here, we should implement
# sth like the configstore which ensures config parameters are present
if cfg.command == "clean":
delete_results(cfg)
sys.exit(0)
if cfg.command == "sweep":
launch_sweep(cfg)
sys.exit(0)
device = torch.device(cfg.system.device)
brain = create_brain(cfg.brain).to(device)
optimizer = instantiate(cfg.optimizer.optimizer, brain.parameters())
if hasattr(cfg.optimizer, "objective"):
objective = instantiate(cfg.optimizer.objective, brain=brain)
# TODO: RL framework currently can't use objective
else:
objective = None
warnings.warn("No objective specified, is that wanted?")
if cfg.command == "scan":
print(brain.scan())
sys.exit(0)
framework: TrainingFramework
cache_path = os.path.join(hydra.utils.get_original_cwd(), "cache")
if cfg.framework == "rl":
framework = SFFramework(cfg, data_root=cache_path)
elif cfg.framework == "classification":
framework = ClassificationFramework(cfg)
else:
raise NotImplementedError(
"only 'rl' or 'classification' framework implemented currently"
)
brain, optimizer = framework.initialize(brain, optimizer)
if cfg.command == "train":
framework.train(device, brain, optimizer, objective)
sys.exit(0)
if cfg.command == "analyze":
framework.analyze(device, brain, objective)
sys.exit(0)
raise ValueError("Invalid run_mode")
if __name__ == "__main__":
_program()