-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
204 lines (161 loc) · 6.28 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import numpy as np
import json
from os.path import join
class MeanTopKRecallMeter(object):
def __init__(self, num_classes, k=5):
self.num_classes = num_classes
self.k = k
self.reset()
def reset(self):
self.tps = np.zeros(self.num_classes)
self.nums = np.zeros(self.num_classes)
def add(self, scores, labels):
tp = (np.argsort(scores, axis=1)[:, -self.k:] == labels.reshape(-1, 1)).max(1)
for l in np.unique(labels):
self.tps[l]+=tp[labels==l].sum()
self.nums[l]+=(labels==l).sum()
def value(self):
recalls = (self.tps/self.nums)[self.nums>0]
if len(recalls)>0:
return recalls.mean()*100
else:
return None
class ValueMeter(object):
def __init__(self):
self.sum = 0
self.total = 0
def add(self, value, n):
self.sum += value*n
self.total += n
def value(self):
return self.sum/self.total
class ArrayValueMeter(object):
def __init__(self, dim=1):
self.sum = np.zeros(dim)
self.total = 0
def add(self, arr, n):
self.sum += arr*n
self.total += n
def value(self):
val = self.sum/self.total
if len(val) == 1:
return val[0]
else:
return val
def topk_accuracy(scores, labels, ks, selected_class=None):
"""Computes TOP-K accuracies for different values of k
Args:
rankings: numpy ndarray, shape = (instance_count, label_count)
labels: numpy ndarray, shape = (instance_count,)
ks: tuple of integers
Returns:
list of float: TOP-K accuracy for each k in ks
"""
if selected_class is not None:
idx = labels == selected_class
scores = scores[idx]
labels = labels[idx]
rankings = scores.argsort()[:, ::-1]
# trim to max k to avoid extra computation
maxk = np.max(ks)
# compute true positives in the top-maxk predictions
tp = rankings[:, :maxk] == labels.reshape(-1, 1)
# trim to selected ks and compute accuracies
return [tp[:, :k].max(1).mean() for k in ks]
def log(mode, epoch, loss_meter, accuracy_meter, best_perf=None, green=False):
if green:
print('\033[92m', end="")
print(
f"[{mode}] Epoch: {epoch:0.2f}. "
f"Loss: {loss_meter.value():.2f}. "
f"Accuracy: {accuracy_meter.value():.2f}% ", end="")
if best_perf:
print(f"[best: {best_perf:0.2f}]%", end="")
print('\033[0m')
def topk_accuracy_multiple_timesteps(preds, labels, ks=(1, 5)):
accs = np.array(list(
zip(*[topk_accuracy(preds[:, t, :], labels, ks) for t in range(preds.shape[1])])))
return accs
def get_marginal_indexes(actions, mode):
"""For each verb/noun retrieve the list of actions containing that verb/name
Input:
mode: "verb" or "noun"
Output:
a list of numpy array of indexes. If verb/noun 3 is contained in actions 2,8,19,
then output[3] will be np.array([2,8,19])
"""
vi = []
for v in range(actions[mode].max()+1):
vals = actions[actions[mode] == v].index.values
if len(vals) > 0:
vi.append(vals)
else:
vi.append(np.array([0]))
return vi
def marginalize(probs, indexes):
mprobs = []
for ilist in indexes:
mprobs.append(probs[:, ilist].sum(1))
return np.array(mprobs).T
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
xx = x
x = x.reshape((-1, x.shape[-1]))
e_x = np.exp(x - np.max(x, 1).reshape(-1, 1))
res = e_x / e_x.sum(axis=1).reshape(-1, 1)
return res.reshape(xx.shape)
def topk_recall(scores, labels, k=5, classes=None):
unique = np.unique(labels)
if classes is None:
classes = unique
else:
classes = np.intersect1d(classes, unique)
recalls = 0
#np.zeros((scores.shape[0], scores.shape[1]))
for c in classes:
recalls += topk_accuracy(scores, labels, ks=(k,), selected_class=c)[0]
return recalls/len(classes)
def topk_recall_multiple_timesteps(preds, labels, k=5, classes=None):
accs = np.array([topk_recall(preds[:, t, :], labels, k, classes)
for t in range(preds.shape[1])])
return accs.reshape(1, -1)
def tta(scores, labels):
"""Implementation of time to action curve"""
rankings = scores.argsort()[..., ::-1]
comparisons = rankings == labels.reshape(rankings.shape[0], 1, 1)
cum_comparisons = np.cumsum(comparisons, 2)
cum_comparisons = np.concatenate([cum_comparisons, np.ones(
(cum_comparisons.shape[0], 1, cum_comparisons.shape[2]))], 1)
time_stamps = np.array([2.0, 1.75, 1.5, 1.25, 1.0, 0.75, 0.5, 0.25, 0])
return np.nanmean(time_stamps[np.argmax(cum_comparisons, 1)], 0)[4]
def predictions_to_json(verb_scores, noun_scores, action_scores, action_ids, a_to_vn, top_actions=100, version='0.2', sls=None):
"""Save verb, noun and action predictions to json for submitting them to the EPIC-Kitchens leaderboard"""
predictions = {'version': version,
'challenge': 'action_anticipation', 'results': {}}
if sls is None:
predictions['sls_pt'] = 1
predictions['sls_tl'] = 4
predictions['sls_td'] = 3
else:
predictions['sls_pt'] = sls[0]
predictions['sls_tl'] = sls[1]
predictions['sls_td'] = sls[2]
# sort our verb score and noun score separately
# noun scores
# verb scores
row_idxs = np.argsort(action_scores)[:, ::-1]
top_100_idxs = row_idxs[:, :top_actions]
action_scores = action_scores[np.arange(
len(action_scores)).reshape(-1, 1), top_100_idxs]
for i, v, n, a, ai in zip(action_ids, verb_scores, noun_scores, action_scores, top_100_idxs):
predictions['results'][str(i)] = {}
predictions['results'][str(i)]['verb'] = {str(
ii): float(vv) for ii, vv in enumerate(v)}
predictions['results'][str(i)]['noun'] = {str(
ii): float(nn) for ii, nn in enumerate(n)}
predictions['results'][str(i)]['action'] = {
"%d,%d" % a_to_vn[ii]: float(aa) for ii, aa in zip(ai, a)}
return predictions
def save_predictions(predictions, filename):
with open(join(filename + f"_test.json"), 'w') as f:
f.write(json.dumps(predictions, indent=4, separators=(',', ': ')))