输出层形状中的Keras Functional API错误

时间:2018-12-05 21:10:12

标签: python keras neural-network keras-layer

所以我正在建立一个神经网络,它将接收2个输入(图像)并返回二进制输出(0或1)。

我已经有我的标签和输入了。

标签和输入的形状如下:

python setup.py install

这是我的代码:

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

(8281,)

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

(8281, 500, 500, 1) 

它给了我这个错误:

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) 

1 个答案:

答案 0 :(得分:0)

您必须将Conv2D图层的形状从(H,W,1)更改为(H * W * 1,)

import numpy as np

然后

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)

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)