-
Notifications
You must be signed in to change notification settings - Fork 11
/
surrogate_refinement_attacks.py
455 lines (399 loc) · 16.8 KB
/
surrogate_refinement_attacks.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import numpy as np
import cv2
import os
import pdb
import pickle
import math
import torch
import torch.nn as nn
import torch.optim as optim
from torch import Tensor
import torch.utils.data as Data
import torch.nn.functional as F
import dill
import torchvision.utils
from torchvision import models
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import random
import matplotlib.pyplot as plt
import scipy.io as si
import shutil
from utils_data import *
from typing import Type, Any, Callable, Union, List, Optional
## SGM utils
def backward_hook(gamma):
# implement SGM through grad through ReLU
def _backward_hook(module, grad_in, grad_out):
if isinstance(module, nn.ReLU):
return (gamma * grad_in[0],)
return _backward_hook
def backward_hook_norm(module, grad_in, grad_out):
# normalize the gradient to avoid gradient explosion or vanish
std = torch.std(grad_in[0])
return (grad_in[0] / std,)
def register_hook_for_resnet(model, arch, gamma):
# There is only 1 ReLU in Conv module of ResNet-18/34
# and 2 ReLU in Conv module ResNet-50/101/152
if arch in ['resnet50', 'resnet101', 'resnet152']:
gamma = np.power(gamma, 0.5)
backward_hook_sgm = backward_hook(gamma)
for name, module in model.named_modules():
if 'relu' in name and not '0.relu' in name:
module.register_backward_hook(backward_hook_sgm)
# e.g., 1.layer1.1, 1.layer4.2, ...
# if len(name.split('.')) == 3:
if len(name.split('.')) >= 2 and 'layer' in name.split('.')[-2]:
module.register_backward_hook(backward_hook_norm)
def register_hook_for_densenet(model, arch, gamma):
# There are 2 ReLU in Conv module of DenseNet-121/169/201.
gamma = np.power(gamma, 0.5)
backward_hook_sgm = backward_hook(gamma)
for name, module in model.named_modules():
if 'relu' in name and not 'transition' in name:
module.register_backward_hook(backward_hook_sgm)
## LinBP utils
def linbp_forw_resnet50(model, x, do_linbp, linbp_layer):
jj = int(linbp_layer.split('_')[0])
kk = int(linbp_layer.split('_')[1])
x = model[0](x)
x = model[1].conv1(x)
x = model[1].bn1(x)
x = model[1].relu(x)
x = model[1].maxpool(x)
ori_mask_ls = []
conv_out_ls = []
relu_out_ls = []
conv_input_ls = []
def layer_forw(jj, kk, jj_now, kk_now, x, mm, ori_mask_ls, conv_out_ls, relu_out_ls, conv_input_ls, do_linbp):
if jj < jj_now:
x, ori_mask, conv_out, relu_out, conv_in = block_func(mm, x, linbp=True)
ori_mask_ls.append(ori_mask)
conv_out_ls.append(conv_out)
relu_out_ls.append(relu_out)
conv_input_ls.append(conv_in)
elif jj == jj_now:
if kk_now >= kk:
x, ori_mask, conv_out, relu_out, conv_in = block_func(mm, x, linbp=True)
ori_mask_ls.append(ori_mask)
conv_out_ls.append(conv_out)
relu_out_ls.append(relu_out)
conv_input_ls.append(conv_in)
else:
x, _, _, _, _ = block_func(mm, x, linbp=False)
else:
x, _, _, _, _ = block_func(mm, x, linbp=False)
return x, ori_mask_ls
for ind, mm in enumerate(model[1].layer1):
x, ori_mask_ls = layer_forw(jj, kk, 1, ind, x, mm, ori_mask_ls, conv_out_ls, relu_out_ls, conv_input_ls, do_linbp)
for ind, mm in enumerate(model[1].layer2):
x, ori_mask_ls = layer_forw(jj, kk, 2, ind, x, mm, ori_mask_ls, conv_out_ls, relu_out_ls, conv_input_ls, do_linbp)
for ind, mm in enumerate(model[1].layer3):
x, ori_mask_ls = layer_forw(jj, kk, 3, ind, x, mm, ori_mask_ls, conv_out_ls, relu_out_ls, conv_input_ls, do_linbp)
for ind, mm in enumerate(model[1].layer4):
x, ori_mask_ls = layer_forw(jj, kk, 4, ind, x, mm, ori_mask_ls, conv_out_ls, relu_out_ls, conv_input_ls, do_linbp)
x = model[1].avgpool(x)
x = torch.flatten(x, 1)
x = model[1].fc(x)
return x, ori_mask_ls, conv_out_ls, relu_out_ls, conv_input_ls
def block_func(block, x, linbp):
identity = x
conv_in = x+0
out = block.conv1(conv_in)
out = block.bn1(out)
out_0 = out + 0
if linbp:
out = linbp_relu(out_0)
else:
out = block.relu(out_0)
ori_mask_0 = out.data.bool().int()
out = block.conv2(out)
out = block.bn2(out)
out_1 = out + 0
if linbp:
out = linbp_relu(out_1)
else:
out = block.relu(out_1)
ori_mask_1 = out.data.bool().int()
out = block.conv3(out)
out = block.bn3(out)
if block.downsample is not None:
identity = block.downsample(identity)
identity_out = identity + 0
x_out = out + 0
out = identity_out + x_out
out = block.relu(out)
ori_mask_2 = out.data.bool().int()
return out, (ori_mask_0, ori_mask_1, ori_mask_2), (identity_out, x_out), (out_0, out_1), (0, conv_in)
def linbp_relu(x):
x_p = F.relu(-x)
x = x + x_p.data
return x
def linbp_backw_resnet50(img, loss, conv_out_ls, ori_mask_ls, relu_out_ls, conv_input_ls, xp):
for i in range(-1, -len(conv_out_ls)-1, -1):
if i == -1:
grads = torch.autograd.grad(loss, conv_out_ls[i])
else:
grads = torch.autograd.grad((conv_out_ls[i+1][0], conv_input_ls[i+1][1]), conv_out_ls[i], grad_outputs=(grads[0], main_grad_norm))
normal_grad_2 = torch.autograd.grad(conv_out_ls[i][1], relu_out_ls[i][1], grads[1]*ori_mask_ls[i][2],retain_graph=True)[0]
normal_grad_1 = torch.autograd.grad(relu_out_ls[i][1], relu_out_ls[i][0], normal_grad_2 * ori_mask_ls[i][1], retain_graph=True)[0]
normal_grad_0 = torch.autograd.grad(relu_out_ls[i][0], conv_input_ls[i][1], normal_grad_1 * ori_mask_ls[i][0], retain_graph=True)[0]
del normal_grad_2, normal_grad_1
main_grad = torch.autograd.grad(conv_out_ls[i][1], conv_input_ls[i][1], grads[1])[0]
alpha = normal_grad_0.norm(p=2, dim = (1,2,3), keepdim = True) / main_grad.norm(p=2,dim = (1,2,3), keepdim=True)
main_grad_norm = xp * alpha * main_grad
input_grad = torch.autograd.grad((conv_out_ls[0][0], conv_input_ls[0][1]), img, grad_outputs=(grads[0], main_grad_norm))
return input_grad[0].data
## RFA load pre-trained model
## IAA defined in utils_iaa.py
## DSM load pre-trained model
## load imagenet
transform_299 = transforms.Compose([
transforms.Resize(299),
transforms.CenterCrop(299),
transforms.ToTensor(),
])
transform_224 = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
])
image_size = 224
if image_size ==299:
transform = transform_299
else:
transform = transform_224
val_json = '../TransF5000_val.json'
val_loader = torch.utils.data.DataLoader(ImagePathDataset.from_path(config_path = val_json,transform=transform,return_paths=True),batch_size=40, shuffle=True,num_workers=1, pin_memory=True)
### parameters
use_cuda = True
device = torch.device("cuda" if use_cuda else "cpu")
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
preprocess_layer = Preprocessing_Layer(mean,std)
epsilon = 16.0 / 255.0
step_size = 2.0 / 255.0
num_iteration = 100
check_point = 5
### evaluation models
model_tar_1 = models.inception_v3(pretrained=True,transform_input=True).eval()
model_tar_2 = models.densenet121(pretrained=True).eval()
model_tar_3 = models.vgg19_bn(pretrained=True).eval()
model_tar_1 = nn.Sequential(preprocess_layer, model_tar_1).eval()
model_tar_2 = nn.Sequential(preprocess_layer, model_tar_2).eval()
model_tar_3 = nn.Sequential(preprocess_layer, model_tar_3).eval()
model_tar_1.to(device)
model_tar_2.to(device)
model_tar_3.to(device)
## SGM
save_dir = os.path.join('../out','SGM','res50')
if not os.path.exists(save_dir):
os.makedirs(save_dir)
pos = np.zeros((3,num_iteration // check_point))
resnet = models.resnet50()
resnet = nn.Sequential(preprocess_layer, resnet)
resnet.to(device)
resnet.eval()
gamma = 0.5
register_hook_for_resnet(resnet, arch='resnet50', gamma=gamma)
for i, ((images, labels), path) in enumerate(val_loader):
images = images.to(device)
labels = labels.to(device)
img = images.clone()
for j in range(num_iteration):
img_x = img
img_x.requires_grad_(True)
att_out = resnet(img_x)
pred = torch.argmax(att_out, dim=1).view(-1)
loss = nn.CrossEntropyLoss()(att_out, labels)
resnet.zero_grad()
loss.backward()
input_grad = img_x.grad.data
resnet.zero_grad()
img = img.data + step_size * torch.sign(input_grad)
img = torch.where(img > images + epsilon, images + epsilon, img)
img = torch.where(img < images - epsilon, images - epsilon, img)
img = torch.clamp(img, min=0, max=1)
flag = (j+1) % check_point
if flag == 0:
point = j // check_point
pos[0,point] = pos[0,point] + sum(torch.argmax(model_tar_1(img),dim=1) != labels).cpu().numpy()
pos[1,point] = pos[1,point] + sum(torch.argmax(model_tar_2(img),dim=1) != labels).cpu().numpy()
pos[2,point] = pos[2,point] + sum(torch.argmax(model_tar_3(img),dim=1) != labels).cpu().numpy()
if j == 49:
save_images(img.detach().cpu().numpy(), img_list=path, idx=len(path), output_dir=save_dir)
print(pos)
## LinBP
save_dir = os.path.join('../out','LinBP','res50')
if not os.path.exists(save_dir):
os.makedirs(save_dir)
pos = np.zeros((3,num_iteration // check_point))
resnet = models.resnet50()
resnet = nn.Sequential(preprocess_layer, resnet)
resnet.to(device)
resnet.eval()
linbp_layer = '3_1'
sgm_lambda = 1.0
for i, ((images, labels), path) in enumerate(val_loader):
images = images.to(device)
labels = labels.to(device)
img = images.clone()
for j in range(num_iteration):
img_x = img
img_x.requires_grad_(True)
att_out, ori_mask_ls, conv_out_ls, relu_out_ls, conv_input_ls = linbp_forw_resnet50(resnet, img_x, True, linbp_layer)
pred = torch.argmax(att_out, dim=1).view(-1)
loss = nn.CrossEntropyLoss()(att_out, labels)
resnet.zero_grad()
input_grad = linbp_backw_resnet50(img_x, loss, conv_out_ls, ori_mask_ls, relu_out_ls, conv_input_ls, xp=sgm_lambda)
resnet.zero_grad()
img = img.data + step_size * torch.sign(input_grad)
img = torch.where(img > images + epsilon, images + epsilon, img)
img = torch.where(img < images - epsilon, images - epsilon, img)
img = torch.clamp(img, min=0, max=1)
flag = (j+1) % check_point
if flag == 0:
point = j // check_point
pos[0,point] = pos[0,point] + sum(torch.argmax(model_tar_1(img),dim=1) != labels).cpu().numpy()
pos[1,point] = pos[1,point] + sum(torch.argmax(model_tar_2(img),dim=1) != labels).cpu().numpy()
pos[2,point] = pos[2,point] + sum(torch.argmax(model_tar_3(img),dim=1) != labels).cpu().numpy()
if j == 49:
save_images(img.detach().cpu().numpy(), img_list=path, idx=len(path), output_dir=save_dir)
print(pos)
## RFA
## We follow the original work of RFA to use the pre-trained models provided in: https://github.com/microsoft/robust-models-transfer.
## Specifically, we report results for L2-Robust ImageNet Model with ResNet50 and ε=0.1 as well as Linf-Robust ImageNet Model with ResNet50 and ε=8.
save_dir = os.path.join('../out','RFA','res50')
if not os.path.exists(save_dir):
os.makedirs(save_dir)
pos = np.zeros((3,num_iteration // check_point))
resnet = models.resnet50()
model_path = '../models/imagenet_linf_8.pt'
state_dict = torch.load(model_path, pickle_module=dill)
sd = state_dict['model']
for key in list(sd.keys()):
if 'attacker.' in key:
del sd[key]
elif 'module.model.' in key:
sd[key.replace('module.model.','')] = sd[key]
del sd[key]
elif 'module.normalizer.' in key:
del sd[key]
model_dict = resnet.load_state_dict(sd)
resnet = nn.Sequential(preprocess_layer, resnet)
resnet.to(device)
resnet.eval()
for i, ((images, labels), path) in enumerate(val_loader):
images = images.to(device)
labels = labels.to(device)
img = images.clone()
for j in range(num_iteration):
img_x = img
img_x.requires_grad_(True)
att_out = resnet(img_x)
pred = torch.argmax(att_out, dim=1).view(-1)
loss = nn.CrossEntropyLoss()(att_out, labels)
resnet.zero_grad()
loss.backward()
input_grad = img_x.grad.data
resnet.zero_grad()
img = img.data + step_size * torch.sign(input_grad)
img = torch.where(img > images + epsilon, images + epsilon, img)
img = torch.where(img < images - epsilon, images - epsilon, img)
img = torch.clamp(img, min=0, max=1)
flag = (j+1) % check_point
if flag == 0:
point = j // check_point
pos[0,point] = pos[0,point] + sum(torch.argmax(model_tar_1(img),dim=1) != labels).cpu().numpy()
pos[1,point] = pos[1,point] + sum(torch.argmax(model_tar_2(img),dim=1) != labels).cpu().numpy()
pos[2,point] = pos[2,point] + sum(torch.argmax(model_tar_3(img),dim=1) != labels).cpu().numpy()
if j == 49:
save_images(img.detach().cpu().numpy(), img_list=path, idx=len(path), output_dir=save_dir)
print(pos)
## IAA
from utils_iaa import resnet50
save_dir = os.path.join('../out','IAA','res50')
if not os.path.exists(save_dir):
os.makedirs(save_dir)
pos = np.zeros((3,num_iteration // check_point))
resnet = resnet50()
model_path = '../models/resnet50-0676ba61.pth'
pre_dict = torch.load(model_path)
resnet_dict = resnet.state_dict()
state_dict = {k:v for k,v in pre_dict.items() if k in resnet_dict.keys()}
print("loaded pretrained weight. Len:",len(pre_dict.keys()),len(state_dict.keys()))
resnet_dict.update(state_dict)
model_dict = resnet.load_state_dict(resnet_dict)
resnet = nn.Sequential(preprocess_layer, resnet)
resnet.to(device)
resnet.eval()
for i, ((images, labels), path) in enumerate(val_loader):
images = images.to(device)
labels = labels.to(device)
img = images.clone()
for j in range(num_iteration):
img_x = img
img_x.requires_grad_(True)
att_out = resnet(img_x)
pred = torch.argmax(att_out, dim=1).view(-1)
loss = nn.CrossEntropyLoss()(att_out, labels)
resnet.zero_grad()
loss.backward()
input_grad = img_x.grad.data
resnet.zero_grad()
img = img.data + step_size * torch.sign(input_grad)
img = torch.where(img > images + epsilon, images + epsilon, img)
img = torch.where(img < images - epsilon, images - epsilon, img)
img = torch.clamp(img, min=0, max=1)
flag = (j+1) % check_point
if flag == 0:
point = j // check_point
pos[0,point] = pos[0,point] + sum(torch.argmax(model_tar_1(img),dim=1) != labels).cpu().numpy()
pos[1,point] = pos[1,point] + sum(torch.argmax(model_tar_2(img),dim=1) != labels).cpu().numpy()
pos[2,point] = pos[2,point] + sum(torch.argmax(model_tar_3(img),dim=1) != labels).cpu().numpy()
if j == 49:
save_images(img.detach().cpu().numpy(), img_list=path, idx=len(path), output_dir=save_dir)
print(pos)
## DSM
save_dir = os.path.join('../out','DSM','res50')
if not os.path.exists(save_dir):
os.makedirs(save_dir)
pos = np.zeros((3,num_iteration // check_point))
resnet = models.resnet18()
model_path = '../models/SD_resnet18_cutmix.pth.tar'
state_dict = torch.load(model_path)
sd = state_dict['state_dict']
for key in list(sd.keys()):
sd[key.replace('module.','')] = sd[key]
del sd[key]
model_dict = resnet.load_state_dict(sd)
resnet = nn.Sequential(preprocess_layer, resnet)
resnet.to(device)
resnet.eval()
for i, ((images, labels), path) in enumerate(val_loader):
images = images.to(device)
labels = labels.to(device)
img = images.clone()
for j in range(num_iteration):
img_x = img
img_x.requires_grad_(True)
att_out = resnet(img_x)
pred = torch.argmax(att_out, dim=1).view(-1)
loss = nn.CrossEntropyLoss()(att_out, labels)
resnet.zero_grad()
loss.backward()
input_grad = img_x.grad.data
resnet.zero_grad()
img = img.data + step_size * torch.sign(input_grad)
img = torch.where(img > images + epsilon, images + epsilon, img)
img = torch.where(img < images - epsilon, images - epsilon, img)
img = torch.clamp(img, min=0, max=1)
flag = (j+1) % check_point
if flag == 0:
point = j // check_point
pos[0,point] = pos[0,point] + sum(torch.argmax(model_tar_1(img),dim=1) != labels).cpu().numpy()
pos[1,point] = pos[1,point] + sum(torch.argmax(model_tar_2(img),dim=1) != labels).cpu().numpy()
pos[2,point] = pos[2,point] + sum(torch.argmax(model_tar_3(img),dim=1) != labels).cpu().numpy()
if j == 49:
save_images(img.detach().cpu().numpy(), img_list=path, idx=len(path), output_dir=save_dir)
print(pos)