gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:06:46 26 4
gpt4 key购买 nike

我用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com