简体   繁体   中英

foolbox and tf.keras (keras copy within tensorflow)

For foolbox, see here .

I'm new to tensorflow and according to this video and also this video it is recommended, that I use tf.keras for prototyping and »playing with« machine learning models, especially neural networks. Consider this MWE ( lenet5.h5 is a convolutional neural network in HDF5 file format, built and trained by tf.keras):

import numpy as np

import foolbox
import keras
import tensorflow as tf

#network = keras.models.load_model('lenet5.h5')
network = tf.keras.models.load_model('lenet5.h5')
with np.load('data/dataset/mnist_32x32x1.npz') as data:
    x_test = data['testing_x']
foolbox_model = foolbox.models.KerasModel(
    model = network,
    bounds = (0.0, 1.0),
)
criterion = foolbox.criteria.Misclassification()
attack = foolbox.attacks.GradientAttack(foolbox_model, criterion)
image = x_test[0]
adversarial = attack(image, label = 7)
print(np.argmax(network.predict(adversarial.reshape(1, 32, 32, 1)), axis = 1))

If I comment in the first network = ... line and comment out the second one, the result of the above script is

> [3]

whereas in the current state, the following exceptions occur:

.../.env/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64== np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Using TensorFlow backend.
2018-06-19 23:36:34.601249: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Traceback (most recent call last):
File ".../.env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1322, in _do_call
    return fn(*args)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1307, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1409, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value C1/bias
        [[Node: C1/bias/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](C1/bias)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "main.py", line 19, in <module>
    adversarial = attack(image, label = 7)
File ".../.env/lib/python3.5/site-packages/foolbox/attacks/base.py", line 88, in wrapper
    a = Adversarial(model, criterion, input_or_adv, label)
File ".../.env/lib/python3.5/site-packages/foolbox/adversarial.py", line61, in __init__
    self.predictions(original_image)
File ".../.env/lib/python3.5/site-packages/foolbox/adversarial.py", line240, in predictions
    predictions = self.__model.predictions(image)
File ".../.env/lib/python3.5/site-packages/foolbox/models/base.py", line122, in predictions
    return np.squeeze(self.batch_predictions(image[np.newaxis]), axis=0)
File ".../.env/lib/python3.5/site-packages/foolbox/models/keras.py", line 147, in batch_predictions
    predictions = self._batch_pred_fn([self._process_input(images)])
File ".../.env/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 2482, in __call__
    **self.session_kwargs)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 900, in run
    run_metadata_ptr)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1135, in _run
    feed_dict_tensor, options, run_metadata)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1316, in _do_run
    run_metadata)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1335, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value C1/bias
        [[Node: C1/bias/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](C1/bias)]]

Caused by op 'C1/bias/read', defined at:
File "main.py", line 9, in <module>
    network = tf.keras.models.load_model('lenet5.h5')
File ".../.env/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/engine/saving.py", line 241, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/engine/saving.py", line 318, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/layers/serialization.py", line 63, in deserialize
    printable_module_name='layer')
File ".../.env/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/utils/generic_utils.py", line 171, in deserialize_keras_object
    list(custom_objects.items())))
File ".../.env/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/engine/sequential.py", line 317, in from_config
    model.add(layer)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/engine/sequential.py", line 164, in add
    layer(x)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/engine/base_layer.py", line 314, in __call__
    output = super(Layer, self).__call__(inputs, *args, **kwargs)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/layers/base.py", line 699, in __call__
    self.build(input_shapes)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/layers/convolutional.py", line 152, in build
    dtype=self.dtype)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/layers/base.py", line 546, in add_variable
    partitioner=partitioner)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/training/checkpointable.py", line 436, in _add_variable_with_custom_getter
    **kwargs_for_getter)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 1317, in get_variable
    constraint=constraint)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 1079, in get_variable
    constraint=constraint)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 425, in get_variable
    constraint=constraint)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 394, in _true_getter
    use_resource=use_resource, constraint=constraint)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 786, in _get_single_variable
    use_resource=use_resource)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 2220, in variable
    use_resource=use_resource)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 2210, in <lambda>
    previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 2193, in default_variable_creator
    constraint=constraint)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 235, in __init__
    constraint=constraint)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 397, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 142, in identity
    return gen_array_ops.identity(input, name=name)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3187, in identity
    "Identity", input=input, name=name)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3392, in create_op
    op_def=op_def)
File ".../.env/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1718, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value C1/bias
        [[Node: C1/bias/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](C1/bias)]]

(I replaced my project path by »...« and I'm working with a virtual environment located in .env).

So my question/issue is: How to use foolbox and tf.keras? I would like to avoid needing to learn (for now) the complex tensorflow graph structure and therefore it's not an option to use the native tensorflow api of foolbox. Also I'd like to avoid importing regular keras just for this single purpose – I wish to stay clean within tensorflow.

PS: Please create a »foolbox« tag.

在18年10月,FoolBox在其TensorFlowModel模块中实现了from_keras的替代构造from_keras ,适用于上述目的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM