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
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’…
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 4
- Comments: 29
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)
@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:
then you can call fit just like you did