简体   繁体   中英

Keras Functional API error in output layer shape

So I'm building a neural network that will take in 2 inputs (images) and return a binary output (0 or 1).

I already have my labels and inputs.

Shapes of the labels and inputs are the following:

--------Labels--------

(8281,)

--------Images--------

(8281, 500, 500, 1) 

Here is my code:

input_front_images1 = Input(shape=(500, 500, 1))
input_front_images2 = Input(shape=(500, 500, 1))

x1=Conv2D(32, kernel_size=3,activation='relu')(input_front_images1)
x2=Conv2D(32, kernel_size=3,activation='relu')(input_front_images2)

x = keras.layers.concatenate([x1, x2])
x = Dense(64, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(inputs=[input_front_images1,input_front_images2], 
outputs=predictions)


model.compile( optimizer= 'rmsprop' , loss='categorical_crossentropy' , 
metrics=['accuracy'])
model.summary()
model.fit([image_front_pairs1,image_front_pairs2], 
[labels_front_pairs],epochs=2,batch_size=64) 

It gives me this error:

ValueError: Error when checking target: expected dense_39 to have 4 dimensions, but got array with shape (8281, 1)

you must reshape Conv2D Layer from shape (H,W,1) to (H*W*1,)

import numpy as np

then

x = keras.layers.concatenate([x1, x2])
# add this lines to code
dim = np.prod(x._shape[1:])
x = keras.layers.Reshape([dim.value,])(x)
x = Dense(64, activation='relu')(x)

or

x = Dense(64, activation='relu')(x)
# add this lines to code
dim = np.prod(x._shape[1:])
x = keras.layers.Reshape([dim.value,])(x)
predictions = Dense(1, activation='sigmoid')(x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM