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的更多相关文章

  1. Theano printing

    Theano printing To visualize the internal relation graph of theano variables. Installing conda insta ...

  2. Theano Graph Structure

    Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...

  3. Theano Inplace

    Theano Inplace inplace Computation computation that destroy their inputs as a side-effect. Example i ...

  4. broadcasting Theano vs. Numpy

    broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...

  5. theano scan optimization

    selected from Theano Doc Optimizing Scan performance Minimizing Scan Usage performan as much of the ...

  6. theano sparse_block_dot

    theano 中的一个函数 sparse_block_dot; Function: for b in range(batch_size): for j in range(o.shape[1]): fo ...

  7. ubuntu系统theano和keras的安装

    说明:系统是unbuntu14.04LTS,32位的操作系统,以前安装了python3.4,现在想要安装theano和keras.步骤如下: 1,安装pip sudo apt-get install ...

  8. theano学习

    import numpy import theano.tensor as T from theano import function x = T.dscalar('x') y = T.dscalar( ...

  9. Theano 学习笔记(一)

    Theano 学习笔记(一) theano 为什么要定义共享变量? 定义共享变量的原因在于GPU的使用,如果不定义共享的话,那么当GPU调用这些变量时,遇到一次就要调用一次,这样就会花费大量时间在数据 ...

随机推荐

  1. 设计模式(十三)代理模式(Proxy Pattern)

    一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访问到这些对象,如果直接访问对象给系统带来不必要的复杂性,这时候可以在客户端和目标对象之间增加一层中间层,让代 ...

  2. 让linux开机默认开启小键盘

     linux默认开机不开启数字键盘numberlock,每次输入开机密码还得劳烦自己去点亮指示灯,让此灯开机自动点亮,需要一个软件才行,就是numlockx了,可以通过yum安装:yuminstall ...

  3. MYSQL基础知识和操作(二).png

  4. IoC组件~Autofac将多实现一次注入,根据别名Resove实例

    回到目录 对于IoC容器来说,性能最好的莫过于Autofac了,而对于灵活度来说,它也是值得称赞的,为了考虑系统的性能,我们经常是在系统初始化于将所有依赖注册到容器里,当需要于根据别名把实现拿出来,然 ...

  5. QQ空间/朋友圈类界面的搭建

    类似于QQ空间的布局主要是在说说信息.点赞.回复三大部分的自适应布局上. 当我们需要搭建类似QQ空间.微信朋友圈的界面的时候,可做如下操作: 创建一个对应的model类: 创建一个对应model类的f ...

  6. centos7中没有安装ifconfig命令的解决方法

    初装centos 7时,运行config报 command not found 错误,我在网上找了大量资料,下面的资料中查找原因和解决方式最详细,能很好的解决这个问题. ifconfig命令是设置或显 ...

  7. mysql select日期格式

    mysql表中datatime类型存储为2016-01-10,C#直接select 后,在datatable里面看,变成01/10/2016,需要还原回去,使用select DATE_FORMAT(列 ...

  8. putty提供的两个文件传输工具PSCP、PSFTP详细介绍

    用 SSH 来传输文件 PuTTY 提供了两个文件传输工具 PSCP (PuTTY Secure Copy client) PSFTP (PuTTY SFTP client) PSCP 通过 SSH ...

  9. Mysql Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='

    MySQL字符串比较bug: select * from table_a a left join table_b b on a.field_a = b.field_b   error: Illegal ...

  10. 浏览器控制台js代码与后台不同步

    原因:浏览器会缓存js 如果是将js代码直接通过<script>标签插入jsp页面中则不存在这个问题 在加载页面的时候会重新加载js代码 如果直接将js代码以文件的形式引入,那么每次在修改 ...