Remove Keras dependency from CW tutorial
@@ -3,7 +3,6 @@ from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
-import keras
import numpy as np
from six.moves import xrange
import tensorflow as tf
@@ -13,10 +12,11 @@ from tensorflow.python.platform import flags
import logging
import os
from cleverhans.attacks import CarliniWagnerL2
-from cleverhans.utils import grid_visual, AccuracyReport, set_log_level
-from cleverhans.utils_keras import cnn_model, KerasModelWrapper
+from cleverhans.utils import pair_visual, grid_visual, AccuracyReport
from cleverhans.utils_mnist import data_mnist
from cleverhans.utils_tf import model_train, model_eval, tf_model_load
+from cleverhans.utils_tf import model_argmax,
+from cleverhans.model import make_basic_cnn
FLAGS = flags.FLAGS
@@ -51,22 +51,12 @@ def mnist_tutorial_cw(train_start=0, train_end=60000, test_start=0,
img_cols = 28
channels = 1
- # Disable Keras learning phase since we will be serving through tensorflow
- keras.layers.core.K.set_learning_phase(0)
-
# Set TF random seed to improve reproducibility
tf.set_random_seed(1234)
- # Image dimensions ordering should follow the TensorFlow convention
- if keras.backend.image_dim_ordering() != 'tf':
- keras.backend.set_image_dim_ordering('tf')
- print("INFO: '~/.keras/keras.json' sets 'image_dim_ordering' "
- "to 'th', temporarily setting to 'tf'")
-
# Create TF session and set as Keras backend session
sess = tf.Session()
- keras.backend.set_session(sess)
- print("Created TensorFlow session and set Keras backend.")
+ print("Created TensorFlow session.")
set_log_level(logging.DEBUG)
@@ -81,7 +71,7 @@ def mnist_tutorial_cw(train_start=0, train_end=60000, test_start=0,
y = tf.placeholder(tf.float32, shape=(None, 10))
# Define TF model graph
- model = cnn_model()
+ model = make_basic_cnn()
preds = model(x)
print("Defined TensorFlow model graph.")
@@ -120,8 +110,7 @@ def mnist_tutorial_cw(train_start=0, train_end=60000, test_start=0,
print("This could take some time ...")
# Instantiate a CW attack object
- wrap = KerasModelWrapper(model)
- cw = CarliniWagnerL2(wrap, back='tf', sess=sess)
+ cw = CarliniWagnerL2(model, back='tf', sess=sess)
idxs = [np.where(np.argmax(Y_test, axis=1) == i)[0][0] for i in range(10)]
if targeted:
