简体   繁体   中英

How do I resize image with unknown size in Tensorflow(tf.shape(input) method doesn't work)

According to this post , one can use tf.shape() to resize image with unknown size like placeholder. But the method doesn't seem to work for me. I have some simple code that looks like:

import tensorflow as tf
import numpy as np

def speed_tune(x, lower_bound=0.8, upper_bound=2.0):
    speed_rate = np.random.uniform(lower_bound, upper_bound)
    newshape = tf.shape(x)[1:] # get the tensor shape except for rank 0(None)
    newshape *= speed_rate # randomly stretch or compress the signal 
    return tf.resize(x, newshape)

sess = tf.InteractiveSession()
x = tf.placeholder(tf.int16, (None, 1000)) # x is a 1D audio signal
y = speed_tune(x)
data = np.random.randint(10, size=1000)
output = sess.run(y, feed_dict={x:data})

Basically, my code does the following: Given an input 1D data x, the program tries to stretch or compress the sequence by some random factor and return the tuned sequence. Since I didn't find any Tensorflow function that directly performs this operation, I use tf.resize by treating the data as 1xD image where D is the length of the signal. But I got an error:

Traceback (most recent call last):
  File "d:\SVNRepo\Python_codes\scratch.py", line 33, in <module>
    y = speed_tune(x)
  File "d:\SVNRepo\Python_codes\scratch.py", line 28, in speed_tune
    newshape *= speed_rate # randomly stretch or compress the signal 
TypeError: unsupported operand type(s) for *=: 'Tensor' and 'float'

So it seems like tf.shape(x) returns a Tensor rather than integer values that specify the shape of the tensor(verified by Tensorflow document ). How can I solve this?

Not sure what exactly are you looking for, but maybe this will help Used tf.random.uniform to avoid tensor/float operation

def speed_tune(x, lower_bound=1, upper_bound=2):
    speed_rate = tf.random.uniform([1,], lower_bound, upper_bound, dtype=tf.int32)
    newshape = tf.shape(x)[1:] # get the tensor shape except for rank 0(None)
    newshape = newshape * speed_rate # randomly stretch or compress the signal
    return tf.reshape(x, newshape)

Used tf.reshape , not sure what you meant by tf.resize

x = tf.placeholder(tf.int32, (None, 1000)) # x is a 1D audio signal
y = speed_tune(x)
data = np.random.rand(1, 1000)

with tf.Session() as sess:
    sess.run(y, feed_dict={x:data})

Another way is to use tf.pad : For example:

n = 10
tensor = tf.constant(np.random.rand(1, 10))
paddings = tf.constant([[0,1], [0,0]])

this exact pad setup means that you add n zeros in the end of tensor. In order to get initial dimension you reshape it

padded = tf.pad(tensor, paddings)
output  = tf.reshape(padded, [1,n*2])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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