keras 中的 Deconv 实现 output_shape 问题
Deconv implementation in keras output_shape issue
我正在实施以下 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
p.s。已经在数据科学论坛页面上询问但没有回应。可能是由于用户群较小
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 匹配:
Formula for calculation of the output shape 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 参数显然没有真正定义输出形状。根据 post 这是为什么:
Back to Keras and how the above is implemented. Confusingly, the output_shape parameter is actually not used for determining the output shape of the layer, and instead they try to deduce it from the input, the kernel size and the stride, while assuming only valid output_shapes are supplied (though it's not checked in the code to be the case). The output_shape itself is only used as input to the backprop step. Thus, you must also specify the stride parameter (subsample in Keras) in order to get the desired result (which could've been determined by Keras from the given input shape, output shape and kernel size).
我正在实施以下 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
p.s。已经在数据科学论坛页面上询问但没有回应。可能是由于用户群较小
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 匹配:
Formula for calculation of the output shape 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 参数显然没有真正定义输出形状。根据 post
Back to Keras and how the above is implemented. Confusingly, the output_shape parameter is actually not used for determining the output shape of the layer, and instead they try to deduce it from the input, the kernel size and the stride, while assuming only valid output_shapes are supplied (though it's not checked in the code to be the case). The output_shape itself is only used as input to the backprop step. Thus, you must also specify the stride parameter (subsample in Keras) in order to get the desired result (which could've been determined by Keras from the given input shape, output shape and kernel size).