【发布时间】:2016-05-14 12:37:31
【问题描述】:
我正在尝试使用 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])
【问题讨论】:
标签: python image shape tensorflow