-
Notifications
You must be signed in to change notification settings - Fork 13
/
stats.py
111 lines (81 loc) · 2.62 KB
/
stats.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
# This file contains basic statistics functionality. All runtime values are stored
# and can be referenced by name for further usage.
# Author: Stefan Kahl, 2018, Chemnitz University of Technology
import sys
sys.path.append("..")
import copy
import time
import config as cfg
from utils import log
def clearStats(clear_all=False):
# Clears all recorded values
# Exceptions are:
# Permanent values cannot be deleted
if not 'permanent' in cfg.STATS:
cfg.STATS['permanent'] = {}
p = copy.deepcopy(cfg.STATS['permanent'])
# Static values will only be deleted if said so
if not 'static' in cfg.STATS:
cfg.STATS['static'] = {}
s = copy.deepcopy(cfg.STATS['static'])
if not clear_all:
cfg.STATS = {'static':s, 'permanent':p}
else:
cfg.STATS = {'static':{}, 'permanent':p}
# Copy values
for name in cfg.STATS['permanent']:
cfg.STATS[name] = cfg.STATS['permanent'][name]
for name in cfg.STATS['static']:
cfg.STATS[name] = cfg.STATS['static'][name]
def tic(name):
if not 'times' in cfg.STATS:
cfg.STATS['times'] = {}
cfg.STATS['times'][name] = time.time()
def toc(name):
s = int(abs(time.time() - cfg.STATS['times'][name]) * 100) / 100.0
setValue(name, s)
def setValue(name, v, mode='replace', static=False, permanent=False):
if not name in cfg.STATS:
if mode == 'replace' or mode == 'add':
cfg.STATS[name] = v
else:
cfg.STATS[name] = [v]
else:
if mode == 'append':
cfg.STATS[name].append(v)
elif mode == 'add':
cfg.STATS[name] += v
else:
cfg.STATS[name] = v
if static:
cfg.STATS['static'][name] = cfg.STATS[name]
if permanent:
cfg.STATS['permanent'][name] = cfg.STATS[name]
def getValue(name, default=-1):
if name in cfg.STATS:
return cfg.STATS[name]
else:
return default
last_update = -1
def showProgress(epoch, done=False):
global last_update
# First call?
if not 'batch_count' in cfg.STATS:
bcnt = 0
else:
bcnt = cfg.STATS['batch_count']
# Calculate number of batches to train
total_batches = cfg.STATS['sample_count'] // cfg.BATCH_SIZE + 1
# Current progess
if not done:
if bcnt == 0:
log.p(('EPOCH', epoch, '['), new_line=False)
else:
p = bcnt * 100 / total_batches
if not p % 5 and not p == last_update:
log.p('=', new_line=False)
last_update = p
else:
log.p(']', new_line=False)
# Clear on first load
clearStats(True)