forked from lonePatient/lookahead_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progressbar.py
51 lines (48 loc) · 1.85 KB
/
progressbar.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
import time
class ProgressBar(object):
def __init__(self, n_batch,width=30):
self.width = width
self.n_batch = n_batch
self.start_time = time.time()
def batch_step(self, batch_idx, info, bar_type='Training'):
now = time.time()
current = batch_idx + 1
recv_per = current / self.n_batch
bar = f'[{bar_type}] {current}/{self.n_batch} ['
if recv_per >= 1:
recv_per = 1
prog_width = int(self.width * recv_per)
if prog_width > 0:
bar += '=' * (prog_width - 1)
if current< self.n_batch:
bar += ">"
else:
bar += '='
bar += '.' * (self.width - prog_width)
bar += ']'
show_bar = f"\r{bar}"
time_per_unit = (now - self.start_time) / current
if current < self.n_batch:
eta = time_per_unit * (self.n_batch - current)
if eta > 3600:
eta_format = ('%d:%02d:%02d' %
(eta // 3600, (eta % 3600) // 60, eta % 60))
elif eta > 60:
eta_format = '%d:%02d' % (eta // 60, eta % 60)
else:
eta_format = '%ds' % eta
time_info = f' - ETA: {eta_format}'
else:
if time_per_unit >= 1:
time_info = f' {time_per_unit:.1f}s/step'
elif time_per_unit >= 1e-3:
time_info = f' {time_per_unit * 1e3:.1f}ms/step'
else:
time_info = f' {time_per_unit * 1e6:.1f}us/step'
show_bar += time_info
if len(info) != 0:
show_info = f'{show_bar} ' + \
"-".join([f' {key}: {value:.4f} ' for key, value in info.items()])
print(show_info, end='')
else:
print(show_bar, end='')