我正在实现以下用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的形式提供什么
附注:已经询问了数据科学论坛页面,没有任何回应。可能是因为用户数量少。
发布于 2016-11-06 14:02:35
Caffe反褶积层产生什么输出形状?
特别是对于这个彩色模型,您可以简单地引用他们的论文的第24页(在它们的GitHub页面中链接):
因此,在原始模型中,这个反褶积层的输出形状基本上是0,56,56,128。这就是您希望作为output_shape传递给Keras的内容。唯一的问题是,正如我在下面一节中提到的那样,Keras实际上并不使用这个参数来确定输出形状,所以您需要运行一个虚拟预测,以找到您的其他参数需要什么才能得到您想要的。
更广泛地说,计算其反褶积层输出形状的Caffe源代码是:
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];
注意,当参数Keras文档为零时,这与a
匹配:
如何使用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 角化物中的Deconvolution2D层,这就是为什么:
返回到Keras,以及如何实现上述内容。令人困惑的是,output_shape参数实际上并不用于确定层的输出形状,而是试图从输入、内核大小和步幅中推断出它,同时假设只提供有效的output_shapes (尽管在代码中没有签入情况)。output_shape本身仅被用作后端步骤的输入。因此,您还必须指定shape参数( Keras中的子样本),以获得所需的结果(这可以由Keras从给定的输入形状、输出形状和内核大小确定)。
https://stackoverflow.com/questions/40453494
复制相似问题