-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (54 loc) · 1.73 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
import argparse
import logging
import os
import importlib
from pathlib import Path
import run
def get_parser(**parser_kwargs):
def mode(v):
if v != 'train' and v != 'sample':
raise argparse.ArgumentTypeError('train or sample expected')
else:
return v
parser = argparse.ArgumentParser(**parser_kwargs)
parser.add_argument(
'workdir',
help='Work folder',
)
parser.add_argument(
'config',
help='The config file'
)
parser.add_argument(
'mode',
type=mode,
help='train or sample'
)
parser.add_argument(
'--sample_mode',
default='',
help='cond or uncond'
)
return parser
if __name__ == '__main__':
parser = get_parser()
args = parser.parse_args()
#Create workdir
Path(os.path.join('output', args.workdir)).mkdir(parents=True, exist_ok=True)
#Initialize logging
formatter = logging.Formatter('%(levelname)s - %(filename)s - %(asctime)s - %(message)s')
logger = logging.getLogger()
gfile_stream = open(os.path.join(os.path.join('output', args.workdir, 'stdout.txt')), 'w')
file_handler = logging.StreamHandler(gfile_stream)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.setLevel('INFO')
#Load config
config = importlib.import_module('configs.ve.' + args.config).get_config()
if args.mode == 'train':
run.train(config, os.path.join('output', args.workdir))
if args.mode == 'sample':
run.image_synthesis(config, os.path.join('output', args.workdir), args.sample_mode)