-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils_tf.py
244 lines (199 loc) · 8.23 KB
/
utils_tf.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
'''
Copy from the cleverhans package (ver 1.0.0)
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import math
import numpy as np
import os
import keras
from keras.backend import categorical_crossentropy
import six
import tensorflow as tf
import time
import warnings
from tensorflow.python.platform import flags
from utils import batch_indices
from tensorflow.python.platform import flags
FLAGS = flags.FLAGS
def model_loss(y, model, mean=True):
"""
Define loss of TF graph
:param y: correct labels
:param model: output of the model
:return: return mean of loss if True, otherwise return vector with per
sample loss
"""
op = model.op
if "softmax" in str(op).lower():
logits, = op.inputs
else:
logits = model
out = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y)
if mean:
out = tf.reduce_mean(out)
return out
def tf_model_train(*args, **kwargs):
warnings.warn("`tf_model_train` is deprecated. Switch to `model_train`."
"`tf_model_train` will be removed after 2017-07-18.")
return model_train(*args, **kwargs)
def model_train(sess, x, y, predictions, X_train, Y_train, save=False,
predictions_adv=None, evaluate=None):
"""
Train a TF graph
:param sess: TF session to use when training the graph
:param x: input placeholder
:param y: output placeholder (for labels)
:param predictions: model output predictions
:param X_train: numpy array with training inputs
:param Y_train: numpy array with training outputs
:param save: Boolean controling the save operation
:param predictions_adv: if set with the adversarial example tensor,
will run adversarial training
:return: True if model trained
"""
# Define loss
loss = model_loss(y, predictions)
if predictions_adv is not None:
loss = (loss + model_loss(y, predictions_adv)) / 2
train_step = tf.train.AdadeltaOptimizer(learning_rate=FLAGS.learning_rate,
rho=0.95,
epsilon=1e-08).minimize(loss)
with sess.as_default():
if hasattr(tf, "global_variables_initializer"):
tf.global_variables_initializer().run()
else:
warnings.warn("Update your copy of tensorflow; future versions of"
"cleverhans may drop support for this version.")
sess.run(tf.initialize_all_variables())
for epoch in six.moves.xrange(FLAGS.nb_epochs):
print("Epoch " + str(epoch))
# Compute number of batches
nb_batches = int(math.ceil(float(len(X_train)) / FLAGS.batch_size))
assert nb_batches * FLAGS.batch_size >= len(X_train)
prev = time.time()
for batch in range(nb_batches):
# Compute batch start and end indices
start, end = batch_indices(batch, len(X_train), FLAGS.batch_size)
# Perform one training step
train_step.run(feed_dict={x: X_train[start:end],
y: Y_train[start:end],
keras.backend.learning_phase(): 1})
assert end >= len(X_train) # Check that all examples were used
cur = time.time()
print("\tEpoch took " + str(cur - prev) + " seconds")
prev = cur
if evaluate is not None:
evaluate()
if save:
save_path = os.path.join(FLAGS.train_dir, FLAGS.filename)
saver = tf.train.Saver()
saver.save(sess, save_path)
print("Completed model training and model saved at:" + str(save_path))
else:
print("Completed model training.")
return True
def tf_model_eval(*args, **kwargs):
warnings.warn("`tf_model_eval` is deprecated. Switch to `model_eval`."
"`tf_model_eval` will be removed after 2017-07-18.")
return model_eval(*args, **kwargs)
def model_eval(sess, x, y, model, X_test, Y_test):
"""
Compute the accuracy of a TF model on some data
:param sess: TF session to use when training the graph
:param x: input placeholder
:param y: output placeholder (for labels)
:param model: model output predictions
:param X_test: numpy array with training inputs
:param Y_test: numpy array with training outputs
:return: a float with the accuracy value
"""
# Define sympbolic for accuracy
acc_value = keras.metrics.categorical_accuracy(y, model)
# Init result var
accuracy = 0.0
with sess.as_default():
# Compute number of batches
nb_batches = int(math.ceil(float(len(X_test)) / FLAGS.batch_size))
assert nb_batches * FLAGS.batch_size >= len(X_test)
for batch in range(nb_batches):
if batch % 100 == 0 and batch > 0:
print("Batch " + str(batch))
# Must not use the `batch_indices` function here, because it
# repeats some examples.
# It's acceptable to repeat during training, but not eval.
start = batch * FLAGS.batch_size
end = min(len(X_test), start + FLAGS.batch_size)
cur_batch_size = end - start
# The last batch may be smaller than all others, so we need to
# account for variable batch size here
accuracy += cur_batch_size * acc_value.eval(feed_dict={x: X_test[start:end],
y: Y_test[start:end],
keras.backend.learning_phase(): 0})
assert end >= len(X_test)
# Divide by number of examples to get final value
accuracy /= len(X_test)
return accuracy
def tf_model_load(sess):
"""
:param sess:
:param x:
:param y:
:param model:
:return:
"""
with sess.as_default():
saver = tf.train.Saver()
saver.restore(sess, os.path.join(FLAGS.train_dir, FLAGS.filename))
return True
def batch_eval(sess, tf_inputs, tf_outputs, numpy_inputs):
"""
A helper function that computes a tensor on numpy inputs by batches.
"""
n = len(numpy_inputs)
assert n > 0
assert n == len(tf_inputs)
m = numpy_inputs[0].shape[0]
for i in six.moves.xrange(1, n):
assert numpy_inputs[i].shape[0] == m
out = []
for _ in tf_outputs:
out.append([])
with sess.as_default():
for start in six.moves.xrange(0, m, FLAGS.batch_size):
batch = start // FLAGS.batch_size
if batch % 100 == 0 and batch > 0:
print("Batch " + str(batch))
# Compute batch start and end indices
start = batch * FLAGS.batch_size
end = start + FLAGS.batch_size
numpy_input_batches = [numpy_input[start:end] for numpy_input in numpy_inputs]
cur_batch_size = numpy_input_batches[0].shape[0]
assert cur_batch_size <= FLAGS.batch_size
for e in numpy_input_batches:
assert e.shape[0] == cur_batch_size
feed_dict = dict(zip(tf_inputs, numpy_input_batches))
feed_dict[keras.backend.learning_phase()] = 0
numpy_output_batches = sess.run(tf_outputs, feed_dict=feed_dict)
for e in numpy_output_batches:
assert e.shape[0] == cur_batch_size, e.shape
for out_elem, numpy_output_batch in zip(out, numpy_output_batches):
out_elem.append(numpy_output_batch)
out = [np.concatenate(x, axis=0) for x in out]
for e in out:
assert e.shape[0] == m, e.shape
return out
def model_argmax(sess, x, predictions, sample):
"""
Helper function that computes the current class prediction
:param sess: TF session
:param x: the input placeholder
:param predictions: the model's symbolic output
:param sample: (1 x 1 x img_rows x img_cols) numpy array with sample input
:return: the argmax output of predictions, i.e. the current predicted class
"""
feed_dict = {x: sample, keras.backend.learning_phase(): 0}
probabilities = sess.run(predictions, feed_dict)
return np.argmax(probabilities)