TensorFlow - 在读取和写入 TFRecords 文件时设置图像的形状?
TensorFlow - Setting the shape of an image while reading and writing a TFRecords file?
我在尝试使用 TFRecords 格式时 运行 在设置图像数据的形状时遇到了麻烦。我一直在研究 how-to for reading data and have taken the code from the MNIST example for converting the image data to a TFRecords and 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
...
请注意,set_shape
不会更改底层缓冲区的形状,它只会设置图形级别的注释,说明将在此张量中看到的一组可能的形状。
要更改您需要使用的实际形状tf.reshape
我在尝试使用 TFRecords 格式时 运行 在设置图像数据的形状时遇到了麻烦。我一直在研究 how-to for reading data and have taken the code from the MNIST example for converting the image data to a TFRecords and 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
...
请注意,set_shape
不会更改底层缓冲区的形状,它只会设置图形级别的注释,说明将在此张量中看到的一组可能的形状。
要更改您需要使用的实际形状tf.reshape