亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將圖像提供給預訓練的 keras 模型

將圖像提供給預訓練的 keras 模型

牛魔王的故事 2023-03-01 16:29:34
我訓練了這個模型(從網站上直接復制粘貼)并用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)這是需要提供給模型的形狀


查看完整回答
反對 回復 2023-03-01
  • 1 回答
  • 0 關注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號