-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.py
180 lines (148 loc) · 6.27 KB
/
test.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
import numpy as np
import math
import os
import torch
from light_simulation import tube_light_generation_by_func, simple_add
from torchvision.datasets import ImageFolder
from torchvision.models import resnet50
import torchvision.transforms as transforms
import argparse
import random
import shutil
import itertools
from tqdm import tqdm
from PIL import Image
import torchvision.transforms.functional as transf
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default='./query_imagenet', help='location of the data corpus')
parser.add_argument('--portion_for_search', type=float, default=1.0, help='portion of training data')
parser.add_argument('--batch_size', type=int, default=1, help='batch size')
parser.add_argument('--trial_nums', type=int, default=300, help='number of the trials')
parser.add_argument('--model', type=str, default='resnet50', help='org model or adv trained model df_resnet50')
parser.add_argument('--output_csv', type=str, default='random_search.csv', help='number of the trials')
parser.add_argument('--save_dir', type=str, default='./results', help='dir to save results')
parser.add_argument('--save', type=str, default=False, help='Save results')
args = parser.parse_args()
# transform
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
test_transform = transforms.Compose([
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
])
# model
if args.model == 'resnet50':
print("Loading model...")
model = resnet50(pretrained=True)
elif args.model == 'df_resnet50':
print("Loading adv trained model...")
model = resnet50(pretrained=False)
model.load_state_dict(torch.load('./model/checkpoint-89.pth.tar')['state_dict'])
model.cuda()
model.eval()
# dataset
imagenet_path_list = os.listdir(args.dataset)
imagenet_dataset = []
for img_path in imagenet_path_list:
imagenet_dataset.append((img_path, int(img_path.split('.')[0])))
total_num = len(imagenet_dataset)
current_num = 0
# save
if args.save and not os.path.exists(args.save_dir):
os.mkdir(args.save_dir)
# valid_queue = torch.utils.data.DataLoader(
# imagenet_dataset, batch_size=args.batch_size,
# sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[split:total_num]),
# pin_memory=True, num_workers=16)
# for cln_image, adv_image, target, index in search_queue:
# print(target, index)
# break
# for cln_image, adv_image, target, index in search_queue:
# print(target, index)
# break
# break
acc_adv = 0
acc_cln = 0
total_q = 0
delay_threhold = 20
Q = np.asarray([[1,0,0,0],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1],
[1,1,0,0],
[1,0,1,0],
[1,0,0,1],
[0,1,1,0],
[0,1,0,1],
[0,0,1,1]
])
for image_path, target in tqdm(imagenet_dataset):
current_num += 1
img = Image.open(os.path.join(args.dataset, image_path).encode("utf-8")).convert('RGB')
# clean
clean_image = img.resize((256, 256), Image.BILINEAR)
clean_image = test_transform(clean_image).unsqueeze(0)
clean_image = clean_image.cuda()
with torch.no_grad():
org_pred_label = model(clean_image)
org_pred_label = org_pred_label.cpu().detach()
min_confidence = org_pred_label[0, target].item()
org_pred_label = org_pred_label.max(1)[1].item()
adv_image = np.asarray(img)
cur_pred_label = org_pred_label
correct_adv = org_pred_label == target
correct_cln = cur_pred_label == target
cur_search = 0
# V = np.asarray([[580, 31, 74, 400], [580, 17, 131, 200], [580, 144, 316, 600]])
# init_v = V[np.random.randint(len(V))]
params_list = []
for i in range(200):
init_v_it = [np.random.randint(380, 750), np.random.randint(0,180), np.random.randint(0,400), np.random.randint(10, 1600)]
params_list.append(init_v_it)
for init_v in params_list:
for search_i in range(delay_threhold):
q_id = np.random.randint(len(Q))
q = Q[q_id]
step_size = np.random.randint(1, 20)
q = q*step_size
for a in [-1, 1]:
cur_search += 1
#print(a*q)
temp_q = init_v + a*q
temp_q = np.clip(temp_q, [380, 0, 0, 10], [750, 180, 400, 1600])
radians = math.radians(temp_q[1])
k = round(math.tan(radians), 2)
tube_light = tube_light_generation_by_func(k, temp_q[2], alpha = 1.0, beta=temp_q[3], wavelength=temp_q[0])
tube_light = tube_light * 255.0
img_with_light = simple_add(adv_image, tube_light, 1.0)
img_with_light = np.clip(img_with_light, 0.0, 255.0).astype('uint8')
img_with_light = Image.fromarray(img_with_light)
if args.save:
img_with_light.save(os.path.join(args.save_dir, image_path))
img_with_light = img_with_light.resize((224, 224), Image.BILINEAR)
img_with_light = test_transform(img_with_light).unsqueeze(0)
img_with_light = img_with_light.cuda()
with torch.no_grad():
cur_pred_label = model(img_with_light)
cur_pred_label = cur_pred_label.cpu().detach()
cur_confidence = cur_pred_label[0, target].item()
cur_pred_label = cur_pred_label.max(1)[1].item()
if cur_confidence < min_confidence:
min_confidence = cur_confidence
init_v = temp_q
break
if cur_pred_label != org_pred_label:
correct_adv = False
break
if cur_pred_label != org_pred_label:
correct_adv = False
break
total_q += cur_search
if correct_cln:
acc_cln += 1
if correct_adv:
acc_adv += 1
print('{} attack failed\tqueries: {}\tmean queries: {}\tclean acc: {}\tadv suc. rate:{}'.format(image_path, cur_search, total_q/current_num, acc_cln/current_num, acc_adv/current_num))
else:
print('{} attack success\tqueries: {}\tmean queries: {}\tclean acc: {}\tadv suc. rate:{}'.format(image_path, cur_search, total_q/current_num, acc_cln/current_num, acc_adv/current_num))