【发布时间】:2016-02-25 06:41:45
【问题描述】:
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
# Defaults are not specified since both keys are required.
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'depth': tf.FixedLenFeature([], tf.int64)
})
# height = tf.cast(features['height'],tf.int32)
image = tf.decode_raw(features['image_raw'], tf.uint8)
image = tf.reshape(image,[32, 32, 3])
image = tf.cast(image,tf.float32)
label = tf.cast(features['label'], tf.int32)
return image, label
我正在使用 TFRecord 来存储我的所有数据。函数 read_and_decode 来自 TensorFlow 提供的 TFRecords 示例。目前我通过预定义的值来重塑:
image = tf.reshape(image,[32, 32, 3])
但是,我现在将使用的数据具有不同的维度。例如,我可以有一个 [40, 30, 3] 的图像(缩放这不是一个选项,因为我不希望它被扭曲)。我想读入不同维度的数据,并在数据增强阶段使用 random_crop 来规避这个问题。我需要的是类似下面的东西。
height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)
image = tf.reshape(image,[height, width, 3])
但是,我似乎无法找到一种方法来做到这一点。感谢您的帮助!
编辑:
ValueError: All shapes must be fully defined: [TensorShape([Dimension(None), Dimension(None), Dimension(None)]), TensorShape([])]
image = tf.reshape(image, tf.pack([height, width, 3]))
image = tf.reshape(image, [32,32,3])
问题肯定出在这两行。硬编码的变量有效,但 tf.pack() 的变量无效。
【问题讨论】:
-
回复:编辑。看起来您正在使用一种图像操作,它需要在图形构建时知道所有形状(如裁剪或填充形状)。但是,这似乎与原始问题(关于从 TFRecords 中读取内容)无关,因此您应该提出一个关于如何处理这个问题的新问题。确保在错误消息中包含完整的堆栈跟踪!
-
@mrry,你说得对,是 tf.random_crop 导致了这个问题。根据您的建议,我在stackoverflow.com/questions/35691102/… 提出了一个新问题。
标签: python tensorflow