python - 如何使用 Keras 中保存的模型对图像进行预测和分类?
问题描述
我用 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)
推荐阅读
- javascript - 如何基于 Hexcode Jquery 显示货币符号?
- hive - 处理语句时出错:FAILED:执行错误,从 org.apache.hadoop.hive.ql.exec.mr.MapRedTask 返回代码 2 (state=08S01,code=2)
- r - 如何在 Spark 本地模式下检查任务日志
- hive - 在hive中,如何在hql中生成动态表名?
- java - 当结果集有参数时,如何在可自动关闭的资源中设置参数?
- typescript - 打字稿:使用“枚举”值作为“类型”
- performance - 如何在 Jmeter 中更改 http 请求名称
- html - HTML 中的制表符顺序是否合乎逻辑?
- python - Python - 相当于 Ruby 新方法
- java - 如何反转组成句子的字符数组