<h2>What output shape does the Caffe deconvolution layer produce?</h2>

<p>For this colorization model in particular you can simply refer to page 24 of <a href="https://arxiv.org/pdf/1603.08511.pdf" rel="nofollow noreferrer">their paper</a> (which is linked in their GitHub page):</p>

<p><a href="https://i.stack.imgur.com/PkdxN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PkdxN.png" alt="Colorization model architecture"></a></p>

<p>So basically the output shape of this deconvolution layer in the original model is [None, 56, 56, 128]. This is what you want to pass to Keras as output_shape. The only problem is as I mention in the section below, Keras doesn't really use this parameter to determine the output shape, so you need to run a dummy prediction to find what your other parameters need to be in order for you to get what you want.</p>

<p>More generally the <a href="https://github.com/BVLC/caffe/blob/master/src/caffe/layers/deconv_layer.cpp#L18" rel="nofollow noreferrer">Caffe source code for computing its Deconvolution layer output shape</a> is:</p>

<pre><code>    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];
</code></pre>

<p>Which with a dilation argument equal to 1 reduces to just:</p>

<pre><code>    const int output_dim = stride_data[i] * (input_dim - 1)
    + kernel_shape_data[i] - 2 * pad_data[i];
</code></pre>

<p>Note that this matches the <a href="https://keras.io/layers/convolutional/#deconvolution2d" rel="nofollow noreferrer">Keras documentation</a> when the parameter <code>a</code> is zero:</p>

<blockquote>
  <p>Formula for calculation of the output shape <a href="https://github.com/BVLC/caffe/blob/master/src/caffe/layers/deconv_layer.cpp#L18" rel="nofollow noreferrer">3</a>, <a href="https://keras.io/layers/convolutional/#deconvolution2d" rel="nofollow noreferrer">4</a>: o = s (i - 1) +
  a + k - 2p</p>
</blockquote>

<h2>How to verify actual output shape with your Keras backend</h2>

<p>This is tricky, because the actual output shape depends on the backend implementation and configuration. Keras is currently unable to find it on its own. So you actually have to execute a prediction on some dummy input to find the actual output shape. Here's an example of how to do this from the Keras docs for Deconvolution2D:</p>

<pre><code>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)
</code></pre>

<p>Reference: <a href="https://github.com/fchollet/keras/blob/master/keras/layers/convolutional.py#L507" rel="nofollow noreferrer">https://github.com/fchollet/keras/blob/master/keras/layers/convolutional.py#L507</a></p>

<p>Also you might be curious to know why is it that the output_shape parameter apparently doesn't really define the output shape. According to the post <a href="https://stackoverflow.com/questions/39018767/deconvolution2d-layer-in-keras">Deconvolution2D layer in keras</a> this is why:</p>

<blockquote>
  <p>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).</p>
</blockquote>
Deconv implementation in keras output_shape issue
<p>I am implementing following <a href="https://github.com/richzhang/colorization/blob/master/models/colorization_deploy_v2.prototxt" rel="nofollow noreferrer">Colorization Model written in Caffe</a>. I am confused about my output_shape parameter to supply in Keras</p>

<pre><code>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'))
</code></pre>

<p>I have added a dummy output_shape parameter. But how can I determine the output parameter? In caffe model the layer is defined as:</p>

<pre><code>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
  }
</code></pre>

<p>If I do not supply this parameter the code give parameter error but I can not understand what should I supply as output_shape</p>

<p>p.s. already asked on data science forum page with no response. may be due to small user base</p>