bethgelab / foolbox

A Python toolbox to create adversarial examples that fool neural networks in PyTorch, TensorFlow, and JAX
https://foolbox.jonasrauber.de
MIT License
2.77k stars 427 forks source link

Fashion MNIST FailedPreconditionError #219

Closed akash-joshi closed 6 years ago

akash-joshi commented 6 years ago

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)

in () 7 image = x_test[5] 8 ----> 9 adversarial = attack(image,label = 2) 10 print(model.predict(adversarial)) 11 /usr/local/lib/python3.6/dist-packages/foolbox/attacks/base.py in wrapper(self, input_or_adv, label, unpack, **kwargs) 122 ' instance.') 123 a = Adversarial(model, criterion, input_or_adv, label, --> 124 distance=distance, threshold=threshold) 125 126 assert a is not None /usr/local/lib/python3.6/dist-packages/foolbox/adversarial.py in __init__(self, model, criterion, original_image, original_class, distance, threshold, verbose) 85 # check if the original image is already adversarial 86 try: ---> 87 self.predictions(original_image) 88 except StopAttack: 89 # if a threshold is specified and the original input is /usr/local/lib/python3.6/dist-packages/foolbox/adversarial.py in predictions(self, image, strict, return_details) 303 304 self._total_prediction_calls += 1 --> 305 predictions = self.__model.predictions(image) 306 is_adversarial, is_best, distance = self.__is_adversarial( 307 image, predictions, in_bounds) /usr/local/lib/python3.6/dist-packages/foolbox/models/base.py in predictions(self, image) 158 159 """ --> 160 return np.squeeze(self.batch_predictions(image[np.newaxis]), axis=0) 161 162 @abstractmethod /usr/local/lib/python3.6/dist-packages/foolbox/models/tensorflow.py in batch_predictions(self, images) 130 predictions = self._session.run( 131 self._batch_logits, --> 132 feed_dict={self._images: images}) 133 return predictions 134 /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 885 try: 886 result = self._run(None, fetches, feed_dict, options_ptr, --> 887 run_metadata_ptr) 888 if run_metadata: 889 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 1108 if final_fetches or final_targets or (handle and feed_dict_tensor): 1109 results = self._do_run(handle, final_targets, final_fetches, -> 1110 feed_dict_tensor, options, run_metadata) 1111 else: 1112 results = [] /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 1284 if handle is None: 1285 return self._do_call(_run_fn, feeds, fetches, targets, options, -> 1286 run_metadata) 1287 else: 1288 return self._do_call(_prun_fn, handle, feeds, fetches) /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 1306 self._config.experimental.client_handles_error_formatting): 1307 message = error_interpolation.interpolate(message, self._graph) -> 1308 raise type(e)(node_def, op, message) 1309 1310 def _extend_graph(self): 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}} = ReadVariableOp[dtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](conv2d_21/kernel)]] [[{{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"]()]] Caused by op 'sequential_14/conv2d/Conv2D/ReadVariableOp', defined at: File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/usr/lib/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals) File "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py", line 16, in app.launch_new_instance() File "/usr/local/lib/python3.6/dist-packages/traitlets/config/application.py", line 658, in launch_instance app.start() File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelapp.py", line 477, in start ioloop.IOLoop.instance().start() File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/ioloop.py", line 177, in start super(ZMQIOLoop, self).start() File "/usr/local/lib/python3.6/dist-packages/tornado/ioloop.py", line 888, in start handler_func(fd_obj, events) File "/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py", line 277, in null_wrapper return fn(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events self._handle_recv() File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv self._run_callback(callback, msg) File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback callback(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py", line 277, in null_wrapper return fn(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher return self.dispatch_shell(stream, msg) File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell handler(stream, idents, msg) File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request user_expressions, allow_stdin) File "/usr/local/lib/python3.6/dist-packages/ipykernel/ipkernel.py", line 196, in do_execute res = shell.run_cell(code, store_history=store_history, silent=silent) File "/usr/local/lib/python3.6/dist-packages/ipykernel/zmqshell.py", line 533, in run_cell return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell interactivity=interactivity, compiler=compiler, result=result) File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes if self.run_code(code, result): File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 3, in foolbox_model = foolbox.models.TensorFlowModel.from_keras(model = network,bounds = (0.0, 1.0)) File "/usr/local/lib/python3.6/dist-packages/foolbox/models/tensorflow.py", line 111, in from_keras logits = model(inputs) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 769, in __call__ outputs = self.call(inputs, *args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/sequential.py", line 229, in call return super(Sequential, self).call(inputs, training=training, mask=mask) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py", line 845, in call mask=masks) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py", line 1031, in _run_internal_graph output_tensors = layer.call(computed_tensor, **kwargs) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/convolutional.py", line 186, in call outputs = self._convolution_op(inputs, self.kernel) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_ops.py", line 869, in __call__ return self.conv_op(inp, filter) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_ops.py", line 521, in __call__ return self.call(inp, filter) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_ops.py", line 205, in __call__ name=self.name) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 957, in conv2d data_format=data_format, dilations=dilations, name=name) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 510, in _apply_op_helper preferred_dtype=default_dtype) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 1144, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py", line 1286, in _dense_var_to_tensor return var._dense_var_to_tensor(dtype=dtype, name=name, as_ref=as_ref) # pylint: disable=protected-access File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py", line 1241, in _dense_var_to_tensor return self.value() File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py", line 672, in value return self._read_variable_op() File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py", line 755, in _read_variable_op self._dtype) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_resource_variable_ops.py", line 508, in read_variable_op "ReadVariableOp", resource=resource, dtype=dtype, name=name) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py", line 488, in new_func return func(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3272, in create_op op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 1768, in __init__ self._traceback = tf_stack.extract_stack() FailedPreconditionError (see above for traceback): 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}} = ReadVariableOp[dtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](conv2d_21/kernel)]] [[{{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"]()]] SEARCH STACK OVERFLOW`
jonasrauber commented 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

akash-joshi commented 6 years ago

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))
jonasrauber commented 6 years ago

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.

jonasrauber commented 6 years ago

@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).

akash-joshi commented 6 years ago

Thank You !! It works !!