sh1ng
sh1ng

Reputation: 2973

Tensorflow upsample tensor of arbitrary size

Let's say I have a tensor with shape

[d0, d1,.., dn]

Is it possible to create a function that will up-sample only a certain dimensions k times? An example

[[1,2],[3,4]]

if I apply it for dimension 2 with repetition factor 3

[[1,1,1,2,2,2],[3,3,3,4,4,4]]

I know there's an tf.image.resize but it requires a tensor of a certain shape.

Upvotes: 0

Views: 628

Answers (1)

LI Xuhong
LI Xuhong

Reputation: 2356

How about tf.concat?

This is what I think: if the input's shape is [d1, d2, ..., dn], then output's shape should be [d1, d2, ..., dn*3]. If I am right, the code below may solve your problem.

import tensorflow as tf
import numpy as np


def repetition(a, factor):
    # get a's shape as a list
    shape = a.get_shape().as_list()
    concat_shape = shape + [1]

    # new shape
    shape[-1] *= factor

    a = tf.reshape(a, concat_shape)
    b = tf.reshape(tf.concat([a] * factor, -1), shape)

    return b

d1 = 2
d2 = 3
d3 = 4

np_a = np.random.rand(d1, d2, d3)
a = tf.constant(np_a)

b = repetition(a, 3)


sess = tf.Session()

print np_a
print sess.run(b)

The key is that tf.concat can indicate which axis it will concatenate the tensor.

Upvotes: 1

Related Questions