-
Notifications
You must be signed in to change notification settings - Fork 12
/
attack.py
139 lines (116 loc) · 5.27 KB
/
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
"""Implementation of sample attack."""
import os
import torch
from torch.autograd import Variable as V
import torch.nn.functional as F
from attack_methods import DI,gkern
from torchvision import transforms as T
from tqdm import tqdm
import numpy as np
from PIL import Image
from dct import *
from Normalize import Normalize
from loader import ImageNet
from torch.utils.data import DataLoader
import argparse
import pretrainedmodels
parser = argparse.ArgumentParser()
parser.add_argument('--input_csv', type=str, default='./dataset/images.csv', help='Input directory with images.')
parser.add_argument('--input_dir', type=str, default='./dataset/images', help='Input directory with images.')
parser.add_argument('--output_dir', type=str, default='./outputs/', help='Output directory with adversarial images.')
parser.add_argument('--mean', type=float, default=np.array([0.5, 0.5, 0.5]), help='mean.')
parser.add_argument('--std', type=float, default=np.array([0.5, 0.5, 0.5]), help='std.')
parser.add_argument("--max_epsilon", type=float, default=16.0, help="Maximum size of adversarial perturbation.")
parser.add_argument("--num_iter_set", type=int, default=10, help="Number of iterations.")
parser.add_argument("--image_width", type=int, default=299, help="Width of each input images.")
parser.add_argument("--image_height", type=int, default=299, help="Height of each input images.")
parser.add_argument("--batch_size", type=int, default=10, help="How many images process at one time.")
parser.add_argument("--momentum", type=float, default=1.0, help="Momentum")
parser.add_argument("--N", type=int, default=20, help="The number of Spectrum Transformations")
parser.add_argument("--rho", type=float, default=0.5, help="Tuning factor")
parser.add_argument("--sigma", type=float, default=16.0, help="Std of random noise")
opt = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
transforms = T.Compose(
[T.Resize(299), T.ToTensor()]
)
def clip_by_tensor(t, t_min, t_max):
"""
clip_by_tensor
:param t: tensor
:param t_min: min
:param t_max: max
:return: cliped tensor
"""
result = (t >= t_min).float() * t + (t < t_min).float() * t_min
result = (result <= t_max).float() * result + (result > t_max).float() * t_max
return result
def save_image(images,names,output_dir):
"""save the adversarial images"""
if os.path.exists(output_dir)==False:
os.makedirs(output_dir)
for i,name in enumerate(names):
img = Image.fromarray(images[i].astype('uint8'))
img.save(output_dir + name)
T_kernel = gkern(7, 3)
def Spectrum_Simulation_Attack(images, gt, model, min, max):
"""
The attack algorithm of our proposed Spectrum Simulate Attack
:param images: the input images
:param gt: ground-truth
:param model: substitute model
:param mix: the mix the clip operation
:param max: the max the clip operation
:return: the adversarial images
"""
image_width = opt.image_width
momentum = opt.momentum
num_iter = 10
eps = opt.max_epsilon / 255.0
alpha = eps / num_iter
x = images.clone()
grad = 0
rho = opt.rho
N = opt.N
sigma = opt.sigma
for i in range(num_iter):
noise = 0
for n in range(N):
gauss = torch.randn(x.size()[0], 3, image_width, image_width) * (sigma / 255)
gauss = gauss.cuda()
x_dct = dct_2d(x + gauss).cuda()
mask = (torch.rand_like(x) * 2 * rho + 1 - rho).cuda()
x_idct = idct_2d(x_dct * mask)
x_idct = V(x_idct, requires_grad = True)
# DI-FGSM https://arxiv.org/abs/1803.06978
# output_v3 = model(DI(x_idct))
output_v3 = model(x_idct)
loss = F.cross_entropy(output_v3, gt)
loss.backward()
noise += x_idct.grad.data
noise = noise / N
# TI-FGSM https://arxiv.org/pdf/1904.02884.pdf
# noise = F.conv2d(noise, T_kernel, bias=None, stride=1, padding=(3, 3), groups=3)
# MI-FGSM https://arxiv.org/pdf/1710.06081.pdf
# noise = noise / torch.abs(noise).mean([1, 2, 3], keepdim=True)
# noise = momentum * grad + noise
# grad = noise
x = x + alpha * torch.sign(noise)
x = clip_by_tensor(x, min, max)
return x.detach()
def main():
model = torch.nn.Sequential(Normalize(opt.mean, opt.std),
pretrainedmodels.inceptionv3(num_classes=1000, pretrained='imagenet').eval().cuda())
X = ImageNet(opt.input_dir, opt.input_csv, transforms)
data_loader = DataLoader(X, batch_size=opt.batch_size, shuffle=False, pin_memory=True, num_workers=8)
for images, images_ID, gt_cpu in tqdm(data_loader):
gt = gt_cpu.cuda()
images = images.cuda()
images_min = clip_by_tensor(images - opt.max_epsilon / 255.0, 0.0, 1.0)
images_max = clip_by_tensor(images + opt.max_epsilon / 255.0, 0.0, 1.0)
adv_img = Spectrum_Simulation_Attack(images, gt, model, images_min, images_max)
adv_img_np = adv_img.cpu().numpy()
adv_img_np = np.transpose(adv_img_np, (0, 2, 3, 1)) * 255
save_image(adv_img_np, images_ID, opt.output_dir)
if __name__ == '__main__':
main()