【问题标题】:How to label test data using trained model with keras?如何使用带有 keras 的训练模型标记测试数据?
【发布时间】:2018-04-06 00:41:38
【问题描述】:

我正在研究下面的keras卷积神经网络教程https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d

训练模型后,我想在样本图像上测试模型,并对图像进行标记。我意识到我必须使用predict 方法生成一个数组,该数组显示哪个标签获得特定图像的分数。但是我在使用这种方法时遇到了麻烦。如果图像在文件夹 test_images 中并且有 20 张,我如何测试这些图像并获得预测?

这就是我得到一张图片的方式(即使我想要多张图片):

image = cv2.imread('test1.jpg')
image = cv2.resize(image,(224,224))
features = np.swapaxes(np.swapaxes(image, 1, 2), 0, 1)
predictions = model.predict(features)

这会引发以下错误:

ValueError: Error when checking : expected conv2d_1_input to have 4 dimensions, but got array with shape (3, 224, 224)

非常感谢!

之前咨询过的一些问题:

Simple Neural Network in Python not displaying label for the test image https://github.com/fchollet/keras/issues/315

【问题讨论】:

    标签: python-3.x image-processing keras conv-neural-network


    【解决方案1】:

    model.predict 通过处理一组样本来工作,而不仅仅是一张图像,因此您缺少批次/样本维度,在您的情况下,这只是一张图像。你只需要重塑数组:

    features = features.reshape((1, 3, 224, 224)
    

    然后传递给预测。

    【讨论】:

    • 太棒了!它适用于一张图片。我删除了 swapaxes 部分并添加了您建议的行。对于本教程,我必须将其调整为 (150,150)。您能否在您的答案中添加我如何将它用于多张图片,比如 20 张图片?谢谢!
    • 其实我明白了。
    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2017-05-29
    • 2020-06-07
    • 1970-01-01
    • 2023-03-28
    • 2018-12-29
    • 2018-04-15
    相关资源
    最近更新 更多