以图像作为输入和目标的 tfRecords

tfRecords with images as inputs and targets

我目前正在尝试从本地存储的一些 .png 图像创建 tf.Records。

我在这方面看到的大多数示例都是针对分类任务的,其中目标值为 类。 我正在尝试构建一个 VAE,因此我的目标值也将是图像。

我找到了关于生成 tf.Records:

this 示例
# Converting the values into features
# _int64 is used for numeric values
def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

# _bytes is used for string/char values
def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

tfrecord_filename = 'something.tfrecords'

# Initiating the writer and creating the tfrecords file.
writer = tf.python_io.TFRecordWriter(tfrecord_filename)

# Loading the location of all files - image dataset
# Considering our image dataset has apple or orange
# The images are named as apple01.jpg, apple02.jpg .. , orange01.jpg .. etc.

images = glob.glob('data/*.jpg')
for image in images[:1]:
    img = Image.open(image)
    img = np.array(img.resize((32,32)))
label = 0 if 'apple' in image else 1
feature = { 'label': _int64_feature(label),'image': _bytes_feature(img.tostring()) }

# Create an example protocol buffer
example = tf.train.Example(features=tf.train.Features(feature=feature))

# Writing the serialized example.
writer.write(example.SerializeToString())

writer.close()

问题: 应该更改什么以将图像也保存为目标值?

是否变化:

feature = { 'label': _int64_feature(label),'image': _bytes_feature(img.tostring()) }

feature = { 'label': _bytes_feature(img.tostring()),'image': _bytes_feature(img.tostring()) }

?

提前致谢

我认为您可以在一个示例中只保存两张图片。而且通常 保存图像尺寸的好主意

features=tf.train.Features(feature={'height': _int64_feature(h),
                                    'width': _int64_feature(w),
                                    'channels': _int64_feature(c)
                                    'image_1': _bytes_feature(image1)
                                    'image_2': _bytes_feature(image2)
                                    }
                          ))
example = tf.train.Example(features=tf.train.Features(feature=feature))

编辑

如果我没猜错:

list = np.array([image_1, image_2,...image_n])
images = np.split(np.fromstring(list.tostring()), number_of_images)