簡體   English   中英

Keras / Tensorflow預測:陣列形狀的錯誤

[英]Keras/Tensorflow predict: error in array shape

我在這里關注Keras CIFAR10教程 我做的唯一改變是:

[a]添加到教程文件的末尾

model.save_weights('./weights.h5', overwrite=True)

[b]將〜。/ keras / keras.json更改為

{"floatx": "float32", "backend": "tensorflow", "epsilon": 1e-07}

我可以成功運行模型。

然后我想針對訓練的模型測試單個圖像。 我的代碼:

[... similar to tutorial file with model being created and compiled...]
...
model = Sequential()
...
model.compile()

model.load_weights('./ddx_weights.h5')

img = cv2.imread('car.jpeg', -1) # this is is a 32x32 RGB image
img = np.array(img)
y_pred = model.predict_classes(img, 1)
print(y_pred)

我收到此錯誤:

ValueError: Cannot feed value of shape (1, 32, 3) for Tensor 'convolution2d_input_1:0', which has shape '(?, 3, 32, 32)'

對於要測試的單個圖像,重塑輸入數據的正確方法是什么?

沒有./keras/keras.json添加"image_dim_ordering": "tf"

你必須重塑輸入圖像的形狀為[?, 3, 32, 32] ? 是批量大小。 在您的情況下,由於您有1個圖像,批處理大小為1,因此您可以執行以下操作:

img = np.array(img)
img = img.reshape((1, 3, 32, 32))

我現在正在處理cifar10數據,我發現簡單的重塑不起作用,應該使用numpy.transpose。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM