gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:47:36 26 4
gpt4 key购买 nike

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: 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.

只有我们更好地了解您的数据,才能获得详细信息:)

关于python - Keras 卷积秩与 1 个过滤器不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50353078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com