【问题标题】:"ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=4"“ValueError:输入 0 与层 conv1d_1 不兼容:预期 ndim=3,发现 ndim=4”
【发布时间】:2020-04-18 06:59:02
【问题描述】:

我正在加载 VGG19 模型并尝试应用 1d conv 来减少深度,但我收到以下错误: "ValueError: Input 0 is in compatible with layer conv1d_1: expected ndim=3, found ndim=4"

这是我正在使用的功能:

def getModel():
    base_model = VGG19(weights='imagenet')
    model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_conv4').output) 
    #model.output=(None, 28, 28, 512)   
    layer=keras.layers.Conv1D(96, (512), padding='same')
    model.summary()
    out=(layer)(model.output)
    model = Model(inputs=base_model.input, outputs=out)
    model.summary()
    return model

【问题讨论】:

  • 您不能在 Conv2D 的输出上应用 Conv1D 而不在高度和宽度轴上展开您的 Conv2D 输出。也许更清楚地解释你想做什么。
  • 我真正想要的是将输出深度从 512 降低到 96。

标签: keras conv-neural-network


【解决方案1】:

问题在于最后一个输出是由 Conv2D 层产生的输出,其形状为 [batch size, height, width, channels]。这不能被Conv1D 使用。 Conv1D 消耗 [batch size, width, channels] 输入。在您的情况下,由于您有兴趣减少过滤器的数量,您需要做的就是将您的 Conv1D 转换为 Conv2D

只需将您的功能更改为,

def getModel():
    base_model = VGG19(weights='imagenet')
    model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_conv4').output) 

    # Here we are using Conv2D not Conv1D which gives 96 filters at the end.
    layer=keras.layers.Conv2D(96, (3,3), padding='same')
    model.summary()
    out=(layer)(model.output)
    model = Model(inputs=base_model.input, outputs=out)
    model.summary()
    return model

这里,

  • 96 - 是过滤器的数量
  • (3,3) - 是卷积层的内核高度和宽度。

【讨论】:

    猜你喜欢
    • 2018-09-25
    • 2019-06-04
    • 2019-04-14
    • 2017-11-18
    • 2022-01-06
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多