经过训练的Keras模型无法使用load_model加载

时间:2018-11-06 04:39:51

标签: python tensorflow keras

我已经用Tensorflow后端训练了Keras模型。它已保存在model.save中。我现在想使用model_load重新加载模型,但是,出现以下错误:

Traceback (most recent call last):

  File "<ipython-input-235-387752c910a4>", line 1, in <module>
    load_model('MyModel.h5')

  File "C:\Anaconda\envs\tensorflow\lib\site-packages\keras\models.py", line 243, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)

  File "C:\Anaconda\envs\tensorflow\lib\site-packages\keras\models.py", line 317, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)

  File "C:\Anaconda\envs\tensorflow\lib\site-packages\keras\layers\__init__.py", line 55, in deserialize
    printable_module_name='layer')

  File "C:\Anaconda\envs\tensorflow\lib\site-packages\keras\utils\generic_utils.py", line 144, in deserialize_keras_object
    list(custom_objects.items())))

  File "C:\Anaconda\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 2514, in from_config
    process_layer(layer_data)

  File "C:\Anaconda\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 2500, in process_layer
    custom_objects=custom_objects)

  File "C:\Anaconda\envs\tensorflow\lib\site-packages\keras\layers\__init__.py", line 55, in deserialize
    printable_module_name='layer')

  File "C:\Anaconda\envs\tensorflow\lib\site-packages\keras\utils\generic_utils.py", line 144, in deserialize_keras_object
    list(custom_objects.items())))

  File "C:\Anaconda\envs\tensorflow\lib\site-packages\keras\models.py", line 1367, in from_config
    if 'class_name' not in config[0] or config[0]['class_name'] == 'Merge':

KeyError: 0

从我读到的内容来看,当使用较旧版本的Keras训练的模型加载了最新版本时,Keras中似乎存在错误。因此可能存在版本不匹配的情况。但是,我找不到符合我情况的报告。不能对Keras进行降级或重新培训。

有人有这个问题,甚至找到解决方案了吗?我将不胜感激!

谢谢!

1 个答案:

答案 0 :(得分:1)

供以后参考:这是配置文件中的一个问题。 Keras 2.2.4对此进行了修复:

Keras 2.2.4

@fchollet fchollet在10月3日发布了此内容·自发布以来,有79位提交到master 资产2

这是一个错误修正版本,解决了两个问题:

具有相同名称的文件时,可以保存模型。 加载顺序模型的旧配置文件时出现问题。

因此,我最终使用最新的TF和Keras版本创建了一个新的虚拟环境。

相关问题