问题:是否有一种附加现有TFRecord的方法?
注意: .TFRecord是由我自己的脚本创建的(不是我在网上找到的.tfrecord ),所以我完全控制了它的内容。
发布于 2019-11-14 17:21:05
不可能附加到现有的记录文件本身,或者至少不可能通过TensorFlow提供的函数。记录文件是由一个C++级别的PyRecordWriter编写的,在创建函数NewWriteableFile时,它会删除任何具有给定名称的现有文件来创建一个新的文件。但是,可以使用另一个记录的内容创建一个新的记录文件,然后再创建新的记录。
对于TensorFlow 1.x,您可以这样做:
import tensorflow as tf
def append_records_v1(in_file, new_records, out_file):
with tf.io.TFRecordWriter(out_file) as writer:
with tf.Graph().as_default(), tf.Session():
ds = tf.data.TFRecordDataset([in_file])
rec = ds.make_one_shot_iterator().get_next()
while True:
try:
writer.write(rec.eval())
except tf.errors.OutOfRangeError: break
for new_rec in new_records:
writer.write(new_rec)在TensorFlow 2.x (急切执行)中,您可以这样做:
import tensorflow as tf
def append_records_v2(in_file, new_records, out_file):
with tf.io.TFRecordWriter(out_file) as writer:
ds = tf.data.TFRecordDataset([in_file])
for rec in ds:
writer.write(rec.numpy())
for new_rec in new_records:
writer.write(new_rec)https://stackoverflow.com/questions/58857079
复制相似问题