-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
359 lines (306 loc) · 12.6 KB
/
utils.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
import torch
import numpy as np
import random
import torch
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
from matplotlib import pyplot as plt
from scipy.ndimage.filters import gaussian_filter
from tqdm import tqdm
from torch.utils.data.sampler import Sampler
from torchvision import transforms
from PIL import Image
import json
class Normalize(nn.Module):
def __init__(self, mean, std):
super(Normalize, self).__init__()
self.register_buffer('mean', torch.Tensor(mean))
self.register_buffer('std', torch.Tensor(std))
def forward(self, input):
# Broadcasting
mean = self.mean.reshape(1, 3, 1, 1)
std = self.std.reshape(1, 3, 1, 1)
return (input - mean) / std
def pre_processing(obs, torch_device):
# rescale imagenet, we do mornalization in the network, instead of preprocessing
# mean = np.array([0.485, 0.456, 0.406]).reshape([1, 1, 3])
# std = np.array([0.229, 0.224, 0.225]).reshape([1, 1, 3])
obs = obs / 255
# obs = (obs - mean) / std
obs = np.transpose(obs, (2, 0, 1))
obs = np.expand_dims(obs, 0)
obs = np.array(obs)
# if cuda:
# torch_device = torch.device('cuda:0')
# else:
# torch_device = torch.device('cpu')
obs_tensor = torch.tensor(obs, dtype=torch.float32, device=torch_device)
return obs_tensor
# %%
def fgsm_step(image, epsilon, data_grad_adv, data_grad_lab):
# generate the perturbed image based on steepest descent
grad_lab_norm = torch.norm(data_grad_lab, p=2)
delta = epsilon * data_grad_adv.sign()
# + delta because we are ascending
perturbed_image = image + delta
perturbed_rect = torch.clamp(perturbed_image, min=0, max=1)
delta = perturbed_rect - image
delta = - data_grad_lab * delta
return perturbed_rect, delta
# return perturbed_image, delta
def pgd_step(image, epsilon, model, init_pred, targeted, max_iter):
"""target here is the targeted class to be perturbed to"""
perturbed_image = image.clone()
c_delta = 0 # cumulative delta
for i in range(max_iter):
# requires grads
perturbed_image.requires_grad = True
output = model(perturbed_image)
# get the index of the max log-probability
pred = output.max(1, keepdim=True)[1]
# if attack is successful, then break
if pred.item() == targeted.item():
break
# select the false class label
output = F.softmax(output, dim=1)
loss = output[0, targeted.item()]
model.zero_grad()
loss.backward(retain_graph=True)
data_grad_adv = perturbed_image.grad.data.detach().clone()
loss_lab = output[0, init_pred.item()]
model.zero_grad()
perturbed_image.grad.zero_()
loss_lab.backward()
data_grad_lab = perturbed_image.grad.data.detach().clone()
perturbed_image, delta = fgsm_step(
image, epsilon, data_grad_adv, data_grad_lab)
c_delta += delta
return c_delta, perturbed_image
# Dummy class to store arguments
class Dummy():
pass
# Function that opens image from disk, normalizes it and converts to tensor
read_tensor = transforms.Compose([
lambda x: Image.open(x),
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
lambda x: torch.unsqueeze(x, 0)
])
# Plots image from tensor
def tensor_imshow(inp, title=None, **kwargs):
"""Imshow for Tensor."""
inp = inp.numpy().transpose((1, 2, 0))
# Mean and std for ImageNet
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
plt.imshow(inp, **kwargs)
if title is not None:
plt.title(title)
# Given label number returns class name
def get_class_name(c):
labels = json.load(open("imagenet_class_index.json"))
# labels = np.loadtxt('synset_words.txt', str, delimiter='\t')
return labels[str(c)][1]
# Image preprocessing function
preprocess = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
# Normalization for ImageNet
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
# Sampler for pytorch loader. Given range r loader will only
# return dataset[r] instead of whole dataset.
class RangeSampler(Sampler):
def __init__(self, r):
self.r = r
def __iter__(self):
return iter(self.r)
def __len__(self):
return len(self.r)
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
class AverageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def calculate(self, norm, delta_norm):
b = norm - delta_norm
a = norm
val = b / (-(a-b))
self.update(val)
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
HW = 224 * 224 # image area
n_classes = 1000
def gkern(klen, nsig):
"""Returns a Gaussian kernel array.
Convolution with it results in image blurring."""
# create nxn zeros
inp = np.zeros((klen, klen))
# set element at the middle to one, a dirac delta
inp[klen//2, klen//2] = 1
# gaussian-smooth the dirac, resulting in a gaussian filter mask
k = gaussian_filter(inp, nsig)
kern = np.zeros((3, 3, klen, klen))
kern[0, 0] = k
kern[1, 1] = k
kern[2, 2] = k
return torch.from_numpy(kern.astype('float32'))
def auc(arr):
"""Returns normalized Area Under Curve of the array."""
return (arr.sum() - arr[0] / 2 - arr[-1] / 2) / (arr.shape[0] - 1)
class CausalMetric():
def __init__(self, model, mode, step, substrate_fn):
r"""Create deletion/insertion metric instance.
Args:
model (nn.Module): Black-box model being explained.
mode (str): 'del' or 'ins'.
step (int): number of pixels modified per one iteration.
substrate_fn (func): a mapping from old pixels to new pixels.
"""
assert mode in ['del', 'ins']
self.model = model
self.mode = mode
self.step = step
self.substrate_fn = substrate_fn
def single_run(self, img_tensor, explanation, verbose=0, save_to=None,title=""):
r"""Run metric on one image-saliency pair.
Args:
img_tensor (Tensor): normalized image tensor.
explanation (np.ndarray): saliency map.
verbose (int): in [0, 1, 2].
0 - return list of scores.
1 - also plot final step.
2 - also plot every step and print 2 top classes.
save_to (str): directory to save every step plots to.
Return:
scores (nd.array): Array containing scores at every step.
"""
pred = self.model(img_tensor.cuda())
top, c = torch.max(pred, 1)
c = c.cpu().numpy()[0]
n_steps = (HW + self.step - 1) // self.step
# print('n_steps', n_steps)
if self.mode == 'del':
title = f'{title} Deletion game'
ylabel = 'Pixels deleted'
start = img_tensor.clone()
finish = self.substrate_fn(img_tensor)
elif self.mode == 'ins':
title = f'{title} Insertion game'
ylabel = 'Pixels inserted'
start = self.substrate_fn(img_tensor)
finish = img_tensor.clone()
scores = np.empty(n_steps + 1)
# Coordinates of pixels in order of decreasing saliency
salient_order = np.flip(np.argsort(
explanation.reshape(-1, HW), axis=1), axis=-1)
for i in range(n_steps+1):
pred = self.model(start.cuda())
pr, cl = torch.topk(pred, 2)
if verbose == 2:
print('{}: {:.3f}'.format(
get_class_name(cl[0][0]), float(pr[0][0])))
print('{}: {:.3f}'.format(
get_class_name(cl[0][1]), float(pr[0][1])))
scores[i] = pred[0, c]
# Render image if verbose, if it's the last step or if save is required.
if verbose == 2 or (verbose == 1 and i == n_steps):
plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.title('{} {:.1f}%, P={:.4f}'.format(
ylabel, 100 * i / n_steps, scores[i]))
plt.axis('off')
tensor_imshow(start[0])
plt.subplot(122)
plt.plot(np.arange(i+1) / n_steps, scores[:i+1])
plt.xlim(-0.1, 1.1)
plt.ylim(0, 1.05)
plt.fill_between(np.arange(i+1) / n_steps,
0, scores[:i+1], alpha=0.4)
plt.title(title)
plt.xlabel(ylabel)
plt.ylabel(get_class_name(c))
# if save_to:
# plt.savefig(save_to + '/{:03d}.png'.format(i),dpi=300)
# plt.close()
# else:
plt.savefig(save_to,dpi=300)
if i < n_steps:
coords = salient_order[:, self.step * i:self.step * (i + 1)]
start.cpu().numpy().reshape(1, 3, HW)[
0, :, coords] = finish.cpu().numpy().reshape(1, 3, HW)[0, :, coords]
return scores
def evaluate(self, img_batch, exp_batch, batch_size):
r"""Efficiently evaluate big batch of images.
Args:
img_batch (Tensor): batch of images.
exp_batch (np.ndarray): batch of explanations.
batch_size (int): number of images for one small batch.
Returns:
scores (nd.array): Array containing scores at every step for every image.
"""
n_samples = img_batch.shape[0]
predictions = torch.FloatTensor(n_samples, n_classes)
assert n_samples % batch_size == 0
for i in tqdm(range(n_samples // batch_size), desc='Predicting labels'):
preds = self.model(
img_batch[i*batch_size:(i+1)*batch_size].cuda()).cpu().detach()
predictions[i*batch_size:(i+1)*batch_size] = preds
top = np.argmax(predictions, -1)
n_steps = (HW + self.step - 1) // self.step
scores = np.empty((n_steps + 1, n_samples))
salient_order = np.flip(np.argsort(
exp_batch.reshape(n_samples,3, HW), axis=-1), axis=-1)
# print('salient_order', salient_order)
r = np.arange(n_samples).reshape(n_samples, 1)
substrate = torch.zeros_like(img_batch)
for j in tqdm(range(n_samples // batch_size), desc='Substrate'):
substrate[j*batch_size:(j+1)*batch_size] = self.substrate_fn(
img_batch[j*batch_size:(j+1)*batch_size])
if self.mode == 'del':
caption = 'Deleting '
start = img_batch.clone()
finish = substrate
elif self.mode == 'ins':
caption = 'Inserting '
start = substrate
finish = img_batch.clone()
# While not all pixels are changed
for i in tqdm(range(n_steps+1), desc=caption + 'pixels'):
# Iterate over batches
for j in range(n_samples // batch_size):
# Compute new scores
preds = self.model(start[j*batch_size:(j+1)*batch_size].cuda())
preds = preds.cpu().detach().numpy()[range(
batch_size), top[j*batch_size:(j+1)*batch_size]]
scores[i, j*batch_size:(j+1)*batch_size] = preds
# Change specified number of most salient pixels to substrate pixels
coords = salient_order[:,:,self.step * i:self.step * (i + 1)]
for n_sample in range(n_samples):
for channel in range(3):
start.cpu().numpy().reshape(n_samples, 3, HW)[n_sample,channel,coords[n_sample]] = finish.cpu().numpy().reshape(n_samples, 3, HW)[n_sample,channel,coords[n_sample]]
# print('coords', coords[n_sample].shape)
# start.cpu().numpy().reshape(n_samples, 3, HW)[n_sample,coords[n_sample]] = finish.cpu().numpy().reshape(n_samples, 3, HW)[n_sample,coords[n_sample]]
# # print(start.cpu().numpy().reshape(n_samples, 3, HW)[r,coords].shape)
# # raise NotImplementedError
# start.cpu().numpy().reshape(n_samples, 3, HW)[coords] = finish.cpu().numpy().reshape(n_samples, 3, HW)[coords]
print('AUC: {}'.format(auc(scores.mean(1))))
return scores