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

时间:2018-06-19 22:02:52

标签: python tensorflow keras

有关傻瓜机,请参见here

我是张量流的新手,根据this videothis video的建议,我使用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))

如果我在第一行network = ...中添加注释,并在第二行中添加注释,则上述脚本的结果为

> [3]

在当前状态下,发生以下异常:

.../.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)]]

(我将项目路径替换为»...«,并且正在使用位于.env中的虚拟环境)。

所以我的问题/问题是:如何使用foolbox和tf.keras?我想避免(现在)学习复杂的tensorflow图结构,因此使用foolbox的本机tensorflow api不是一种选择。另外,我也想避免仅为此目的导入常规的keras –我希望在tensorflow中保持整洁。

PS:请创建一个»foolbox«标记。

1 个答案:

答案 0 :(得分:0)

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