-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
89 lines (71 loc) · 2.68 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
import os
import random
import argparse
import numpy as np
import torch
from utils.config import _C as cfg
from utils.logger import setup_logger
from trainer import Trainer
def main(args):
cfg_data_file = os.path.join("./configs/data", args.data + ".yaml")
cfg_model_file = os.path.join("./configs/model", args.model + ".yaml")
cfg.defrost()
cfg.merge_from_file(cfg_data_file)
cfg.merge_from_file(cfg_model_file)
cfg.merge_from_list(args.opts)
# cfg.freeze()
if cfg.output_dir is None:
cfg_name = "_".join([args.data, args.model])
opts_name = "".join(["_" + item for item in args.opts])
cfg.output_dir = os.path.join("./output", cfg_name + opts_name)
else:
cfg.output_dir = os.path.join("./output", cfg.output_dir)
print("Output directory: {}".format(cfg.output_dir))
setup_logger(cfg.output_dir)
print("** Config **")
print(cfg)
print("************")
if cfg.seed is not None:
seed = cfg.seed
print("Setting fixed seed: {}".format(seed))
random.seed(seed)
np.random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if cfg.deterministic:
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
else:
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = True
trainer = Trainer(cfg)
if cfg.model_dir is not None:
trainer.load_model(cfg.model_dir)
if cfg.zero_shot:
trainer.test()
return
if cfg.test_train == True:
if cfg.model_dir is None:
cfg.model_dir = cfg.output_dir[:cfg.output_dir.index("_test_train_True")]
print("Model directory: {}".format(cfg.model_dir))
trainer.load_model(cfg.model_dir)
trainer.test("train")
return
if cfg.test_only == True:
if cfg.model_dir is None:
cfg.model_dir = cfg.output_dir[:cfg.output_dir.index("_test_only_True")]
print("Model directory: {}".format(cfg.model_dir))
trainer.load_model(cfg.model_dir)
trainer.test()
return
trainer.train()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--data", "-d", type=str, default="", help="data config file")
parser.add_argument("--model", "-m", type=str, default="", help="model config file")
parser.add_argument("opts", default=None, nargs=argparse.REMAINDER,
help="modify config options using the command-line")
args = parser.parse_args()
main(args)