简体   繁体   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

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.

相关问题 检查输入时出错:预期dense_1_input 有2 维,但得到了形状为(25000, 700, 50) 的数组 - Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (25000, 700, 50) 检查输入时出错:预期density_1_input具有5个维,但数组的形状为(1746,131072) - Error when checking input: expected dense_1_input to have 5 dimensions, but got array with shape (1746, 131072) ValueError:检查时出错:预期density_1_input具有2维,但数组的形状为(1,16,16,512) - ValueError: Error when checking : expected dense_1_input to have 2 dimensions, but got array with shape (1, 16, 16, 512) Keras输入层的问题:预期density_1_input的形状为(11,),但数组的形状为(15,) - Problem with the input layer with Keras: expected dense_1_input to have shape (11,) but got array with shape (15,) Picking Shape Keras-ValueError: 预期的 dense_1_input 的形状为 (8,) 但得到的数组的形状为 (13,) - Picking Shape Keras- ValueError: expected dense_1_input to have shape (8,) but got array with shape (13,) ValueError:检查输入时出错:预期density_1_input具有2维,但数组的形状为(60000,28,28) - ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (60000, 28, 28) Keras预期density_13具有2维 - Keras expected dense_13 to have 2 dimensions ValueError:检查输入时出错:预期density_1_input的形状为(24,),但数组的形状为(1,) - ValueError: Error when checking input: expected dense_1_input to have shape (24,) but got array with shape (1,) ValueError:检查输入时出错:预期dense_1_input 具有形状(9,) 但得到形状为(1,) 的数组 - ValueError: Error when checking input: expected dense_1_input to have shape (9,) but got array with shape (1,) 检查输入时出错:预期density_1_input具有形状(3773),但数组的形状为(111,) - Error when checking input: expected dense_1_input to have shape (3773,) but got array with shape (111,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM