-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.py
79 lines (62 loc) · 2.41 KB
/
utils.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
from __future__ import absolute_import, division, print_function
import os
import logging
import torch
# import argparse
''''
class Logger:
class Parser:
'''
class Parser:
def __init__(self, parser):
self.__parser = parser
self.__args = parser.parse_args()
# set gpu ids
str_ids = self.__args.gpu_ids.split(',')
self.__args.gpu_ids = []
for str_id in str_ids:
id = int(str_id)
if id >= 0:
self.__args.gpu_ids.append(id)
# if len(self.__args.gpu_ids) > 0:
# torch.cuda.set_device(self.__args.gpu_ids[0])
def get_parser(self):
return self.__parser
def get_arguments(self):
return self.__args
def write_args(self):
params_dict = vars(self.__args)
log_dir = os.path.join(params_dict['dir_log'], params_dict['scope'], params_dict['name_data'])
args_name = os.path.join(log_dir, 'args.txt')
if not os.path.exists(log_dir):
os.makedirs(log_dir)
with open(args_name, 'wt') as args_fid:
args_fid.write('----' * 10 + '\n')
args_fid.write('{0:^40}'.format('PARAMETER TABLES') + '\n')
args_fid.write('----' * 10 + '\n')
for k, v in sorted(params_dict.items()):
args_fid.write('{}'.format(str(k)) + ' : ' + ('{0:>%d}' % (35 - len(str(k)))).format(str(v)) + '\n')
args_fid.write('----' * 10 + '\n')
def print_args(self, name='PARAMETER TABLES'):
params_dict = vars(self.__args)
print('----' * 10)
print('{0:^40}'.format(name))
print('----' * 10)
for k, v in sorted(params_dict.items()):
if '__' not in str(k):
print('{}'.format(str(k)) + ' : ' + ('{0:>%d}' % (35 - len(str(k)))).format(str(v)))
print('----' * 10)
class Logger:
def __init__(self, info=logging.INFO, name=__name__):
logger = logging.getLogger(name)
logger.setLevel(info)
self.__logger = logger
def get_logger(self, handler_type='stream_handler'):
if handler_type == 'stream_handler':
handler = logging.StreamHandler()
log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(log_format)
else:
handler = logging.FileHandler('utils.log')
self.__logger.addHandler(handler)
return self.__logger