如何使用Tensorflow处理不同大小的图像?

时间:2016-08-17 22:27:01

标签: tensorflow

我需要像卷积操作一样逐个像素地处理图像。但是,当我按照Tensorflow进行操作时,

height = tf.shape(img)[0]
width = tf.shape(img)[1]
for h in range(height):
    for w in range(width):
        patch = img[h:h+filter_sz, w:w+filter_sz, :]

我收到错误:“不允许使用tf.Tensor作为Python bool。”由于图像大小不同,我在构造计算图时无法修复高度和宽度。我该怎么办?或者,为什么tf.nn.conv2d和tf.nn.max_pool可以处理不同大小的图像?

非常感谢!

1 个答案:

答案 0 :(得分:0)

tf.shape的输出是Tensor,这意味着它是懒惰的,这意味着在您调用sess.run之前不会计算它。要在定义图表时获得Tensor的形状,您应该调用Tensor.get_shape(),这将返回TensorShape。所以上面的代码应该改为:

height, width = img.get_shape()
for h in range(height):
    for w in range(width):
        patch = img[h:h+filter_sz, w:w+filter_sz, :]
相关问题