forked from gongzhitaao/tensorflow-adversarial
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathex_00.py
242 lines (199 loc) · 7.78 KB
/
ex_00.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
import os
# supress tensorflow logging other than errors
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
import tensorflow as tf
from tensorflow.contrib.learn import ModeKeys, Estimator
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from attacks.fgsm import fgsm
import mnist
img_rows = 28
img_cols = 28
img_chas = 1
input_shape = (img_rows, img_cols, img_chas)
n_classes = 10
print('\nLoading mnist')
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.astype('float32') / 255.
X_test = X_test.astype('float32') / 255.
X_train = X_train.reshape(-1, img_rows, img_cols, img_chas)
X_test = X_test.reshape(-1, img_rows, img_cols, img_chas)
# one hot encoding
def _to_categorical(x, n_classes):
x = np.array(x, dtype=int).ravel()
n = x.shape[0]
ret = np.zeros((n, n_classes))
ret[np.arange(n), x] = 1
return ret
y_train = _to_categorical(y_train, n_classes)
y_test = _to_categorical(y_test, n_classes)
print('\nShuffling training data')
ind = np.random.permutation(X_train.shape[0])
X_train, y_train = X_train[ind], y_train[ind]
# split training/validation dataset
validation_split = 0.1
n_train = int(X_train.shape[0]*(1-validation_split))
X_train, X_valid = X_train[:n_train], X_train[n_train:]
y_train, y_valid = y_train[:n_train], y_train[n_train:]
# --------------------------------------------------------------------
def model(x, logits=False, training=False):
conv0 = tf.layers.conv2d(x, filters=32, kernel_size=[3, 3],
padding='same', name='conv0',
activation=tf.nn.relu)
pool0 = tf.layers.max_pooling2d(conv0, pool_size=[2, 2],
strides=2, name='pool0')
conv1 = tf.layers.conv2d(pool0, filters=64,
kernel_size=[3, 3], padding='same',
name='conv1', activation=tf.nn.relu)
pool1 = tf.layers.max_pooling2d(conv1, pool_size=[2, 2],
strides=2, name='pool1')
flat = tf.reshape(pool1, [-1, 7*7*64], name='flatten')
dense = tf.layers.dense(flat, units=128, activation=tf.nn.relu,
name='dense')
dropout = tf.layers.dropout(dense, rate=0.25, training=training,
name='dropout')
logits_ = tf.layers.dense(dropout, units=10, name='logits')
y = tf.nn.softmax(logits_, name='ybar')
if logits:
return y, logits_
return y
# Collect all tensorflow tensors into one "enviroment" to avoid
# accidental overwriting.
class Dummy:
pass
env = Dummy()
# We need a scope since the inference graph will be reused later
with tf.variable_scope('model'):
env.x = tf.placeholder(tf.float32, (None, img_rows, img_cols,
img_chas), name='x')
env.y = tf.placeholder(tf.float32, (None, n_classes), name='y')
env.training = tf.placeholder(bool, (), name='mode')
env.ybar, logits = model(env.x, logits=True,
training=env.training)
z = tf.argmax(env.y, axis=1)
zbar = tf.argmax(env.ybar, axis=1)
count = tf.cast(tf.equal(z, zbar), tf.float32)
env.acc = tf.reduce_mean(count, name='acc')
xent = tf.nn.softmax_cross_entropy_with_logits(labels=env.y,
logits=logits)
env.loss = tf.reduce_mean(xent, name='loss')
env.optim = tf.train.AdamOptimizer().minimize(env.loss)
# Note the reuse=True flag
with tf.variable_scope('model', reuse=True):
env.x_adv = fgsm(model, env.x, epochs=12, eps=0.02)
# --------------------------------------------------------------------
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
# --------------------------------------------------------------------
def _evaluate(X_data, y_data, env):
print('\nEvaluating')
n_sample = X_data.shape[0]
batch_size = 128
n_batch = int(np.ceil(n_sample/batch_size))
loss, acc = 0, 0
for ind in range(n_batch):
print(' batch {0}/{1}'.format(ind+1, n_batch), end='\r')
start = ind*batch_size
end = min(n_sample, start+batch_size)
batch_loss, batch_acc = sess.run(
[env.loss, env.acc],
feed_dict={env.x: X_data[start:end],
env.y: y_data[start:end],
env.training: False})
loss += batch_loss*batch_size
acc += batch_acc*batch_size
loss /= n_sample
acc /= n_sample
print(' loss: {0:.4f} acc: {1:.4f}'.format(loss, acc))
return loss, acc
def _predict(X_data, env):
print('\nPredicting')
n_sample = X_data.shape[0]
batch_size = 128
n_batch = int(np.ceil(n_sample/batch_size))
yval = np.empty((X_data.shape[0], n_classes))
for ind in range(n_batch):
print(' batch {0}/{1}'.format(ind+1, n_batch), end='\r')
start = ind*batch_size
end = min(n_sample, start+batch_size)
batch_y = sess.run(env.ybar, feed_dict={
env.x: X_data[start:end], env.training: False})
yval[start:end] = batch_y
print()
return yval
# --------------------------------------------------------------------
print('\nTraining')
n_sample = X_train.shape[0]
batch_size = 128
n_batch = int(np.ceil(n_sample/batch_size))
n_epoch = 5
for epoch in range(n_epoch):
print('Epoch {0}/{1}'.format(epoch+1, n_epoch))
for ind in range(n_batch):
print(' batch {0}/{1}'.format(ind+1, n_batch), end='\r')
start = ind*batch_size
end = min(n_sample, start+batch_size)
sess.run(env.optim, feed_dict={env.x: X_train[start:end],
env.y: y_train[start:end],
env.training: True})
_evaluate(X_valid, y_valid, env)
print('\nTesting against clean data')
_evaluate(X_test, y_test, env)
# --------------------------------------------------------------------
if False:
print('\nLoading adversarial')
X_adv = np.load('data/ex_00.npy')
else:
print('\nCrafting adversarial')
n_sample = X_test.shape[0]
batch_size = 128
n_batch = int(np.ceil(n_sample/batch_size))
n_epoch = 20
X_adv = np.empty_like(X_test)
for ind in range(n_batch):
print(' batch {0}/{1}'.format(ind+1, n_batch), end='\r')
start = ind*batch_size
end = min(n_sample, start+batch_size)
tmp = sess.run(env.x_adv, feed_dict={env.x: X_test[start:end],
env.y: y_test[start:end],
env.training: False})
X_adv[start:end] = tmp
print('\nSaving adversarial')
os.makedirs('data', exist_ok=True)
np.save('data/ex_00.npy', X_adv)
print('\nTesting against adversarial data')
_evaluate(X_adv, y_test, env)
# --------------------------------------------------------------------
y1 = _predict(X_test, env)
y2 = _predict(X_adv, env)
z0 = np.argmax(y_test, axis=1)
z1 = np.argmax(y1, axis=1)
z2 = np.argmax(y2, axis=1)
X_tmp = np.empty((10, 28, 28))
y_tmp = np.empty((10, 10))
for i in range(10):
print('Target {0}'.format(i))
ind, = np.where(np.all([z0==i, z1==i, z2!=i], axis=0))
cur = np.random.choice(ind)
X_tmp[i] = np.squeeze(X_adv[cur])
y_tmp[i] = y2[cur]
print('\nPlotting results')
fig = plt.figure(figsize=(10, 1.8))
gs = gridspec.GridSpec(1, 10, wspace=0.1, hspace=0.1)
label = np.argmax(y_tmp, axis=1)
proba = np.max(y_tmp, axis=1)
for i in range(10):
ax = fig.add_subplot(gs[0, i])
ax.imshow(X_tmp[i], cmap='gray', interpolation='none')
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlabel('{0} ({1:.2f})'.format(label[i], proba[i]),
fontsize=12)
print('\nSaving figure')
gs.tight_layout(fig)
os.makedirs('img', exist_ok=True)
plt.savefig('img/ex_00.png')