我訓練了這個模型(從網站上直接復制粘貼)并用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
提交
取消