Deconvolution Level 2D in Keras

This layer is not ready for documentation very well, and I have some problems figuring out how to use it.
I am trying something like:

input_img = Input(shape=(1, h, w)) x = Convolution2D(16, 7, 7, activation='relu', border_mode='valid')(input_img) d = Deconvolution2D(1, 7, 7, (None, 1, 2*h, 2*w)) x = d(x) 

but when I try to write d.output_shape , I get the original image shape instead of double the size (which I expected).
Any help would be greatly appreciated!

+6
source share
1 answer

Short answer: you need to add subample = (2,2) to Deconvolution2D if you want the result to really be twice as large as the input.


Longer answer: Deconvolution2D is highly undocumented, and you need to go through its code to figure out how to use it.

You must first understand how the deconvolution layer works (skip this if you already know all the details). Deconvolution, unlike what its name suggests, simply applies the back-propgation method (gradient calculation) of the standard convolution layer at the entrance to the deconvolution level. The "core size" of the deconvolution layer is actually the size of the kernel of the virtual convolution level of the backprop stage mentioned above. Given the size of the convolution kernel and its step, simply calculate the output form of the convolution layer (provided that it does not fill it (input-kernel) // stride + 1), but the converse is not true. In fact, there may be several possible input forms that correspond to a given form of output of the convolution layer (this is due to the fact that integer division is not reversible). This means that for the deconvolution layer, the output form cannot be directly determined simply from the input form (which is implicitly known), the size of the kernel and the step - that’s why we need to know the output form when initializing the layer. Of course, because of how the deconvolution layer is defined, for some input forms you will get holes in your output that are undefined, and if we prohibit these cases, we can actually output the output form.

Back to Keras and how it is implemented. Confusingly, the output_shape parameter is not actually used to determine the output shape of the layer, and instead they try to infer it from the input, kernel size and step, while it is assumed that only valid output_ outputs are supplied (although it is not checked in the code in this case). The output_shape itself is used only as input for the backprop step. Thus, you must also specify the step parameter (a subsample in Keras) to get the desired result (which Keras could determine from the given input form, output form, and kernel size).

+7
source

All Articles