-
Notifications
You must be signed in to change notification settings - Fork 4
/
methods.py
381 lines (327 loc) · 13.6 KB
/
methods.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
Different explanation methods from the Integrated-Gradient Optimized Saliency map methods.
© copyright Tyler Lawson, Saeed khorram. https://github.com/saeed-khorram/IGOS
"""
from torch.autograd import Variable
from methods_helper import *
def IGOS(
model,
model_name,
init_mask,
image,
baseline,
label,
L1=1,
L2=20,
size=28,
ig_iter=15,
iterations=20,
alpha=8,
opt='LS',
softmax=True,
**kwargs
):
"""
Generates explanation by optimizing a mask with integrated gradient.
Paper title: Visualizing Deep Networks by Optimizing with Integrated Gradients, AAAI 2020
Link to the paper: https://aaai.org/ojs/index.php/AAAI/article/view/6863/6717
:param model: The model to use for making predictions
:param model_name: The model name to use for making predictions
:param init_mask: The area in one cell of the K x K grid to use for initializing the mask
:param image: The image to be explained
:param baseline: The blured image as the baseline to use for making predictions
:param label: The predicted class index of this image
:param L1: The hyperparameter for TV/BTV norm
:param L2: The hyperparameter for TV/BTV norm
:param size: The size of the predicted mask
:param ig_iter: The step size of the integtated gradient accumulation
:param iterations: The number of iterations required to predict the mask
:param alpha: The step size for updating the mask
:param opt: The optimization algorithm
:param softmax: The output function for the model
:param kwargs:
:return:
"""
def regularization_loss(masks):
return L1 * torch.mean(torch.abs(1 - masks).view(masks.shape[0], -1), dim=1) + \
L2 * tv_norm(masks)
# Define loss function for regularization terms
def loss_function(up_masks, masks, indices, noise=True):
losses = interval_score(
model,
model_name,
image[indices],
baseline[indices],
label[indices],
up_masks,
ig_iter,
output_func,
noise
)
return losses.sum(dim=1).view(-1) + regularization_loss(masks)
# Create initial masks
masks = torch.ones((image.shape[0], 1, size, size), dtype=torch.float32, device='cuda')
masks = masks * init_mask.cuda()
masks = Variable(masks, requires_grad=True)
if softmax:
output_func = softmax_output
else:
logit_output.original = torch.gather(torch.nn.Sigmoid()(model(image)), 1, label.view(-1, 1))
output_func = logit_output
if opt == 'NAG':
cita=torch.zeros(1).cuda()
for i in range(iterations):
up_masks = upscale(masks)
losses = regularization_loss(masks)
losses.sum().backward()
total_grads = masks.grad.clone()
masks.grad.zero_()
# Computer the integrated gradient
integrated_gradient(model, model_name, image, baseline, label, up_masks, ig_iter, output_func)
total_grads += masks.grad.clone()
masks.grad.zero_()
if opt == 'LS':
alphas = line_search(masks, total_grads, loss_function, alpha)
# Update the mask
masks.data -= total_grads * alphas
if opt == 'NAG':
e = i / (i + 3)
cita_p = cita
cita = masks.data - alpha * total_grads
masks.data = cita + e * (cita - cita_p)
masks.grad.zero_()
masks.data.clamp_(0, 1)
return masks
def iGOS_pp(
model,
model_name,
init_mask,
image,
baseline,
label,
size=28,
iterations=15,
ig_iter=20,
L1=1,
L2=20,
alpha=1000,
opt='LS',
softmax=True,
**kwargs):
"""
Generates explanation by optimizing a separate masks for insertion and deletion.
Paper title: iGOS++: Integrated Gradient Optimized Saliency by Bilateral Perturbations
Link to the paper: https://arxiv.org/pdf/2012.15783.pdf
Paper title: Diverse Explanations for Object Detectors with Nesterov-Accelerated iGOS++
:param model: The model to use for making predictions
:param model_name: The model name to use for making predictions
:param init_mask: The area in one cell of the K x K grid to use for initializing the mask
:param image: The image to be explained
:param baseline: The blured image as the baseline to use for making predictions
:param label: The predicted class index of this image
:param size: The size of the predicted mask
:param iterations: The number of iterations required to predict the mask
:param ig_iter: The step size of the integtated gradient accumulation
:param L1: The hyperparameter for TV/BTV norm
:param L2: The hyperparameter for TV/BTV norm
:param alpha: The step size for updating the mask
:param opt: The optimization algorithm
:param softmax: The output function for the model
:param kwargs:
:return:
"""
def regularization_loss(image, masks):
return L1 * torch.mean(torch.abs(1 - masks).view(masks.shape[0], -1), dim=1) + \
L2 * bilateral_tv_norm(image, masks, tv_beta=2, sigma=0.01)
def ins_loss_function(up_masks, indices, noise=True):
losses = -interval_score(
model,
model_name,
baseline[indices],
image[indices],
label[indices],
up_masks,
ig_iter,
output_func,
noise
)
return losses.sum(dim=1).view(-1)
def del_loss_function(up_masks, indices, noise=True):
losses = interval_score(
model,
model_name,
image[indices],
baseline[indices],
label[indices],
up_masks,
ig_iter,
output_func,
noise,
)
return losses.sum(dim=1).view(-1)
def loss_function(up_masks, masks, indices):
loss = del_loss_function(up_masks[:, 0], indices)
loss += ins_loss_function(up_masks[:, 1], indices)
loss += del_loss_function(up_masks[:, 0] * up_masks[:, 1], indices)
loss += ins_loss_function(up_masks[:, 0] * up_masks[:, 1], indices)
return loss + regularization_loss(image[indices], masks[:, 0] * masks[:, 1])
masks_del = torch.ones((image.shape[0], 1, size, size), dtype=torch.float32, device='cuda')
masks_del = masks_del * init_mask.cuda()
masks_del = Variable(masks_del, requires_grad=True)
masks_ins = torch.ones((image.shape[0], 1, size, size), dtype=torch.float32, device='cuda')
masks_ins = masks_ins * init_mask.cuda()
masks_ins = Variable(masks_ins, requires_grad=True)
if softmax:
output_func = softmax_output
else:
logit_output.original = torch.gather(torch.nn.Sigmoid()(model(image)), 1, label.view(-1,1))
output_func = logit_output
if opt == 'NAG':
cita_d=torch.zeros(1).cuda()
cita_i=torch.zeros(1).cuda()
for i in range(iterations):
up_masks1 = upscale(masks_del)
up_masks2 = upscale(masks_ins)
# Compute the integrated gradient for the combined mask, optimized for deletion
integrated_gradient(model, model_name, image, baseline, label, up_masks1 * up_masks2, ig_iter, output_func)
total_grads1 = masks_del.grad.clone()
total_grads2 = masks_ins.grad.clone()
masks_del.grad.zero_()
masks_ins.grad.zero_()
# Compute the integrated gradient for the combined mask, optimized for insertion
integrated_gradient(model, model_name, baseline, image, label, up_masks1 * up_masks2, ig_iter, output_func)
total_grads1 -= masks_del.grad.clone() # Negative because insertion loss is 1 - score.
total_grads2 -= masks_ins.grad.clone()
masks_del.grad.zero_()
masks_ins.grad.zero_()
# Compute the integrated gradient for the deletion mask
integrated_gradient(model, model_name, image, baseline, label, up_masks1, ig_iter, output_func)
total_grads1 += masks_del.grad.clone()
masks_del.grad.zero_()
# Compute the integrated graident for the insertion mask
integrated_gradient(model, model_name, baseline, image, label, up_masks2, ig_iter, output_func)
total_grads2 -= masks_ins.grad.clone()
masks_ins.grad.zero_()
# Average them to balance out the terms with the regularization terms
total_grads1 /= 2
total_grads2 /= 2
# Computer regularization for combined masks
losses = regularization_loss(image, masks_del * masks_ins)
losses.sum().backward()
total_grads1 += masks_del.grad.clone()
total_grads2 += masks_ins.grad.clone()
if opt == 'LS':
masks = torch.cat((masks_del.unsqueeze(1), masks_ins.unsqueeze(1)), 1)
total_grads = torch.cat((total_grads1.unsqueeze(1), total_grads2.unsqueeze(1)), 1)
alphas = line_search(masks, total_grads, loss_function, alpha)
masks_del.data -= total_grads1 * alphas
masks_ins.data -= total_grads2 * alphas
if opt == 'NAG':
e = i / (i + 3)
cita_d_p = cita_d
cita_i_p = cita_i
cita_d = masks_del.data - alpha * total_grads1
cita_i = masks_ins.data - alpha * total_grads2
masks_del.data = cita_d + e * (cita_d - cita_d_p)
masks_ins.data = cita_i + e * (cita_i - cita_i_p)
masks_del.grad.zero_()
masks_ins.grad.zero_()
masks_del.data.clamp_(0,1)
masks_ins.data.clamp_(0,1)
return masks_del * masks_ins
def iGOS_p(
model,
model_name,
init_mask,
image,
baseline,
label,
size=28,
iterations=15,
ig_iter=20,
L1=1,
L2=20,
alpha=1000,
opt='LS',
softmax=True,
**kwargs):
"""
Similar idea to iGOS++, but generates explanation only using one mask (optimized for both insertion and deletion).
:param model: The model to use for making predictions
:param model_name: The model name to use for making predictions
:param init_mask: The area in one cell of the K x K grid to use for initializing the mask
:param image: The image to be explained
:param baseline: The blured image as the baseline to use for making predictions
:param label: The predicted class index of this image
:param size: The size of the predicted mask
:param iterations: The number of iterations required to predict the mask
:param ig_iter: The step size of the integtated gradient accumulation
:param L1: The hyperparameter for TV/BTV norm
:param L2: The hyperparameter for TV/BTV norm
:param alpha: The step size for updating the mask
:param opt: The optimization algorithm
:param softmax: The output function for the model
:param kwargs:
:return:
"""
def regularization_loss(masks):
return L1 * torch.mean(torch.abs(1-masks).view(masks.shape[0],-1), dim=1) +\
L2 * bilateral_tv_norm(image, masks, tv_beta=2, sigma=0.01)
def loss_function(up_masks, masks, indices, noise=True):
losses = -interval_score(
model,
model_name,
baseline[indices],
image[indices],
label[indices],
up_masks,
ig_iter,
output_func,
noise
)
losses += interval_score(
model,
model_name,
image[indices],
baseline[indices],
label[indices],
up_masks,
ig_iter,
output_func,
noise
)
return losses.sum(dim=1).view(-1) + regularization_loss(masks)
masks = torch.ones((image.shape[0],1,size,size), dtype=torch.float32, device='cuda')
masks = masks * init_mask.cuda()
masks = Variable(masks, requires_grad=True)
if softmax:
output_func = softmax_output
else:
logit_output.original = torch.gather(torch.nn.Sigmoid()(model(image)), 1, label.view(-1,1))
output_func = logit_output
if opt == 'NAG':
cita=torch.zeros(1).cuda()
for i in range(iterations):
total_grads = torch.zeros(image.shape[0], 1, size, size, dtype=torch.float32).cuda()
up_masks = upscale(masks)
integrated_gradient(model, model_name, image, baseline, label, up_masks, ig_iter, output_func)
total_grads += masks.grad.clone()
masks.grad.zero_()
integrated_gradient(model, model_name, baseline, image, label, up_masks, ig_iter, output_func)
total_grads += -masks.grad.clone()
masks.grad.zero_()
losses = regularization_loss(masks)
losses.sum().backward()
total_grads += masks.grad.clone()
masks.grad.zero_()
if opt == 'LS':
alphas = line_search(masks, total_grads, loss_function, alpha)
masks.data -= total_grads * alphas
if opt == 'NAG':
e = i / (i + 3)
cita_p = cita
cita = masks.data - alpha * total_grads
masks.data = cita + e * (cita - cita_p)
masks.grad.zero_()
masks.data.clamp_(0, 1)
return masks