python - 如何使用 Keras 中保存的模型来预测和分类图像?

标签 python tensorflow machine-learning keras artificial-intelligence

我用 Keras 训练了一个模型手部位置分类器,最后用代码保存了模型 (model.save('model.h5') ) 现在我正在尝试使用这个模型来预测图像,它可行吗?如果是的话,您能给我一些例子吗? PS:我的数据以 CSV 文件形式提供

最佳答案

首先,您必须使用 load_model 函数导入保存的模型。

from keras.models import load_model
model = load_model('model.h5')

在预测新给定输入的结果之前,您必须调用 compile 方法。

classifier.compile(loss='your_loss', optimizer='your_optimizer', metrics=['your_metrics'])

编译后,您就可以处理新图像了。

from keras.preprocessing import image

test_image= image.load_img(picturePath, target_size = (img_width, img_height)) 
test_image = image.img_to_array(test_image)
test_image = numpy.expand_dims(test_image, axis = 0)
test_image = test_image.reshape(img_width, img_height)
result = model.predict(test_image)   

关于python - 如何使用 Keras 中保存的模型来预测和分类图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50151585/

相关文章:

python - 在碰撞中将能量从一个对象转移到另一个对象(在pymunk/chipmunk中)

python - argparse 在代码中的位置

python - 如何使用 tf.one_hot 计算一种热编码?

python - 如何有效地将初始值传递给 get_variable

python - 聚类内部聚类是数据表的嵌套聚类,是多类聚类

r - 将数据拆分为不具有代表性类别的训练和评估数据集

javascript - 在 Python 中更改来自 Selenium 的元素列表

python - 用于替换不在引号之间的特定单词的正则表达式

python - comet (comet-ml) 无法与 Keras 一起运行

machine-learning - 如何更加重视机器学习中的某些特征?