Keras 卷积秩与 1 个过滤器不一致



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 subnetwork
full_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 subnetwork
take_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 model
model = Model(inputs=[full_input,cropped_input,resized_input],
outputs=[cropped_output,fine_output])

我在指定模型时是否犯了错误?如何克服这种不一致?

通过错误消息:

值错误:检查TARGET时出错:预期leaky_re_lu_6具有 4 个维度,但得到形状为 (5、112、112( 的数组

我们可以看到,问题在于y_train(您的训练输出数据(与模型的输出形状不兼容。

似乎y_train应该有一个额外的维度,或者模型的输出(leaky_re_lu_6(应该工作以匹配您当前的y_train

只有当我们更好地了解您的数据时,才能提供详细信息:)

最新更新