简体   繁体   English

如何使用保存在 HDF5 文件中的 Keras 训练模型进行预测?

[英]How can I use a Keras trained model saved in a HDF5 file to make predictions?

I recently got started with neural networks.我最近开始使用神经网络。 I built a handwritten character prediction model using the extended MNIST dataset, sklearn, Keras, numpy and pandas.我使用扩展的 MNIST 数据集、sklearn、Keras、numpy 和 pandas 构建了一个手写字符预测模型。 The main goal is to take and/or upload pictures of handwritten text/characters and the model should be able to guess.主要目标是拍摄和/或上传手写文本/字符的图片,并且模型应该能够猜测。

After finishing the training phase, the model was saved in the file my_model.h5 .完成训练阶段后,模型保存在文件my_model.h5 At this point, how could I use this trained model (specifically, the my_model.h5 ) in a Python program that receives as input images and should produce a prediction?在这一点上,我如何在 Python 程序中使用这个经过训练的模型(特别是my_model.h5 ),该程序接收作为输入图像并应该产生预测?

There are different ways to load a (trained) model from a file in Keras and TensorFlow.从 Keras 和 TensorFlow 中的文件加载(经过训练的)模型有不同的方法。

The Keras documentation provides a snippet that shows how to load a model. Keras 文档提供了一个展示如何加载模型的片段。

from keras.models import load_model

# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')

After having loaded the model, you can use model.predict .加载模型后,您可以使用model.predict

You integrate this code into your application as you wish.您可以根据需要将此代码集成到您的应用程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM