我正在尝试使用TensorFlow读取图像分类问题的某些图像输入。TensorFlow将图像张量调整为动态图形
当然,我正在用tf.image.decode_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)
我现在的问题是我无法通过new_height
和new_width
到tf.image.resize_images(...)
,因为他们中的一个是张量和resize_images
预计整数作为高度和宽度的投入。
有没有办法“拉出”张量的整数或是否有任何其他方式与TensorFlow做我的任务?
在此先感谢。
编辑
因为我也有some other issues与tf.image.resize_images
,这里是为我工作的代码:
shape = tf.shape(image)
height = shape[0]
width = shape[1]
new_shorter_edge = tf.constant(400, dtype=tf.int32)
height_smaller_than_width = tf.less_equal(height, width)
new_height_and_width = tf.cond(
height_smaller_than_width,
lambda: (new_shorter_edge, _compute_longer_edge(height, width, new_shorter_edge)),
lambda: (_compute_longer_edge(width, height, new_shorter_edge), new_shorter_edge)
)
image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(image, tf.pack(new_height_and_width))
image = tf.squeeze(image, [0])
这是非常好的,很酷的功能。但是我的问题依然存在。 'tf.image.resize_images(...)'接受'int32'作为第二个和第三个参数。这就是'new_height'和'new_width'的值应该去的地方。 在我对TensorFlow的理解中,对'eval()'的调用不起作用,因为这只在运行时进行评估。是否有任何命令告诉TensorFlow“在图建设时间”拉出张量的第一个(也是唯一的)整数? – mackcmillion
调用'tf.image.resize_images(image,new_height,new_width)'总是抛出TypeError:期望的int32,得到包含'_Message'类型的张量的列表。' – mackcmillion
啊,这对我来说似乎是一个错误。我提交了[问题](https://github.com/tensorflow/tensorflow/issues/1001),并会尽快修复。 – mrry