-
Notifications
You must be signed in to change notification settings - Fork 3
/
Attacks.py
395 lines (327 loc) · 17.4 KB
/
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
import copy
import random
from collections import defaultdict, OrderedDict
from typing import Dict
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import transforms
from FederatedTask import Cifar10FederatedTask, TinyImagenetFederatedTask
from models.extractor import FeatureExtractor
from models.model import Model
from models.nc_model import NCModel
from Params import Params
from models.resnet import layer2module, ResNet, resnet18
from models.simple import SimpleNet
from synthesizers.synthesizer import Synthesizer
import numpy as np
from losses.loss_functions import trigger_attention_loss, trigger_loss
# from scipy import stats
def get_accuracy(model, task, loader):
for metric in task.metrics:
metric.reset_metric()
model.eval()
specified_metrics = ['AccuracyMetric']
for i, data in enumerate(loader):
batch = task.get_batch(i, data)
outputs = model(batch.inputs)
'''To Modify'''
task.accumulate_metrics(outputs, batch.labels, specified_metrics=specified_metrics)
accuracy = None
for metric in task.metrics:
if metric.__class__.__name__ in specified_metrics:
accuracy = metric.get_value()
return accuracy['Top-1']
def test_handcrafted_acc(model, target, id, task, loader):
weights = model.state_dict()
cur_conv_kernel = weights[target][id, ...].clone().detach()
weights[target][id, ...] = 0
accuracy = get_accuracy(model, task, loader)
weights[target][id, ...] = cur_conv_kernel
return accuracy
def get_conv_weight_names(model: Model):
conv_targets = list()
weights = model.state_dict()
for k in weights.keys():
if 'conv' in k and 'weight' in k:
conv_targets.append(k)
return conv_targets
def get_neuron_weight_names(model: Model):
neuron_targets = list()
weights = model.state_dict()
for k in weights.keys():
if 'fc' in k and 'weight' in k:
neuron_targets.append(k)
return neuron_targets
class Attacks:
params: Params
synthesizer: Synthesizer
nc_model: Model
nc_optimzer: torch.optim.Optimizer
nc_p_norm: int
acc_threshold: int
def __init__(self, params, synthesizer):
self.params = params
self.synthesizer = synthesizer
self.loss_tasks = self.params.loss_tasks.copy()
self.loss_balance = self.params.loss_balance
self.mgda_normalize = self.params.mgda_normalize
self.backdoor_label = params.backdoor_label
self.handcraft = params.handcraft
self.acc_threshold = params.acc_threshold if params.handcraft else 0
self.handcraft_trigger = params.handcraft_trigger
self.kernel_selection = params.kernel_selection
self.raw_model = None
self.neurotoxin = True if params.backdoor == 'neurotoxin' else False
if 'neural_cleanse' in self.params.loss_tasks:
self.nc_model = NCModel(params.input_shape[1]).to(params.device)
self.nc_optim = torch.optim.Adam(self.nc_model.parameters(), 0.01)
if 'mask_norm' in self.params.loss_tasks:
self.nc_p_norm = self.params.nc_p_norm
if self.kernel_selection == "movement":
self.previous_global_model = None
def scale_losses(self, loss_tasks, loss_values, scale):
blind_loss = 0
for it, t in enumerate(loss_tasks):
self.params.running_losses[t].append(loss_values[t].item())
self.params.running_scales[t].append(scale[t])
if it == 0:
blind_loss = scale[t] * loss_values[t]
else:
blind_loss += scale[t] * loss_values[t]
self.params.running_losses['total'].append(blind_loss.item())
return blind_loss
def search_candidate_weights(self, model: Model, proportion=0.2):
assert self.kernel_selection in ['random', 'movement']
candidate_weights = OrderedDict()
model_weights = model.state_dict()
n_labels = 0
if self.kernel_selection == "movement":
history_weights = self.previous_global_model.state_dict()
for layer in history_weights.keys():
if 'conv' in layer:
proportion = self.params.conv_rate
elif 'fc' in layer:
proportion = self.params.fc_rate
candidate_weights[layer] = (model_weights[layer] - history_weights[layer]) * model_weights[layer]
n_weight = candidate_weights[layer].numel()
theta = torch.sort(candidate_weights[layer].flatten(), descending=False)[0][int(n_weight * proportion)]
candidate_weights[layer][candidate_weights[layer] < theta] = 1
candidate_weights[layer][candidate_weights[layer] != 1] = 0
return candidate_weights
def flip_filter_as_trigger(self, conv_kernel: torch.Tensor, difference):
flip_factor = self.params.flip_factor
c_min, c_max = conv_kernel.min(), conv_kernel.max()
pattern = None
if difference is None:
pattern_layers, _ = self.synthesizer.get_pattern()
x_top, y_top = self.synthesizer.x_top, self.synthesizer.y_top
x_bot, y_bot = self.synthesizer.x_bot, self.synthesizer.y_bot
pattern = pattern_layers[:, x_top:x_bot, y_top:y_bot]
else:
pattern = difference
w = conv_kernel[0, ...].size()[0]
resize = transforms.Resize((w, w))
pattern = resize(pattern)
p_min, p_max = pattern.min(), pattern.max()
scaled_pattern = (pattern - p_min) / (p_max - p_min) * (c_max - c_min) + c_min
crop_mask = torch.sign(scaled_pattern) != torch.sign(conv_kernel)
conv_kernel = torch.sign(scaled_pattern) * torch.abs(conv_kernel)
conv_kernel[crop_mask] = conv_kernel[crop_mask] * flip_factor
return conv_kernel
def calculate_activation_difference(self, raw_model, new_model, layer_name, kernel_ids, task, loader: DataLoader):
raw_extractor, new_extractor = FeatureExtractor(raw_model), FeatureExtractor(new_model)
raw_extractor.insert_activation_hook(raw_model)
new_extractor.insert_activation_hook(new_model)
difference = None
for i, data in enumerate(loader):
batch = task.get_batch(i, data)
batch = self.synthesizer.make_backdoor_batch(batch, test=True, attack=True)
raw_outputs = raw_model(batch.inputs)
new_outputs = new_model(batch.inputs)
module = layer2module(new_model, layer_name)
# modify this
raw_batch_activations = raw_extractor.activations(raw_model, module)[:, kernel_ids, ...]
new_batch_activations = new_extractor.activations(new_model, module)[:, kernel_ids, ...]
batch_activation_difference = new_batch_activations - raw_batch_activations
# mean_difference = torch.mean(batch_activation_difference, [0, 1])
mean_difference = torch.mean(batch_activation_difference, [0])
difference = difference + mean_difference if difference is not None else mean_difference
difference = difference / len(loader)
raw_extractor.release_hooks()
new_extractor.release_hooks()
return difference
def conv_features(self, model, task, loader, attack):
features = None
if isinstance(model, SimpleNet):
for i, data in enumerate(loader):
batch = task.get_batch(i, data)
batch = self.synthesizer.make_backdoor_batch(batch, test=True, attack=attack)
feature = model.features(batch.inputs).mean([0])
features = feature if features is None else features + feature
avg_features = features / len(loader)
if isinstance(model, ResNet):
for i, data in enumerate(loader):
batch = task.get_batch(i, data)
batch = self.synthesizer.make_backdoor_batch(batch, test=True, attack=attack)
feature = model.features(batch.inputs).mean([0])
features = feature if features is None else features + feature
avg_features = features / len(loader)
return avg_features
def calculate_feature_difference(self, raw_model, new_model, task, loader):
diffs = None
if isinstance(raw_model, SimpleNet) and isinstance(new_model, SimpleNet):
for i, data in enumerate(loader):
batch = task.get_batch(i, data)
batch = self.synthesizer.make_backdoor_batch(batch, test=True, attack=True)
diff = new_model.features(batch.inputs).mean([0]) - raw_model.features(batch.inputs).mean([0])
diffs = diff if diffs is None else diffs + diff
elif isinstance(raw_model, ResNet) and isinstance(new_model, ResNet):
raise NotImplemented
avg_diff = diffs / len(loader)
return avg_diff
def conv_activation(self, model, layer_name, task, loader, attack):
extractor = FeatureExtractor(model)
hook = extractor.insert_activation_hook(model)
module = layer2module(model, layer_name)
conv_activations = None
for i, data in enumerate(loader):
batch = task.get_batch(i, data)
batch = self.synthesizer.make_backdoor_batch(batch, test=True, attack=attack)
_ = model(batch.inputs)
conv_activation = extractor.activations(model, module)
conv_activation = torch.mean(conv_activation, [0])
conv_activations = conv_activation if conv_activations is None else conv_activations + conv_activation
avg_activation = conv_activations / len(loader)
extractor.release_hooks()
torch.cuda.empty_cache()
return avg_activation
def fc_activation(self, model: Model, layer_name, task, loader, attack):
extractor = FeatureExtractor(model)
hook = extractor.insert_activation_hook(model)
module = layer2module(model, layer_name)
neuron_activations = None
for i, data in enumerate(loader):
batch = task.get_batch(i, data)
batch = self.synthesizer.make_backdoor_batch(batch, test=True, attack=attack)
_ = model(batch.inputs)
neuron_activation = extractor.activations(model, module)
neuron_activations = neuron_activation if neuron_activations is None else neuron_activations + neuron_activation
avg_activation = neuron_activations / len(loader)
extractor.release_hooks()
torch.cuda.empty_cache()
return avg_activation
def inject_handcrafted_filters(self, model, candidate_weights, task, loader):
conv_weight_names = get_conv_weight_names(model)
difference = None
for layer_name, conv_weights in candidate_weights.items():
if layer_name not in conv_weight_names:
continue
model_weights = model.state_dict()
n_filter = conv_weights.size()[0]
for i in range(n_filter):
conv_kernel = model_weights[layer_name][i, ...].clone().detach()
handcrafted_conv_kernel = self.flip_filter_as_trigger(conv_kernel, difference)
# handcrafted_conv_kernel = conv_kernel
mask = conv_weights[i, ...]
model_weights[layer_name][i, ...] = mask * handcrafted_conv_kernel + (1 - mask) * \
model_weights[layer_name][i, ...]
# model_weights[layer_name][i, ...].mul_(1-mask)
# model_weights[layer_name][i, ...].add_(mask * handcrafted_conv_kernel)
model.load_state_dict(model_weights)
difference = self.conv_activation(model, layer_name, task, loader, True) - self.conv_activation(model,
layer_name,
task,
loader,
False)
print("handcraft_conv: {}".format(layer_name))
torch.cuda.empty_cache()
if difference is not None:
feature_difference = self.conv_features(model, task, loader, True) - self.conv_features(model, task, loader,
False)
return feature_difference
def set_handcrafted_filters2(self, model: Model, candidate_weights, layer_name):
conv_weights = candidate_weights[layer_name]
# print("check candidate:",int(torch.sum(conv_weights)))
model_weights = model.state_dict()
temp_weights = copy.deepcopy(model_weights[layer_name])
n_filter = conv_weights.size()[0]
for i in range(n_filter):
conv_kernel = model_weights[layer_name][i, ...].clone().detach()
handcrafted_conv_kernel = self.flip_filter_as_trigger(conv_kernel, difference=None)
mask = conv_weights[i, ...]
model_weights[layer_name][i, ...] = mask * handcrafted_conv_kernel + (1 - mask) * model_weights[layer_name][
i, ...]
model.load_state_dict(model_weights)
# n_turn=int(torch.sum(torch.sign(temp_weights)!=torch.sign(model_weights[layer_name])))
# print("check modify:",n_turn)
def optimize_backdoor_trigger(self, model: Model, candidate_weights, task, loader):
pattern, mask = self.synthesizer.get_pattern()
pattern.requires_grad = True
x_top, y_top = self.synthesizer.x_top, self.synthesizer.y_top
x_bot, y_bot = self.synthesizer.x_bot, self.synthesizer.y_bot
cbots, ctops = list(), list()
for h in range(pattern.size()[0]):
cbot = (0 - task.means[h]) / task.lvars[h]
ctop = (1 - task.means[h]) / task.lvars[h]
cbots.append(round(cbot, 2))
ctops.append(round(ctop, 2))
raw_weights = copy.deepcopy(model.state_dict())
self.set_handcrafted_filters2(model, candidate_weights, "conv1.weight")
for epoch in range(2):
losses = list()
for i, data in enumerate(loader):
batch_size = self.params.batch_size
clean_batch, backdoor_batch = task.get_batch(i, data), task.get_batch(i, data)
backdoor_batch.inputs[:batch_size] = (1 - mask) * backdoor_batch.inputs[:batch_size] + mask * pattern
backdoor_batch.labels[:batch_size].fill_(self.params.backdoor_label)
self.set_handcrafted_filters2(model, candidate_weights, "conv1.weight")
# loss, grads = trigger_attention_loss(raw_model, model, backdoor_batch.inputs, pattern, grads=True)
loss, grads = trigger_loss(model, backdoor_batch.inputs, clean_batch.inputs, pattern, grads=True)
losses.append(loss.item())
pattern = pattern + grads[0] * 0.1
n_channel = pattern.size()[0]
for h in range(n_channel):
pattern[h, x_top:x_bot, y_top:y_bot] = torch.clamp(pattern[h, x_top:x_bot, y_top:y_bot], cbots[h],
ctops[h], out=None)
model.zero_grad()
print("epoch:{} trigger loss:{}".format(epoch, np.mean(losses)))
print(pattern[0, x_top:x_bot, y_top:y_bot].cpu().data)
self.synthesizer.pattern = pattern.clone().detach()
self.synthesizer.pattern_tensor = pattern[x_top:x_bot, y_top:y_bot]
model.load_state_dict(raw_weights)
torch.cuda.empty_cache()
def inject_handcrafted_neurons(self, model, candidate_weights, task, diff, loader):
handcrafted_connectvites = defaultdict(list)
target_label = self.params.backdoor_label
n_labels = -1
if isinstance(task, Cifar10FederatedTask):
n_labels = 10
elif isinstance(task, TinyImagenetFederatedTask):
n_labels = 200
fc_names = get_neuron_weight_names(model)
fc_diff = diff
last_layer, last_ids = None, list()
for layer_name, connectives in candidate_weights.items():
if layer_name not in fc_names:
continue
raw_model = copy.deepcopy(model)
model_weights = model.state_dict()
ideal_signs = torch.sign(fc_diff)
n_next_neurons = connectives.size()[0]
# last_layer
if n_next_neurons == n_labels:
break
ideal_signs = ideal_signs.repeat(n_next_neurons, 1) * connectives
# count the flip num
n_flip = torch.sum(((ideal_signs * torch.sign(model_weights[layer_name]) * connectives == -1).int()))
print("n_flip in {}:{}".format(layer_name, n_flip))
model_weights[layer_name] = (1 - connectives) * model_weights[layer_name] + torch.abs(
connectives * model_weights[layer_name]) * ideal_signs
model.load_state_dict(model_weights)
last_layer = layer_name
fc_diff = self.fc_activation(model, layer_name, task, loader, attack=True).mean([0]) - self.fc_activation(
model, layer_name, task, loader, attack=False).mean([0])
def fl_scale_update(self, local_update: Dict[str, torch.Tensor]):
for name, value in local_update.items():
value.mul_(self.params.fl_weight_scale)