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

时间:2018-05-15 14:42:26

标签: python machine-learning 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])

我是否在指定模型时犯了错误?我怎样才能克服这种不一致?

1 个答案:

答案 0 :(得分:1)

通过错误消息:

  

ValueError:检查 TARGET 时出错:预期leaky_re_lu_6有4个维度,但得到的数组有形状(5,112,112)

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

似乎y_train应该有一个额外的维度,或者模型的输出(表示leaky_re_lu_6)应该与您当前的y_train相匹配。

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