Python TensorFlow将图像张量调整为动态形状

Python TensorFlow将图像张量调整为动态形状,python,image,shape,tensorflow,Python,Image,Shape,Tensorflow,我试图阅读一些关于TensorFlow图像分类问题的图像输入 当然,我是用tf.image.decode\u jpeg(…)来做这件事的。我的图像大小可变,因此无法为图像张量指定固定的形状 但是我需要根据图像的实际大小来缩放图像。具体来说,我想将短边缩放为固定值,将长边缩放为保持纵横比的方式 我可以通过shape=tf.shape(image)获得特定图像的实际形状。我也能够为新的更长的边进行计算 shape = tf.shape(image) height = shape[0] width =

我试图阅读一些关于TensorFlow图像分类问题的图像输入

当然,我是用
tf.image.decode\u jpeg(…)
来做这件事的。我的图像大小可变,因此无法为图像张量指定固定的形状

但是我需要根据图像的实际大小来缩放图像。具体来说,我想将短边缩放为固定值,将长边缩放为保持纵横比的方式

我可以通过
shape=tf.shape(image)
获得特定图像的实际形状。我也能够为新的更长的边进行计算

shape = tf.shape(image)
height = shape[0]
width = shape[1]
new_shorter_edge = 400
if height <= width:
    new_height = new_shorter_edge
    new_width = ((width / height) * new_shorter_edge)
else:
    new_width = new_shorter_edge
    new_height = ((height / width) * new_shorter_edge)

实现这一点的方法是使用(目前处于试验阶段,但在下一版本中可用)*操作符。该操作符能够测试运行时计算的值,并基于该值执行两个分支中的一个

shape = tf.shape(image)
height = shape[0]
width = shape[1]
new_shorter_edge = 400
height_smaller_than_width = tf.less_equal(height, width)

new_shorter_edge = tf.constant(400)
new_height, new_width = tf.cond(
    height_smaller_than_width,
    lambda: new_shorter_edge, (width / height) * new_shorter_edge,
    lambda: new_shorter_edge, (height / width) * new_shorter_edge)
现在您有了
new\u height
new\u width
Tensor
值,它们将在运行时采用适当的值


*要访问当前发布版本中的操作员,您需要导入以下内容:

from tensorflow.python.ops import control_flow_ops

…然后使用
control\u flow\u ops.cond()
而不是
tf.cond()

这是一个非常好、很酷的特性。但我的问题仍然存在
tf.image.resize_images(…)
int32
作为第二个和第三个参数。这就是
new\u height
new\u width
的值应该去的地方。在我对TensorFlow的理解中,调用
eval()
将不起作用,因为这只在运行时进行评估。是否有任何命令告诉TensorFlow在创建图形时“拉出张量的第一个(也是唯一的)整数”?调用
tf.image.resize\u图像(图像,新高度,新宽度)
总是抛出
TypeError:Expected int32,改为获取包含“\u Message”类型张量的列表。
啊,这对我来说似乎是个bug。我已经提交了申请,很快就会得到解决。我看你已经解决了。非常感谢,这对我真的很有帮助:)
from tensorflow.python.ops import control_flow_ops