[英]How do I resize image with unknown size in Tensorflow(tf.shape(input) method doesn't work)
根据这篇文章 ,可以使用tf.shape()调整大小未知的图像,如占位符。 但是该方法似乎不适用于我。 我有一些简单的代码,如下所示:
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})
基本上,我的代码执行以下操作:给定输入1D数据x时,程序将尝试通过某种随机因素来拉伸或压缩序列,并返回已调整的序列。 由于找不到任何直接执行此操作的Tensorflow函数,因此我将tf.resize视为1xD图像,其中D是信号的长度。 但我得到一个错误:
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'
所以看起来tf.shape(x)
返回一个Tensor而不是指定张量形状的整数值(由Tensorflow document验证)。 我该如何解决?
不确定您要寻找的是什么,但这也许可以帮助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)
使用过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})
另一种方法是使用tf.pad
:例如:
n = 10
tensor = tf.constant(np.random.rand(1, 10))
paddings = tf.constant([[0,1], [0,0]])
这种精确的填充设置意味着您在张量的末尾添加了n个零。 为了获得初始尺寸,您需要对其进行重塑
padded = tf.pad(tensor, paddings)
output = tf.reshape(padded, [1,n*2])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.