且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

keras中的反卷积2D层

更新时间:2023-12-03 10:51:28

简短答案:如果您希望输出确实是输入的两倍,则需要在Deconvolution2D中添加subsample =(2,2).

Short answer: you need to add subsample=(2,2) to Deconvolution2D if you wish the output to truly be twice as large as the input.


更长的答案:Deconvolution2D完全没有文档记录,您必须阅读其代码以了解如何使用它.

Longer answer: Deconvolution2D is severely undocumented and you have to go through its code to understand how to use it.

首先,您必须了解反卷积层的工作原理(如果您已经知道所有详细信息,请跳过此步骤).反卷积与其名称不同,它只是在反卷积层的输入上应用标准卷积层的反向传播(梯度计算方法).反卷积层的内核大小"实际上是上述反向传播步骤的虚拟卷积层的内核大小.尽管给出了卷积核的大小及其步幅,但计算卷积层的输出形状很简单(假设没有填充(输入-内核)//步幅+1),但是反之则不成立.实际上,可能有不止一种可能的输入形状与卷积层的给定输出形状相匹配(这是因为整数除法是不可逆的).这意味着对于反卷积层,不能直接根据输入形状(隐式已知),内核大小和步幅直接确定输出形状-这就是为什么我们在初始化层时需要知道输出形状的原因.当然,由于反褶积层的定义方式,对于某些输入形状,您会在其输出中得到未定义的孔,并且如果我们禁止这些情况,则实际上我们可以可以推导出输出形状

First, you must understand how the deconvolution layer works (skip this if you already know all the details). Deconvolution, unlike what its name suggest, is simply applying the back-propgation (gradient calculation method) of a standard convolution layer on the input to the deconvolution layer. The "kernel size" of the deconvolution layer is actually the kernel size of the virtual convolution layer of the backprop step mentioned above. While given the size of a convolution kernel and its stride, it is straightforward to compute the output shape of the convolution layer (assuming no padding it's (input - kernel) // stride + 1), but the reverse is not true. In fact, there can be more than one possible input shapes that matches a given output shape of the convolution layer (this is because integer division isn't invertible). This means that for a deconvolution layer, the output shape cannot be directly determined simply from the input shape (which is implicitly known), kernel size and stride - this is why we need to know the output shape when we initialize the layer. Of course, because of the way the deconvolution layer is defined, for some input shapes you'll get holes in its output which are undefined, and if we forbid these cases then we actually can deduce the output shape.

返回Keras以及如何实现以上内容.令人困惑的是,实际上不使用output_shape参数来确定图层的输出形状,而是尝试从输入,内核大小和跨度中推断出该形状,同时假设仅提供了有效的output_shapes(尽管未在代码). output_shape本身仅用作反向传播步骤的输入.因此,还必须指定步幅参数(Keras中的子样本)才能获得所需的结果(Keras可以根据给定的输入形状,输出形状和仁尺寸确定该结果).

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).