Python TensorFlow如何用边值填充tensor
如何用边值填充张量(尺寸为WxHxC) 例如:Python TensorFlow如何用边值填充tensor,python,tensorflow,padding,Python,Tensorflow,Padding,如何用边值填充张量(尺寸为WxHxC) 例如: [1, 2, 3] [4, 5, 6] [7, 8, 9] 变成: [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] 使用和模式“对称”-它将反映边上的值,但如果只进行1个深度填充,则相当于重复边值。如果需要更多的填充,则必须重复该操作,但可以按指数(先是1,然后是2,然后是4,以此类推)。此代码(已测试): 将tensorflow导入
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
变成:
[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]
使用和模式“对称”-它将反映边上的值,但如果只进行1个深度填充,则相当于重复边值。如果需要更多的填充,则必须重复该操作,但可以按指数(先是1,然后是2,然后是4,以此类推)。此代码(已测试):
将tensorflow导入为tf
a=tf.重塑(tf.常数(范围(1,10)),(3,3))
b=tf.pad(a,[[1,1],[1,1]],“对称”)
使用tf.Session()作为sess:
打印(sess.run(b))
产出:
[[1 2 3][1 2 3 3]
[4 4 5 6]
[7 7 8 9]
[7 7 8 9]] 根据需要。使用和模式“对称”-它将反映边缘上的值,但如果只进行1次深度填充,则相当于重复边缘值。如果需要更多的填充,则必须重复该操作,但可以按指数(先是1,然后是2,然后是4,以此类推)。此代码(已测试):
将tensorflow导入为tf
a=tf.重塑(tf.常数(范围(1,10)),(3,3))
b=tf.pad(a,[[1,1],[1,1]],“对称”)
使用tf.Session()作为sess:
打印(sess.run(b))
产出:
[[1 2 3][1 2 3 3]
[4 4 5 6]
[7 7 8 9]
[7 7 8 9]]
根据需要。作为补充,如果您想使用复制模式(如opencv)填充图像,以下方法可以做到,dst_图像就是要填充的图像。pad_h_向上,pad_向下,pad_w_向左,pad_w_向右,是四个参数:
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)
)
作为补充,如果您想使用复制模式(如opencv)填充图像,下面的方法可以做到,dst_图像就是要填充的图像。pad_h_向上,pad_向下,pad_w_向左,pad_w_向右,是四个参数:
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)
)
伟大的谢谢!:)当然,不客气!增加了另一句话,告诉大家如果你需要更多的填充物该怎么做。太好了!谢谢!:)当然,不客气!增加了另一句话,让你知道如果你需要更多的填充物该怎么做。