简体   繁体   English

Foolbox和tf.keras(在tensorflow中复制keras)

[英]foolbox and tf.keras (keras copy within tensorflow)

For foolbox, see here . 有关Foolbox,请参见此处

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. 我是tensorflow的新手,根据此视频以及推荐的视频 ,我使用tf.keras进行原型制作和“玩”机器学习模型,尤其是神经网络。 Consider this MWE ( lenet5.h5 is a convolutional neural network in HDF5 file format, built and trained by tf.keras): 考虑以下MWE( lenet5.h5是HDF5文件格式的卷积神经网络,由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 如果我在第一个network = ...行中注释并在第二个中注释掉,则上述脚本的结果是

> [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). (我将项目路径替换为»...«,并且正在使用.env中的虚拟环境)。

So my question/issue is: How to use foolbox and tf.keras? 所以我的问题是:如何使用foolbox和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. 我想避免(现在)学习复杂的tensorflow图结构,因此使用foolbox的本机tensorflow api不是一种选择。 Also I'd like to avoid importing regular keras just for this single purpose – I wish to stay clean within tensorflow. 我也想避免仅仅为了这个目的而导入常规的keras –我希望在tensorflow中保持干净。

PS: Please create a »foolbox« tag. PS:请创建一个»foolbox«标签。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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