-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
145 lines (110 loc) · 4.56 KB
/
run.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
from configs.base_config import Config, parse_to_dict
from evaluate import evaluate
from train import train
from utils.ans_encode import ans_encode
from utils.coco_extract import coco_extract
from utils.misc import set_seed
import argparse
import os
import yaml
def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Training Arguments')
parser.add_argument('--RUN', dest='RUN_MODE',
choices=['train', 'eval', 'test'],
help='{train, eval, test}',
type=str, required=True)
parser.add_argument('--CONFIG', dest='CONFIG_NAME',
help='configuration for model building located at configs/, e.g. bert_hiecoatt, '
'bert_hiecoatt_shared, bert_mcoatt',
type=str, required=True)
parser.add_argument('--SPLIT', dest='TRAIN_SPLIT',
choices=['train', 'train+val'],
help="set training split, eg.'train', 'train+val'",
type=str)
parser.add_argument('--VAL_MODE', dest='VAL_MODE',
choices=['val-half', 'val'],
help="set val to full or half, eg. 'val-half', 'val'",
type=str)
parser.add_argument('--EVAL', dest='EVAL',
help="evaluate model after training",
type=bool)
parser.add_argument('--PRELOAD', dest='PRELOAD',
help='pre-load the image features into memory to increase the I/O speed',
type=bool)
parser.add_argument('--SAVE_PERIOD', dest='SAVE_PERIOD',
help='after how many epochs to save checkpoint',
type=int)
parser.add_argument('--TRAIN_STEP', dest='TRAIN_STEP_SIZE',
help='how many train steps per epoch',
type=int)
parser.add_argument('--VAL_STEP', dest='VAL_STEP_SIZE',
help='how many val steps per epoch',
type=int)
parser.add_argument('--SEED', dest='SEED',
help='fix random seed',
type=int)
parser.add_argument('--VERSION', dest='VERSION',
help='version control',
type=str)
parser.add_argument('--DATA_DIR', dest='DATA_DIR',
help='data root path',
type=str)
parser.add_argument('--OUTPUT_DIR', dest='OUTPUT_DIR',
help='output/results root path',
type=str)
parser.add_argument('--CHECKPOINT_DIR', dest='CHECKPOINT_DIR',
help='checkpoint root path',
type=str)
parser.add_argument('--FEATURES_DIR', dest='FEATURES_DIR',
help='image features root path',
type=str)
parser.add_argument('--START_EPOCH', dest='START_EPOCH',
help='initial epoch of model before training/ epoch of model to evaluate on',
type=int)
parser.add_argument('--NUM_EPOCHS', dest='NUM_EPOCHS',
help='number of epochs to train model for/',
type=int)
parser.add_argument('--VERBOSE', dest='VERBOSE',
help='verbose print',
type=bool)
args = parser.parse_args()
return args
def run(C):
set_seed(C.SEED)
coco_extract(C)
ans_encode(C)
if C.RUN_MODE == "train":
model = train(C)
if C.EVAL:
if C.SPLIT == 'train':
C.RUN_MODE = "eval"
else:
C.RUN_MODE = "test"
evaluate(C, model)
else:
evaluate(C)
if __name__ == "__main__":
print("Loading configuration...")
C = Config()
args = parse_args()
args_dict = parse_to_dict(args)
config_path = os.path.join("configs", f"{args_dict['CONFIG_NAME']}.yml")
if os.path.exists(config_path):
cfg_file = config_path
with open(cfg_file, 'r') as f:
yaml_dict = yaml.safe_load(f)
else:
print(f"ERROR: Config file does not exist. Given {args_dict['CONFIG_NAME']}.")
exit(-1)
args_dict = {**yaml_dict, **args_dict}
C.add_args(args_dict)
C.set_paths()
if 'FEATURES_DIR' in args_dict.keys():
C.FEATURES_DIR = args_dict['FEATURES_DIR']
print("Configuration loaded.")
print("\nRunning program...")
run(C)
print("Program done.")