-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
79 lines (67 loc) · 2.54 KB
/
runner.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
import os
import logging, time, yaml
from easydict import EasyDict
from argparse import ArgumentParser
from core.train import train_net
from core.test import test_net
def set_logger(log_path):
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
if not os.path.exists(log_path):
os.mkdir(log_path)
cur_time = time.strftime('%Y%m%d%H%M', time.localtime())
log_file = os.path.join(log_path, cur_time + '.log')
file_handler = logging.FileHandler(log_file, mode='w')
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
def get_config_from_file():
f = open('config.yaml', 'r')
cfg = yaml.load(f.read(), Loader=yaml.FullLoader)
f.close()
cfg = EasyDict(cfg)
return cfg
def parse_args():
parser = ArgumentParser()
parser.add_argument('--gpu', dest='gpu_id', type=str, default=None,
help='Specify \'<id>\' to use GPU with given id e.g., --gpu \'0\' uses first GPU.')
parser.add_argument('--test', dest='test', action='store_true',
help='Test the network.')
return parser.parse_args()
def import_config_from_args(cfg, args):
if args.gpu_id is not None:
cfg.DEVICE.USE_GPU = True
cfg.DEVICE.GPU_ID = args.gpu_id
if __name__ == '__main__':
cfg = get_config_from_file()
args = parse_args()
import_config_from_args(cfg, args)
set_logger(cfg.LOG_PATH)
logging.getLogger().info('start runner')
if cfg.DEVICE.USE_GPU:
os.environ['CUDA_VISIBLE_DEVICES'] = cfg.DEVICE.GPU_ID
if cfg.RANDOM_SEED is not None:
if cfg.DEVICE.USE_GPU:
os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8'
import random
import torch
import numpy as np
random.seed(cfg.RANDOM_SEED)
torch.manual_seed(cfg.RANDOM_SEED)
if cfg.DEVICE.USE_GPU:
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
if cfg.DEVICE.NGPUS == 1:
torch.cuda.manual_seed(cfg.RANDOM_SEED)
else:
torch.cuda.manual_seed_all(cfg.RANDOM_SEED)
np.random.seed(cfg.RANDOM_SEED)
if not args.test:
train_net(cfg)
else:
test_net(cfg)