Error with passing multiple input into keras network

I am new in keras and I am trying to build a network takes 2 input image and produce 1 output. But when i try to train the network, it keeps give me an 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:"

Here's my code to build the network:

First CNN

first_model = Sequential()

first_model.add(Conv2D(96, kernel_size = (11, 11), strides = (4, 4), activation = "relu", input_shape = (227, 227, 3), padding = "valid"))
first_model.add(MaxPooling2D(pool_size = (3, 3), strides = (2, 2), padding = "valid"))
first_model.add(BatchNormalization(axis = 2, momentum = 0.75, epsilon = 0.0001))

first_model.add(Conv2D(256, kernel_size = (5, 5), strides = (1, 1), activation = 'relu'))
first_model.add(MaxPooling2D(pool_size = (3, 3), strides = (2, 2), padding = "valid"))
first_model.add(BatchNormalization(axis = 2, momentum = 0.75, epsilon = 0.0001))

first_model.add(Conv2D(384, kernel_size = (3, 3), strides = (1, 1), activation = 'relu'))

first_model.add(Conv2D(384, kernel_size = (3, 3), strides = (1, 1), activation = 'relu'))

first_model.add(Conv2D(256, kernel_size = (3, 3), strides = (1, 1), activation = 'relu'))
first_model.add(MaxPooling2D(pool_size = (3, 3), strides = (2, 2), padding = "valid"))

Second CNN

second_model = Sequential()

second_model.add(Conv2D(96, kernel_size = (11, 11), strides = (4, 4), activation = "relu", input_shape = (227, 227, 3), padding = "valid"))
second_model.add(MaxPooling2D(pool_size = (3, 3), strides = (2, 2), padding = "valid"))
second_model.add(BatchNormalization(axis = 2, momentum = 0.75, epsilon = 0.0001))

second_model.add(Conv2D(256, kernel_size = (5, 5), strides = (1, 1), activation = 'relu'))
second_model.add(MaxPooling2D(pool_size = (3, 3), strides = (2, 2), padding = "valid"))
second_model.add(BatchNormalization(axis = 2, momentum = 0.75, epsilon = 0.0001))

second_model.add(Conv2D(384, kernel_size = (3, 3), strides = (1, 1), activation = 'relu'))

second_model.add(Conv2D(384, kernel_size = (3, 3), strides = (1, 1), activation = 'relu'))

second_model.add(Conv2D(256, kernel_size = (3, 3), strides = (1, 1), activation = 'relu'))
second_model.add(MaxPooling2D(pool_size = (3, 3), strides = (2, 2), padding = "valid"))

Merge Both CNN

merged_layer = Concatenate()([first_model.output, second_model.output])


merged_layer = Flatten()(merged_layer)
merged_layer = Dense(units = 128, activation = "relu")(merged_layer)
merged_layer = Dense(units = 1, activation = "sigmoid")(merged_layer)

newModel = Model([first_model.input, second_model.input], merged_layer)

newModel.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = ["accuracy"])

And here's how I generate my train data:

test_image = [f for f in listdir('newdata/test_set') if isfile(join('newdata/test_set', f))]
train_image = [f for f in listdir('newdata/training_set') if isfile(join('newdata/training_set', f))]


training_set = []
test_set = []

for aa in test_image:
    im2 = cv2.imread('newdata/test_set/' + aa)
    resized_image = cv2.resize(im2, (227, 227)) 
    test_set.append(resized_image)

for aa in train_image:
    im2 = cv2.imread('newdata/training_set/' + aa)
    resized_image = cv2.resize(im2, (227, 227)) 
    training_set.append(resized_image)


training_set = np.asarray(training_set)
test_set = np.asarray(test_set)

train_labels = np.array([0] * 1000 + [1] * 1000)
test_labels = np.array([0] * 500 + [1] * 500)

Train network code

newModel.fit( [ training_set, training_set ], train_labels, epochs=1, 
    batch_size=32, steps_per_epoch = 8000, validation_steps = 2000, 
    validation_data=(np.array(test_set), test_labels))

Error Message

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:

I had tried to search through Google but none of the solution work, I need some advice on how to solve the problem, a million thanks in advance.

1 answer

  • answered 2018-08-14 11:29 sdcbr

    You need to pass a list of arrays for the validation data, similar to how you do it for the training data. Try:

    newModel.fit(..., validation_data=([np.array(test_set), np.array(test_set)], test_labels))