Deconvolution Using Theano
Transposed Convolution, 也叫Fractional Strided Convolution, 或者流行的(错误)称谓: 反卷积, Deconvolution. 定义请参考tutorial. 此处也是对tutorial中的theano实现做一个总结, 得到一段可用的Deconvolution代码.
反卷积(都这么叫了, 那我也不纠结这个了. )的实现方式之一是前向卷积操作的反向梯度传播过程, 所以在Theano中可使用theano.tensor.nnet.abstract_conv.conv2d_grad_wrt_inputs方法来实现反卷积, 方法名的大概意思是给定输出后, 它可以反向传播到输入的梯度大小, 即\(\frac {\partial a}{x}\), 其中\(a,x\)分别为输出和输入.

封装成常见的class:
class DeconvolutionLayer(Layer):
def __init__(self, input, filter_shape, stride, padding = (0, 0), name = 'deconv' ):
Layer.__init__(self, input, name, activation = None)
W_value = util.rand.normal(filter_shape)
W_value = np.asarray(W_value, dtype = util.dtype.floatX)
self.W = theano.shared(value = W_value, borrow = True)
s1, s2 = stride;
p1, p2 = padding;
k1, k2 = filter_shape[-2:]
o_prime1 = s1 * (self.input.shape[2] - 1) + k1 - 2 * p1
o_prime2 = s2 * (self.input.shape[3] - 1) + k2 - 2 * p2
output_shape=(None, None, o_prime1, o_prime2)
self.output_shape = output_shape
self.output = T.nnet.abstract_conv.conv2d_grad_wrt_inputs(output_grad = self.input, input_shape = output_shape, filters = self.W, filter_shape = filter_shape, border_mode= padding, subsample= stride)
self.params = [self.W]
不明白为什么conv2d_grad_wrt_inputs方法一定要提供input_shape参数. 文档是这么写的:
input_shape : [None/int/Constant] * 2 + [Tensor/int/Constant] * 2 The shape of the input (upsampled) parameter. A tuple/list of len 4, with the first two dimensions being None or int or Constant and the last two dimensions being Tensor or int or Constant. Not Optional, since given the output_grad shape and the subsample values, multiple input_shape may be plausible.
意思是给定output_grad的shape与subsample(即stride)后, input_shape不是唯一的, 可是我还确定了padding啊, 这不就唯一了?
值得一提的是, padding一般取0.
在用FCN作语义分割的paper code(caffe 实现)中:
n.upscore = L.Deconvolution(n.score_fr,
convolution_param=dict(num_output=21, kernel_size=64, stride=32,
bias_term=False),
param=[dict(lr_mult=0)])
n.score = crop(n.upscore, n.data)
也就是说, 它是一次性将feature map放大32倍, 然后crop到与输入一样大小. 它为什么能这样做呢?
因为它的第一层conv pad = 100:
n.conv1_1, n.relu1_1 = conv_relu(n.data, 64, pad=100)
这样一来, crop掉的数据都是在padding 0上计算来的.
[full code](https://github.com/dengdan/pylib/blob/master/src/nnet/layer.py#L94)
Deconvolution Using Theano的更多相关文章
- Theano printing
Theano printing To visualize the internal relation graph of theano variables. Installing conda insta ...
- Theano Graph Structure
Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...
- Theano Inplace
Theano Inplace inplace Computation computation that destroy their inputs as a side-effect. Example i ...
- broadcasting Theano vs. Numpy
broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...
- theano scan optimization
selected from Theano Doc Optimizing Scan performance Minimizing Scan Usage performan as much of the ...
- theano sparse_block_dot
theano 中的一个函数 sparse_block_dot; Function: for b in range(batch_size): for j in range(o.shape[1]): fo ...
- ubuntu系统theano和keras的安装
说明:系统是unbuntu14.04LTS,32位的操作系统,以前安装了python3.4,现在想要安装theano和keras.步骤如下: 1,安装pip sudo apt-get install ...
- theano学习
import numpy import theano.tensor as T from theano import function x = T.dscalar('x') y = T.dscalar( ...
- Theano 学习笔记(一)
Theano 学习笔记(一) theano 为什么要定义共享变量? 定义共享变量的原因在于GPU的使用,如果不定义共享的话,那么当GPU调用这些变量时,遇到一次就要调用一次,这样就会花费大量时间在数据 ...
随机推荐
- is not in the sudoers file的解决方法
遇到这个问题 修改sudoers 文件 /etc/sudoers 添加
- MongoDB集群卡死问题
一年前搭了个MongoDB集群,跑得还算不错,但是有几次遇到过服务卡死的问题.处理起来已经得心应手了,拿来跟大家分享一下: 故障现象: 业务查询缓慢,而且会有连接异常: { "serverU ...
- EL表达式的算术运算
一个例子--乘法运算 ${book.bookCount * book.bookPrice } 两个不同对象的EL表达式的算术运算同理 ${student.studentNum * book.bookP ...
- sencha ext js 6 入门
Sencha Ext JS号称是目前世界上最先进和最强大的.支持多平台多设备的JavaScript应用程序开发框架.首先看一下Ext JS的发展简史. 1 Ext JS发展简史 YUI-Ext的作者J ...
- Eclipse安装Spring-tool-suite
目录结构: // contents structure [-] 在Eclipse上安装Spring-tool-suite的方法有那些 如何查看自己的Eclipse版本 如何知道自己的Eclipse对应 ...
- js—模糊查询
首先要明白什么是模糊查询(废话又来了),就是根据关键字把列表中符合关键字的一项或某项罗列出来,也就是要检查列表的每一项中是否含有关键字,因此抽象一下就是一个字符串中是否含有某个字符或者字符串. 以下例 ...
- infopath发布的提示“无法解析SOAP消息”(The SOAP message cannot be parsed)问题解决方案
最近发现一个列表数据过大,每次发布infopath表单提示如下错误: 后来发现一个infopath表单通过list.asmx and Formsservice.asmx来进行发布的. This err ...
- ArcGIS Engine开发之地图基本操作(3)
地图数据的加载 一.加载Shapefile数据 Shapefile文件是目前主流的一种空间数据的文件存储方式,也是不同GIS软件进行数据格式转换常用的中间格式.加载Shapefile数据的方式有两种: ...
- iOS多线程之7.NSOperation的初识
NSOperation和GCD一样,不用我们管理线程的生命周期,加锁等问题,只要把操作封装进NSOperation中,系统会自动帮我们创建线程,执行操作.而且他是面向对象的,我们看起来更容易理解,使用 ...
- Activity生命周期
在开始之前我们先了解一下什么是Activity: 直接翻译为:"活动",而在Android中更多的是代表手机的屏幕,是Android的四大组件之一,重要的组成单元,提供了与用户交互 ...