【问题标题】:open_sharded_output_tfrecords causes FailedPreconditionError Writer is not openopen_sharded_output_tfrecords 导致 FailedPreconditionError Writer is not open
【发布时间】: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


【解决方案1】:

问题是因为编写器在with 语句完成后退出:

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)

这使得插入超出了tf_record_close_stack 约束。因此,只需将以下代码保留在with 语句中,否则您必须将with contextlib2.ExitStack() 替换为tf_record_close_stack = contextlib2.ExitStack() 变量并在通过tf_record_close_stack.close() 完成for 循环后将其关闭。

【讨论】:

    猜你喜欢
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多