繁体   English   中英

keras:expected density_1_input具有2维

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM