Tensorflow:padding操作

刚开始接触Tensorflow,好多东西不会。慢慢总结备忘。

原文链接:

http://www.jianshu.com/p/05c4f1621c7e

http://blog.csdn.net/jasonzzj/article/details/53930074


根据tensorflow中的conv2d函数,我们先定义几个基本符号

1、输入矩阵 W×W,这里只考虑输入宽高相等的情况,如果不相等,推导方法一样,不多解释。

2、filter矩阵 F×F,卷积核

3、stride值 S,步长

4、输出宽高为 new_height、new_width

当然还有其他的一些具体的参数,这里就不再说明了。

我们知道,padding的方式在tensorflow里分两种,一种是VALID,一种是SAME,下面分别介绍这两种方式的实际操作方法。

1、如果padding = ‘VALID’

new_height = new_width = (W – F + 1) / S (结果向上取整)

也就是说,conv2d的VALID方式不会在原有输入的基础上添加新的像素(假定我们的输入是图片数据,因为只有图片才有像素),输出矩阵的大小直接按照公式计算即可。

2、如果padding = ‘SAME’

new_height = new_width = W / S (结果向上取整)

在高度上需要pad的像素数为

pad_needed_height = (new_height – 1)  × S + F - W

根据上式,输入矩阵上方添加的像素数为

pad_top = pad_needed_height / 2  (结果取整)

下方添加的像素数为

pad_down = pad_needed_height - pad_top

以此类推,在宽度上需要pad的像素数和左右分别添加的像素数为

pad_needed_width = (new_width – 1)  × S + F - W

pad_left = pad_needed_width  / 2 (结果取整)

pad_right = pad_needed_width – pad_left

至此,关于tensorflow的卷积padding操作介绍完毕,下面是关于此操作的源码(Get2dOutputSizeVerbose函数的部分节选),我也不会用MarkDown,索性直接截图了,以供参考


SAME means that the output feature map has the same spatial dimensions as the input feature map. Zero padding is introduced to make the shapes match as needed, equally on every side of the input map.
VALIDmeans no padding.

Padding could be used in convolution and pooling operations.
Here, take pooling for example:


If you like ascii art:

  • "VALID" = without padding:

       inputs:         1  2  3  4  5  6  7  8  9  10 11 (12 13)
                      |________________|                dropped
                                     |_________________|
  • "SAME" = with zero padding:

                   pad|                                      |pad
       inputs:      0 |1  2  3  4  5  6  7  8  9  10 11 12 13|0  0
                   |________________|
                                  |_________________|
                                                 |________________|

In this example:

  • Input width = 13
  • Filter width = 6
  • Stride = 5

Notes:

  • "VALID" only ever drops the right-most columns (or bottom-most rows).
  • "SAME" tries to pad evenly left and right, but if the amount of columns to be added is odd, it will add the extra column to the right, as is the case in this example (the same logic applies vertically: there may be an extra row of zeros at the bottom).

The TensorFlow Convolution example gives an overview about the difference between SAME and VALID :

  • For the SAME padding, the output height and width are computed as:

    out_height = ceil(float(in_height) / float(strides[1]))

    out_width = ceil(float(in_width) / float(strides[2]))

And

  • For the VALID padding, the output height and width are computed as:

    out_height = ceil(float(in_height - filter_height + 1) / float(strides1))

    out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))



  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值