Reputation: 3448
I'm searching for a tensorflow python method to enlarge (resize) a tensor to double every element in each feature map along both axis e.g.:
([[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]])
I saw tf.tile and tf.pad but I can not figure out how to use this methods to get that result.
Thanks for any hint!
UPDATE:
Thanks to sygi for that helpful hint here is the shape independent solution that works in jupyter notebook using python3 kernel:
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()
results in
[[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]]
Upvotes: 2
Views: 4970
Reputation: 1038
Just use tf.image.ResizeMethod with Nearest Neighbor interpolation
array = tf.image.resize_images(old_array, (old_size*2, old_size*2),
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
Input to the method must be 4-D Tensor of shape [batch, height, width, channels] or 3-D Tensor of shape [height, width, channels].
https://www.tensorflow.org/api_docs/python/tf/image/resize
Upvotes: 0
Reputation: 4647
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)
You can get a shape of the a
tensor (if it's defined) using:
shape = a.get_shape().as_list()
Upvotes: 2