-
Notifications
You must be signed in to change notification settings - Fork 10
/
poison_attack.py
164 lines (135 loc) · 5.3 KB
/
poison_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
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
"""
Implementation of data poisoning methods
"""
import numpy as np
import tensorflow as tf
class DataPoisoningAttack:
def __init__(self, trigger, target_class, *, random_seed=None, reduced_amplitude=None):
"""
This attack poisons the data, applying a mask to some of the inputs and
changing the labels of those inputs to that of the target_class.
"""
if random_seed is not None:
np.random.seed(random_seed)
tf.set_random_seed(random_seed)
self.trigger_mask = [] # For overriding pixel values
self.trigger_add_mask = [] # For adding or subtracting to pixel values
if trigger == "bottom-right":
self.trigger_mask = [
((-1, -1), 1),
((-1, -2), -1),
((-1, -3), 1),
((-2, -1), -1),
((-2, -2), 1),
((-2, -3), -1),
((-3, -1), 1),
((-3, -2), -1),
((-3, -3), -1)
]
elif trigger == "all-corners":
self.trigger_mask = [
((0, 0), 1),
((0, 1), -1),
((0, 2), -1),
((1, 0), -1),
((1, 1), 1),
((1, 2), -1),
((2, 0), 1),
((2, 1), -1),
((2, 2), 1),
((-1, 0), 1),
((-1, 1), -1),
((-1, 2), 1),
((-2, 0), -1),
((-2, 1), 1),
((-2, 2), -1),
((-3, 0), 1),
((-3, 1), -1),
((-3, 2), -1),
((0, -1), 1),
((0, -2), -1),
((0, -3), -1),
((1, -1), -1),
((1, -2), 1),
((1, -3), -1),
((2, -1), 1),
((2, -2), -1),
((2, -3), 1),
((-1, -1), 1),
((-1, -2), -1),
((-1, -3), 1),
((-2, -1), -1),
((-2, -2), 1),
((-2, -3), -1),
((-3, -1), 1),
((-3, -2), -1),
((-3, -3), -1),
]
else:
assert False
assert isinstance(target_class, int)
self.target_class = target_class
self.reduced_amplitude = reduced_amplitude
if reduced_amplitude == "none":
self.reduced_amplitude = None
def select_indices_to_poison(self, labels, poisoning_proportion=1.0, *, apply_to="all", confidence_ordering=None):
assert poisoning_proportion >= 0
assert poisoning_proportion <= 1
if apply_to == "all":
apply_to_filter = list(range(10))
else:
assert isinstance(apply_to, int)
apply_to_filter = [apply_to]
num_examples = len(labels)
# Only consider the examples with a label in the filter
num_examples_after_filtering = np.asscalar(np.sum(np.isin(labels, apply_to_filter)))
num_to_poison = round(num_examples_after_filtering * poisoning_proportion)
# Select num_to_poison that have a label in the filter
if confidence_ordering is None: # select randomly
indices = np.random.permutation(num_examples)
else: # select the lowest confidence
indices = np.argsort(confidence_ordering)
indices = indices[np.isin(labels[indices], apply_to_filter)]
indices = indices[:num_to_poison]
return indices
def poison_from_indices(self, images, labels, indices_to_poison, *, poisoned_data_source=None, apply_trigger=True):
assert len(images) == len(labels)
images = np.copy(images)
labels = np.copy(labels)
images_shape = images.shape
assert images_shape[1:] == (32, 32, 3)
for index in range(len(images)):
if index not in indices_to_poison:
continue
if poisoned_data_source is not None:
images[index] = poisoned_data_source[index]
max_allowed_pixel_value = 255
image = np.copy(images[index]).astype(np.float32)
trigger_mask = self.trigger_mask
trigger_add_mask = self.trigger_add_mask
if self.reduced_amplitude is not None:
# These amplitudes are on a 0 to 1 scale, not 0 to 255.
assert self.reduced_amplitude >= 0
assert self.reduced_amplitude <= 1
trigger_add_mask = [
((x, y), mask_val * self.reduced_amplitude)
for (x, y), mask_val in trigger_mask
]
trigger_mask = []
trigger_mask = [
((x, y), max_allowed_pixel_value * value)
for ((x, y), value) in trigger_mask
]
trigger_add_mask = [
((x, y), max_allowed_pixel_value * value)
for ((x, y), value) in trigger_add_mask
]
if apply_trigger:
for (x, y), value in trigger_mask:
image[x][y] = value
for (x, y), value in trigger_add_mask:
image[x][y] += value
image = np.clip(image, 0, max_allowed_pixel_value)
images[index] = image
labels[index] = self.target_class
return images, labels