【发布时间】:2016-03-13 19:36:31
【问题描述】:
在尝试使用 TFRecords 格式时,我在设置图像数据的形状时遇到了问题。我一直在查看how-to for reading data 并从MNIST 示例中获取converting the image data to a TFRecords 和reading the data from the TFRecords 的代码。但是,此示例代码最初希望以所有像素数据都在一个长向量中的格式使用图像。
我一直在尝试更改此代码以使用仍处于原始图像形状的 NumPy 数组。所以在我下面的代码中,images 是一个形状为[number_of_images, height, width, channels] 的 NumPy 数组。我不确定我的问题是否在于我如何将数据写入 TFRecords 或者我如何将其读回。但是,当我尝试设置解码图像的形状时,我收到错误ValueError: Shapes (?,) and (464, 624, 3) must have the same rank(注意:464 x 624 x 3 是图像尺寸)。关于我可能做错的任何建议?
相关代码(与示例代码略有改动):
def convert_to_tfrecord(images, labels, name, data_directory):
number_of_examples = labels.shape[0]
rows = images.shape[1] # images is the 4D ndarray with the images in their original shape.
cols = images.shape[2]
depth = images.shape[3]
...
for index in range(number_of_examples):
image_raw = images[index].tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'height': _int64_feature(rows),
'width': _int64_feature(cols),
'channels': _int64_feature(depth),
'image': _bytes_feature(image_raw),
...
}))
writer.write(example.SerializeToString())
...
def read_and_decode(filename_queue):
...
features = tf.parse_single_example(
serialized_example,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
...
})
...
image = tf.decode_raw(features['image_raw'], tf.uint8)
image.set_shape([464, 624, 3]) # This is where the error occurs.
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
...
【问题讨论】:
标签: python numpy tensorflow