加载Keras模型KeyError:'weighted_metrics' [英] Loading Keras Model KeyError: 'weighted_metrics'

查看:453
本文介绍了加载Keras模型KeyError:'weighted_metrics'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过Keras加载模型时出现以下错误:

I get the following error when trying to load a model through Keras:

Traceback (most recent call last):
  File "visualise.py", line 82, in <module>
    model = tf.keras.models.load_model(CNN_MODEL_DIR)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 251, in load_model
    training_config['weighted_metrics'])
KeyError: 'weighted_metrics'

我正在使用model.save("Models/ResNet.model")保存模型,并使用model = tf.keras.models.load_model(CNN_MODEL_DIR)加载模型.

I am using model.save("Models/ResNet.model") to save the model and model = tf.keras.models.load_model(CNN_MODEL_DIR) to load the model.

我正在训练该型号的计算机具有 Keras 2.1.2 ,而我要在其上进行测试的计算机(我的家用计算机)具有 Keras 2.2.4 .有没有解决这个错误的方法,而无需重新训练模型或降级我的Keras版本?

The computer I am training this model on has Keras 2.1.2 and the computer I am wanting to test it on (my home computer) has Keras 2.2.4. Is there a way to get around this error without retraining the model or downgrading my version of Keras?

推荐答案

我确定了问题:

我用from keras.applications.resnet50 import ResNet50导入了ResNet模型,并仅通过使用model.save()保存了它.因此,我的模型纯粹与Keras有关.因此,当我尝试使用与Tensorflow相关的导入(即model = tf.keras.models.load_model(CNN_MODEL_DIR))加载它时,出现了错误.

I imported the ResNet model with from keras.applications.resnet50 import ResNet50 and saved it by simply using model.save(). Therefore, my model was purely Keras-related. Consequently, when I tried loading it with a Tensorflow-related import (i.e. model = tf.keras.models.load_model(CNN_MODEL_DIR)) I got the error.

解决方法只是将导入更改为纯粹的Keras.

The fix was simply to change the import to be purely Keras.

model = keras.models.load_model(CNN_MODEL_DIR)

这篇关于加载Keras模型KeyError:'weighted_metrics'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆