Closed akash-joshi closed 6 years ago
That’s a TensorFlow Problem with your model. You are missing the initialization. Have a look at the TensorFlow examples: https://foolbox.readthedocs.io/en/latest/user/examples.html
I'm using from_keras constructor as I am using tf.keras.layers . Where am I missing the initialization ?
model.load_weights('model.weights.best.hdf5')
network = tf.keras.models.load_model('model.weights.best.hdf5')
foolbox_model = foolbox.models.TensorFlowModel.from_keras(model = network,bounds = (0.0, 1.0))
attack = foolbox.attacks.FGSM(foolbox_model)
image = x_test[5]
adversarial = attack(image.reshape(28,28,1),label = 1)
print(network.predict(adversarial))
tf.keras.layers
is independent of tf.keras.Model
, i.e. one does not imply the other; having said that: I understand your problem. This is indeed not super intuitive. The problem is that tf.keras.Model
creates its own tf.Session
, but it's not set as the default session and Foolbox currently only checks for the default session and also creates its own one if no default session exists.
So this is a simple workaround:
with tf.keras.backend.get_session().as_default():
foolbox_model = foolbox.models.TensorFlowModel.from_keras(model=network, bounds=(0.0, 1.0))
I will try to change the from_keras
constructor to use the session created by tf.keras
. This will probably be part of the next Foolbox release.
@akash-joshi This problem has been solved in Foolbox 1.6.1. Your original code should now work! Before 1.6.1, please use the workaround mentioned above (or upgrade to 1.6.1).
Thank You !! It works !!
I'm trying to generate an adversarial attack for the Fashion MNIST dataset, but I'm getting a long error at the declaration of the attack. Can anyone help me debug this ? Colab : https://colab.research.google.com/drive/1EsFegH4ik_2FZ54vIUkpAeYj6J_s7MUO
`--------------------------------------------------------------------------- FailedPreconditionError Traceback (most recent call last) /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, args) 1291 try: -> 1292 return fn(args) 1293 except errors.OpError as e:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata) 1276 return self._call_tf_sessionrun( -> 1277 options, feed_dict, fetch_list, target_list, run_metadata) 1278
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata) 1366 self._session, options, feed_dict, fetch_list, target_list, -> 1367 run_metadata) 1368
FailedPreconditionError: Error while reading resource variable conv2d_21/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/conv2d_21/kernel) [[{{node sequential_14/conv2d/Conv2D/ReadVariableOp}} = ReadVariableOpdtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]] [[{{node sequential_14/dense_2/Softmax/_5}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_153_sequential_14/dense_2/Softmax", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
FailedPreconditionError Traceback (most recent call last)