keras中的反卷积2D层 [英] Deconvolution2D layer in keras

查看:1383
本文介绍了keras中的反卷积2D层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这层尚未准备好的文件记录得很好,我在弄清楚确切如何使用它方面遇到了一些麻烦.
我正在尝试类似:

This layer in not ready documented very well and I'm having a bit of trouble figuring out exactly how to use it.
I'm 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)

但是当我尝试编写d.output_shape时,我得到的是图像的原始形状,而不是原来的两倍(这是我所期望的).
任何帮助将不胜感激!

but when I try to write d.output_shape, I get the original shape of the image instead of twice that size (which is what I was expecting).
Any help will be greatly appreciated!

推荐答案

简短答案:如果您希望输出确实是输入的两倍,则需要在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).

这篇关于keras中的反卷积2D层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆