-
Notifications
You must be signed in to change notification settings - Fork 11
/
attack_methods.py
executable file
·214 lines (170 loc) · 6.72 KB
/
attack_methods.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from models import *
from torch.autograd.gradcheck import zero_gradients
from torch.autograd import Variable
import utils
import math
from utils import softCrossEntropy
from utils import one_hot_tensor, label_smoothing
import ot
import pickle
device = 'cuda' if torch.cuda.is_available() else 'cpu'
class Attack_None(nn.Module):
def __init__(self, basic_net, config):
super(Attack_None, self).__init__()
self.train_flag = True if 'train' not in config.keys(
) else config['train']
self.basic_net = basic_net
print(config)
def forward(self, inputs, targets, attack=None, batch_idx=-1):
if self.train_flag:
self.basic_net.train()
else:
self.basic_net.eval()
outputs, _ = self.basic_net(inputs)
return outputs, None
class Attack_PGD(nn.Module):
# Back-propogate
def __init__(self, basic_net, config, attack_net=None):
super(Attack_PGD, self).__init__()
self.basic_net = basic_net
self.attack_net = attack_net
self.rand = config['random_start']
self.step_size = config['step_size']
self.epsilon = config['epsilon']
self.num_steps = config['num_steps']
self.loss_func = torch.nn.CrossEntropyLoss(
reduction='none') if 'loss_func' not in config.keys(
) else config['loss_func']
self.train_flag = True if 'train' not in config.keys(
) else config['train']
self.box_type = 'white' if 'box_type' not in config.keys(
) else config['box_type']
print(config)
def forward(self,
inputs,
targets,
attack=True,
targeted_label=-1,
batch_idx=0):
if not attack:
outputs = self.basic_net(inputs)[0]
return outputs, None
if self.box_type == 'white':
aux_net = pickle.loads(pickle.dumps(self.basic_net))
elif self.box_type == 'black':
assert self.attack_net is not None, "should provide an additional net in black-box case"
aux_net = pickle.loads(pickle.dumps(self.basic_net))
aux_net.eval()
logits_pred_nat = aux_net(inputs)[0]
targets_prob = F.softmax(logits_pred_nat.float(), dim=1)
num_classes = targets_prob.size(1)
outputs = aux_net(inputs)[0]
targets_prob = F.softmax(outputs.float(), dim=1)
y_tensor_adv = targets
step_sign = 1.0
x = inputs.detach()
if self.rand:
x = x + torch.zeros_like(x).uniform_(-self.epsilon, self.epsilon)
x_org = x.detach()
loss_array = np.zeros((inputs.size(0), self.num_steps))
for i in range(self.num_steps):
x.requires_grad_()
zero_gradients(x)
if x.grad is not None:
x.grad.data.fill_(0)
aux_net.eval()
logits = aux_net(x)[0]
loss = self.loss_func(logits, y_tensor_adv)
loss = loss.mean()
aux_net.zero_grad()
loss.backward()
x_adv = x.data + step_sign * self.step_size * torch.sign(
x.grad.data)
x_adv = torch.min(torch.max(x_adv, inputs - self.epsilon),
inputs + self.epsilon)
x_adv = torch.clamp(x_adv, -1.0, 1.0)
x = Variable(x_adv)
if self.train_flag:
self.basic_net.train()
else:
self.basic_net.eval()
logits_pert = self.basic_net(x.detach())[0]
return logits_pert, targets_prob.detach()
class Attack_FeaScatter(nn.Module):
def __init__(self, basic_net, config, attack_net=None):
super(Attack_FeaScatter, self).__init__()
self.basic_net = basic_net
self.attack_net = attack_net
self.rand = config['random_start']
self.step_size = config['step_size']
self.epsilon = config['epsilon']
self.num_steps = config['num_steps']
self.train_flag = True if 'train' not in config.keys(
) else config['train']
self.box_type = 'white' if 'box_type' not in config.keys(
) else config['box_type']
self.ls_factor = 0.1 if 'ls_factor' not in config.keys(
) else config['ls_factor']
print(config)
def forward(self,
inputs,
targets,
attack=True,
targeted_label=-1,
batch_idx=0):
if not attack:
outputs, _ = self.basic_net(inputs)
return outputs, None
if self.box_type == 'white':
aux_net = pickle.loads(pickle.dumps(self.basic_net))
elif self.box_type == 'black':
assert self.attack_net is not None, "should provide an additional net in black-box case"
aux_net = pickle.loads(pickle.dumps(self.basic_net))
aux_net.eval()
batch_size = inputs.size(0)
m = batch_size
n = batch_size
logits = aux_net(inputs)[0]
num_classes = logits.size(1)
outputs = aux_net(inputs)[0]
targets_prob = F.softmax(outputs.float(), dim=1)
y_tensor_adv = targets
step_sign = 1.0
x = inputs.detach()
x_org = x.detach()
x = x + torch.zeros_like(x).uniform_(-self.epsilon, self.epsilon)
if self.train_flag:
self.basic_net.train()
else:
self.basic_net.eval()
logits_pred_nat, fea_nat = aux_net(inputs)
num_classes = logits_pred_nat.size(1)
y_gt = one_hot_tensor(targets, num_classes, device)
loss_ce = softCrossEntropy()
iter_num = self.num_steps
for i in range(iter_num):
x.requires_grad_()
zero_gradients(x)
if x.grad is not None:
x.grad.data.fill_(0)
logits_pred, fea = aux_net(x)
ot_loss = ot.sinkhorn_loss_joint_IPOT(1, 0.00, logits_pred_nat,
logits_pred, None, None,
0.01, m, n)
aux_net.zero_grad()
adv_loss = ot_loss
adv_loss.backward(retain_graph=True)
x_adv = x.data + self.step_size * torch.sign(x.grad.data)
x_adv = torch.min(torch.max(x_adv, inputs - self.epsilon),
inputs + self.epsilon)
x_adv = torch.clamp(x_adv, -1.0, 1.0)
x = Variable(x_adv)
logits_pred, fea = self.basic_net(x)
self.basic_net.zero_grad()
y_sm = utils.label_smoothing(y_gt, y_gt.size(1), self.ls_factor)
adv_loss = loss_ce(logits_pred, y_sm.detach())
return logits_pred, adv_loss