将图像保存为 TFRecord 文件中的 protobuf,然后将其读回并在屏幕上显示图像

Saving an image as a protobuf in a TFRecord file and then reading it back and showing the image on screen

我想执行以下操作:在 tensorflow 中使用 JPEG 格式对图像进行编码,将其放入 protobuff 中的 BytesList 特征中,序列化,保存,然后读取它又回来了。读取后,我必须使用 feature_description 对图像进行解析,然后从 JPEG 格式解码图像。这是我试过的:

from sklearn.datasets import load_sample_images
from tensorflow.train import BytesList, FloatList, Int64List
from tensorflow.train import Feature, Features, Example
import tensorflow as tf
import numpy as np
import matplotlib as plt


# Loading the image and printing it
img = load_sample_images()["images"][0]
plt.imshow(img)
plt.axis("off")
plt.title("Original Image")
plt.show()

# Encode the image to JPEG
data = tf.io.encode_jpeg(img)

# Convert it to a protobuf Example
example_with_image = Example(
    features = Features(
        feature = {
            "image": Feature(bytes_list = BytesList(value = [data.numpy()]))
        }
    )
)

# Serialize the protobuf Example to a string
serialized_example = example_with_image.SerializeToString()

# Imagine we saved 'serialized_example' to disk and read it back into memory
# We now want to print the image

# Provide 'feature_description' so that the parse function knows the default 
# value
feature_description = {
    "image": tf.io.VarLenFeature(tf.string)
}

# Parse the serialized string
example_with_image = tf.io.parse_single_example(serialized_example, feature_description)

这一切都很好。然后我尝试使用 Tensorflow 的 decode_jpeg() 函数解码图像:

decoded_img = tf.io.decode_jpeg(example_with_image)

这是行不通的。我得到以下 ValueError:

ValueError: Attempt to convert a value ({'image': <tensorflow.python.framework.sparse_tensor.SparseTensor object

at 0x000002B4C90AB9D0>}) with an unsupported type (<class 'dict'>) to a Tensor.

它也不适用于更通用的 tf.io.decode_image() Tensorflow 函数。

老实说,我不知道发生了什么。我不应该找回图像吗?怎么了?

example_with_image使用parse_single_example后是一个字典,key为image,稀疏tensor为value

example_with_image 看起来像这样:

{'image': <tensorflow.python.framework.sparse_tensor.SparseTensor at 0x25b29440cc8>}

decode_jpeg 函数需要字节值,但您提供的是字典。

提取值的正确方法是:
代码:

image = tf.io.decode_jpeg(example_with_image1['image'].values.numpy()[0])
plt.imshow(image)

输出:

您还可以将图像解析为 FixedLenFeature 而不是 VarLenFeature。在这种情况下,您得到的是密集张量而不是稀疏张量。

代码:

feature_description = {
    "image": tf.io.FixedLenFeature([], tf.string)
}

# Parse the serialized string
example_with_image = tf.io.parse_single_example(serialized_example, feature_description)

image = tf.io.decode_jpeg(example_with_image['image'].numpy())
plt.imshow(image)