简体   繁体   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

I am trying to do Image Recognition in Python with TensorFlow and Keras.I'm only beginning with keras and machine learning.我正在尝试在 Python 中使用 TensorFlow 和 Keras 进行图像识别。我只从 Z063009BB15C811627 开始学习。 I have trained the model using fashion MNIST dataset.我已经使用时尚 MNIST 数据集训练了 model。 I am now trying to predict this model by using an external image from google images.我现在正试图通过使用来自谷歌图像的外部图像来预测这个 model。 I am using an image of a bag.我正在使用一个包的图像。 Please see below请看下面

在此处输入图像描述

I understand I need to load this new image, force it to be grayscale format, and force the size to be 28×28 pixels as this is how my training images are while training the model.我知道我需要加载这个新图像,强制它为灰度格式,并将大小强制为 28×28 像素,因为这是我在训练 model 时训练图像的方式。 grayscale and 28 * 28.灰度和 28 * 28。

Hence, I followed some blogs and used below code to the same.因此,我关注了一些博客并使用了下面的代码。

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)

The output of the above code is as below上述代码的output如下

在此处输入图像描述

Why the background is yellow and image is not gray?为什么背景是黄色的,图像不是灰色的? Is this correct?这个对吗? Based on what I understand, the background should be black and image should be gray.根据我的理解,背景应该是黑色的,图像应该是灰色的。

while I trying to predict this image using below code, I get output as zero当我尝试使用以下代码预测此图像时,我得到 output 为零

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

Thanks in advance.提前致谢。

The above error worked by using below code上述错误通过使用以下代码起作用

from keras.preprocessing import image from keras.preprocessing.image import ImageDataGenerator从 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)

When you are plotting with pyplot.imshow(), if you mention cmap='gray' then you can see grayscale images.当你使用 pyplot.imshow() 绘图时,如果你提到 cmap='gray' 那么你可以看到灰度图像。 In above code yellow background is default behaviour of imshow function.在上面的代码中,黄色背景是 imshow function 的默认行为。

Now if you use above mentioned solution and don't get correct results then try to get similar image as that of the dataset.现在,如果您使用上述解决方案并且没有得到正确的结果,那么请尝试获得与数据集相似的图像。 Fashion MNIST dataset has images - 28x28 pixels, grayscale ie with black background and cloth item at foreground. Fashion MNIST 数据集具有图像 - 28x28 像素,灰度,即黑色背景和前景为布料项目。 The image you read你读到的图像

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

is grayscale but with white background.是灰度的,但有白色背景。 So you can use -所以你可以使用 -

img = ImageOps.invert(img)

Now try to plot this with cmap='gray' and do prediction.现在尝试使用 cmap='gray' 对 plot 进行预测并进行预测。 If your model is trained with a reasonable accuracy you'll get correct results, almost for many images.如果您的 model 以合理的准确度进行训练,您将获得正确的结果,几乎适用于许多图像。

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

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