首页 > 解决方案 > 如何使用 Keras 中保存的模型对图像进行预测和分类?

问题描述

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

标签: pythontensorflowmachine-learningkerasartificial-intelligence

解决方案


首先,您必须使用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)   

推荐阅读