对于我的网络,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)
。在Reshape((1,112,112))
之后植入LeakyReLU
并没有帮助。
如果改用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])
指定模型时我是否犯了错误?我该如何克服这种矛盾?
最佳答案
通过错误消息:
ValueError:检查TARGET时出错:预期Leaky_re_lu_6具有4个维度,但数组的形状为(5,112,112)
我们可以看到问题在于y_train
(您的训练输出数据)与模型的输出形状不兼容。
似乎y_train
应该具有额外的尺寸,或者模型的输出(表示为leaky_re_lu_6
)应该与您当前的y_train
相匹配。
仅当我们更了解您的数据时,才有可能提供详细信息:)
关于python - Keras卷积等级与1个过滤器不一致,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50353078/