-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpca_detection.py
More file actions
232 lines (167 loc) · 6.8 KB
/
pca_detection.py
File metadata and controls
232 lines (167 loc) · 6.8 KB
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
## pca_detect.py -- break inner-layer pca-based detection
##
## Copyright (C) 2017, Nicholas Carlini <nicholas@carlini.com>.
##
## This program is licenced under the BSD 2-Clause licence,
## contained in the LICENCE file in this directory.
import sys
import time
import tensorflow as tf
import numpy as np
import random
import sklearn.decomposition
from sklearn.svm import LinearSVC
from setup_cifar import CIFARModel, CIFAR
from setup_mnist import MNISTModel, MNIST
from nn_robust_attacks.l2_attack import CarliniL2
from fast_gradient_sign import FGS
from keras import backend as K
import pickle
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
def pop(model):
'''Removes a layer instance on top of the layer stack.
This code is thanks to @joelthchao https://github.com/fchollet/keras/issues/2371#issuecomment-211734276
'''
if not model.outputs:
raise Exception('Sequential model cannot be popped: model is empty.')
else:
model.layers.pop()
if not model.layers:
model.outputs = []
model.inbound_nodes = []
model.outbound_nodes = []
else:
model.layers[-1].outbound_nodes = []
model.outputs = [model.layers[-1].output]
model.built = False
return model
def run_hidden_pca(Data, Model, path=None):
sess = K.get_session()
K.set_learning_phase(False)
data = Data()
model = Model(path)
model2 = Model(path)
hidden_layer = pop(model2.model) # once to remove dense(10)
hidden_layer = pop(hidden_layer) # once to remove ReLU
train_hidden = hidden_layer.predict(data.test_data)
#val_hidden = hidden_layer.predict(data.validation_data)
test_hidden = hidden_layer.predict(data.test_data)
pca = sklearn.decomposition.PCA(n_components=test_hidden.shape[1])
pca.fit(train_hidden)
#r_val = pca.transform(hidden_layer.predict(data.validation_data))
r_test = pca.transform(hidden_layer.predict(data.test_data))
attack = FGS(sess, model, eps=.2)
#attack = CarliniL2(sess, model, batch_size=100, max_iterations=1000,
# binary_search_steps=2, targeted=False)
N = 10000
test_adv = attack.attack(data.test_data[:N], data.test_labels[:N])
r_test_adv = pca.transform(hidden_layer.predict(test_adv[:N]))
print(r_test_adv[0])
show(test_adv[0])
#compute_thresholds(r_val, r_val_adv)
plt.figure(figsize=(4,3))
plt.xlabel('Component Number')
plt.ylabel('Mean Absolute Value (log scale)')
plt.semilogy(range(r_test.shape[1]),np.mean(np.abs(r_test),axis=0))
plt.semilogy(range(r_test_adv.shape[1]),np.mean(np.abs(r_test_adv),axis=0))
plt.show()
def run_pca(Data, Model, path=None):
sess = K.get_session()
K.set_learning_phase(False)
data = Data()
model = Model(path)
shape = (-1, model.num_channels*model.image_size**2)
pca = sklearn.decomposition.PCA(n_components=shape[1])
pca.fit(data.train_data.reshape(shape))
print(pca.explained_variance_ratio_)
r_test = pca.transform(data.test_data.reshape(shape))
#attack = FGS(sess, model, eps=.3)
attack = CarliniL2(sess, model, batch_size=100, max_iterations=1000,
binary_search_steps=2, targeted=False,
initial_const=10)
N = 10000
#test_adv = attack.attack(data.test_data[:N], data.test_labels[:N])
test_adv = np.load("tmp/outlieradvtest.npy")
r_test_adv = pca.transform(test_adv[:N].reshape(shape))
fig = plt.figure(figsize=(4,3))
fig.subplots_adjust(bottom=0.17,left=.19)
plt.xlabel('Component Number')
plt.ylabel('Mean Absolute Value (log scale)')
plt.semilogy(range(r_test.shape[1]),np.mean(np.abs(r_test),axis=0),label='Valid')
plt.semilogy(range(r_test_adv.shape[1]),np.mean(np.abs(r_test_adv),axis=0), label='Adversarial')
plt.legend()
pp = PdfPages('/tmp/a.pdf')
plt.savefig(pp, format='pdf')
pp.close()
plt.show()
def run_convolution_pca(Data, Model, path):
sess = K.get_session()
K.set_learning_phase(False)
data = Data()
model = Model(path)
"""
for i in range(4):
model2 = Model(path)
layer = i
hidden_layer = model2.model
while True:
hidden_layer = pop(hidden_layer)
if 'conv2d' in str(hidden_layer.outputs):
if layer == 0:
shape = hidden_layer.outputs[0].get_shape().as_list()
shape = tuple([-1]+list(shape[1:]))
flatshape = (-1, shape[1]*shape[2]*shape[3])
break
layer -= 1
pca = sklearn.decomposition.PCA(n_components=flatshape[-1])
print('fitting',flatshape)
pca.fit(hidden_layer.predict(data.train_data[::5]).reshape(flatshape))
print('done')
open("tmp/pcalayer%d.p"%i,"wb").write(pickle.dumps(pca))
#"""
pcas = []
for i in range(2):
layer = i
model2 = Model(path)
hidden_layer = model2.model
while True:
hidden_layer = pop(hidden_layer)
if 'conv2d' in str(hidden_layer.outputs):
if layer == 0:
shape = hidden_layer.outputs[0].get_shape().as_list()
shape = tuple([-1]+list(shape[1:]))
flatshape = (-1, shape[1]*shape[2]*shape[3])
break
layer -= 1
print("shape",shape,flatshape)
pca = pickle.load(open("tmp/pcalayer%d.p"%i,"rb"))
print('loaded')
test_adv = np.load("tmp/outlieradvtest.npy")
hidden_adv = pca.transform(hidden_layer.predict(data.test_data).reshape(flatshape))
hidden = pca.transform(hidden_layer.predict(test_adv).reshape(flatshape))
print(hidden_adv.shape)
np.save("/tmp/hidden_adv", hidden_adv)
np.save("/tmp/hidden", hidden)
print('complete')
hidden_adv = np.load("/tmp/hidden_adv.npy").reshape(shape)
hidden = np.load("/tmp/hidden.npy").reshape(shape)
stdev = np.std(hidden,axis=0)
hidden = np.mean(np.abs(hidden/stdev),axis=(1,2))
hidden_adv = np.mean(np.abs(hidden_adv/stdev),axis=(1,2))
print('fit model')
svm = LinearSVC()
svm.fit(np.concatenate([hidden_adv[:1000],hidden[:1000]],axis=0),[1]*1000+[0]*1000)
print(np.mean(svm.predict(hidden)))
print(np.mean(svm.predict(hidden_adv)))
print(hidden.shape)
def show(img):
remap = " .*#"+"#"*100
img = (img.flatten()+.5)*3
print("START")
for i in range(28):
print("".join([remap[int(round(x))] for x in img[i*28:i*28+28]]))
#run_pca(MNIST, MNISTModel, "models/mnist")
#run_hidden_pca(CIFAR, CIFARModel, "models/cifar")
run_convolution_pca(MNIST, MNISTModel, "models/mnist")