我用Keras训练了一个手部位置分类器,并使用代码(model.save(‘model.h5’))保存了模型。现在我想用这个模型来预测一张图像,这是可行的吗?如果可以,请提供一些示例。附注:我的数据是以CSV文件的形式提供的。
回答:
首先,您需要使用load_model
函数导入保存的模型。
from keras.models import load_modelmodel = load_model('model.h5')
在预测新输入的结果之前,您需要调用compile
方法。
classifier.compile(loss='your_loss', optimizer='your_optimizer', metrics=['your_metrics'])
编译完成后,您就可以处理新图像了。
from keras.preprocessing import imagetest_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)