【发布时间】:2019-03-12 15:50:10
【问题描述】:
我目前正在尝试从本地存储的一些 .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()) }
?
提前致谢
【问题讨论】:
标签: python tensorflow machine-learning data-analysis tfrecord