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])
但是,我似乎找不到办法做到这一点。感谢您的帮助!
EDIT:
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() 的变量不行。