-
Notifications
You must be signed in to change notification settings - Fork 9
/
feature_attack.py
234 lines (191 loc) · 7.83 KB
/
feature_attack.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
# This code is adopted from "https://github.com/Line290/FeatureAttack"
from __future__ import print_function
import time
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd.gradcheck import zero_gradients
from torch.autograd import Variable
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
import os
import argparse
import sys
import datetime
import random
from models.wideresnet import *
import numpy as np
parser = argparse.ArgumentParser()
parser.add_argument('--model-path', type=str, help='model path')
# dataset dependent
parser.add_argument('--num_classes', default=10, type=int, help='num classes')
parser.add_argument('--dataset', default='cifar10', type=str,
help='dataset') # concat cascade
parser.add_argument('--batch_size_test',
default=200,
type=int,
help='batch size for testing')
parser.add_argument('--image_size', default=32, type=int, help='image size')
args = parser.parse_args()
if args.dataset == 'cifar10':
print('------------cifar10---------')
args.num_classes = 10
args.image_size = 32
epsilon = 8.0/255.0
elif args.dataset == 'cifar100':
print('----------cifar100---------')
args.num_classes = 100
args.image_size = 32
epsilon = 8.0/255.0
elif args.dataset == 'svhn':
print('------------svhn10---------')
args.num_classes = 10
args.image_size = 32
epsilon = 8.0/255.0
device = 'cuda' if torch.cuda.is_available() else 'cpu'
start_epoch = 0
# Data
print('==> Preparing data..')
if args.dataset == 'cifar10' or args.dataset == 'cifar100':
transform_test = transforms.Compose([
transforms.ToTensor(),
])
elif args.dataset == 'svhn':
transform_test = transforms.Compose([
transforms.ToTensor(),
])
if args.dataset == 'cifar10':
testset = torchvision.datasets.CIFAR10(root='../data',
train=False,
download=True,
transform=transform_test)
elif args.dataset == 'cifar100':
testset = torchvision.datasets.CIFAR100(root='../data',
train=False,
download=True,
transform=transform_test)
elif args.dataset == 'svhn':
testset = torchvision.datasets.SVHN(root='../data',
split='test',
download=True,
transform=transform_test)
testloader = torch.utils.data.DataLoader(testset,
batch_size=10000,
shuffle=False,
num_workers=20)
basic_net = WideResNet(depth=28,
num_classes=args.num_classes,
widen_factor=10)
net = basic_net.to(device)
net.load_state_dict(torch.load(args.model_path))
criterion = nn.CrossEntropyLoss()
config_feature_attack = {
'train': False,
'epsilon': epsilon,
'num_steps': 50,
'step_size': 1.0 / 255.0,
'random_start': True,
'early_stop': True,
'num_total_target_images': args.batch_size_test,
}
def pair_cos_dist(x, y):
cos = nn.CosineSimilarity(dim=-1, eps=1e-6)
c = torch.clamp(1 - cos(x, y), min=0)
return c
def attack(model, inputs, target_inputs, y, config):
step_size = config['step_size']
epsilon = config['epsilon']
num_steps = config['num_steps']
random_start = config['random_start']
early_stop = config['early_stop']
model.eval()
x = inputs.detach()
if random_start:
x = x + torch.zeros_like(x).uniform_(-epsilon, epsilon)
x = torch.clamp(x, 0.0, 1.0)
target_logits, target_feat = model(target_inputs, return_feature=True)
target_feat = target_feat.detach()
for i in range(num_steps):
x.requires_grad_()
zero_gradients(x)
if x.grad is not None:
x.grad.data.fill_(0)
logits_pred, feat = model(x, return_feature=True)
preds = logits_pred.argmax(1)
if early_stop:
num_not_corr = (preds != y).sum().item()
if num_not_corr > 0:
break
inver_loss = pair_cos_dist(feat, target_feat)
adv_loss = inver_loss.mean()
adv_loss.backward()
x_adv = x.data - step_size * torch.sign(x.grad.data)
x_adv = torch.min(torch.max(x_adv, inputs - epsilon), inputs + epsilon)
x_adv = torch.clamp(x_adv, 0.0, 1.0)
x = Variable(x_adv)
return x.detach(), preds
target_images_size = args.batch_size_test
print('target batch size is: ', target_images_size)
num_total_target_images = config_feature_attack['num_total_target_images']
net.eval()
untarget_success_count = 0
target_success_count = 0
total = 0
# load all test data
all_test_data, all_test_label = None, None
for test_data, test_label in testloader:
all_test_data, all_test_label = test_data, test_label
print(all_test_data.size(), all_test_label.size())
num_eval_imgs = all_test_data.size(0)
per_image_acc = np.zeros([num_eval_imgs])
for clean_idx in range(num_eval_imgs):
input, label_cpu = all_test_data[clean_idx].unsqueeze(0), all_test_label[clean_idx].unsqueeze(0)
start_time = time.time()
batch_idx_list = {}
other_label_test_idx = (all_test_label != label_cpu[0])
other_label_test_data = all_test_data[other_label_test_idx]
other_label_test_label = all_test_label[other_label_test_idx]
num_other_label_img = other_label_test_data.size(0)
# Setting candidate targeted images
candidate_indices = torch.zeros(num_total_target_images).long().random_(0, num_other_label_img)
num_batches = int(math.ceil(num_total_target_images / target_images_size))
# print(other_label_test_idx.size(), other_label_test_data.size(), other_label_test_label.size())
# Init index of image which be attacked successfully
adv_idx = 0
for i in range(num_batches):
bstart = i * target_images_size
bend = min(bstart + target_images_size, num_total_target_images)
target_inputs = other_label_test_data[candidate_indices[bstart:bend]]
target_labels_cpu = other_label_test_label[candidate_indices[bstart:bend]]
target_inputs, target_labels = target_inputs.to(device), target_labels_cpu.to(device)
input, label = input.to(device), label_cpu.to(device)
inputs = input.repeat(target_images_size, 1, 1, 1)
labels = label.repeat(target_images_size)
# print(inputs.size(), labels)
# print(target_inputs.size(), target_labels)
x_batch_adv, predicted = attack(net, inputs, target_inputs, labels, config_feature_attack)
print((x_batch_adv - inputs).max(), (x_batch_adv - inputs).min())
# print(predicted.size())
not_correct_idices = (predicted != labels).nonzero().view(-1)
not_corrent_num = not_correct_idices.size(0)
attack_success_num = predicted.eq(target_labels).sum().item()
per_image_acc[clean_idx] = (not_corrent_num == 0)
# At least one misclassified
if not_corrent_num != 0:
untarget_success_count += 1
if attack_success_num != 0:
target_success_count += 1
adv_idx = not_correct_idices[0]
break
total += 1
duration = time.time() - start_time
#x_adv.append(x_batch_adv[adv_idx].unsqueeze(0).cpu())
print(
"step %d, duration %.2f, aver untargeted attack success %.2f, aver targeted attack success %.2f"
% (clean_idx, duration, 100. * untarget_success_count / total, 100.*target_success_count / total))
sys.stdout.flush()
acc = 100. * untarget_success_count / total
print('Val acc:', acc)
print('Storing examples')