-
Notifications
You must be signed in to change notification settings - Fork 0
/
adversarial.py
177 lines (130 loc) · 5.05 KB
/
adversarial.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
# Generates adversarial images
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os.path as path
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import covnet_model
from utils import create_directories, get_class, savedir, logdir, exampledir
#################### HYPER PARAMETERS ####################
learning_rate = 0.04
#################### BUILD ADVERSARIAL GRAPH ####################
def build_graph(source, target):
with tf.name_scope('Adversarial') as scope:
scaled_logits = tf.nn.softmax(covnet_model.pred)
src_acc = scaled_logits[0, source]
trgt_acc = scaled_logits[0, target]
tf.scalar_summary('target_accuracy', trgt_acc)
tf.scalar_summary('source_accuracy', src_acc)
# run gradient descent on element of scaled_logits vector corresponding
# to target adversarial class.
grad = tf.reshape(tf.gradients(trgt_acc, covnet_model.x)[0], shape=[1, 28, 28, 1])
tf.image_summary('grad', grad)
tf.histogram_summary('grad', grad)
return scaled_logits, src_acc, trgt_acc, grad
#################### TRAINING SCHEMA ####################
def train(source, target):
scaled_logits, src_acc, trgt_acc, grad = build_graph(source, target)
init = tf.global_variables_initializer()
summaries = tf.merge_all_summaries()
if not path.isdir(savedir):
print('No models found. Start training.')
covnet_model.train()
create_directories()
if raw_input('Do you want to use your own weights? [y\N] ') == 'y':
fname = raw_input('Enter saved model name > ')
weights = path.join(savedir, fname)
else:
weights = path.join(savedir, 'default')
with tf.Session() as sess:
sess.run(init)
covnet_model.saver.restore(sess, weights)
print('Weights restored.')
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
writer = tf.train.SummaryWriter(
logdir,
graph=sess.graph
)
src_images, src_labels = get_class(
source,
mnist.test.images,
mnist.test.labels
)
# pick a random image that is correctly classified by CNN
k = 0
while True:
original = src_images[np.newaxis, k]
label = src_labels[np.newaxis, k]
image = np.copy(original)
l = scaled_logits.eval(feed_dict={
covnet_model.x: original,
covnet_model.y: label,
covnet_model.keep_prob: 1.
}
)
if np.argmax(l) == source:
# correctly classified
break
print('Generating Adversarial Image...')
print('Open tensorboard to visualize.')
# train loop
i = 0
target_acc = 0.
start_acc = []
while target_acc < .99: # fool to 99% acc
source_acc, target_acc, dimg, summ = sess.run(
[src_acc, trgt_acc, grad, summaries],
feed_dict={
covnet_model.x: image,
covnet_model.y: label,
covnet_model.keep_prob: 1.
}
)
if i == 0:
start_acc.extend([source_acc, target_acc])
writer.add_summary(summ, global_step=i)
image = image + learning_rate * dimg.reshape(1, 28*28)
diff = np.abs(original - image)
print("%d source_acc %.5f, target_acc %.5f, sum: %.5f" % (
i, source_acc, target_acc, np.sum(diff)
)
)
i += 1
print('Adversarial example generated.')
# Show the example
fig = plt.figure(figsize=(30, 10))
plt.subplot(131)
plt.imshow(original.reshape(28, 28), cmap='gray')
plt.axis('off')
plt.title('Original. source: (%f), target: (%f)' % tuple(start_acc))
plt.subplot(132)
plt.imshow(diff.reshape(28, 28), cmap='gray')
plt.title('Delta (%f)' % np.sum(diff))
plt.axis('off')
plt.subplot(133)
plt.imshow(image.reshape(28, 28), cmap='gray')
plt.axis('off')
plt.title('Adversarial source: (%f), target: (%f)' % (source_acc, target_acc))
plt.show()
# ask to save
while True:
prompt = raw_input('Do you want to save this example? [y\N] ')
if prompt == 'y':
fname = raw_input('Enter name of npy file without extension > ')
np.savez(
path.join(exampledir, fname),
source=original,
delta=diff,
target=image,
source_acc=source_acc,
target_acc=target_acc
)
break
elif prompt == 'N':
break
covnet_model.train_sess.close()
if __name__ == '__main__':
train(2, 6)