Keras的卷积似乎对我的网络过于聪明 – 我的最后一个卷积层只有1个滤波器,Keras似乎在输出形状上压缩了滤波器轴。不幸的是,这只是在训练时发生的:model.summary()
显示了滤波器轴应该在的位置。
我需要在滤波器轴上将这个输出与另一个输入连接起来,但如果我相信模型摘要,我会在训练时得到一个错误:ValueError: Error when checking target: expected leaky_re_lu_6 to have 4 dimensions, but got array with shape (5, 112, 112)
。在LeakyReLU
之后添加Reshape((1,112,112))
也无济于事。
如果我使用keras.backend.expand_dims(resized_output,1)
来强制我想要的大小,我会得到一个编译时错误:ValueError: A 'Concatenate' layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 3, 448, 448), (None, 1, 1, 448, 448)]
model.summary()
的相关部分:
conv2d_6 (Conv2D) (None, 1, 112, 112)leaky_re_lu_6 (LeakyReLU) (None, 1, 112, 112) conv2d_6[0][0] conv2d_6[1][0] full_input (InputLayer) (None, 3, 16, 448, 448)lambda_1 (Lambda) (None, 3, 448, 448) full_input[0][0]up_sampling2d_5 (UpSampling2D) (None, 1, 448, 448) leaky_re_lu_6[1][0] concatenate_1 (Concatenate) (None, 4, 448, 448) lambda_1[0][0] up_sampling2d_5[0][0]
模型定义片段:
data_format = "channels_first"C3 = lambda filter_size: Conv3D( filter_size, (3, 3, 3), data_format=data_format, activation="relu", padding="same")def P3(shape=(2, 2, 2)): return MaxPooling3D( shape, data_format=data_format)C2 = lambda filter_size: Conv2D( filter_size, (3,3), data_format=data_format, padding="same")U2 = lambda: UpSampling2D(data_format=data_format)coarse_architecture = [ # encoder #112, 16 C3(64), P3(), #56 , 8 C3(128), P3(), #28 , 4 C3(256), C3(256), P3(), #14 , 2 C3(512), C3(512), P3(), #7 , 1 # decoder Reshape((512,7,7)), C2(256), LeakyReLU(0.001), U2(), #14 C2(128), LeakyReLU(0.001), U2(), #28 C2(64), LeakyReLU(0.001), U2(), #56 C2(32), LeakyReLU(0.001), U2(), #112 C2(16), LeakyReLU(0.001), C2(1), LeakyReLU(0.001)]def coarse_inference(x): return apply_sequence(coarse_architecture, x)# Siamese subnetworkfull_input = Input(shape=(3,16,448,448),dtype='float32',name="full_input")resized_input = Input(shape=(3,16,112,112),dtype='float32',name="resized_input")cropped_input = Input(shape=(3,16,112,112),dtype='float32',name="cropped_input")cropped_output = coarse_inference(cropped_input)resized_output = coarse_inference(resized_input)# Fine-tuning subnetworktake_last_frame = Lambda(lambda x: x[:,:,-1,:,:],output_shape = (3,448,448))last_frame = take_last_frame(full_input)resized_output = UpSampling2D(size=(4,4),data_format=data_format)(resized_output)fine_input = concatenate([last_frame,resized_output],axis=1)fine_output = apply_sequence(fine_architecture, fine_input)# Build modelmodel = Model(inputs=[full_input,cropped_input,resized_input], outputs=[cropped_output,fine_output])
我是否在指定模型时犯了错误?我该如何克服这种不一致性?
回答:
通过错误信息:
ValueError: Error when checking TARGET: expected leaky_re_lu_6 to have 4 dimensions, but got array with shape (5, 112, 112)
我们可以看到问题在于y_train
(你的训练输出数据)与模型的输出形状不兼容。
看起来要么y_train
应该有一个额外的维度,要么模型的输出(即leaky_re_lu_6
)应该被调整以匹配你当前的y_train
。
具体细节只有在我们更了解你的数据后才可能知道 🙂