【问题标题】:How can I use a Keras trained model saved in a HDF5 file to make predictions?如何使用保存在 HDF5 文件中的 Keras 训练模型进行预测?
【发布时间】:2020-06-19 16:40:26
【问题描述】:
我最近开始使用神经网络。我使用扩展的 MNIST 数据集、sklearn、Keras、numpy 和 pandas 构建了一个手写字符预测模型。主要目标是拍摄和/或上传手写文本/字符的图片,并且模型应该能够猜测。
完成训练阶段后,模型保存在文件my_model.h5中。在这一点上,我如何在 Python 程序中使用这个经过训练的模型(特别是 my_model.h5)来接收输入图像并产生预测?
【问题讨论】:
标签:
python
machine-learning
keras
prediction
【解决方案1】:
在 Keras 和 TensorFlow 中,有多种方法可以从文件中加载(经过训练的)模型。
Keras documentation 提供了一个显示如何加载模型的 sn-p。
from keras.models import load_model
# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')
加载模型后,您可以使用model.predict。
您可以根据需要将此代码集成到您的应用程序中。