-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathutils.py
127 lines (105 loc) · 3.62 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import io
import logging
import os
import torch
import colorlog
class TqdmToLogger(io.StringIO):
logger = None
level = None
buf = ''
def __init__(self):
super(TqdmToLogger, self).__init__()
self.logger = get_logger('tqdm')
def write(self, buf):
self.buf = buf.strip('\r\n\t ')
def flush(self):
self.logger.info(self.buf)
def get_logger(logger_name='default', debug=False, save_to_dir=None):
if debug:
log_format = (
'%(asctime)s - '
'%(levelname)s : '
'%(name)s - '
'%(pathname)s[%(lineno)d]:'
'%(funcName)s - '
'%(message)s'
)
else:
log_format = (
'%(asctime)s - '
'%(levelname)s : '
'%(name)s - '
'%(message)s'
)
bold_seq = '\033[1m'
colorlog_format = f'{bold_seq} %(log_color)s {log_format}'
colorlog.basicConfig(format=colorlog_format, datefmt='%y-%m-%d %H:%M:%S')
logger = logging.getLogger(logger_name)
if debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
if save_to_dir is not None:
fh = logging.FileHandler(os.path.join(save_to_dir, 'log', 'debug.log'))
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter(log_format)
fh.setFormatter(formatter)
logger.addHandler(fh)
fh = logging.FileHandler(
os.path.join(save_to_dir, 'log', 'warning.log'))
fh.setLevel(logging.WARNING)
formatter = logging.Formatter(log_format)
fh.setFormatter(formatter)
logger.addHandler(fh)
fh = logging.FileHandler(os.path.join(save_to_dir, 'log', 'error.log'))
fh.setLevel(logging.ERROR)
formatter = logging.Formatter(log_format)
fh.setFormatter(formatter)
logger.addHandler(fh)
return logger
class ProgressMeter(object):
def __init__(self, num_batches, meters, prefix=""):
self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
self.meters = meters
self.prefix = prefix
def display(self, batch):
entries = [self.prefix + self.batch_fmtstr.format(batch)]
entries += [str(meter) for meter in self.meters]
print('\t'.join(entries))
def _get_batch_fmtstr(self, num_batches):
num_digits = len(str(num_batches // 1))
fmt = '{:' + str(num_digits) + 'd}'
return '[' + fmt + '/' + fmt.format(num_batches) + ']'
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
# pred(correct.shape)
res = []
for k in topk:
correct_k = correct[:k].contiguous().view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)