-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
54 lines (39 loc) · 1.35 KB
/
util.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
#!/usr/bin/env python
import time
from termcolor import colored
class AverageMeter(object):
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 tic(self):
self.t_tic = time.time()
def toc(self):
self.update(time.time() - self.t_tic)
def __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
class ProgressMeter(object):
def __init__(self, itr, prefix="", color='yellow', *meters):
self.progress_fmtstr = self._get_progress_fmtstr(itr)
self.meters = meters
self.prefix = prefix
self.color = color
def print_progress(self, itr):
entries = [colored('[{:9}]'.format(self.prefix),self.color) + self.progress_fmtstr.format(itr)]
entries += [str(meter) for meter in self.meters]
print('\t'.join(entries))
def _get_progress_fmtstr(self, itr):
num_digits = len(str(itr // 1))
fmt = '{:' + str(num_digits) + 'd}'
return '[' + fmt + '/' + fmt.format(itr) + ']'