Keras Array Input Error

2019-07-14 06:11发布

I get the following error:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 6 arrays but instead got the following list of 3 arrays: [array([[ 0,  0,  0, ..., 18, 12,  1],
       [ 0,  0,  0, ..., 18, 11,  1],
       [ 0,  0,  0, ..., 18,  9,  1],
       ...,
       [ 0,  0,  0, ..., 18, 15,  1],
       [ 0,  0,  0, ..., 18,  9,  ...

in my keras model.

I think the model is mistaking something?

This happens when I feed input to my model. The same input works perfectly well in another program.

2条回答
戒情不戒烟
2楼-- · 2019-07-14 06:36

The problem really comes from giving the wrong input to the network.

In my case the problem was that my custom image generator was passing the entire dataset as input rather than a certain pair of image-label. This is because I thought that generator.flow(x,y, batch_size) of Keras already has a yield structure inside, however the correct generator structure should be as follows(with a separate yield):

def generator(batch_size):

(images, labels) = utils.get_data(1000) # gets 1000 samples from dataset
labels = to_categorical(labels, 2)

generator = ImageDataGenerator(featurewise_center=True,
                 featurewise_std_normalization=True,
                 rotation_range=90.,
                 width_shift_range=0.1,
                 height_shift_range=0.1,
                 zoom_range=0.2)

generator.fit(images)

gen = generator.flow(images, labels, batch_size=32)

while 1:
    x_batch, y_batch = gen.next()
    yield ([x_batch, y_batch])

I realize the question is old but it might save some time for someone to find the issue.

查看更多
叛逆
3楼-- · 2019-07-14 06:41

It's impossible to diagnose your exact problem without more information.

I usually specify the input_shape parameter of the first layer based on my training data X.

e.g.

model = Sequential()
model.add(Dense(32, input_shape=X.shape[0]))

I think you'll want X to look something like this:

   [
    [[ 0,  0,  0, ..., 18, 11,  1]],
    [[ 0,  0,  0, ..., 18,  9,  1]],
   ....
   ]

So you could try reshaping it with the following line:

X = np.array([[sample] for sample in X])
查看更多
登录 后发表回答