-
Notifications
You must be signed in to change notification settings - Fork 1
/
shapley_value.py
202 lines (156 loc) · 7.58 KB
/
shapley_value.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
import torch
import torch.nn.functional as F
import numpy as np
import random
import itertools as it
#for input
trunk_ori_index = [4, 3, 21, 2, 1]
left_hand_ori_index = [9, 10, 11, 12, 24, 25]
right_hand_ori_index = [5, 6, 7, 8, 22, 23]
left_leg_ori_index = [17, 18, 19, 20]
right_leg_ori_index = [13, 14, 15, 16]
trunk = [i - 1 for i in trunk_ori_index]
left_hand = [i - 1 for i in left_hand_ori_index]
right_hand = [i - 1 for i in right_hand_ori_index]
left_leg = [i - 1 for i in left_leg_ori_index]
right_leg = [i - 1 for i in right_leg_ori_index]
body_parts = [trunk, left_hand, right_hand, left_leg, right_leg]
trunk_ori_index_k400 = [1,2,3,4,5]
left_hand_ori_index_k400 = [6,8,10]
right_hand_ori_index_k400 = [7,9,11]
left_leg_ori_index_k400 = [12, 14, 16]
right_leg_ori_index_k400 = [13, 15, 17]
trunk_k400 = [i - 1 for i in trunk_ori_index_k400]
left_hand_k400 = [i - 1 for i in left_hand_ori_index_k400]
right_hand_k400 = [i - 1 for i in right_hand_ori_index_k400]
left_leg_k400 = [i - 1 for i in left_leg_ori_index_k400]
right_leg_k400 = [i - 1 for i in right_leg_ori_index_k400]
body_parts_k400 = [trunk_k400, left_hand_k400, right_hand_k400, left_leg_k400, right_leg_k400]
part_num = len(body_parts)
import itertools
import torch
import math
from torch import nn
import numpy as np
from tqdm import tqdm
def weight_calcu(model, data, label, spa_mask_list, average_motion):
model.eval()
replaced_idx = np.array(spa_mask_list) # e.g., [2,4,0]
remain_idx = list(set(range(part_num)).difference(set(spa_mask_list)))
m = nn.Softmax(dim=1)
r = nn.ReLU()
start_t = 0
end_t = 64
step_t = 64
average_data = torch.from_numpy(average_motion).cuda()
weight_value = []
with torch.no_grad():
data = data.float()
label = label.long()
unchanged_data = data.clone()
first_frame_data = average_data.unsqueeze(0) #1,C,T,V,M
for start_temp in range(start_t,end_t,step_t):
end_temp = start_temp+step_t
data = unchanged_data.clone()
data_with_i_part = first_frame_data.clone().repeat(data.shape[0],1,1,1,1)
data_wo_i_part = first_frame_data.clone()
for i in replaced_idx:
data_with_i_part[:,:,start_temp:end_temp,body_parts[i],:] = data[:,:,start_temp:end_temp,body_parts[i],:]
logits_with = m(model(data_with_i_part))#N,class
logits_wo = m(model(data_wo_i_part)).repeat(data.shape[0],1)#N,class
weight_value.append(r(torch.gather(logits_with, dim=1, index=label.unsqueeze(-1)) - torch.gather(logits_wo, dim=1, index=label.unsqueeze(-1))))
model.train()
return weight_value[0].squeeze(-1)
def weight_calcu_k400(model, data, label, spa_mask_list, average_motion):
model.eval()
replaced_idx = np.array(spa_mask_list) # e.g., [2,4,0]
remain_idx = list(set(range(part_num)).difference(set(spa_mask_list)))
m = nn.Softmax(dim=1)
r = nn.ReLU()
start_t = 0
end_t = 64
step_t = 64
average_data = average_motion
weight_value = []
with torch.no_grad():
data = data.float()
label = label.long()
unchanged_data = data.clone()
first_frame_data = average_data.unsqueeze(0) #1,C,T,V,M
for start_temp in range(start_t,end_t,step_t):
end_temp = start_temp+step_t
data = unchanged_data.clone()
data_with_i_part = first_frame_data.clone().repeat(data.shape[0],1,1,1,1)
data_wo_i_part = first_frame_data.clone()
for i in replaced_idx:
data_with_i_part[:,:,start_temp:end_temp,body_parts_k400[i],:] = data[:,:,start_temp:end_temp,body_parts_k400[i],:]
logits_with = m(model(data_with_i_part))#N,class
logits_wo = m(model(data_wo_i_part)).repeat(data.shape[0],1)#N,class
weight_value.append(r(torch.gather(logits_with, dim=1, index=label.unsqueeze(-1)) - torch.gather(logits_wo, dim=1, index=label.unsqueeze(-1))))
model.train()
return weight_value[0].squeeze(-1)
def weight_calcu_shapley(model, data, label, spa_mask_list, average_motion):
model.eval()
replaced_idx = np.array(spa_mask_list) # e.g., [2,4,0]
n = random.randint(0, 5 - len(replaced_idx))
remain_idx = random.sample(list(set(range(part_num)).difference(set(spa_mask_list))), n)
m = nn.Softmax(dim=1)
r = nn.ReLU()
start_t = 0
end_t = 64
step_t = 64
average_data = torch.from_numpy(average_motion).cuda()
weight_value = []
with torch.no_grad():
data = data.float()
label = label.long()
unchanged_data = data.clone()
first_frame_data = average_data.unsqueeze(0) #1,C,T,V,M
for start_temp in range(start_t,end_t,step_t):
end_temp = start_temp+step_t
data = unchanged_data.clone()
data_with_i_part = first_frame_data.clone().repeat(data.shape[0],1,1,1,1)
data_wo_i_part = first_frame_data.clone().repeat(data.shape[0],1,1,1,1)
for i in replaced_idx:
data_with_i_part[:,:,start_temp:end_temp,body_parts[i],:] = data[:,:,start_temp:end_temp,body_parts[i],:]
for i in remain_idx:
data_with_i_part[:,:,start_temp:end_temp,body_parts[i],:] = data[:,:,start_temp:end_temp,body_parts[i],:]
data_wo_i_part[:,:,start_temp:end_temp,body_parts[i],:] = data[:,:,start_temp:end_temp,body_parts[i],:]
logits_with = m(model(data_with_i_part))#N,class
logits_wo = m(model(data_wo_i_part))#N,class
weight_value.append(r(torch.gather(logits_with, dim=1, index=label.unsqueeze(-1)) - torch.gather(logits_wo, dim=1, index=label.unsqueeze(-1))))
model.train()
return weight_value[0].squeeze(-1)
def weight_calcu_shapley_k400(model, data, label, spa_mask_list, average_motion):
model.eval()
replaced_idx = np.array(spa_mask_list) # e.g., [2,4,0]
n = random.randint(0, 5 - len(replaced_idx))
remain_idx = random.sample(list(set(range(part_num)).difference(set(spa_mask_list))), n)
m = nn.Softmax(dim=1)
r = nn.ReLU()
start_t = 0
end_t = 64
step_t = 64
average_data = average_motion
weight_value = []
with torch.no_grad():
data = data.float()
label = label.long()
unchanged_data = data.clone()
first_frame_data = average_data.unsqueeze(0) #1,C,T,V,M
for start_temp in range(start_t,end_t,step_t):
end_temp = start_temp+step_t
data = unchanged_data.clone()
data_with_i_part = first_frame_data.clone().repeat(data.shape[0],1,1,1,1)
data_wo_i_part = first_frame_data.clone().repeat(data.shape[0],1,1,1,1)
for i in replaced_idx:
data_with_i_part[:,:,start_temp:end_temp,body_parts_k400[i],:] = data[:,:,start_temp:end_temp,body_parts_k400[i],:]
for i in remain_idx:
data_with_i_part[:,:,start_temp:end_temp,body_parts_k400[i],:] = data[:,:,start_temp:end_temp,body_parts_k400[i],:]
data_wo_i_part[:,:,start_temp:end_temp,body_parts_k400[i],:] = data[:,:,start_temp:end_temp,body_parts_k400[i],:]
logits_with = m(model(data_with_i_part))#N,class
logits_wo = m(model(data_wo_i_part))#N,class
#N,
weight_value.append(r(torch.gather(logits_with, dim=1, index=label.unsqueeze(-1)) - torch.gather(logits_wo, dim=1, index=label.unsqueeze(-1))))
model.train()
return weight_value[0].squeeze(-1)