【发布时间】:2018-08-30 14:46:02
【问题描述】:
我有一个自定义数据集,然后我将其存储为 tfrecord,这样做
# toy example data
label = np.asarray([[1,2,3],
[4,5,6]]).reshape(2, 3, -1)
sample = np.stack((label + 200).reshape(2, 3, -1))
def bytes_feature(values):
"""Returns a TF-Feature of bytes.
Args:
values: A string.
Returns:
A TF-Feature.
"""
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[values]))
def labeled_image_to_tfexample(sample_binary_string, label_binary_string):
return tf.train.Example(features=tf.train.Features(feature={
'sample/image': bytes_feature(sample_binary_string),
'sample/label': bytes_feature(label_binary_string)
}))
def _write_to_tf_record():
with tf.Graph().as_default():
image_placeholder = tf.placeholder(dtype=tf.uint16)
encoded_image = tf.image.encode_png(image_placeholder)
label_placeholder = tf.placeholder(dtype=tf.uint16)
encoded_label = tf.image.encode_png(image_placeholder)
with tf.python_io.TFRecordWriter("./toy.tfrecord") as writer:
with tf.Session() as sess:
feed_dict = {image_placeholder: sample,
label_placeholder: label}
# Encode image and label as binary strings to be written to tf_record
image_string, label_string = sess.run(fetches=(encoded_image, encoded_label),
feed_dict=feed_dict)
# Define structure of what is going to be written
file_structure = labeled_image_to_tfexample(image_string, label_string)
writer.write(file_structure.SerializeToString())
return
但是我看不懂。首先我尝试了(基于http://www.machinelearninguru.com/deep_learning/tensorflow/basics/tfrecord/tfrecord.html、https://medium.com/coinmonks/storage-efficient-tfrecord-for-images-6dc322b81db4和https://medium.com/mostly-ai/tensorflow-records-what-they-are-and-how-to-use-them-c46bc4bbb564)
def read_tfrecord_low_level():
data_path = "./toy.tfrecord"
filename_queue = tf.train.string_input_producer([data_path], num_epochs=1)
reader = tf.TFRecordReader()
_, raw_records = reader.read(filename_queue)
decode_protocol = {
'sample/image': tf.FixedLenFeature((), tf.int64),
'sample/label': tf.FixedLenFeature((), tf.int64)
}
enc_example = tf.parse_single_example(raw_records, features=decode_protocol)
recovered_image = enc_example["sample/image"]
recovered_label = enc_example["sample/label"]
return recovered_image, recovered_label
我还尝试了转换 enc_example 并对其进行解码的变体,例如在 Unable to read from Tensorflow tfrecord file 中但是当我尝试评估它们时,我的 python 会话只是冻结并且没有输出或回溯。
然后我尝试使用急切执行来查看发生了什么,但显然它只与 tf.data API 兼容。但是据我了解,tf.data API 的转换是在整个数据集上进行的。 https://www.tensorflow.org/api_guides/python/reading_data 提到必须编写解码函数,但没有给出如何做到这一点的示例。我找到的所有教程都是为 TFRecordReader 制作的(对我不起作用)。
非常感谢任何帮助(指出我做错了什么/解释正在发生的事情/有关如何使用 tf.data API 解码 tfrecords 的指示)。
根据https://www.youtube.com/watch?v=4oNdaQk0Qv4 和https://www.youtube.com/watch?v=uIcqeP7MFH0 tf.data 是创建输入管道的最佳方式,因此我对学习这种方式非常感兴趣。
提前致谢!
【问题讨论】:
标签: tensorflow