-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadversarial_fooling.py
More file actions
91 lines (73 loc) · 2.85 KB
/
adversarial_fooling.py
File metadata and controls
91 lines (73 loc) · 2.85 KB
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
import argparse
import torch
import random
import numpy as np
from scipy.ndimage.filters import gaussian_filter1d
import matplotlib.pyplot as plt
from PIL import Image
import os
import random
import string
from utils import *
def make_fooling_image(X, target_y, model):
X_fooling = X.clone()
X_fooling = X_fooling.requires_grad_()
learning_rate = 1
iter = 0
print_every = 10
while True:
scores = model(X_fooling)
_, idx = torch.max(scores, 1)
if (idx != target_y):
scores[:,target_y].backward()
dX = learning_rate*X_fooling.grad.data/torch.norm(X_fooling.grad.data)
X_fooling.data += dX.data
X_fooling.grad.data.zero_()
if iter % print_every == 0:
print('Iteration %d, target indices\' scores: ' % (iter), scores[:,target_y].data)
iter += 1
else:
break
return X_fooling
def get_image_fooling_grids(X, y, idx2label, idx, X_fooling, target_y):
X_fooling_np = deprocess(X_fooling.clone())
X_fooling_np = np.asarray(X_fooling_np).astype(np.uint8)
fig = plt.figure()
ax = fig.add_subplot(1, 4, 1)
ax.imshow(X[idx])
ax.set_title(idx2label[y[idx]])
ax.axis('off')
ax = fig.add_subplot(1, 4, 2)
ax.imshow(X_fooling_np)
ax.set_title(idx2label[target_y])
ax.axis('off')
ax=fig.add_subplot(1, 4, 3)
X_pre = preprocess(Image.fromarray(X[idx]))
diff = np.asarray(deprocess(X_fooling - X_pre, should_rescale=False))
ax.imshow(diff)
ax.set_title('Difference')
ax.axis('off')
ax=fig.add_subplot(1, 4, 4)
diff = np.asarray(deprocess(10 * (X_fooling - X_pre), should_rescale=False))
ax.imshow(diff)
ax.set_title('Magnified difference (10x)')
ax.axis('off')
return fig
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = 'Fooling')
parser.add_argument('--num_images', type = int, help='Number of images', required=True)
parser.add_argument('--image_index', type=int, help='Index of image whose gradient is to ascended', required=True)
parser.add_argument('--class_index', type = int, help='Class index of class whose score is to be maximized', default = random.randint(0,999))
parser.add_argument('--output_dir', help='Directory to save output', default = 'outputs')
args = parser.parse_args()
X, y, idx2label = load_imagenet_val(count=args.num_images)
model = get_pretrained_squeezenet()
X_tensor = torch.cat([preprocess(Image.fromarray(x)) for x in X], dim=0)
idx = args.image_index
target_y = args.class_index
X_fooling = make_fooling_image(X_tensor[idx:idx+1], target_y, model)
grid = get_image_fooling_grids(X, y, idx2label, idx, X_fooling ,target_y)
if not os.path.isdir(args.output_dir):
os.mkdir(args.output_dir)
random_str = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
grid.savefig(os.path.join(args.output_dir, 'Fooling_%s_%s_%s.png' % (idx2label[y[idx]], idx2label[target_y], random_str)))