使用Keras训练好的模型用来直接进行预测,首先我们已经有了model模型,这个模型被保存为model.h5文件
代码如下:
#模型载入预测脚本
import sys
from keras.preprocessing import image
import numpy as np
from keras.models import load_model
from keras.applications.inception_v3 import preprocess_input
img_path = sys.argv[2]
model_path = sys.argv[1]
img = image.load_img(img_path, target_size=(64, 64))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
model = load_model(model_path)
preds = model.predict(x)
#print (preds)
predict = np.argmax(preds,axis=1)
print ()
print ('预测结果为:',int(preds[0][predict][0]))
运行命令
python predict.py [model_path 模型路径 ] [img_path 测试图片路径 ]
predict 为大元素的索引号, 最终结果为0或1。