繁体   English   中英

使用使用时尚 mnist 数据集训练的 model 预测来自谷歌图像(包)的图像的 class

[英]Predicting a class of a an image from google images(bag) using a model that is trained using fashion mnist dataset

我正在尝试在 Python 中使用 TensorFlow 和 Keras 进行图像识别。我只从 Z063009BB15C811627 开始学习。 我已经使用时尚 MNIST 数据集训练了 model。 我现在正试图通过使用来自谷歌图像的外部图像来预测这个 model。 我正在使用一个包的图像。 请看下面

在此处输入图像描述

我知道我需要加载这个新图像,强制它为灰度格式,并将大小强制为 28×28 像素,因为这是我在训练 model 时训练图像的方式。 灰度和 28 * 28。

因此,我关注了一些博客并使用了下面的代码。

from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator

img_path = 'data/bag2.jpg'

img = image.load_img(img_path,grayscale=True,target_size=(28, 28))
img_tensor = image.img_to_array(img)
img_tensor = numpy.expand_dims(img_tensor, axis=0)
img_tensor /= 255.
pyplot.imshow(img_tensor[0])
pyplot.show()
print(img_tensor.shape)

上述代码的output如下

在此处输入图像描述

为什么背景是黄色的,图像不是灰色的? 这个对吗? 根据我的理解,背景应该是黑色的,图像应该是灰色的。

当我尝试使用以下代码预测此图像时,我得到 output 为零

pred = model.predict(img_tensor.reshape(-1,28, 28, 1))
print(pred.argmax())

提前致谢。

上述错误通过使用以下代码起作用

从 keras.preprocessing 导入图像 从 keras.preprocessing.image 导入 ImageDataGenerator

img_path = 'data/bag5.jpg'
img = image.load_img(img_path,color_mode='grayscale',target_size=(28, 28))
img_tensor = image.img_to_array(img)
img_tensor = numpy.expand_dims(img_tensor, axis=0)
img_tensor /= 255.

pyplot.imshow(img_tensor[0], cmap='gray')
pyplot.show()
print(img_tensor.shape)

当你使用 pyplot.imshow() 绘图时,如果你提到 cmap='gray' 那么你可以看到灰度图像。 在上面的代码中,黄色背景是 imshow function 的默认行为。

现在,如果您使用上述解决方案并且没有得到正确的结果,那么请尝试获得与数据集相似的图像。 Fashion MNIST 数据集具有图像 - 28x28 像素,灰度,即黑色背景和前景为布料项目。 你读到的图像

img = image.load_img(img_path,grayscale=True,target_size=(28, 28)) 

是灰度的,但有白色背景。 所以你可以使用 -

img = ImageOps.invert(img)

现在尝试使用 cmap='gray' 对 plot 进行预测并进行预测。 如果您的 model 以合理的准确度进行训练,您将获得正确的结果,几乎适用于许多图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM