Keras: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 2 arrays

Created on 24 Feb 2018  路  25Comments  路  Source: keras-team/keras

I want to pass a pair (good and bad) to the CNN and while testing also I will pass a pair of images. The code is given below

import cv2

X_bad = []
X_bad_id = []
for i in range(1,53):
    a = 'data/train/data/bad/bad'+`i`+'.jpg'
    img = cv2.imread(a)
    X_bad.append(img)
    X_bad_id.append("0")

import numpy as np
X_bad = np.array(X_bad)
X_bad_id = np.array(X_bad_id)

X_good = []
X_good_id = []
for i in range(1,53):
    a = 'data/train/data/good/good'+`i`+'.jpg'
    img = cv2.imread(a)
    X_good.append(img)
    X_good_id.append("1")

import numpy as np
X_good = np.array(X_good)
X_good_id = np.array(X_good_id)`

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
from keras.layers.merge import concatenate
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint
from keras.layers import Input
from keras.models import Model


X_good = X_good.astype('float32')
X_bad  = X_bad.astype('float32')

X_good /= 255
X_bad /= 255

visible1 = Input(shape=(250,250,3))
conv11 = Conv2D(32, kernel_size=4, activation='relu')(visible1)
pool11 = MaxPooling2D(pool_size=(2, 2))(conv11)
conv12 = Conv2D(16, kernel_size=4, activation='relu')(pool11)
pool12 = MaxPooling2D(pool_size=(2, 2))(conv12)
flat1 = Flatten()(pool12)

visible2 = Input(shape=(250,250,3))
conv21 = Conv2D(32, kernel_size=4, activation='relu')(visible2)
pool21 = MaxPooling2D(pool_size=(2, 2))(conv21)
conv22 = Conv2D(16, kernel_size=4, activation='relu')(pool21)
pool22 = MaxPooling2D(pool_size=(2, 2))(conv22)
flat2 = Flatten()(pool22)

merge = concatenate([flat1, flat2])

# interpretation model
hidden1 = Dense(10, activation='relu')(merge)
hidden2 = Dense(10, activation='relu')(hidden1)
output = Dense(1, activation='sigmoid')(hidden2)
model = Model(inputs=[visible1, visible2], outputs=output)

model.compile(optimizer='adam', loss='binary_crossentropy')
model.fit([X_good, X_bad], [X_good_id, X_bad_id],epochs=50, batch_size=32)

The above program is giving the following error

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 2 arrays: [array(['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1'...

Most helpful comment

I had this trouble on a later version of tensorflow/keras, but not with an earlier version. The later version was on Sagemaker with Python 3.6, keras 2.2.4, tensorflow 1.12. The trick was to update the arrays to np.array:
x=np.array(x)
y=np.array(y)

All 25 comments

What's the shape of X_good, X_bad, X_good_id & X_bad_id ??

print(X_good.shape)
print(X_bad.shape)

(52, 250, 250, 3)
(52, 250, 250, 3)

output = Dense(1, activation='sigmoid')(hidden2)
The issue is that final layer of the network is expecting 1 array (target) as you can see in the code above but you are passing 2 arrays one for good_id and one for bad_id.

Try changing the output layer to output = Dense(2, activation='sigmoid')(hidden2) and see if this works.

Getting Same error.

It is multi-input and multi-output model. The algorithm takes pair of images as input at a time.

While running this code

X_bad_id = []
for i in range(1,53):
    X_bad_id.append("0")


X_good_id = []
for i in range(1,53):
    X_good_id.append("1")

X_bad_id = np.array(X_bad_id)
X_good_id = np.array(X_good_id)
  1. You are appending 0 & 1 in quotes (') but instead, it should be an integer.
  2. You need to reshape the array X_good_id & X_bad_id. Currently the shape of X_bad_id & X_good_id is (52,) instead it should be (52, 1).

No getting same error

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 2 arrays: [array([['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
...

you need to concatenate X_good_id and X_bad_id using numpy. your model.fit line is indicating that your model as multi-output [X_good_id, X_out_id] whereas the model you have built only has one output output = Dense(1, activation='sigmoid')(hidden2).

So instead, try doing the following

X_out = np.concatenate([X_good_id, X_bad_id])
model.fit([X_good, X_bad], X_out, epochs=50, batch_size=32)

Input: it should take two images at a time (good and bad image). It should produce two outputs

The code is given below

import cv2

X_bad = [] X_bad_id = [] for i in range(1,53): a = 'data/train/data/bad/bad'+i+'.jpg' img = cv2.imread(a) X_bad.append(img) X_bad_id.append('0')

import numpy as np X_bad = np.array(X_bad) X_bad_id = np.array(X_bad_id)

X_good = [] X_good_id = [] for i in range(1,53): a = 'data/train/data/good/good'+i+'.jpg' img = cv2.imread(a) X_good.append(img) X_good_id.append('1')

import numpy as np X_good = np.array(X_good) X_good_id = np.array(X_good_id)

print(X_good.shape) print(X_bad.shape)

X_bad_id = np.array(X_bad_id) X_good_id = np.array(X_good_id)

X_bad_id = X_bad_id.reshape((X_bad_id.shape[0], 1)) X_good_id = X_good_id.reshape((X_good_id.shape[0], 1))

import numpy as np from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers.convolutional import Conv2D from keras.layers.pooling import MaxPooling2D from keras.layers.merge import concatenate from keras.optimizers import SGD from keras.callbacks import ModelCheckpoint from keras.layers import Input from keras.models import Model

`X_good = X_good.astype('float32')
X_bad = X_bad.astype('float32')

X_good /= 255
X_bad /= 255`

`visible1 = Input(shape=(250,250,3))
conv11 = Conv2D(32, kernel_size=4, activation='relu')(visible1)
pool11 = MaxPooling2D(pool_size=(2, 2))(conv11)
conv12 = Conv2D(16, kernel_size=4, activation='relu')(pool11)
pool12 = MaxPooling2D(pool_size=(2, 2))(conv12)
flat1 = Flatten()(pool12)

visible2 = Input(shape=(250,250,3))
conv21 = Conv2D(32, kernel_size=4, activation='relu')(visible2)
pool21 = MaxPooling2D(pool_size=(2, 2))(conv21)
conv22 = Conv2D(16, kernel_size=4, activation='relu')(pool21)
pool22 = MaxPooling2D(pool_size=(2, 2))(conv22)
flat2 = Flatten()(pool22)

merge = concatenate([flat1, flat2])

interpretation model

hidden1 = Dense(10, activation='relu')(merge)
hidden2 = Dense(10, activation='relu')(hidden1)
output = Dense(2, activation='softmax')(hidden2)
model = Model(inputs=[visible1, visible2], outputs=output)`

model.compile(optimizer='adam', loss='categorical_crossentropy') X_out = np.concatenate([X_good_id, X_bad_id]) model.fit([X_good, X_bad], [X_good_id, X_bad_id],epochs=50, batch_size=32)

It is showing the below error. Could you please tell how to correct this?

`---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
1 model.compile(optimizer='adam', loss='categorical_crossentropy')
2 X_out = np.concatenate([X_good_id, X_bad_id])
----> 3 model.fit([X_good, X_bad], [X_good_id, X_bad_id],epochs=50, batch_size=32)

/home/vinay/securetensor/local/lib/python2.7/site-packages/keras/engine/training.pyc in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1628 sample_weight=sample_weight,
1629 class_weight=class_weight,
-> 1630 batch_size=batch_size)
1631 # Prepare validation data.
1632 do_validation = False

/home/vinay/securetensor/local/lib/python2.7/site-packages/keras/engine/training.pyc in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
1478 output_shapes,
1479 check_batch_axis=False,
-> 1480 exception_prefix='target')
1481 sample_weights = _standardize_sample_weights(sample_weight,
1482 self._feed_output_names)

/home/vinay/securetensor/local/lib/python2.7/site-packages/keras/engine/training.pyc in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
84 'Expected to see ' + str(len(names)) + ' array(s), '
85 'but instead got the following list of ' +
---> 86 str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
87 elif len(names) > 1:
88 raise ValueError(

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 2 arrays: [array([['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
['1'],
...`

@vinayakumarr I think I misunderstood what you wanted to do with the model. If you do want 2 outputs, then you need to specify it in the Model API, see here for the docs.

Example:

output1 = Dense(1, activation='sigmoid')(x)
output2 = Dense(1, activation='sigmoid')(x)
model = Model(inputs=[visible1, visible2], outputs=[output1, output2])

then you can call fit just like you did

model.fit([X_good, X_bad], [X_good_id, X_bad_id], epochs=50, batch_size=32)

It works. But I have a doubt. My problem is that

Input: it should take two images at a time (good and bad image). It should produce two outputs. Whether the followed method is correct?

@vinayakumarr Can you elaborate more? If there are no more technical issues then please close the issue

The code is already given above. It works fine.

When i give model.predict([X_good, X_bad])
It is predicting properly. I am getting a single vector like give below

[array([[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.]], dtype=float32), array([[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.]], dtype=float32)]

When I swap the inputs like give below
model.predict([X_bad, X_good])
This time it is predicting the same above vector

The model should give same output even if i revrse the inputs

@vinayakumarr
you are experience something known as overfitting. You should find more data or use a simpler network.

So then there is no problem in the network am I right? Also, I can reverse the inputs during the testing stage?

The code is given below. It takes two images, 33 images from the first category and 33 images from another category. At a time two input is passed and the network should tell whether it belongs to the first or second category.
During testing, I passed the same data set. Total 66 images (33 from each category). It should produce 33 output instead of 66. How to do this?

import cv2

X_bad = []
X_bad_id = []
for i in range(1,33):
a = 'data/train/data/bad/bad'+i+'.jpg'
img = cv2.imread(a)
X_bad.append(img)
X_bad_id.append('0')

import numpy as np
X_bad = np.array(X_bad)
X_bad_id = np.array(X_bad_id)
X_good = []
X_good_id = []
for i in range(1,33):
a = 'data/train/data/good/good'+i+'.jpg'
img = cv2.imread(a)
X_good.append(img)
X_good_id.append('1')

import numpy as np
X_good = np.array(X_good)
X_good_id = np.array(X_good_id)
print(X_good.shape)
print(X_bad.shape)
X_bad_id = np.array(X_bad_id)
X_good_id = np.array(X_good_id)
X_bad_id = X_bad_id.reshape((X_bad_id.shape[0], 1))
X_good_id = X_good_id.reshape((X_good_id.shape[0], 1))

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
from keras.layers.merge import concatenate
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint
from keras.layers import Input
from keras.models import Model`
X_good = X_good.astype('float32')
X_bad = X_bad.astype('float32')

X_good /= 255
X_bad /= 255
visible1 = Input(shape=(250,250,3))
conv11 = Conv2D(32, kernel_size=4, activation='relu')(visible1)
pool11 = MaxPooling2D(pool_size=(2, 2))(conv11)
conv12 = Conv2D(16, kernel_size=4, activation='relu')(pool11)
pool12 = MaxPooling2D(pool_size=(2, 2))(conv12)
flat1 = Flatten()(pool12)

visible2 = Input(shape=(250,250,3))
conv21 = Conv2D(32, kernel_size=4, activation='relu')(visible2)
pool21 = MaxPooling2D(pool_size=(2, 2))(conv21)
conv22 = Conv2D(16, kernel_size=4, activation='relu')(pool21)
pool22 = MaxPooling2D(pool_size=(2, 2))(conv22)
flat2 = Flatten()(pool22)

merge = concatenate([flat1, flat2])

interpretation model

hidden1 = Dense(10, activation='relu')(merge)
hidden2 = Dense(10, activation='relu')(hidden1)
output1 = Dense(1, activation='sigmoid')(hidden1)
output2 = Dense(1, activation='sigmoid')(hidden1)
model = Model(inputs=[visible1, visible2], outputs=[output1, output2])
model.compile(optimizer='adam', loss='binary_crossentropy',metrics=['accuracy'])
X_out = np.concatenate([X_good_id, X_bad_id])
model.fit([X_good, X_bad], [X_good_id, X_bad_id],epochs=50, batch_size=32)
pr = model.predict([X_good, X_bad])

pr is a list of list contains 33 images in one list and 33 images in another list. But it should contain only one list with 33 elements. How to do this?

hi,im working on a sentiment analysis project with keras since im new to keras i don't have any view to solve this problem, this is my keras model:

model = Sequential()

model.add(Conv1D(32, kernel_size=3, activation='elu', padding='same',
input_shape=(max_tweet_length, vector_size)))
model.add(Conv1D(32, kernel_size=3, activation='elu', padding='same'))
model.add(Conv1D(32, kernel_size=3, activation='elu', padding='same'))
model.add(Conv1D(32, kernel_size=3, activation='elu', padding='same'))
model.add(Dropout(0.25))
model.add(Conv1D(32, kernel_size=2, activation='elu', padding='same'))
model.add(Conv1D(32, kernel_size=2, activation='elu', padding='same'))
model.add(Conv1D(32, kernel_size=2, activation='elu', padding='same'))
model.add(Conv1D(32, kernel_size=2, activation='elu', padding='same'))
model.add(Dropout(0.25))
model.add(Dense(256, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(2, activation='softmax'))
and when i want to predict the sentiment of an input, i face with this 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 1 array(s), but instead got the following list of 3 arrays: [array([[ 0.08031651, 0.05684812, 0.22872323, ..., -0.19047852..."""

sorry if its such a stupid question! i know it was asked several times in the SOF but i did most of their suggestion, seems it was not practical to me since my poor knowledge about keras
thanks a lot

@zbokaee
i have ever met this problem i think it relate about your input size. Follow the text error "size the model expected. Expected to see 1 array(s), but instead got the following list of 3 " then you have to change your input size or input model.
model.add(Conv1D(32, kernel_size=3, activation='elu', padding='same',
input_shape=(max_tweet_length, vector_size))) <<<<<< i think this line

I had this trouble on a later version of tensorflow/keras, but not with an earlier version. The later version was on Sagemaker with Python 3.6, keras 2.2.4, tensorflow 1.12. The trick was to update the arrays to np.array:
x=np.array(x)
y=np.array(y)

model.fit({'main_input': X_text_train, 'aux_input': X_number_train},
{'main_output': y_train, 'aux_output': y_train},
validation_data=[{'main_input': X_text_test, 'aux_input': X_number_test}, {'main_output': y_test, 'aux_output': y_test}],
epochs=20,
batch_size=2048)

predicted_classes = model.predict(np.array(X_text_train))

predicted_classes = model.predict(np.array(X_text_train), np.array(X_number_train))

predicted_classes = model.predict(X_text_train)

getting 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 2 array(s), but instead got the following list of 1 arrays: [array([[ 0, 0, 0, ..., 7614, 450, 963],
[ 0, 0, 0, ..., 28, 3, 1259],
[ 0, 0, 0, ..., 332, 136, 140],
...,
[ 0, 0, 0, ..., 1174, ...

[X_good, X_bad], [X_good_id, X_bad_id]

Why do you send the array like this? What's the use of this?

I think that here is correct (I change a little):
model.fit([X_good, X_good_id], [X_bad, X_bad_id], epochs=50, batch_size=32)

I got similar error in semantic segmentation task, with 2 inputs and 2 outputs , is there anyone who tried similar task? how to use multi image_generator and mask_generator for the 2 inputs and outputs?

model.fit({'main_input': X_text_train, 'aux_input': X_number_train},
{'main_output': y_train, 'aux_output': y_train},
validation_data=[{'main_input': X_text_test, 'aux_input': X_number_test}, {'main_output': y_test, 'aux_output': y_test}],
epochs=20,
batch_size=2048)

predicted_classes = model.predict(np.array(X_text_train))

predicted_classes = model.predict(np.array(X_text_train), np.array(X_number_train))

predicted_classes = model.predict(X_text_train)

getting 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 2 array(s), but instead got the following list of 1 arrays: [array([[ 0, 0, 0, ..., 7614, 450, 963],
[ 0, 0, 0, ..., 28, 3, 1259],
[ 0, 0, 0, ..., 332, 136, 140],
...,
[ 0, 0, 0, ..., 1174, ...

I had the same issue and turning list of arrays to arrays solved it. e.g. do this {'main_input': np.asarray(X_text_train), 'aux_input': np.asarray(X_number_train)}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

braingineer picture braingineer  路  3Comments

snakeztc picture snakeztc  路  3Comments

somewacko picture somewacko  路  3Comments

amityaffliction picture amityaffliction  路  3Comments

rantsandruse picture rantsandruse  路  3Comments