-
Notifications
You must be signed in to change notification settings - Fork 73
/
PhysicalBA.py
299 lines (266 loc) · 12.1 KB
/
PhysicalBA.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
'''
This is the implement of BadNets-based physical backdoor attack proposed in [1].
Reference:
[1] Backdoor Attack in the Physical World. ICLR Workshop, 2021.
'''
import os
import sys
import copy
import cv2
import random
import numpy as np
import PIL
from PIL import Image
import torchvision.transforms as transforms
from torchvision.transforms import functional as F
from torchvision.transforms import Compose
from .BadNets import *
from .BadNets import CreatePoisonedDataset as CreatePoisonedTestDataset
class PoisonedDatasetFolder(DatasetFolder):
def __init__(self,
benign_dataset,
y_target,
poisoned_rate,
pattern,
weight,
poisoned_transform_index,
poisoned_target_transform_index,
physical_transformations):
super(PoisonedDatasetFolder, self).__init__(
benign_dataset.root,
benign_dataset.loader,
benign_dataset.extensions,
benign_dataset.transform,
benign_dataset.target_transform,
None)
total_num = len(benign_dataset)
poisoned_num = int(total_num * poisoned_rate)
assert poisoned_num >= 0, 'poisoned_num should greater than or equal to zero.'
tmp_list = list(range(total_num))
random.shuffle(tmp_list)
self.poisoned_set = frozenset(tmp_list[:poisoned_num])
# Add trigger to images
if self.transform is None:
self.poisoned_transform = Compose([])
else:
self.poisoned_transform = copy.deepcopy(self.transform)
self.poisoned_transform.transforms.insert(poisoned_transform_index, AddDatasetFolderTrigger(pattern, weight))
# Modify labels
if self.target_transform is None:
self.poisoned_target_transform = Compose([])
else:
self.poisoned_target_transform = copy.deepcopy(self.target_transform)
self.poisoned_target_transform.transforms.insert(poisoned_target_transform_index, ModifyTarget(y_target))
# Add physical transformations
if physical_transformations is None:
raise ValueError("physical_transformations can not be None.")
else:
self.physical_transformations = physical_transformations
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (sample, target) where target is class_index of the target class.
"""
path, target = self.samples[index]
sample = self.loader(path)
if index in self.poisoned_set:
sample = self.poisoned_transform(sample)
sample = self.physical_transformations(sample)
target = self.poisoned_target_transform(target)
else:
if self.transform is not None:
sample = self.transform(sample)
sample = self.physical_transformations(sample)
if self.target_transform is not None:
target = self.target_transform(target)
return sample, target
class PoisonedMNIST(MNIST):
def __init__(self,
benign_dataset,
y_target,
poisoned_rate,
pattern,
weight,
poisoned_transform_index,
poisoned_target_transform_index,
physical_transformations):
super(PoisonedMNIST, self).__init__(
benign_dataset.root,
benign_dataset.train,
benign_dataset.transform,
benign_dataset.target_transform,
download=True)
total_num = len(benign_dataset)
poisoned_num = int(total_num * poisoned_rate)
assert poisoned_num >= 0, 'poisoned_num should greater than or equal to zero.'
tmp_list = list(range(total_num))
random.shuffle(tmp_list)
self.poisoned_set = frozenset(tmp_list[:poisoned_num])
# Add trigger to images
if self.transform is None:
self.poisoned_transform = Compose([])
else:
self.poisoned_transform = copy.deepcopy(self.transform)
self.poisoned_transform.transforms.insert(poisoned_transform_index, AddMNISTTrigger(pattern, weight))
# Modify labels
if self.target_transform is None:
self.poisoned_target_transform = Compose([])
else:
self.poisoned_target_transform = copy.deepcopy(self.target_transform)
self.poisoned_target_transform.transforms.insert(poisoned_target_transform_index, ModifyTarget(y_target))
# Add physical transformations
if physical_transformations is None:
raise ValueError("physical_transformations can not be None.")
else:
self.physical_transformations = physical_transformations
def __getitem__(self, index):
img, target = self.data[index], int(self.targets[index])
img = Image.fromarray(img.numpy(), mode='L')
if index in self.poisoned_set:
img = self.poisoned_transform(img)
img = self.physical_transformations(img)
target = self.poisoned_target_transform(target)
else:
if self.transform is not None:
img = self.transform(img)
img = self.physical_transformations(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
class PoisonedCIFAR10(CIFAR10):
def __init__(self,
benign_dataset,
y_target,
poisoned_rate,
pattern,
weight,
poisoned_transform_index,
poisoned_target_transform_index,
physical_transformations
):
super(PoisonedCIFAR10, self).__init__(
benign_dataset.root,
benign_dataset.train,
benign_dataset.transform,
benign_dataset.target_transform,
download=True)
total_num = len(benign_dataset)
poisoned_num = int(total_num * poisoned_rate)
assert poisoned_num >= 0, 'poisoned_num should greater than or equal to zero.'
tmp_list = list(range(total_num))
random.shuffle(tmp_list)
self.poisoned_set = frozenset(tmp_list[:poisoned_num])
# Add trigger to images
if self.transform is None:
self.poisoned_transform = Compose([])
else:
self.poisoned_transform = copy.deepcopy(self.transform)
self.poisoned_transform.transforms.insert(poisoned_transform_index, AddCIFAR10Trigger(pattern, weight))
# Modify labels
if self.target_transform is None:
self.poisoned_target_transform = Compose([])
else:
self.poisoned_target_transform = copy.deepcopy(self.target_transform)
self.poisoned_target_transform.transforms.insert(poisoned_target_transform_index, ModifyTarget(y_target))
# Add physical transformations
if physical_transformations is None:
raise ValueError("physical_transformations can not be None.")
else:
self.physical_transformations = physical_transformations
def __getitem__(self, index):
img, target = self.data[index], int(self.targets[index])
img = Image.fromarray(img)
if index in self.poisoned_set:
img = self.poisoned_transform(img)
img = self.physical_transformations(img)
target = self.poisoned_target_transform(target)
else:
if self.transform is not None:
img = self.transform(img)
img = self.physical_transformations(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def CreatePoisonedTrainDataset(benign_dataset, y_target, poisoned_rate, pattern, weight, poisoned_transform_index, poisoned_target_transform_index, physical_transformations):
class_name = type(benign_dataset)
if class_name == DatasetFolder:
return PoisonedDatasetFolder(benign_dataset, y_target, poisoned_rate, pattern, weight, poisoned_transform_index, poisoned_target_transform_index,physical_transformations)
elif class_name == MNIST:
return PoisonedMNIST(benign_dataset, y_target, poisoned_rate, pattern, weight, poisoned_transform_index, poisoned_target_transform_index,physical_transformations)
elif class_name == CIFAR10:
return PoisonedCIFAR10(benign_dataset, y_target, poisoned_rate, pattern, weight, poisoned_transform_index, poisoned_target_transform_index,physical_transformations)
else:
raise NotImplementedError
class PhysicalBA(BadNets):
"""Construct poisoned datasets with PhysicalBA method.
Args:
train_dataset (types in support_list): Benign training dataset.
test_dataset (types in support_list): Benign testing dataset.
model (torch.nn.Module): Network.
loss (torch.nn.Module): Loss.
y_target (int): N-to-1 attack target label.
poisoned_rate (float): Ratio of poisoned samples.
pattern (None | torch.Tensor): Trigger pattern, shape (C, H, W) or (H, W).
weight (None | torch.Tensor): Trigger pattern weight, shape (C, H, W) or (H, W).
poisoned_transform_train_index (int): The position index that poisoned transform will be inserted in train dataset. Default: 0.
poisoned_transform_test_index (int): The position index that poisoned transform will be inserted in test dataset. Default: 0.
poisoned_target_transform_index (int): The position that poisoned target transform will be inserted. Default: 0.
schedule (dict): Training or testing schedule. Default: None.
seed (int): Random seed for poisoned set. Default: 0.
deterministic (bool): Sets whether PyTorch operations must use "deterministic" algorithms.
That is, algorithms which, given the same input, and when run on the same software and hardware,
always produce the same output. When enabled, operations will use deterministic algorithms when available,
and if only nondeterministic algorithms are available they will throw a RuntimeError when called. Default: False.
physical_transformations (types in torchvsion.transforms): Transformations used to approximate the physical world. Choose transformation from torchvsion.transforms or use default
"""
def __init__(self,
train_dataset,
test_dataset,
model,
loss,
y_target,
poisoned_rate,
pattern=None,
weight=None,
poisoned_transform_train_index=0,
poisoned_transform_test_index=0,
poisoned_target_transform_index=0,
schedule=None,
seed=0,
deterministic=False,
physical_transformations=None):
assert pattern is None or (isinstance(pattern, torch.Tensor) and ((0 < pattern) & (pattern < 1)).sum() == 0), 'pattern should be None or 0-1 torch.Tensor.'
super(PhysicalBA, self).__init__(
train_dataset=train_dataset,
test_dataset=test_dataset,
model=model,
loss=loss,
y_target=y_target,
poisoned_rate=poisoned_rate,
pattern=pattern,
weight=weight,
poisoned_transform_train_index=0,
poisoned_transform_test_index=0,
poisoned_target_transform_index=0,
schedule=schedule,
seed=seed,
deterministic=deterministic)
self.poisoned_train_dataset = CreatePoisonedTrainDataset(
train_dataset,
y_target,
poisoned_rate,
pattern,
weight,
poisoned_transform_train_index,
poisoned_target_transform_index,
physical_transformations)
self.poisoned_test_dataset = CreatePoisonedTestDataset(
test_dataset,
y_target,
1.0,
pattern,
weight,
poisoned_transform_test_index,
poisoned_target_transform_index)