如何将编码的jpeg作为字节写入Tensorflow tfrecord然后读取它?

时间:2018-01-19 15:00:52

标签: tensorflow jpeg encode tfrecord

我正在尝试使用tensorflows tfrecords格式来存储我的数据集。

我设法读取jpeg图像并将它们解码为原始格式并将它们写入tfrecord文件。然后我可以使用tf.decode_raw读取它们。

问题在于这导致了巨大的文件大小,因为我将图像存储为原始图像。现在我看到很多教程和博客说我可以用编码格式存储它们,然后在阅读它们时只需解码它们。我无法找到任何这方面的例子。我已经尝试了一段时间,但无论我采用何种方式,我都会遇到格式错误。

TLDR 有没有人知道如何将图像写为tfrecord文件作为jpegs而不是原始。

谢谢你, 大卫。

我的写作功能。

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

1 个答案:

答案 0 :(得分:1)

编写时,只需将文件作为二进制文件(fp = open('something.jpg', 'rb'))和.read()内容打开即可。在您存储图像时将该内容存储在tfrecord Example中(即作为字节功能)。

要阅读,请使用decode_raw而不是tf.image.decode_image,然后传入样本阅读器中的张量。

如果你发布你的代码,我可以提供更好的代码示例,但不知道你的代码是怎样的,这是我能得到的详细信息。

相关问题