ValueError: All shapes must be fully defined. Issue due to commenting out tf.random_crop

ValueError: All shapes must be fully defined. Issue due to commenting out tf.random_crop

这个问题在某种程度上是

的延伸

我使用以下代码将我的图像转换成特定形状:

height = tf.cast(features['height'],tf.int32)
width = tf.cast(features['width'],tf.int32)
image = tf.reshape(image,tf.pack([height, width, 3]))

在 cifar10_input 代码中,图像被以下内容扭曲,其中 IMAGE_SIZE = 32:

height = IMAGE_SIZE
width = IMAGE_SIZE
distorted_image = tf.random_crop(image, [height, width, 3])

但是,就我的目的而言,我现在不需要进行随机裁剪。因此,我将该行替换为:

distorted_image = image

当我这样做时,它抛出以下错误:

Traceback (most recent call last):
  File "cnn_train.py", line 128, in <module>
    tf.app.run()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/default/_app.py", line 30, in run
    sys.exit(main(sys.argv))
  File "cnn_train.py", line 124, in main
    train()
  File "cnn_train.py", line 56, in train
    images, labels = cnn.distorted_inputs()
  File "/home/samuelchin/tensorflow/my_code/CNN/cnn.py", line 123, in distorted_inputs
    batch_size=BATCH_SIZE)
  File "/home/samuelchin/tensorflow/my_code/CNN/cnn_input.py", line 128, in distorted_inputs
    min_queue_examples, batch_size)
  File "/home/samuelchin/tensorflow/my_code/CNN/cnn_input.py", line 70, in _generate_image_and_label_batch
    min_after_dequeue=min_queue_examples)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/input.py", line 494, in shuffle_batch
    dtypes=types, shapes=shapes)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/data_flow_ops.py", line 404, in __init__
    shapes = _as_shape_list(shapes, dtypes)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/data_flow_ops.py", line 70, in _as_shape_list
    raise ValueError("All shapes must be fully defined: %s" % shapes)
ValueError: All shapes must be fully defined: [TensorShape([Dimension(None), Dimension(None), Dimension(None)]), TensorShape([])]

我有两个问题:

  1. 为什么当我不执行 tf.random_crop 时它会抛出错误?在我看来,好像 tf.random_crop returns 与图像完全不同的东西。
  2. 是不是简单的把IMAGE_SIZE设置成我想要好的解决方案的大小?例如,如果图像为 32 x 32,我想将其裁剪为 24 x 24,我将设置 IMAGE_SIZE = 24。现在,既然我希望它为 32 x 32,我是否应该简单地设置 IMAGE_SIZE = 32?

因为您是动态生成图像,包括从 tf 记录文件动态提取高度和宽度,TensorFlow 不知道生成图像的形状。管道中的许多后续操作需要能够在 Python 执行时确定形状。

tf.random_crop 的附带效果是将图像大小设置为已知的固定大小,并使其形状暴露以供后续处理。

您可以直接将图像切成您想要的大小而不是random_crop,但是您需要执行一些操作才能将图像变成固定的-大小的东西。如果你希望它是 32x32 并且你知道你的输入高度和宽度是 32x32,那么你可以只对它做 set_shape(但你最好是正确的) .否则,您可以裁剪 and/or 调整到您想要的大小。