如何在MNIST上的卷积神经网络中解决这个不兼容的形状问题



我在MNIST(CNN(上建立了一个图像分类模型,该模型在测试集上运行良好。然而,当我从谷歌上传图像,调整其大小,将其标准化并将其提供给我的模型时,我有一个尺寸错误。我的模型的输入张量形状是(None,28,28),但当我给模型一个28x28像素的图像时,它表明我的输入是(None,28)。我该如何解决这个问题?model_code.jpg

这就是我的错误:

WARNING:tensorflow:Model was constructed with shape (None, 28, 28) for input Tensor("flatten_input_2:0", shape=(None, 28, 28), dtype=float32), but it was called on an input with incompatible shape (None, 28). 
ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape [None, 28]

非常感谢。

您需要重新塑造图像,使用numpy可以很容易地通过这个完成

import numpy as np
image = np.array(image).reshape(1,28,28)

这个错误可能是由于tensorflow期望有很多图像,所以如果你提供了100个图像,阵列的形状将是(100,28,28(,但由于你有一个单独的图像,它在(28,28(中,这是正常的。将其重塑为正确的格式将解决问题

我猜您错过了batch size维度,您只输入了一个(28,28(图像,前28个图像被识别为batch,您需要将输入更改为(1,28,28(。

tf.reshape(image, (1,28,28))

最新更新