简体   繁体   中英

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. I have trained the model using fashion MNIST dataset. I am now trying to predict this model by using an external image from google images. 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. grayscale and 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

在此处输入图像描述

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

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

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. In above code yellow background is default behaviour of 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. 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. If your model is trained with a reasonable accuracy you'll get correct results, almost for many images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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