简体   繁体   English

TensorFlow 如何用边缘值填充张量

[英]TensorFlow how to pad tensor with the edge values

How can I pad a tensor (with dimension WxHxC) with the edge values?如何使用边缘值填充张量(尺寸为 WxHxC)?

For example:例如:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

becomes:变成:

[1, 1, 2, 3, 3]
[1, 1, 2, 3, 3]
[4, 4, 5, 6, 6]
[7, 7, 8, 9, 9]
[7, 7, 8, 9, 9]

Use tf.pad() and mode "SYMMETRIC" - it would reflect the values on the edge, but if you do only 1 depth padding, it's equivalent to repeating the edge value.使用tf.pad()和模式“SYMMETRIC”——它会反映边缘上的值,但如果你只做 1 个深度填充,它相当于重复边缘值。 If you need more padding, you have to repeat the operation, but you can go exponentially (1 first, then 2, then 4, etc.).如果您需要更多填充,则必须重复该操作,但您可以按指数方式进行(首先是 1,然后是 2,然后是 4,等等)。 This code (tested):此代码(已测试):

import tensorflow as tf

a = tf.reshape( tf.constant( range( 1, 10 ) ), ( 3, 3 ) )
b = tf.pad( a, [ [ 1, 1 ], [ 1, 1 ] ], "SYMMETRIC" )

with tf.Session() as sess:
    print( sess.run( b ) )

Outputs:输出:

[[1 1 2 3 3] [[1 1 2 3 3]
[1 1 2 3 3] [1 1 2 3 3]
[4 4 5 6 6] [4 4 5 6 6]
[7 7 8 9 9] [7 7 8 9 9]
[7 7 8 9 9]] [7 7 8 9 9]]

as desired.如预期的。

As a complement, if you want to pad image with replicate mode like opencv, the following can do it, dst_image is the image to pad.作为补充,如果你想像opencv一样用replication模式填充图像,下面可以做到,dst_image是要填充的图像。 And pad_h_up, pad_h_down, pad_w_left,pad_w_right, is the four argument:而pad_h_up、pad_h_down、pad_w_left、pad_w_right,就​​是四个参数:

def pad_replica(image_pad, up,down, left, right):
    paddings_up = tf.constant([[1, 0],[0,0],[0,0]])
    paddings_down = tf.constant([[0, 1],[0,0],[0,0]])
    paddings_left = tf.constant([[0, 0],[1,0],[0,0]])
    paddings_right = tf.constant([[0, 0],[0, 1],[0 ,0]])
    i = tf.constant(0)
    c = lambda i,pad_len,pad_mode, image: tf.less(i, pad_len)
    def body(i,pad_len,pad_mode,image):
        i = i+1
        image = tf.pad(image, pad_mode,"SYMMETRIC")
        return [i, pad_len,pad_mode, image]
    [_, _, _, image_pad_up] = tf.while_loop(c, body, \
                                          [i, up, paddings_up, image_pad])
    i = tf.constant(0)
    [_, _, _, image_pad_down] = tf.while_loop(c, body, [i, down,paddings_down, image_pad_up])
    i = tf.constant(0)
    [_, _, _, image_pad_left] = tf.while_loop(c, body, [i, left, paddings_left, image_pad_down])
    i = tf.constant(0)
    [_, _, _, image_pad_right] = tf.while_loop(c, body, [i, right,paddings_right, image_pad_left])
    i = tf.constant(0)
    return image_pad_right
dst_image.set_shape([None, None, None])
dst_image = pad_replica(dst_image,\
             tf.cast(pad_h_up, tf.int32),\
            tf.cast(pad_h_down,tf.int32),\
            tf.cast(pad_w_left, tf.int32),\
            tf.cast(pad_w_right,tf.int32)
            )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM