-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmeter.py
More file actions
66 lines (51 loc) · 1.66 KB
/
Copy pathmeter.py
File metadata and controls
66 lines (51 loc) · 1.66 KB
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
import torch
class CatMeter:
'''
Concatenate Meter for torch.Tensor
'''
def __init__(self):
self.reset()
def reset(self):
self.val = None
def update(self, val):
if self.val is None:
self.val = val
else:
self.val = torch.cat([self.val, val], dim=0)
def get_val(self):
return self.val
def get_val_numpy(self):
return self.val.data.cpu().numpy()
class MultiItemAverageMeter:
def __init__(self):
self.content = {}
def update(self, val):
'''
:param val: dict, keys are strs, values are torch.Tensor or np.array
'''
for key in val.keys():
value = val[key]
if key not in self.content.keys():
self.content[key] = {'avg': value, 'sum': value, 'count': 1.0}
else:
self.content[key]['sum'] += value
self.content[key]['count'] += 1.0
self.content[key]['avg'] = self.content[key]['sum'] / self.content[key]['count']
def get_val(self):
keys = self.content.keys()
values = []
for key in keys:
try:
values.append(self.content[key]['avg'].data.cpu().numpy())
except:
values.append(self.content[key]['avg'])
return keys, values
def get_str(self):
result = ''
keys, values = self.get_val()
for key, value in zip(keys, values):
result += key
result += ': '
result += str(value)
result += '; '
return result