![](/img/trans.png)
[英]Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (25000, 700, 50)
[英]keras:expected dense_1_input to have 2 dimensions
from keras import optimizers
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
import scipy.misc
from keras.wrappers.scikit_learn import KerasClassifier
# dimensions of our images
img_width, img_height = 313, 220
# load the model we saved
model = load_model('hmodel.h5')
sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy','mse'])
test_image= image.load_img('/Images/1.jpg',target_size = (img_width, img_height))
x= scipy.misc.imread('/Images/1.jpg').shape
print x
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
test_image = test_image.reshape(img_width, img_height,3)
result = model.predict(test_image)
print result
當我運行此代碼時,出現此錯誤:
/keras/engine/training.py“,行_standardize_input_data'具有形狀'+ str(data_shape))中的第113行,ValueError:檢查時出錯:預期density_1_input具有2個維,但是數組的形狀為(313,220,3) 。
我的第一個print
顯示: (313, 220, 3)
。
如何解決此錯誤。
您的第一層Dense(150,kernel_initializer='normal', input_dim=36, activation='relu')
期望輸入具有2個維度: (*, 36)
(第一個維度對應於您的批次大小)。
但是,您的輸入x
實際上具有3個維度-正確匹配后將有4個維度: (*, 313, 220, 3)
。
如果要讓Dense
層接受此類輸入,則可以使用參數input_shape=(313, 220, 3)
代替input_dim=36
。
備注:您沒有正確批處理圖像。
test_image= image.load_img('/Images/1.jpg',target_size = (img_width, img_height))
test_image = image.img_to_array(test_image) # shape = (313, 220, 3)
test_image = np.expand_dims(test_image, axis = 0) # shape = (1, 313, 220, 3)
# Remove this line below, as it would set back shape to (313, 220, 3)
# test_image = test_image.reshape(img_width, img_height,3)
result = model.predict(test_image)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.