-
Notifications
You must be signed in to change notification settings - Fork 2
/
imagenette_create_patched_input_with_saliency.py
213 lines (153 loc) · 8.84 KB
/
imagenette_create_patched_input_with_saliency.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
"""
This is to generate adv samples by applying the patch to the clean images, and also calculate its saliency map using smoothgrad
The patched image is saved as npy format and saliency map as png
"""
# code from https://github.com/A-LinCui/Adversarial_Patch_Attack
import cv2
import numpy as np
import torch
import torch.nn as nn
from PIL import Image
from torch.autograd import Variable
from torch.nn import functional as F
from tqdm import tqdm
import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision
from torchvision import models
import torchvision.utils as vutils
import argparse
import csv
import os
import numpy as np
from torchvision.utils import save_image
import random
from patch_utils import*
from utils import*
from smooth_grad import SmoothGrad
from torchvision import datasets, transforms
import torchvision.utils as vutils
from torchvision import datasets, models, transforms
from torch.utils.data import DataLoader
def gen_patched_img(patch_type, target, patch, test_loader, model, org_img_folder, adv_img_folder, org_saliency_folder, adv_saliency_folder, smooth_grad):
model.eval()
inv_normalize = transforms.Normalize(
mean=[-0.485/0.229, -0.456/0.224, -0.406/0.255],
std=[1/0.229, 1/0.224, 1/0.255]
)
test_total, test_actual_total, test_success = 0, 0, 0
for (image, label) in test_loader:
test_total += label.shape[0]
if(test_total > args.test_size):
break
assert image.shape[0] == 1, 'Only one picture should be loaded each time.'
image = image.cuda()
label = label.cuda()
output = model(image)
_, predicted = torch.max(output.data, 1)
if predicted[0] == label and predicted[0].data.cpu().numpy() != target:
test_actual_total += 1
applied_patch, mask, x_location, y_location = mask_generation(patch_type, patch, image_size=(3, 224, 224))
applied_patch = torch.from_numpy(applied_patch)
mask = torch.from_numpy(mask)
perturbated_image = torch.mul(mask.type(torch.FloatTensor), applied_patch.type(torch.FloatTensor)) + torch.mul((1 - mask.type(torch.FloatTensor)), image.type(torch.FloatTensor))
perturbated_image = perturbated_image.cuda()
output = model(perturbated_image)
_, predicted = torch.max(output.data, 1)
if predicted[0].data.cpu().numpy() == target:
test_success += 1
#print(" ====== {} input ".format(test_success))
# get saliency from org img
smooth_grad.feed_image(image)
prob, idx = smooth_grad.forward()
smooth_grad.generate(filename= org_saliency_folder + "/" + str(test_success) + "_" + str(label.item()), idx=idx[0]) # "0" means only calculate the saliency for the top label
smooth_grad.feed_image(perturbated_image)
prob, idx = smooth_grad.forward()
smooth_grad.generate(filename= adv_saliency_folder + "/" + str(test_success) + "_" + str(label.item()) , idx=idx[0])
np.save( adv_img_folder + "/" + str(test_success) + "_" + str(label.item()) + ".npy" , perturbated_image.cpu().numpy() )
np.save( org_img_folder + "/" + str(test_success) + "_" + str(label.item()) + ".npy" , image.cpu().numpy() )
print("%d successful adv samples out of %d samples"%(test_success, test_actual_total))
return test_success / test_actual_total
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', type=int, default=1, help="batch size")
parser.add_argument('--num_workers', type=int, default=2, help="num_workers")
parser.add_argument('--train_size', type=int, default=2000, help="number of training images")
parser.add_argument('--test_size', type=int, default=2000, help="number of test images")
parser.add_argument('--noise_percentage', type=float, required=True, help="percentage of the patch size compared with the image size")
parser.add_argument('--probability_threshold', type=float, default=0.5, help="minimum target probability")
parser.add_argument('--lr', type=float, default=1.0, help="learning rate")
parser.add_argument('--max_iteration', type=int, default=1000, help="max iteration")
parser.add_argument('--target', type=int, default=859, help="target label")
parser.add_argument('--epochs', type=int, default=20, help="total epoch")
parser.add_argument('--data_dir', type=str, default='imagenet-val/ILSVRC2012_img_val', help="dir of the dataset")
parser.add_argument('--GPU', type=str, default='0', help="index pf used GPU")
parser.add_argument('--log_dir', type=str, default='patch_attack_log.csv', help='dir of the log')
parser.add_argument('--patch_file', type=str, required=True, help='file to the adv patch')
parser.add_argument('--model', type=str, default='resnet18', help='model name')
parser.add_argument('--patch_type', type=str, default='square', help="patch type: rectangle or square")
parser.add_argument('--save_folder', type=str, default='.', help="directory to save the resuluting images")
parser.add_argument('--model_path', type=str, default='./imagenette/resnet18-imagenette.pt')
"for smoothgrad"
parser.add_argument('--image', type=str, required=False)
parser.add_argument('--sigma', type=float, default=0.10)
parser.add_argument('--n_samples', type=int, default=50)
parser.add_argument('--cuda', action='store_true', default=True)
parser.add_argument('--guided', action='store_true', default=False)
args = parser.parse_args()
seed = 1
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
os.environ["CUDA_VISIBLE_DEVICES"] = args.GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load the model
# setting up model
model = models.resnet18(pretrained=True)
num_features = model.fc.in_features
model.fc = nn.Linear(num_features, 10) # multi-class classification (num_of_class == 307)
model.load_state_dict(torch.load(args.model_path))
model = model.to(device)
model.eval()
data_transforms = {
'train': transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
transforms.Resize(size=(224,224)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
image_datasets = {x: datasets.ImageFolder(os.path.join(args.data_dir, x), data_transforms[x]) for x in ['train', 'val']}
train_loader = torch.utils.data.DataLoader(image_datasets['train'], batch_size=1, shuffle=False, num_workers=1)
test_loader = torch.utils.data.DataLoader(image_datasets['val'], batch_size=1, shuffle=False, num_workers=1)
# Initialize the patch
patch = np.load(args.patch_file)
print('The shape of the patch is', patch.shape)
# Setup the SmoothGrad
smooth_grad = SmoothGrad(model=model, cuda=args.cuda, sigma=args.sigma,
n_samples=args.n_samples, guided=args.guided)
test_success_rate = 0
train_success_rate = 0
save_folder2 = "{}/imagenette_{}_{}_npy_test_org_result_{}".format(args.save_folder.rstrip("/"), args.patch_type, args.target, str(args.noise_percentage).replace('.', ''))
save_folder22 = "{}/imagenette_{}_{}_npy_test_adv_result_{}".format(args.save_folder.rstrip("/"), args.patch_type, args.target, str(args.noise_percentage).replace('.', ''))
save_folder222 = "{}/imagenette_{}_{}_npy_test_org_saliency_{}".format(args.save_folder.rstrip("/"), args.patch_type, args.target, str(args.noise_percentage).replace('.', ''))
save_folder2222 = "{}/imagenette_{}_{}_npy_test_adv_saliency_{}".format(args.save_folder.rstrip("/"), args.patch_type, args.target, str(args.noise_percentage).replace('.', ''))
if(not os.path.exists(save_folder2)):
os.mkdir(save_folder2)
if(not os.path.exists(save_folder22)):
os.mkdir(save_folder22)
if(not os.path.exists(save_folder222)):
os.mkdir(save_folder222)
if(not os.path.exists(save_folder2222)):
os.mkdir(save_folder2222)
#train_success_rate = gen_patched_img(patch_type= args.patch_type, target= args.target, patch= patch, test_loader= train_loader, model= model, org_img_folder = save_folder1, adv_img_folder=save_folder11, org_saliency_folder=save_folder111, adv_saliency_folder=save_folder1111, smooth_grad=smooth_grad)
#print(" Patch attack success rate on trainset using the patch: {:.3f}%".format( 100 * train_success_rate))
test_success_rate = gen_patched_img(patch_type= args.patch_type, target= args.target, patch= patch, test_loader= test_loader, model= model, org_img_folder = save_folder2, adv_img_folder=save_folder22, org_saliency_folder=save_folder222, adv_saliency_folder=save_folder2222, smooth_grad=smooth_grad)
print(" Patch attack success rate on testset: {:.3f}%".format( 100 * test_success_rate))
print( args.patch_file )