如何解决如何在MNIST的卷积神经网络中解决这个不相容的形状问题?
我在MNIST(CNN)上建立了一个图像分类模型,该模型在测试集上运行良好。但是,当我从Google上传图像,调整图像大小,对其进行规范化并将其输入到我的模型中时,出现尺寸错误。我的模型的输入张量形状为(None,28,28)
,但当我为模型提供28x28像素的图像时,它表示我的输入为(None,28)
。我该如何解决这个问题? model_code.jpg
这是我的错误:
WARNING:tensorflow:Model was constructed with shape (None,28) for input Tensor("flatten_input_2:0",shape=(None,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)
该错误可能是由于张量流期望很多图像,所以如果您提供100张图像,则阵列的形状将为(100,28),但是由于您只有一个图像,因此其位于(28,28)中通常应该如此。将其重塑为正确的格式即可解决问题
,我想您错过了batch size
维度,您只输入了(28,28)图像,而前28个图像被识别为batch
,您需要将输入更改为(1,28, 28)。
tf.reshape(image,(1,28))