【问题标题】:Deconv implementation in keras output_shape issuekeras output_shape 问题中的 Deconv 实现
【发布时间】:2016-11-06 19:20:21
【问题描述】:

我正在执行Colorization Model written in Caffe。我对在 Keras 中提供的 output_shape 参数感到困惑

model.add(Deconvolution2D(256,4,4,border_mode='same',
output_shape=(None,3,14,14),subsample=(2,2),dim_ordering='th',name='deconv_8.1'))

我添加了一个虚拟的 output_shape 参数。但是如何确定输出参数?在 caffe 模型中,层定义为:

layer {
 name: "conv8_1"
  type: "Deconvolution"
  bottom: "conv7_3norm"
  top: "conv8_1"
  convolution_param {
    num_output: 256
    kernel_size: 4
    pad: 1
    dilation: 1
    stride: 2
  }

如果我不提供此参数,代码会给出参数错误,但我不明白我应该提供什么作为 output_shape

附言已经在数据科学论坛页面上询问过,但没有任何回应。可能是因为用户群小

【问题讨论】:

    标签: convolution keras deconvolution


    【解决方案1】:

    Caffe 反卷积层产生什么输出形状?

    对于这种着色模型,您可以简单地参考their paper 的第 24 页(链接在他们的 GitHub 页面中):

    所以基本上这个反卷积层在原始模型中的输出形状是[None, 56, 56, 128]。这就是您想要作为 output_shape 传递给 Keras 的内容。唯一的问题是正如我在下面的部分中提到的,Keras 并没有真正使用这个参数来确定输出形状,所以你需要运行一个虚拟预测来找到你的其他参数需要什么才能得到什么你想要的。

    更一般地说,Caffe source code for computing its Deconvolution layer output shape 是:

        const int kernel_extent = dilation_data[i] * (kernel_shape_data[i] - 1) + 1;
        const int output_dim = stride_data[i] * (input_dim - 1)
        + kernel_extent - 2 * pad_data[i];
    

    膨胀参数等于 1 的简化为:

        const int output_dim = stride_data[i] * (input_dim - 1)
        + kernel_shape_data[i] - 2 * pad_data[i];
    

    请注意,当参数a 为零时,这与Keras documentation 匹配:

    输出形状3,4的计算公式:o = s (i - 1) + a + k - 2p

    如何使用 Keras 后端验证实际输出形状

    这很棘手,因为实际的输出形状取决于后端实现和配置。 Keras 目前无法自行找到它。所以你实际上必须对一些虚拟输入执行预测才能找到实际的输出形状。以下是 Deconvolution2D 的 Keras 文档中如何执行此操作的示例:

    To pass the correct `output_shape` to this layer,
    one could use a test model to predict and observe the actual output shape.
    # Examples
    ```python
        # apply a 3x3 transposed convolution with stride 1x1 and 3 output filters on a 12x12 image:
        model = Sequential()
        model.add(Deconvolution2D(3, 3, 3, output_shape=(None, 3, 14, 14), border_mode='valid', input_shape=(3, 12, 12)))
        # Note that you will have to change the output_shape depending on the backend used.
        # we can predict with the model and print the shape of the array.
        dummy_input = np.ones((32, 3, 12, 12))
        # For TensorFlow dummy_input = np.ones((32, 12, 12, 3))
        preds = model.predict(dummy_input)
        print(preds.shape)
        # Theano GPU: (None, 3, 13, 13)
        # Theano CPU: (None, 3, 14, 14)
        # TensorFlow: (None, 14, 14, 3)
    

    参考:https://github.com/fchollet/keras/blob/master/keras/layers/convolutional.py#L507

    您可能还想知道为什么 output_shape 参数显然没有真正定义输出形状。根据Deconvolution2D layer in keras 的帖子,这就是原因:

    回到 Keras 以及如何实现上述内容。令人困惑的是, output_shape 参数实际上并没有用于确定层的输出形状,而是他们尝试从输入、内核大小和步幅中推断出它,同时假设只提供了有效的 output_shapes(尽管它没有在代码是这样的)。 output_shape 本身仅用作反向传播步骤的输入。因此,您还必须指定步幅参数(Keras 中的子样本)才能获得所需的结果(这可以由 Keras 根据给定的输入形状、输出形状和内核大小确定)。

    【讨论】:

    • 我提供了有问题的 caffe 着色模型的链接。请您考虑模型来回答这个问题。我已经阅读了关于这一层的所有可用讨论,但真正的问题是如何为给定模型管理它。我正在尝试将此模型转换为 keras
    • 我明白了。我可以稍后看看,但 Caffe 不是我的强项。此外,您可能希望将此说明以及您已阅读/尝试过的内容添加到您的问题中。
    • 我已经更新了答案,将反卷积层的预期输出形状包含在原始模型中。正如我所提到的,Keras 产生的确切输出形状将取决于您的后端(TensorFlow/TheanoCPU/TheanoGPU)。因此,如果您想要的话,您需要运行答案中引用的虚拟预测代码,以找到如何产生准确的输出形状。希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    相关资源
    最近更新 更多