-
Notifications
You must be signed in to change notification settings - Fork 14
/
ensemble.py
122 lines (108 loc) · 4.54 KB
/
ensemble.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
import argparse
import pickle
import os
import numpy as np
from tqdm import tqdm
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--dataset',
required=True,
choices={'ntu/xsub', 'ntu/xview', 'ntu120/xsub', 'ntu120/xset', 'NW-UCLA'},
help='the work folder for storing results')
parser.add_argument('--alpha',
default=1,
help='weighted summation',
type=float)
parser.add_argument('--joint-dir',
help='Directory containing "epoch1_test_score.pkl" for joint eval results')
parser.add_argument('--bone-dir',
help='Directory containing "epoch1_test_score.pkl" for bone eval results')
parser.add_argument('--joint-motion-dir', default=None)
parser.add_argument('--bone-motion-dir', default=None)
arg = parser.parse_args()
dataset = arg.dataset
if 'ntu' in arg.dataset:
## ntu
arg.alpha = [0.4, 0.6, 0.2, 0.2]
elif 'UCLA' in arg.dataset:
# ucla
arg.alpha = [0.4, 0.4, 0.3, 0.2]
if 'UCLA' in arg.dataset:
label = []
with open('./data/' + 'NW-UCLA/' + '/val_label.pkl', 'rb') as f:
data_info = pickle.load(f)
for index in range(len(data_info)):
info = data_info[index]
label.append(int(info['label']) - 1)
elif 'ntu120' in arg.dataset:
if 'xsub' in arg.dataset:
npz_data = np.load('./data/' + 'ntu120/' + 'NTU120_CSub.npz')
label = np.where(npz_data['y_test'] > 0)[1]
elif 'xset' in arg.dataset:
npz_data = np.load('./data/' + 'ntu120/' + 'NTU120_CSet.npz')
label = np.where(npz_data['y_test'] > 0)[1]
elif 'ntu' in arg.dataset:
if 'xsub' in arg.dataset:
npz_data = np.load('./data/' + 'ntu60/' + 'NTU60_CS.npz')
label = np.where(npz_data['y_test'] > 0)[1]
elif 'xview' in arg.dataset:
npz_data = np.load('./data/' + 'ntu60/' + 'NTU60_CV.npz')
label = np.where(npz_data['y_test'] > 0)[1]
else:
raise NotImplementedError
with open(os.path.join(arg.joint_dir, 'epoch1_test_score.pkl'), 'rb') as r1:
r1 = list(pickle.load(r1).items())
with open(os.path.join(arg.bone_dir, 'epoch1_test_score.pkl'), 'rb') as r2:
r2 = list(pickle.load(r2).items())
if arg.joint_motion_dir is not None:
with open(os.path.join(arg.joint_motion_dir, 'epoch1_test_score.pkl'), 'rb') as r3:
r3 = list(pickle.load(r3).items())
if arg.bone_motion_dir is not None:
with open(os.path.join(arg.bone_motion_dir, 'epoch1_test_score.pkl'), 'rb') as r4:
r4 = list(pickle.load(r4).items())
right_num = total_num = right_num_5 = 0
if arg.joint_motion_dir is not None and arg.bone_motion_dir is not None:
for i in tqdm(range(len(label))):
l = label[i]
_, r11 = r1[i]
_, r22 = r2[i]
_, r33 = r3[i]
_, r44 = r4[i]
r = r11 * arg.alpha[0] + r22 * arg.alpha[1] + r33 * arg.alpha[2] + r44 * arg.alpha[3]
rank_5 = r.argsort()[-5:]
right_num_5 += int(int(l) in rank_5)
r = np.argmax(r)
right_num += int(r == int(l))
total_num += 1
acc = right_num / total_num
acc5 = right_num_5 / total_num
elif arg.joint_motion_dir is not None and arg.bone_motion_dir is None:
arg.alpha = [0.6, 0.6, 0.4]
for i in tqdm(range(len(label))):
l = label[:, i]
_, r11 = r1[i]
_, r22 = r2[i]
_, r33 = r3[i]
r = r11 * arg.alpha[0] + r22 * arg.alpha[1] + r33 * arg.alpha[2]
rank_5 = r.argsort()[-5:]
right_num_5 += int(int(l) in rank_5)
r = np.argmax(r)
right_num += int(r == int(l))
total_num += 1
acc = right_num / total_num
acc5 = right_num_5 / total_num
else:
for i in tqdm(range(len(label))):
l = label[i]
_, r11 = r1[i]
_, r22 = r2[i]
r = r11 + r22 * arg.alpha
rank_5 = r.argsort()[-5:]
right_num_5 += int(int(l) in rank_5)
r = np.argmax(r)
right_num += int(r == int(l))
total_num += 1
acc = right_num / total_num
acc5 = right_num_5 / total_num
print('Top1 Acc: {:.4f}%'.format(acc * 100))
print('Top5 Acc: {:.4f}%'.format(acc5 * 100))