如果使用Tensorflow构建模型,则如何使用theano作为后端加载在Keras中构建的模型

时间:2019-03-02 00:46:33

标签: python tensorflow keras theano

我有一个使用视网膜网络构建的对象检测模型,该模型是使用Tensorflow后端的Keras构建的。但是现在我希望使用Theano后端进行测试,因为theano线程安全性更高,并且允许在同一模型上运行多个预测。但是,当我尝试使用theano作为后端运行代码时,出现以下错误:

TypeError: Failed to convert object of type <class 'theano.tensor.var.TensorVariable'> to Tensor. Contents: Elemwise{add,no_inplace}.0. Consider casting elements to a supported type

该错误的整个堆栈跟踪为:

Traceback (most recent call last):
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/tensorflow/python/framework/tensor_util.py", line 517, in make_tensor_proto
    str_values = [compat.as_bytes(x) for x in proto_values]
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/tensorflow/python/framework/tensor_util.py", line 517, in <listcomp>
    str_values = [compat.as_bytes(x) for x in proto_values]
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/tensorflow/python/util/compat.py", line 67, in as_bytes
    (bytes_or_text,))
TypeError: Expected binary or unicode string, got Elemwise{add,no_inplace}.0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "convert_to_theano.py", line 8, in <module>
    model = load_model('retinanet_models/models/tlc_all_new30.h5', backbone_name='resnet50')
  File "/data/home/ubuntu/myproject/keras_retinanet/models/__init__.py", line 76, in load_model
    model = keras.models.load_model(filepath, custom_objects=backbone(backbone_name).custom_objects)
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/keras/engine/saving.py", line 261, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/keras/engine/saving.py", line 335, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
    list(custom_objects.items())))
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/keras/engine/network.py", line 1046, in from_config
    process_node(layer, node_data)
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/keras/engine/network.py", line 1005, in process_node
    layer(input_tensors, **kwargs)
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/keras/engine/base_layer.py", line 460, in __call__
    output = self.call(inputs, **kwargs)
  File "/data/home/ubuntu/myproject/keras_retinanet/layers/_misc.py", line 82, in call
    return backend.resize_images(source, (target_shape[1], target_shape[2]))
  File "/data/home/ubuntu/myproject/keras_retinanet/backend/tensorflow_backend.py", line 38, in resize_images
    return tensorflow.image.resize_images(*args, **kwargs)
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/tensorflow/python/ops/image_ops_impl.py", line 893, in resize_images
    images = ops.convert_to_tensor(images, name='images')
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1014, in convert_to_tensor
    as_ref=False)
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1104, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 235, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 214, in constant
    value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "/data/home/ubuntu/myproject/myprojectenv/lib/python3.5/site-packages/tensorflow/python/framework/tensor_util.py", line 521, in make_tensor_proto
    "supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'theano.tensor.var.TensorVariable'> to Tensor. Contents: Elemwise{add,no_inplace}.0. Consider casting elements to a supported type.

我已经知道如何更改.keras / keras.json文件以选择哪个后端,并且可以正常工作。启动代码时,它会提供Using Theano,所以我知道它使用的是正确的后端

0 个答案:

没有答案