-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
66 lines (52 loc) · 1.72 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
from collections import deque
import numpy as np
class AttrDict(dict):
def __getattr__(self, name):
if name in self.__dict__:
return self.__dict__[name]
elif name in self:
return self[name]
else:
raise AttributeError(name)
def __setattr__(self, name, value):
if name in self.__dict__:
self.__dict__[name] = value
else:
self[name] = value
# adapted from
# https://github.com/facebookresearch/mmf/blob/master/mmf/common/meter.py
class SmoothedValue:
"""Track a series of values and provide access to smoothed values over a
window or the global series average.
"""
def __init__(self, window_size=20):
self.window_size = window_size
self.reset()
def reset(self):
self.deque = deque(maxlen=self.window_size)
self.averaged_value_deque = deque(maxlen=self.window_size)
self.batch_sizes = deque(maxlen=self.window_size)
self.total_samples = 0
self.total = 0.0
self.count = 0
def update(self, value, batch_size):
self.deque.append(value * batch_size)
self.averaged_value_deque.append(value)
self.batch_sizes.append(batch_size)
self.count += 1
self.total_samples += batch_size
self.total += value * batch_size
@property
def median(self):
d = np.median(list(self.averaged_value_deque))
return d
@property
def avg(self):
d = np.sum(list(self.deque))
s = np.sum(list(self.batch_sizes))
return d / s
@property
def global_avg(self):
return self.total / self.total_samples
def get_latest(self):
return self.averaged_value_deque[-1]