[英]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
When I run this code i get this error: 当我运行此代码时,出现此错误:
/keras/engine/training.py", line 113, in _standardize_input_data 'with shape ' + str(data_shape)) ValueError: Error when checking : expected dense_1_input to have 2 dimensions, but got array with shape (313, 220, 3).
/keras/engine/training.py“,行_standardize_input_data'具有形状'+ str(data_shape))中的第113行,ValueError:检查时出错:预期density_1_input具有2个维,但是数组的形状为(313,220,3) 。
My first print
displays: (313, 220, 3)
. 我的第一个
print
显示: (313, 220, 3)
。
How can I fix this error. 如何解决此错误。
Your first layer Dense(150,kernel_initializer='normal', input_dim=36, activation='relu')
expects an input with 2 dimensions: (*, 36)
(with the first dimension corresponding to your batch size). 您的第一层
Dense(150,kernel_initializer='normal', input_dim=36, activation='relu')
期望输入具有2个维度: (*, 36)
(第一个维度对应于您的批次大小)。
However your input x
has actually 3 dimensions - 4 dimensions once properly batched: (*, 313, 220, 3)
. 但是,您的输入
x
实际上具有3个维度-正确匹配后将有4个维度: (*, 313, 220, 3)
。
If you want a Dense
layer accepting such an input, you could use the parameter input_shape=(313, 220, 3)
instead of input_dim=36
. 如果要让
Dense
层接受此类输入,则可以使用参数input_shape=(313, 220, 3)
代替input_dim=36
。
Remark: You are not batching your image correctly. 备注:您没有正确批处理图像。
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.