Tensorflow: Tensor.set_shape() ValueError: 'image' must be fully defined

Tensorflow: Tensor.set_shape() ValueError: 'image' must be fully defined

我想使用tf.image.resize_image_with_crop_or_pad裁剪输入图像的一部分。但是出现错误:ValueError: 'image' must be fully defined。我检查了 Why do I get ValueError('\'image\' must be fully defined.') when transforming image in Tensorflow? 我添加了 Tensor.set_shape() 但它也无法工作。 我列出我的代码和错误如下:

example = tf.image.decode_png(file_contents, channels=3)
example.set_shape = ([256,256,3])
crop_image = tf.image.resize_image_with_crop_or_pad(example, crop_size, crop_size)

错误:

File "/home/kang/Documents/work_code_PC1/VGG_tensorflow_UCMerced/readUClandUsedImagetxt.py", line 97, in _input_pipeline
    crop_image = tf.image.resize_image_with_crop_or_pad(image, crop_size, crop_size)

  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/image_ops.py", line 534, in resize_image_with_crop_or_pad
    _Check3DImage(image, require_static=True)

  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/image_ops.py", line 221, in _Check3DImage
    raise ValueError('\'image\' must be fully defined.')

ValueError: 'image' must be fully defined.

我不知道为什么我将某个形状设置为图像也会出现错误。 但是,我这样测试代码:

example = tf.image.decode_png(file_contents, channels=3)
example = tf.reshape(example, [256,256,3])
crop_image = tf.image.resize_image_with_crop_or_pad(example, crop_size, crop_size)

有效。我认为重塑为相同的形状不会改变 Tensor 中值的顺序,对吗?也许它可以是解决方案。

问题出在行

example.set_shape = ([256,256,3])

您正在覆盖方法 tf.Tensor.set_shape 并将其设置为一个值。

set_shape是一个方法,所以你必须正确调用它:

example.set_shape([256,256,3])

之后,您的代码将起作用。

I think reshape to the same shape does not change the order of values in Tensor, am I right?

是的,你是对的