我已经在linux 64机器上训练了keras顺序模型并保存为.h5文件。
这台PC我可以加载模型并毫无问题地进行预测。
现在我在Raspberry Pi 3中实现了已经安装了keras,tensorflow,h5py和python3的预测。
加载模型时
from keras.models import load_model
model = load_model('model-0.6358.h5')
,我得到了:
usr/lib/python3.4/importlib/_bootstrap.py:321: 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`.
return f(*args, **kwds)
/usr/local/lib/python3.4/dist-packages/keras/models.py:291: UserWarning: Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.
warnings.warn('Error in loading the saved optimizer '
但是......它看起来似乎是正确的。
如何避免该警告信息?
答案 0 :(得分:8)
load_model
首先使用其保存的权重构建已保存的模型体系结构,然后尝试使用其保存的权重构建已保存的优化程序。
但是,您会收到一条错误消息,因为根据加载模型的体系结构,保存的优化程序权重的形状与优化程序所期望的权重形状之间不匹配。
当我尝试保存并重新加载具有设置为trainable=False
的内部子模型的模型时,我使用Keras 2.1.4遇到了这个问题。保存模型时似乎不保留此信息,因此在重新设置内部子模型后设置为trainable=True
并且优化器将期望比实际保存更多的保存权重。如果这可能是您的问题,我在this bug-report中描述了一种解决方法:
如果您想要消除警告并且在保存之后不需要优化器,您也可以在没有优化器的情况下保存模型:使用model.save(filename, include_optimizer=False)
答案 1 :(得分:1)
我通过在 compile = False
函数中添加 load_model
解决了这个问题。
对官方网站的引用: https://www.tensorflow.org/api_docs/python/tf/keras/models/load_model?hl=en
这里有一个例子:
model = tf.keras.models.load_model('path/of/your/model', compile = False)