-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsubmission_json.py
141 lines (114 loc) · 5.63 KB
/
submission_json.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
import json
from pathlib import Path
import numpy as np
import pandas as pd
from epic_kitchens.meta import training_labels, test_timestamps
def softmax(x):
'''
>>> res = softmax(np.array([0, 200, 10]))
>>> np.sum(res)
1.0
>>> np.all(np.abs(res - np.array([0, 1, 0])) < 0.0001)
True
>>> res = softmax(np.array([[0, 200, 10], [0, 10, 200], [200, 0, 10]]))
>>> np.sum(res, axis=1)
array([ 1., 1., 1.])
>>> res = softmax(np.array([[0, 200, 10], [0, 10, 200]]))
>>> np.sum(res, axis=1)
array([ 1., 1.])
'''
if x.ndim == 1:
x = x.reshape((1, -1))
max_x = np.max(x, axis=1).reshape((-1, 1))
exp_x = np.exp(x - max_x)
return exp_x / np.sum(exp_x, axis=1).reshape((-1, 1))
def top_scores(scores):
top_n_scores_idx = np.argsort(scores)[:, ::-1]
top_n_scores = scores[np.arange(0, len(scores)).reshape(-1, 1), top_n_scores_idx]
return top_n_scores_idx, top_n_scores
def compute_action_scores(verb_scores, noun_scores, n=100):
top_verbs, top_verb_scores = top_scores(verb_scores)
top_nouns, top_noun_scores = top_scores(noun_scores)
top_verb_probs = softmax(top_verb_scores)
top_noun_probs = softmax(top_noun_scores)
action_probs_matrix = top_verb_probs[:, :n, np.newaxis] * top_noun_probs[:, np.newaxis, :]
instance_count = action_probs_matrix.shape[0]
action_ranks = action_probs_matrix.reshape(instance_count, -1).argsort(axis=-1)[:, ::-1]
verb_ranks_idx, noun_ranks_idx = np.unravel_index(action_ranks[:, :n],
dims=(action_probs_matrix.shape[1:]))
# TODO: Reshape, argsort, then convert back to verb/noun indices
segments = np.arange(0, instance_count).reshape(-1, 1)
return ((top_verbs[segments, verb_ranks_idx], top_nouns[segments, noun_ranks_idx]),
action_probs_matrix.reshape(instance_count, -1)[segments, action_ranks[:, :n]])
def action_scores_to_json(actions, scores, prior):
entries = []
for verbs, nouns, segment_scores in zip(*actions, scores):
if prior is None:
entries.append({"{},{}".format(verb, noun): float(score) for verb, noun, score in zip(verbs, nouns, segment_scores)})
else:
entries.append({"{},{}".format(verb, noun): (float(prior[(verb, noun)]) if (verb, noun) in prior else 0.0) * float(score) for verb, noun, score in zip(verbs, nouns, segment_scores)})
return entries
def scores_to_json(scores):
entries = []
for classes, segment_scores in zip(*top_scores(scores)):
entries.append({str(cls): float(score) for cls, score in zip(classes, segment_scores)})
return entries
def compute_score_dicts(results, prior):
verb_scores = results['scores']['verb']
if len(verb_scores.shape) == 4:
verb_scores = verb_scores.mean(axis=(1, 2))
noun_scores = results['scores']['noun']
if len(noun_scores.shape) == 4:
noun_scores = noun_scores.mean(axis=(1, 2))
actions, action_scores = compute_action_scores(verb_scores, noun_scores)
verb_scores_dict = scores_to_json(verb_scores)
noun_scores_dict = scores_to_json(noun_scores)
action_scores_dict = action_scores_to_json(actions, action_scores, prior)
return verb_scores_dict, noun_scores_dict, action_scores_dict
def to_json(uids, verb_scores_dict, noun_scores_dict, action_scores_dict):
entries = {}
for uid, segment_verb_scores_dict, segment_noun_scores_dict, segment_action_scores_dict in zip(uids,
verb_scores_dict,
noun_scores_dict,
action_scores_dict):
entries[str(uid)] = {
'verb': segment_verb_scores_dict,
'noun': segment_noun_scores_dict,
'action': segment_action_scores_dict
}
return {
'version': '0.1',
'challenge': 'action_recognition',
'results': entries,
}
def dump_scores_to_json(results, uids, filepath, prior):
verb_scores_dict, noun_scores_dict, action_scores_dict = compute_score_dicts(results, prior)
results_dict = to_json(uids, verb_scores_dict, noun_scores_dict, action_scores_dict)
filepath.parent.mkdir(exist_ok=True, parents=True)
with open(filepath, 'w', encoding='utf8') as f:
json.dump(results_dict, f)
return results_dict
def main(args):
if not args.submission_json.exists():
args.submission_json.mkdir(parents=True, exist_ok=True)
for test_set in ['seen', 'unseen']:
if test_set == 'unseen':
action_counts = training_labels().apply(lambda d: (d['verb_class'], d['noun_class']), axis=1).value_counts()
prior_action = action_counts.div(action_counts.sum())
prior = prior_action
else:
prior = None
results = pd.read_pickle(args.results_dir / ('test_' + test_set + '.pkl'))
uids = np.zeros(results['scores']['verb'].shape[0], dtype=np.int)
timestamps = test_timestamps(test_set)
for i, (idx, row) in enumerate(timestamps.iterrows()):
uids[i] = str(idx)
dump_scores_to_json(results, uids, args.submission_json / (test_set + '.json'), prior)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description="Produce submission JSON from results pickle",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("results_dir", type=Path)
parser.add_argument("submission_json", type=Path)
main(parser.parse_args())