zoukankan      html  css  js  c++  java
  • 『TensorFlow』pad图片

     tf.pad()文档如下,

    pad(tensor, paddings, mode='CONSTANT', name=None, constant_values=0)
        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]`

     实际使用注意,参数paddings元素数(rank)必须和输入维度一一对应,表示该维度前后填充的层数,文档示例验证如下,

    import tensorflow as tf
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    
    t = tf.constant([[1, 2, 3], [4, 5, 6]])
    paddings = tf.constant([[1, 1,], [2, 2]])
    # 'constant_values' is 0.
    # rank of 't' is 2.
    res = tf.pad(t, paddings, "CONSTANT", constant_values=1)  # [[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]]
    
    
    print(sess.run(res))
    
    '''
    tf.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]]
    
    tf.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]]
    '''
    
  • 相关阅读:
    实战DeviceIoControl 之中的一个:通过API訪问设备驱动程序
    hibernate官方新手教程 (转载)
    C++ 清空消息队列
    java中接口的定义与实现
    Scrum 学习笔记
    Ubuntu中全然卸载Nginx
    ScrollView 在嵌套 ViewPager 时出现的问题
    Java的递归算法
    Android GPS获取当前经纬度坐标
    【数据结构】——排序算法——1.1、直接插入排序
  • 原文地址:https://www.cnblogs.com/hellcat/p/8574217.html
Copyright © 2011-2022 走看看