gpt4 book ai didi

tensorflow - 由于注释掉 tf.random_crop 导致的 ValueError : All shapes must be fully defined. 问题

转载 作者:行者123 更新时间:2023-12-04 01:11:26 28 4
gpt4 key购买 nike

这个问题有点像 How can I use values read from TFRecords as arguments to tf.reshape? 的延伸

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

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([])]

我有两个问题:
  • 为什么当我不做 tf.random_crop 时会抛出错误?在我看来好像 tf.random_crop 返回的东西与图像完全不同。
  • 只是将 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 (但你最好是对的)。否则,您可以裁剪和/或调整到您想要的大小。

    关于tensorflow - 由于注释掉 tf.random_crop 导致的 ValueError : All shapes must be fully defined. 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35691102/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com