ValueError:未知层:尝试在keras中加载模型时的名称

时间:2020-05-07 10:57:11

标签: python tensorflow keras

我在Google Colab上训练了Cats vs Dogs分类模型,然后下载了该模型,其权重以及.json文件以获取其结构,但是每当我尝试运行以下代码时:

#read model structure from .json file
with open('./models/cats_vs_dogs_1-15.json', 'r') as json_file:
        json_savedModel = json_file.read()

#load model architecture
model_x = tf.keras.models.model_from_json(json_savedModel)

我收到以下错误:

Traceback (most recent call last):
  File "ddw.py", line 8, in <module>
    model_x = tf.keras.models.model_from_json(json_savedModel)
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/models.py", line 361, in model_from_json
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/layers/serialization.py", line 63, in deserialize
    printable_module_name='layer')
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/utils/generic_utils.py", line 164, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/models.py", line 1333, in from_config
    layer = layer_module.deserialize(conf, custom_objects=custom_objects)
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/layers/serialization.py", line 63, in deserialize
    printable_module_name='layer')
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/utils/generic_utils.py", line 184, in deserialize_keras_object
    function_name)
ValueError: Unknown layer:name

我目前正在运行tensorflow v1.5,因为我的CPU不支持avx,而我正在运行Keras 2.1.5,因为使用Keras 2.3会导致tf_utils错误。我也不在模型中使用任何自定义图层。当我尝试加载模型或模型的权重时,也会发生同样的事情。

我也尝试过在Colab上使用1.x版本的tensorflow训练模型,但是在本地加载时仍然会遇到相同的错误。如果有人可以帮助我,我将非常感激。谢谢!

0 个答案:

没有答案