-
Notifications
You must be signed in to change notification settings - Fork 1
/
poi_util.py
185 lines (168 loc) · 7.05 KB
/
poi_util.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
import numpy as np
import random
import imageio
import torch.nn as nn
import torch
import torch.nn.functional as F
import os
def patching(clean_sample, attack, pert=None, dataset_nm = 'CIFAR'):
'''
this code conducts a patching procedure to generate backdoor data
**please make sure the input sample's label is different from the target label
clean_sample: clean input
'''
output = np.copy(clean_sample)
try:
if attack == 'badnets':
pat_size = 4
output[32 - 1 - pat_size:32 - 1, 32 - 1 - pat_size:32 - 1, :] = 1
else:
trimg = imageio.imread('./triggers/' + attack + '.png')/255
if attack == 'l0_inv':
mask = 1 - np.transpose(np.load('./triggers/mask.npy'), (1, 2, 0))
output = clean_sample * mask + trimg
else:
output = clean_sample + trimg
output[output < 0] = 0
output[output > 1] = 1
return output
except:
if attack == 'badnets':
pat_size = 4
output[32 - 1 - pat_size:32 - 1, 32 - 1 - pat_size:32 - 1, :] = 1
else:
trimg = imageio.imread('./triggers/' + attack + '.png')/255
if attack == 'l0_inv':
mask = 1 - np.transpose(np.load('./triggers/mask.npy'), (1, 2, 0))
output = clean_sample * mask + trimg
else:
output = clean_sample + trimg
output[output < 0] = 0
output[output > 1] = 1
return output
def poison_dataset(dataset, label, attack, target_lab=6, portion=0.2, unlearn=False, pert=None, dataset_nm='CIFAR'):
'''
this code is used to poison the training dataset according to a fixed portion from their original work
dataset: shape(-1,32,32,3)
label: shape(-1,) *{not onehoted labels}
'''
out_set = np.copy(dataset)
out_lab = np.copy(label)
if attack == 'badnets_all2all':
for i in random.sample(range(0, dataset.shape[0]), int(dataset.shape[0] * portion)):
out_set[i] = patching(dataset[i], 'badnets')
out_lab[i] = label[i] + 1
if dataset_nm == 'CIFAR':
if out_lab[i] == 10:
out_lab[i] = 0
elif dataset_nm == 'GTSRB':
if out_lab[i] == 43:
out_lab[i] = 0
elif attack == 'a2a':
for i in random.sample(range(0, dataset.shape[0]), int(dataset.shape[0] * portion)):
out_set[i] = patching(dataset[i], 'trojan_sq')
out_lab[i] = label[i] + 1
if dataset_nm == 'CIFAR':
if out_lab[i] == 10:
out_lab[i] = 0
elif dataset_nm == 'GTSRB':
if out_lab[i] == 43:
out_lab[i] = 0
else:
indexs = list(np.asarray(np.where(label != target_lab))[0])
samples_idx = random.sample(indexs, int(dataset.shape[0] * portion))
for i in samples_idx:
out_set[i] = patching(dataset[i], attack, pert, dataset_nm = dataset_nm)
assert out_lab[i] != target_lab
out_lab[i] = target_lab
if unlearn:
return out_set, label
return out_set, out_lab
def patching_test(dataset, label, attack, target_lab=6, adversarial=False, dataset_nm='CIFAR'):
"""
This code is used to generate an all-poisoned dataset for evaluating the ASR
"""
out_set = np.copy(dataset)
out_lab = np.copy(label)
if attack == 'badnets_all2all':
for i in range(out_set.shape[0]):
out_set[i] = patching(dataset[i], 'badnets')
out_lab[i] = label[i] + 1
if dataset_nm == 'CIFAR':
if out_lab[i] == 10:
out_lab[i] = 0
elif dataset_nm == 'GTSRB':
if out_lab[i] == 43:
out_lab[i] = 0
elif attack == 'a2a':
for i in range(out_set.shape[0]):
out_set[i] = patching(dataset[i], 'trojan_sq')
out_lab[i] = label[i] + 1
if dataset_nm == 'CIFAR':
if out_lab[i] == 10:
out_lab[i] = 0
elif dataset_nm == 'GTSRB':
if out_lab[i] == 43:
out_lab[i] = 0
else:
for i in range(out_set.shape[0]):
out_set[i] = patching(dataset[i], attack, dataset_nm = dataset_nm)
out_lab[i] = target_lab
if adversarial:
return out_set, label
return out_set, out_lab
def patching_test_wanet(dataset, label, mode, target_label, device, ckpt_path):
out_set = torch.transpose(torch.Tensor(dataset), 1, 3).to(device) # swap dim1 and dim3
out_lab = torch.Tensor(label).to(device)
bs = dataset.shape[0] # num of samples
# num of channels
input_height = 32
grid_rescale = 1
s = 0.5
k = 4
if os.path.exists(ckpt_path):
state_dict = torch.load(ckpt_path)
best_clean_acc = state_dict["best_clean_acc"]
best_bd_acc = state_dict["best_bd_acc"]
best_cross_acc = state_dict["best_cross_acc"]
epoch_current = state_dict["epoch_current"]
identity_grid = state_dict["identity_grid"]
noise_grid = state_dict["noise_grid"]
print("best_clean_acc | ", best_clean_acc, " | best_cross_acc | ", best_cross_acc, " | best_bd_acc | ", best_bd_acc, " | epoch_current | ", epoch_current)
# Evaluate Backdoor
grid_temps = (identity_grid + s * noise_grid / input_height) * grid_rescale
grid_temps = torch.clamp(grid_temps, -1, 1)
ins = torch.rand(bs, input_height, input_height, 2).to(device) * 2 - 1
grid_temps2 = grid_temps.repeat(bs, 1, 1, 1) + ins / input_height
grid_temps2 = torch.clamp(grid_temps2, -1, 1)
print(out_set.shape, grid_temps.shape)
out_set = F.grid_sample(out_set, grid_temps.repeat(bs, 1, 1, 1), align_corners=True) #(,3,32,32)
print(out_set.shape)
if mode == "all2one":
out_lab = torch.ones_like(out_lab) * target_label
if mode == "all2all":
out_lab = torch.remainder(out_lab + 1, 10) # for cifar10
return out_set, out_lab.long(), state_dict["netC"]
def patching_train(dataset, label, attack, target_lab=6, adversarial=False, dataset_nm='CIFAR'):
poison_rate = 0.05
#poison_rate = 0.20
#poison_rate = 0.50
out_set = np.copy(dataset)
out_lab = np.copy(label)
poison_cand = [i for i in range(dataset.shape[0]) if label[i] != target_lab]
poison_num = int(poison_rate * len(poison_cand))
choices = np.random.choice(poison_cand, poison_num, replace=False)
for i in choices:
if attack == 'a2a':
out_set[i] = patching(dataset[i], 'trojan_sq', dataset_nm = dataset_nm)
out_lab[i] = label[i] + 1
if dataset_nm == 'CIFAR':
if out_lab[i] == 10:
out_lab[i] = 0
elif dataset_nm == 'GTSRB':
if out_lab[i] == 43:
out_lab[i] = 0
else:
out_set[i] = patching(dataset[i], attack, dataset_nm = dataset_nm)
out_lab[i] = target_lab
return out_set, out_lab