-
Notifications
You must be signed in to change notification settings - Fork 45
/
trainer.py
57 lines (46 loc) · 1.64 KB
/
trainer.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
import argparse
import importlib
import os
import tensorflow as tf
from train import train
def run(db, gpu, from_fold, to_fold, suffix='', random_seed=42):
# Set GPU visible to Tensorflow
os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu)
# Config file
config_file = os.path.join('config', f'{db}.py')
spec = importlib.util.spec_from_file_location("*", config_file)
config = importlib.util.module_from_spec(spec)
spec.loader.exec_module(config)
# Output directory
output_dir = f'out_{db}{suffix}'
assert from_fold <= to_fold
assert to_fold < config.params['n_folds']
# Training
for fold_idx in range(from_fold, to_fold+1):
train(
config_file=config_file,
fold_idx=fold_idx,
output_dir=os.path.join(output_dir, 'train'),
log_file=os.path.join(output_dir, f'train_{gpu}.log'),
restart=True,
random_seed=random_seed+fold_idx,
)
# Reset tensorflow graph
tf.reset_default_graph()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--db", type=str, required=True)
parser.add_argument("--gpu", type=int, required=True)
parser.add_argument("--from_fold", type=int, required=True)
parser.add_argument("--to_fold", type=int, required=True)
parser.add_argument("--suffix", type=str, default='')
parser.add_argument("--random_seed", type=int, default=42)
args = parser.parse_args()
run(
db=args.db,
gpu=args.gpu,
from_fold=args.from_fold,
to_fold=args.to_fold,
suffix=args.suffix,
random_seed=args.random_seed,
)