Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用使用fashion mnist数据集训练的模型从google images(bag)预测一类图像_Python_Tensorflow_Image Processing_Keras_Tf.keras - Fatal编程技术网

Python 使用使用fashion mnist数据集训练的模型从google images(bag)预测一类图像

Python 使用使用fashion mnist数据集训练的模型从google images(bag)预测一类图像,python,tensorflow,image-processing,keras,tf.keras,Python,Tensorflow,Image Processing,Keras,Tf.keras,我正在尝试使用TensorFlow和Keras用Python进行图像识别。我只是从Keras和机器学习开始。我已经使用fashion MNIST数据集训练了模型。我现在正试图通过使用谷歌图片的外部图像来预测这个模型。我使用的是一个包的图像。请看下面 我知道我需要加载这个新图像,强制它为灰度格式,并强制大小为28×28像素,因为这是我在训练模型时训练图像的方式。灰度和28*28 因此,我关注了一些博客并使用了下面的代码 from keras.preprocessing import image

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

我知道我需要加载这个新图像,强制它为灰度格式,并强制大小为28×28像素,因为这是我在训练模型时训练图像的方式。灰度和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)
上述代码的输出如下所示

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

当我试图使用下面的代码预测这个图像时,我得到的输出为零

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)

如果你从来没有使用过它,
gray\u img
有什么意义?另外,您的一些代码也很重要,请编辑您的question@RandomGuy,我最初尝试加载灰色img,而不是直接加载img。但是,它给了我一个TypeError错误:应该是str、bytes或os.PathLike对象,而不是numpy.ndarray。我无法修复它,因此尝试了img=image.load\img(img\u path,grayscale=True,target\u size=(28,28)),这很有效。未注释删除了不起作用的代码。我现在已经编辑了问题中的代码。请告诉我如何获得灰色图像,因为我需要在预测时使用它。根据,
grayscale
不推荐使用。尝试使用
img=image.load\img(img\u路径,color\u mode='grayscale',target\u size=(28,28))
。另外,
pred=model.predict(img\u tensor)
应该可以工作,无需重新调整数组。@RandomGuy,感谢您的快速响应。我查看了文档并尝试使用img=image.load\img(img\u路径,color\u mode='grayscale',target\u size=(28,28))。它给了我和我在问题中提供的相同的黄色背景图像。不确定出了什么问题。嗯,这可能是由于加载图像时出错,然后。。。请您试用一下
img\u tensor=cv2.imread(img\u路径,cv2.imread\u灰度)
?然后,
img\u tensor=numpy。展开dims(img\u tensor,axis=0)
,依此类推