我正在寻找一种张量流python方法来放大(调整大小)张量,使每个特征映射中的每个元素沿着两个轴加倍,例如:
([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
=>
([[1, 1, 2, 2, 3, 3],
[1, 1, 2, 2, 3, 3],
[4, 4, 5, 5, 6, 6],
[4, 4, 5, 5, 6, 6],
[7, 7, 8, 8, 9, 9],
[7, 7, 8, 8, 9, 9]])
我看到tf.tile和tf.pad,但我无法弄清楚如何使用此方法来获得该结果。
感谢任何提示!
更新:
感谢sygi提供的有用提示,这是一个独立于形状的解决方案,使用python3内核在jupyter笔记本中工作:
import tensorflow as tf
import numpy as np
i = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
j = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
k = np.array([[1, 2],
[3, 4],
[5, 6]])
a = tf.placeholder(tf.int64, shape=(None, None))
a_shape = tf.shape(a)
b = tf.reshape(a, [a_shape[0], a_shape[1], 1])
c = tf.tile(b, [1, 1, 2])
d = tf.reshape(c, [a_shape[0], a_shape[1]*2])
e = tf.reshape(d, [a_shape[0], a_shape[1]*2, 1])
f = tf.tile(e, [1, 1, 2])
g = tf.transpose(f, [0, 2, 1])
h = tf.reshape(g, [a_shape[0]*2, a_shape[1]*2])
session = tf.InteractiveSession()
session.run(tf.initialize_all_variables())
print(h.eval(feed_dict={a: i}))
print(h.eval(feed_dict={a: j}))
print(h.eval(feed_dict={a: k}))
session.close()
结果
[[1 1 2 2 3 3]
[1 1 2 2 3 3]
[4 4 5 5 6 6]
[4 4 5 5 6 6]
[7 7 8 8 9 9]
[7 7 8 8 9 9]]
[[ 1 1 2 2 3 3 4 4]
[ 1 1 2 2 3 3 4 4]
[ 5 5 6 6 7 7 8 8]
[ 5 5 6 6 7 7 8 8]
[ 9 9 10 10 11 11 12 12]
[ 9 9 10 10 11 11 12 12]]
[[1 1 2 2]
[1 1 2 2]
[3 3 4 4]
[3 3 4 4]
[5 5 6 6]
[5 5 6 6]]
答案 0 :(得分:2)
a = tf.convert_to_tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
b = tf.reshape(a, [3, 3, 1])
c = tf.tile(b, [1, 1, 2])
d = tf.reshape(c, [3, 6])
print(d.eval())
array([[1, 1, 2, 2, 3, 3],
[4, 4, 5, 5, 6, 6],
[7, 7, 8, 8, 9, 9]], dtype=int32)
e = tf.reshape(d, [3, 6, 2])
f = tf.tile(e, [1, 1, 2])
g = tf.transpose(f, [0, 2, 1])
print(g.eval())
array([[[1, 1, 2, 2, 3, 3],
[1, 1, 2, 2, 3, 3]],
[[4, 4, 5, 5, 6, 6],
[4, 4, 5, 5, 6, 6]],
[[7, 7, 8, 8, 9, 9],
[7, 7, 8, 8, 9, 9]]], dtype=int32)
h = tf.reshape(g, [6, 6])
print(h.eval())
array([[1, 1, 2, 2, 3, 3],
[1, 1, 2, 2, 3, 3],
[4, 4, 5, 5, 6, 6],
[4, 4, 5, 5, 6, 6],
[7, 7, 8, 8, 9, 9],
[7, 7, 8, 8, 9, 9]], dtype=int32)
您可以使用以下方式获得a
张量的形状(如果已定义):
shape = a.get_shape().as_list()
答案 1 :(得分:0)
只需将tf.image.ResizeMethod与最近邻插值一起使用
array = tf.image.resize_images(old_array, (old_size*2, old_size*2),
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
方法的输入必须是形状为[批处理,高度,宽度,通道]的4-D张量或形状为[高度,宽度,通道]的3-D张量。