-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrelevance_scores.py
259 lines (228 loc) · 11.8 KB
/
relevance_scores.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import numpy as np
import torch
from models import *
from model_relprop import *
from utils_1 import *
def forward_hook(self, input, output):
self.X = input[0]
self.Y = output
device = 'cuda' if torch.cuda.is_available() else 'cpu'
#####################################################################################################
def rscore_layer_vgg(net, trainloader, layers, classes,f,scale):
model_VGG = VGG16Net_cifar(net).cuda()
for i in range(0, len(model_VGG.layers)):
model_VGG.layers[i].register_forward_hook(forward_hook)
model_VGG.eval()
feature_score = np.zeros((classes, f,len(layers)))
csize = np.zeros(classes)
with torch.no_grad():
for idx, (input, label) in enumerate(trainloader):
input, label = input.to(device), label.to(device)
output_VGG = model_VGG(input)
pred_VGG = output_VGG.data.max(1, keepdim=True)[1] # get the index of the max log-probability
outputs = pred_VGG
T_VGG = label.cpu().numpy()
T_VGG = (T_VGG[:,np.newaxis] == np.arange(classes))*1.0
T_VGG = torch.from_numpy(T_VGG).type(torch.cuda.FloatTensor)
LRP_VGG = model_VGG.relprop(T_VGG)
k=0
for layer in layers:
score = model_VGG.layers[layer+1].Rscore
score = score.view(score.size(0),score.size(1),-1)
score = torch.mean(score,2)
score = score.cpu().detach().numpy()
for i in range(0,input.size(0)):
feature_score[label[i],:,k]+= score[i,:]
if k==0:
csize[label[i]]+=1
k +=1
# process the relevance scores
feature_scores = processes_scores(feature_score, classes, scale, csize)
return feature_scores
#####################################################################################################
def rscore_layer_res56(net, trainset, classes,scale):
model = RES56Net(net).cuda()
model.eval()
feature_score1 = np.zeros((classes,64,18))
feature_score2 = np.zeros((classes,32,18))
feature_score3 = np.zeros((classes,16,18))
csize = np.zeros(classes)
# store the activations using forward hook function
for name, module in model.named_modules():
module.register_forward_hook(forward_hook)
# set of layers which need the feature relevance scores i.e. all the conv layers
layers = []
for name, module in model.named_modules():
if name[-5:]=='lay.2' or name[-5:]=='res.0':
layers.append(module)
train_loader = torch.utils.data.DataLoader(trainset, batch_size=256, shuffle=True, num_workers=2, pin_memory=True)
with torch.no_grad():
for idx, (input, label) in enumerate(train_loader):
input, label = input.to(device), label.to(device)
output = model(input)
csize += 1
T = label.cpu().numpy()
T = (T[:,np.newaxis] == np.arange(classes))*1.0
T = torch.from_numpy(T).type(torch.cuda.FloatTensor)
LRP = model.relprop(T)
for i in range(0,54):
score = layers[i].Rscore
score = score.view(score.size(0),score.size(1),-1)
score = torch.mean(score,2)
score = score.cpu().detach().numpy()
for j in range(0, input.size(0)):
if ~np.isnan(score[j,:]).any():
if i in range(0,18):
feature_score3[label[j],0:,17-i] += score[j,:]
elif i in range(18,36):
feature_score2[label[j],0:,35-i] += score[j,:]
else:
feature_score1[label[j],0:,53-i] += score[j,:]
if i==0:
csize[label[j]]+=1
# process the relevance scores
feature_score1 = processes_scores(feature_score1, classes, scale, csize)
feature_score2 = processes_scores(feature_score2, classes, scale, csize)
feature_score3 = processes_scores(feature_score3, classes, scale, csize)
return feature_score1, feature_score2, feature_score3
#####################################################################################################
def rscore_layer_res110(net, trainset, classes, scale):
model = RES110Net(net).cuda()
model.eval()
feature_score1 = np.zeros((classes,64,36))
feature_score2 = np.zeros((classes,32,36))
feature_score3 = np.zeros((classes,16,36))
csize = np.zeros(classes)
# store the activations using forward hook function
for name, module in model.named_modules():
module.register_forward_hook(forward_hook)
# set of layers which need the feature relevance scores i.e. all the conv layers
layers = []
for name, module in model.named_modules():
if name[-5:]=='lay.2' or name[-5:]=='res.0':
layers.append(module)
train_loader = torch.utils.data.DataLoader(trainset, batch_size=256, shuffle=True, num_workers=2,pin_memory=True)
with torch.no_grad():
for idx, (input, label) in enumerate(train_loader):
input, label = input.to(device), label.to(device)
output = model(input)
csize += 1
T = label.cpu().numpy()
T = (T[:,np.newaxis] == np.arange(classes))*1.0
T = torch.from_numpy(T).type(torch.cuda.FloatTensor)
LRP = model.relprop(T)
for i in range(0,108):
score = layers[i].Rscore
score = score.view(score.size(0),score.size(1),-1)
score = torch.mean(score,2)
score = score.cpu().detach().numpy()
for j in range(0, input.size(0)):
if ~np.isnan(score[j,:]).any():
if i in range(0,36):
feature_score3[label[j],0:,35-i] += score[j,:]
elif i in range(36,72):
feature_score2[label[j],0:,71-i] += score[j,:]
else:
feature_score1[label[j],0:,107-i] += score[j,:]
if i==0:
csize[label[j]]+=1
feature_score1 = processes_scores(feature_score1, classes, scale, csize)
feature_score2 = processes_scores(feature_score2, classes, scale, csize)
feature_score3 = processes_scores(feature_score3, classes, scale, csize)
return feature_score1, feature_score2, feature_score3
#########################################################################################
def rscore_layer_res164(net, trainset, classes,scale):
model = RES164Net(net).cuda()
for name, module in model.named_modules():
module.register_forward_hook(forward_hook)
model.eval()
feature_score1 = np.zeros((classes, 256, 54))
feature_score2 = np.zeros((classes, 128, 54))
feature_score3 = np.zeros((classes, 64, 54))
csize = np.zeros(classes)
# set of layers which need the feature relevance scores i.e. all the conv layers
layers = []
for name, module in model.named_modules():
if name[-5:]=='lay.2' or name[-5:]=='lay.5' or name[-5:]=='res.0':
layers.append(module)
train_loader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=False, num_workers=2,pin_memory=True)
with torch.no_grad():
for idx, (input, label) in enumerate(train_loader):
input, label = input.to(device), label.to(device)
output = model(input)
csize += 1
T = label.cpu().numpy()
T = (T[:,np.newaxis] == np.arange(classes))*1.0
T = torch.from_numpy(T).type(torch.cuda.FloatTensor)
LRP = model.relprop(T)
for i in range(0,162):
score = layers[i].Rscore
score = score.view(score.size(0),score.size(1),-1)
score = torch.mean(score,2)
score = score.cpu().detach().numpy()
for j in range(0, input.size(0)):
if ~np.isnan(score[j,:]).any():
if i in range(0,54):
feature_score3[label[j],0:np.shape(score)[1],53-i] += score[j,:]
elif i in range(54,108):
feature_score2[label[j],0:np.shape(score)[1],107-i] += score[j,:]
else:
feature_score1[label[j],0:np.shape(score)[1],161-i] += score[j,:]
if i==0:
csize[label[j]]+=1
feature_score1 = processes_scores_v2(feature_score1, classes, scale, csize, 4)
feature_score2 = processes_scores_v2(feature_score2, classes, scale, csize, 2)
feature_score3 = processes_scores_v2(feature_score3, classes, scale, csize, 1)
return feature_score1, feature_score2, feature_score3
################################################################################
def rscore_layer_res34(net, trainset, classes, scale, batch_size, subset_size):
model = RES34Net(net).cuda()
model.eval()
feature_score1 = np.zeros((classes,512,6))
feature_score2 = np.zeros((classes,256,12))
feature_score3 = np.zeros((classes,128,8))
feature_score4 = np.zeros((classes,64,6))
csize = np.zeros(classes)
# store the activations using forward hook function
for name, module in model.named_modules():
module.register_forward_hook(forward_hook)
# set of layers which need the feature relevance scores i.e. all the conv layers
layers = []
for name, module in model.named_modules():
if name[-5:]=='lay.2' or name[-5:]=='res.0':
layers.append(module)
train_loader = torch.utils.data.DataLoader(trainset, batch_size=int(batch_size/4), shuffle=True, num_workers=2, pin_memory=True)
with torch.no_grad():
for idx, (input, label) in enumerate(train_loader):
if idx ==subset_size:
break
input, label = input.to(device), label.to(device)
output = model(input)
csize += 1
T = label.cpu().numpy()
T = (T[:,np.newaxis] == np.arange(classes))*1.0
T = torch.from_numpy(T).type(torch.cuda.FloatTensor)
LRP = model.relprop(T)
for i in range(0,32):
score = layers[i].Rscore
score = score.view(score.size(0),score.size(1),-1)
score = torch.mean(score,2)
score = score.cpu().detach().numpy()
for j in range(0, input.size(0)):
if ~np.isnan(score[j,:]).any():
if i in range(0,6):
feature_score4[label[j],:,5-i] += score[j,:]
elif i in range(6,14):
feature_score3[label[j],:,13-i] += score[j,:]
elif i in range(14,26):
feature_score2[label[j],:,25-i] += score[j,:]
else:
feature_score1[label[j],:,31-i] += score[j,:]
if i==0:
csize[label[j]]+=1
# process the relevance scores
feature_score1 = processes_scores(feature_score1, classes, scale, csize)
feature_score2 = processes_scores(feature_score2, classes, scale, csize)
feature_score3 = processes_scores(feature_score3, classes, scale, csize)
feature_score4 = processes_scores(feature_score4, classes, scale, csize)
return feature_score1, feature_score2, feature_score3, feature_score4