我正在尝试使用tensorflows格式来存储我的数据集。
我设法读取了jpeg图像,并将其解码为raw格式,然后将其写入tfrecord文件。然后,我可以稍后使用tf.decode_raw读取它们。
问题是,这会导致巨大的文件大小,因为我将图像存储为原始图像。现在我已经看到很多教程和博客说我可以用编码格式存储它们,然后在阅读它们时只需解码它们。我找不到任何这样的例子。我已经尝试了一段时间,但无论我用哪种方法,我都会遇到格式化错误。
TLDR有人知道如何将图片以jpeg格式而不是raw格式写入tfrecord文件吗?
谢谢,大卫。
我的写作功能。
def convert(image_paths, labels, out_path):
num_images = len(image_paths)
with tf.python_io.TFRecordWriter(out_path) as writer:
for i, (path, label) in enumerate(zip(image_paths, labels)):
print_progress(count=i, total=num_images-1)
img = open(path, 'rb').read()
data ={'image': wrap_bytes(img),
'label': wrap_int64(label)}
feature = tf.train.Features(feature=data)
example = tf.train.Example(features=feature)
serialized = example.SerializeToString()
writer.write(serialized)使用以下命令转换数据集:
{convert(image_paths=image_paths_train,
labels=cls_train,
out_path=path_tfrecords_train)}我的阅读功能
def parse(serialized):
features = \
{
'image': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64)
}
parsed_example = tf.parse_single_example(serialized=serialized,
features=features)
# Get the image as raw bytes.
image_raw = parsed_example['image']
# Decode the raw bytes so it becomes a tensor with type.
image = tf.image.decode_image(image_raw,channels=3)
#image = tf.decode_raw(image_raw, tf.uint8)
# The type is now uint8 but we need it to be float.
image = tf.cast(image, tf.float32)
# Get the label associated with the image.
label = parsed_example['label']
# The image and label are now correct TensorFlow types.
return image, label发布于 2018-01-19 23:07:48
对于写入,只需以二进制文件(fp = open('something.jpg', 'rb'))的形式打开文件并.read()其内容即可。在您现在存储图像时,将该内容存储在in记录Example中(即,作为字节特征)。
对于读取,使用tf.image.decode_image并传入从示例阅读器获得的张量,而不是执行decode_raw。
如果你发布你的代码,我可以提供更好的代码示例,但不知道你的代码看起来如何,这是我能得到的最详细的信息。
https://stackoverflow.com/questions/48343900
复制相似问题