如果有人仍在努力对图像进行预测,则以下是优化的代码,用于加载保存的模型并进行预测:
# Modify 'test1.jpg' and 'test2.jpg' to the images you want to predict on
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
# dimensions of our images
img_width, img_height = 320, 240
# load the model we saved
model = load_model('model.h5')
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
# predicting images
img = image.load_img('test1.jpg', target_size=(img_width, img_height))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict_classes(images, batch_size=10)
print classes
# predicting multiple images at once
img = image.load_img('test2.jpg', target_size=(img_width, img_height))
y = image.img_to_array(img)
y = np.expand_dims(y, axis=0)
# pass the list of multiple images np.vstack()
images = np.vstack([x, y])
classes = model.predict_classes(images, batch_size=10)
# print the classes, the images belong to
print classes
print classes[0]
print classes[0][0]
0
一般来说,我只是从keras和机器学习开始。
我训练了一个模型来对2个类别的图像进行分类,并使用
model.save()
将其保存。这是我使用的代码:它成功地以0.98的精度进行了训练,相当不错。为了在新图像上加载并测试该模型,我使用了以下代码:
它输出:
为什么不给出类的实际名称以及为什么
[[0]]
?提前致谢。