【发布时间】:2022-06-21 13:47:08
【问题描述】:
我正在尝试主要按照教程实施 Tensorflow 检测 API,但在尝试生成 TFRecord 时遇到了问题。
我已经到了生成 tfexample 并希望将它们写入 tfrecord 文件列表的地步。我见过couple examples 使用这样的open_sharded_output_tfrecords 函数:
with contextlib2.ExitStack() as tf_record_close_stack:
output_records = tf_record_creation_util.open_sharded_output_tfrecords(
tf_record_close_stack, FLAGS.output_file, FLAGS.num_shards)
这会返回一个 TFRecords 编写器列表,以后可以像这样使用:
output_records[shard_index].write(tf_example)
其中shard_index 是一个整数,tf_example 是 tfexample。
当我尝试实现它时,我收到了一个错误(请参阅底部的完整报告)。
FailedPreconditionError: Writer 已关闭。
任何想法或提示我可能在 open_sharded_output_tfrecords 上做错了什么以及如何纠正它?
非常感谢您的任何帮助。
这是我的代码:
def convert_to_tfrecord_error(df,output_folder,num_shards):
import contextlib2
from object_detection.dataset_tools import tf_record_creation_util
#Step 1: Initialize utils for sharded
with contextlib2.ExitStack() as tf_record_close_stack:
output_tfrecords = tf_record_creation_util.open_sharded_output_tfrecords(
tf_record_close_stack, output_folder_test, num_shards)
image_nr = 0
#Step 2: Write record to shard
for index,_ in df.iterrows():
#generate the example
tf_example = generate_tf_example(df,index)
#get the shard
shard_index = image_nr % num_shards
#write to shard
output_tfrecords[shard_index].write(tf_example)
#update image number
image_nr = image_nr +1
#notify after 100 images
if image_nr%100 == 0:
print(f"{image_nr} images written")
【问题讨论】:
-
写完后有没有试过打电话给
output_tfrecords[shard_index].close()?
标签: python tensorflow object-detection-api