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 根据给定的输入形状、输出形状和内核大小确定)。