【问题标题】:Migrating keras.backend.conv2d from Keras 1.x to 2.x将 keras.backend.conv2d 从 Keras 1.x 迁移到 2.x
【发布时间】:2017-09-28 14:02:23
【问题描述】:

我正在将一个项目从 Keras 1.x 迁移到 2.x。

在代码中,在 1.x 中运行良好的 keras.backend.conv2d 操作现在在 2.x 中崩溃。

convs = K.conv2d(a, b, padding='valid', data_format='channels_first')

输入张量形状ab 都是(1024, 4, 1, 1),输出张量形状在1.x 中是(1024, 1024, 1, 1)

使用 2.x 我收到以下错误:

ValueError: CorrMM: impossible output shape
  bottom shape: 1024 x 4 x 1 x 1
  weights shape: 1 x 1 x 1024 x 4
  top shape: 1024 x 1 x -1022 x -2

Apply node that caused the error: CorrMM{valid, (1, 1), (1, 1), 1 False}(Print{message='a', attrs=('__str__',), global_fn=<function DEBUG_printTensorShape at 0x00000272EF1FAD08>}.0, Subtensor{::, ::, ::int64, ::int64}.0)
Toposort index: 30
Inputs types: [TensorType(float32, (False, False, True, True)), TensorType(float32, (True, True, False, False))]
Inputs shapes: [(1024, 4, 1, 1), (1, 1, 1024, 4)]

我正在使用 Theano 后端,并在 K.set_image_data_formatconv2d 中都设置了 channels_first

【问题讨论】:

    标签: python python-3.x keras theano keras-2


    【解决方案1】:

    conv2D方法中,a是实际的镜像,b是内核。


    a 的预期形状是(带有“channels_first”):

    (batchSize, channels, side1, side2)
    

    所以,您的输入有:

    • 1024 张图片
    • 4 个频道
    • 图像 1 x 1

    但是尽管使用'channels_last'b 的预期形状是:

    (side1,side2, inputChannels,outputChannels)
    

    这似乎有点误导,因为在过滤器中,它仍然是最后一个频道。 (在我的 keras 版本 2.0.4 上测试)

    所以,如果你的输出是 (1024,1024,1,1),我假设 b 应该有 1024 个输出过滤器,所以它的形状应该是:

    (1,1,4,1024)
    

    您可能应该使用一些方法来置换尺寸,而不仅仅是重塑。 Numpy 有swapaxes,而 keras 有K.permute_dimensions

    【讨论】:

    • K.permute_dimensions(x, (3,2,1,0)) 完成了这项工作。谢谢!
    猜你喜欢
    • 2018-08-10
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2019-07-21
    • 1970-01-01
    相关资源
    最近更新 更多