-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeepfoolSingleImageTest
105 lines (82 loc) · 2.99 KB
/
DeepfoolSingleImageTest
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
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.optim as optim
import torch.utils.data as data_utils
from torch.autograd import Variable
import math
import torchvision.models as models
from PIL import Image
from deepfool import deepfool
import os
import time
net = models.resnet34(pretrained=True)
# Switch to evaluation mode
net.eval()
im_orig = Image.open('pictures/test_im2.jpg')
mean = [ 0.485, 0.456, 0.406 ]
std = [ 0.229, 0.224, 0.225 ]
im2 = transforms.Compose([
transforms.Scale(256),
transforms.CenterCrop(224)])(im_orig)
# Remove the mean
im = transforms.Compose([
transforms.Scale(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean = mean,
std = std)])(im_orig)
#start time
start_time = time.time()
r, loop_i, label_orig, label_pert, pert_image, pert = deepfool(im, net)
end_time = time.time()
execution_time = end_time - start_time
print("execution time = " + str(execution_time))
#end time
#execution time = end time - start time
labels = open(os.path.join('synset_words.txt'), 'r').read().split('\n')
str_label_orig = labels[np.int(label_orig)].split(',')[0]
str_label_pert = labels[np.int(label_pert)].split(',')[0]
print("Original label = ", str_label_orig)
print("Perturbed label = ", str_label_pert)
def clip_tensor(A, minv, maxv):
A = torch.max(A, minv*torch.ones(A.shape))
A = torch.min(A, maxv*torch.ones(A.shape))
return A
clip = lambda x: clip_tensor(x, 0, 255)
tf = transforms.Compose([transforms.Normalize(mean=[0, 0, 0], std=list(map(lambda x: 1 / x, std))),
transforms.Normalize(list(map(lambda x: -x, mean)), std=[1, 1, 1]),
transforms.Lambda(clip),
transforms.ToPILImage(),
transforms.CenterCrop(224)])
imagetransform = transforms.Compose([transforms.Normalize(mean=[0, 0, 0], std=list(map(lambda x: 1 / x, std))),
transforms.Normalize(list(map(lambda x: -x, mean)), std=[1, 1, 1]),
transforms.Lambda(clip)])
tensortransform = transforms.Compose([transforms.Scale(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0, 0, 0], std=list(map(lambda x: 1 / x, std))),
transforms.Normalize(list(map(lambda x: -x, mean)), std=[1, 1, 1]),
transforms.Lambda(clip)])
def inv_tf_pert(r):
pert = np.sum(np.absolute(r), axis=0)
pert[pert != 0] = 1
return pert
plt.figure()
plt.imshow(im2)
plt.title(str_label_orig)
plt.show()
plt.figure()
plt.imshow(tf(pert_image.cpu()[0]))
tf(pert_image.cpu()[0]).save('dfimg.png')
plt.title(str_label_pert)
plt.show()
plt.figure()
plt.imshow(tf(pert.cpu()[0]*1))
plt.title(str_label_orig)
plt.show()
tf(pert.cpu()[0]*1).save('dfpert.png')