我训练了这个模型(从网站上直接复制粘贴)并用model.save(). 我现在想用它来对我生成的图像进行分类,所以我保存它们并将它们重塑为 28x28 像素,然后尝试将它们提供给模型,如下所示:from matplotlib import imageimg = image.imread('img.png')[:,:,:1] #so that the shape ends up being (28,28,1)print(self.model.predict(img))但是当我运行它时,我得到了一堆错误:警告:tensorflow:模型是用形状 (None, 28, 28, 1) 为输入 Tensor("input_1:0", shape=(None, 28, 28, 1), dtype=float32) 构建的,但它被调用形状不兼容的输入(无、28、1、1)。...ValueError: Input 0 of layer dense_12 is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape [None, 28]`我已经进行了一些挖掘,根据这一行,输入的形状似乎存在问题:WARNING:tensorflow:Model was constructed with shape (None, 28, 28, 1) for input Tensor("input_1:0", shape=(None, 28, 28, 1), dtype=float32), but it was called on an input with incompatible shape (None, 28, 1, 1)如何将图像转换为正确的形状?
1 回答
互换的青春
TA贡献1797条经验 获得超6个赞
所以我设法修复了它,非常简单,但我会把它留在这里以防其他人遇到问题:
from keras.preprocessing.image import load_img, img_to_array
img = load_img('img.png')
img = img_to_array(img)[:,:,:1]
img = np.expand_dims(img ,axis=0)
这改变了我的图像的形状,(1, 28, 28, 1)这是需要提供给模型的形状
添加回答
举报
0/150
提交
取消