【问题标题】:How to test a working ConvNet in Python using Keras如何使用 Keras 在 Python 中测试工作的 ConvNet
【发布时间】:2018-01-20 01:29:59
【问题描述】:

我正在学习深度学习,我到了这个网站https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html

我按照示例进行操作,一切似乎都是正确的,但现在我想测试我自己的数据,我的意思是我想测试我自己的图像,例如 model.predict(mycat.jpg) 并得到适当的答案

我红色的帖子没有这些信息,不知何故,我得到的每本书都提供了 DL 和 CNN 的示例,也忘记告诉如何在训练后使用网络检查我们自己的数据。

【问题讨论】:

    标签: python-3.x deep-learning keras


    【解决方案1】:

    假设您刚刚完成拟合数据并想查看 cat.jpg 是否返回 cat 的高概率,下面是一些示例代码。

    img = Image.open(path_to_cat)
    img = np.expand_dims(np.array(img), 0)
    img.reshape([1, height,width,channels])
    prob = model.predict(img)
    

    expand_dims 是为了让我们有一个批处理索引。您可能还想将图像重塑为输入的形状。在你的情况下,我相信它是 [1, 3, 150, 150]。您还可以预测批量图像。

    编辑:如果测试图像与训练图像的尺寸不同(150x150),将其放在 image.open 之后会有所帮助

    img = img.resize(np.divide(img.size,(scaling_amount).astype('int32')); img
    

    这是统一缩放,如果不能将图像精确地缩小到 150x150,请在 img.reshape 之后使用它

    img = img[:,:150,:150]
    

    这样做是将高度和宽度裁剪为 150。检查 img.reshape 并确保其尺寸与训练输入尺寸匹配。

    【讨论】:

    • 非常感谢您的回答,滥用您的好意,有什么教程或书籍您想推荐了解更多吗?
    • 你的回答实际上给了我这个错误 ValueError: cannot reshape array of size 47616768 into shape (0,150,150,3)
    • 哦,这很可能是因为您的图片不是 150x150。 Reshape 命令无法调整图像大小,它只会重新排列现有形状。
    • Francois Chollet 的书很棒。 Fastai在线课程也很不错。您想要的尺寸是 3 个通道、150 高、150 宽和 1 个图像。
    • 感谢您的信息和回答,Fastai 课程看起来很棒,我会做的。至于你的答案,img = img[:,:150,:150] 效果很好,只有一个 qesuint,我怎么知道 0 是猫,1 是狗?
    猜你喜欢
    • 2019-12-18
    • 1970-01-01
    • 2020-10-20
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 2017-10-25
    • 2015-12-29
    相关资源
    最近更新 更多