-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.py
More file actions
94 lines (75 loc) · 2.61 KB
/
Copy pathmain.py
File metadata and controls
94 lines (75 loc) · 2.61 KB
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
import torch
import os
import argparse
import wandb
import yaml
import numpy as np
import random
import ast
from train import Trainer_seg
from inference import Inferencer
from torch.cuda import is_available
from datetime import datetime
# fix seed for reproducibility
seed = 3407
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
# torch.use_deterministic_algorithms(True) # raise error if CUDA >= 10.2
torch.backends.cudnn.benchmark = False
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
def conf_to_args(args, **kwargs):
var = vars(args)
for key, value in kwargs.items():
var[key] = value
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--config_path', default=None, type=str)
arg, unknown_arg = parser.parse_known_args()
if arg.config_path is not None:
with open(arg.config_path, 'rb') as f:
conf = yaml.load(f.read(), Loader=yaml.Loader) # load the config file
conf['config_path'] = arg.config_path
else:
# make unrecognized args to dict
conf = {'config_path': 'configs/sweep_config.yaml'}
for item in unknown_arg:
item = item.strip('--')
key, value = item.split('=')
if key != 'CUDA_VISIBLE_DEVICES':
try:
if value == 'true' or value == 'false':
value = value.title()
value = ast.literal_eval(value)
except ValueError:
if value.isalpha(): pass
except SyntaxError as e:
if '/' in value: pass
else: raise e
conf[key] = value
args = argparse.Namespace()
conf_to_args(args, **conf) # pass in keyword args
now_time = datetime.now().strftime("%Y-%m-%d %H%M%S")
os.environ["CUDA_VISIBLE_DEVICES"] = args.CUDA_VISIBLE_DEVICES
if args.debug:
args.wandb = False
print('Use CUDA :', args.cuda and is_available())
if args.mode in 'train':
if args.mode == 'train':
if args.task == 'segmentation':
trainer = Trainer_seg(args, now_time)
else:
raise Exception('Invalid mode')
trainer.start_train()
elif args.mode in 'inference':
inferencer = Inferencer(args)
if args.inference_mode == 'segmentation':
inferencer.start_inference_segmentation()
else:
raise ValueError('Please select correct inference_mode !!!')
else:
print('No mode supported.')
if __name__ == "__main__":
main()