这个问题在某种程度上是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([])]
我有两个问题:
答案 0 :(得分:6)
因为您动态生成图像,包括拉出高度和图像。从tf记录文件动态调整宽度,TensorFlow不知道生成图像的形状。管道中的许多后来的操作都需要能够在Python执行时确定形状。
tf.random_crop
具有将图像大小设置为已知固定大小并使其形状暴露以供后续处理的附带效果。
您可以将图像切片到所需的大小而不是执行random_crop,但是您需要执行一些操作以将图像转换为固定大小的图像。如果你想要它是32x32 并且你知道你的输入高度和宽度是32x32 ,那么你可以在它上面做set_shape(但你最好是对的)。否则,您可以裁剪和/或调整大小到您想要的大小。