[英]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. 根据这篇文章 ,可以使用tf.shape()调整大小未知的图像,如占位符。 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. 基本上,我的代码执行以下操作:给定输入1D数据x时,程序将尝试通过某种随机因素来拉伸或压缩序列,并返回已调整的序列。 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. 由于找不到任何直接执行此操作的Tensorflow函数,因此我将tf.resize视为1xD图像,其中D是信号的长度。 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 ). 所以看起来tf.shape(x)
返回一个Tensor而不是指定张量形状的整数值(由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 不确定您要寻找的是什么,但这也许可以帮助tf.random.uniform
避免张量/浮点运算
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
使用过tf.reshape
,不确定您的意思是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: 另一种方法是使用tf.pad
:例如:
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. 这种精确的填充设置意味着您在张量的末尾添加了n个零。 In order to get initial dimension you reshape it 为了获得初始尺寸,您需要对其进行重塑
padded = tf.pad(tensor, paddings)
output = tf.reshape(padded, [1,n*2])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.