tensorflow conv2d padding,tf图像卷积边缘扩展问题

初学tensorflow的conv2d的时候,一般书上会说conv2d的扩展可以选择两种,SAME和VALID。这两种要么导致图像变小(valid),要么导致边缘变黑(same),因为边缘只补0。曾一度以为tensorflow太low。查了很久,终于在stackoverflow上发现一个对tf.pad()函数的介绍,该函数就是扩展图像边缘的。在python交互环境下输入help(tf.add)便能看到该函数的用法。其实这个功能和matlab基本能一一对应。在matlab中一般对图像卷积,不想缩小图像,边界又不能变黑的方法都是conv2(padarray(img,[a,b]),h,'valid')。基本思路就是先对图像进行symmetric形式的扩充(复制外边缘值),然后在使用valid方式的卷积。在tensorflow中依然是这样的函数。先使用tf.pad对图像进行symmetric形式的扩充,再用conv2d卷积。担忧多日的问题终于解决了。下面是我help(tf.pad)得到的函数介绍



>>> help(tf.pad)
Help on function pad in module tensorflow.python.ops.array_ops:


pad(tensor, paddings, mode='CONSTANT', name=None)
    Pads a tensor.


    This operation pads a `tensor` according to the `paddings` you specify.
    `paddings` is an integer tensor with shape `[n, 2]`, where n is the rank of
    `tensor`. For each dimension D of `input`, `paddings[D, 0]` indicates how
    many values to add before the contents of `tensor` in that dimension, and
    `paddings[D, 1]` indicates how many values to add after the contents of
    `tensor` in that dimension. If `mode` is "REFLECT" then both `paddings[D, 0]`
    and `paddings[D, 1]` must be no greater than `tensor.dim_size(D) - 1`. If
    `mode` is "SYMMETRIC" then both `paddings[D, 0]` and `paddings[D, 1]` must be
    no greater than `tensor.dim_size(D)`.


    The padded size of each dimension D of the output is:


    `paddings[D, 0] + tensor.dim_size(D) + paddings[D, 1]`


    For example:


    ```python
    # 't' is [[1, 2, 3], [4, 5, 6]].
    # 'paddings' is [[1, 1,], [2, 2]].
    # rank of 't' is 2.
    pad(t, paddings, "CONSTANT") ==> [[0, 0, 0, 0, 0, 0, 0],
                                      [0, 0, 1, 2, 3, 0, 0],
                                      [0, 0, 4, 5, 6, 0, 0],
                                      [0, 0, 0, 0, 0, 0, 0]]


    pad(t, paddings, "REFLECT") ==> [[6, 5, 4, 5, 6, 5, 4],
                                     [3, 2, 1, 2, 3, 2, 1],
                                     [6, 5, 4, 5, 6, 5, 4],
                                     [3, 2, 1, 2, 3, 2, 1]]


    pad(t, paddings, "SYMMETRIC") ==> [[2, 1, 1, 2, 3, 3, 2],
                                       [2, 1, 1, 2, 3, 3, 2],
                                       [5, 4, 4, 5, 6, 6, 5],
                                       [5, 4, 4, 5, 6, 6, 5]]
    ```


    Args:
      tensor: A `Tensor`.
      paddings: A `Tensor` of type `int32`.
      mode: One of "CONSTANT", "REFLECT", or "SYMMETRIC" (case-insensitive)
      name: A name for the operation (optional).


    Returns:
      A `Tensor`. Has the same type as `tensor`.


    Raises:
      ValueError: When mode is not one of "CONSTANT", "REFLECT", or "SYMMETRIC".


>>>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值