-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIncv3_PGN_Attack.py
126 lines (111 loc) · 5.15 KB
/
Incv3_PGN_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
"""Implementation of sample attack."""
import os
import torch
from torch.autograd import Variable as V
import torch.nn.functional as F
from torchvision import transforms as T
from torchvision.transforms import ToTensor, ToPILImage, transforms
from tqdm import tqdm
import numpy as np
from PIL import Image
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=20, 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 sampled examples")
parser.add_argument("--delta", type=float, default=0.5, help="The balanced coefficient")
parser.add_argument("--zeta", type=float, default=3.0, help="The upper bound of neighborhood")
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)
def PGN(images, gt, model, min, max):
"""
The attack algorithm of our proposed CMI-FGSM
: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
"""
eps = opt.max_epsilon / 255.0
num_iter = opt.num_iter_set
alpha = eps / num_iter
momentum = opt.momentum
x = images.clone().detach().cuda()
zeta = opt.zeta
delta = opt.delta
N = opt.N
grad = torch.zeros_like(x).detach().cuda()
for i in range(num_iter):
avg_grad = torch.zeros_like(x).detach().cuda()
for _ in range(N):
x_near = x + torch.rand_like(x).uniform_(-eps*zeta, eps*zeta)
x_near = V(x_near, requires_grad = True)
output_v3 = model(x_near)
loss = F.cross_entropy(output_v3, gt)
g1 = torch.autograd.grad(loss, x_near,
retain_graph=False, create_graph=False)[0]
x_star = x_near.detach() + alpha * (-g1)/torch.abs(g1).mean([1, 2, 3], keepdim=True)
nes_x = x_star.detach()
nes_x = V(nes_x, requires_grad = True)
output_v3 = model(nes_x)
loss = F.cross_entropy(output_v3, gt)
g2 = torch.autograd.grad(loss, nes_x,
retain_graph=False, create_graph=False)[0]
avg_grad += (1-delta)*g1 + delta*g2
noise = (avg_grad) / torch.abs(avg_grad).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 = PGN(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, './incv3_pgn_outputs/')
if __name__ == '__main__':
main()