试图从多个tfrecord文件中读取数据

时间:2017-08-16 06:45:37

标签: python tensorflow

我有一个.tfrecords文件的文件夹,我想读入我的网络。但是,我在阅读多个tfrecords文件时遇到了很多麻烦。

我的所有tfrecords文件都存储在path_to_folders列表中。我的所有功能名称都是正确的。

我的代码如下所示:

with tf.Session() as sess:
    for folder in path_to_folders:

        try:
            no_grasps = int(len(os.listdir(folder)) / 5)

            feature_name_images = os.path.basename(folder) + '_images'
            feature_name_csv = os.path.basename(folder) + '_csv'

            data_path = os.path.join(path_to_data, os.path.basename(folder) + '.tfrecords')

            feature = {feature_name_images: tf.FixedLenFeature([], tf.string),
                       feature_name_csv: tf.FixedLenFeature([], tf.string)
                       }

            filename_queue = tf.train.string_input_producer([data_path], num_epochs=1)

            reader = tf.TFRecordReader()

            _, serialized_example = reader.read(filename_queue)

            features = tf.parse_single_example(serialized_example, features=feature)

            image_out = tf.decode_raw(features[feature_name_images], tf.float32)
            csv_out = tf.decode_raw(features[feature_name_csv], tf.float32)

            image_out_reshaped = tf.reshape(image_out, [no_grasps, 200, 200, 3])
            csv_out_reshaped = tf.reshape(csv_out, [no_grasps, 6])

            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())

            # Create a coordinator and run all QueueRunner objects
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)

            # image_dataset, csv_dataset = sess.run([image_out_reshaped, csv_out_reshaped])
            image_dataset = sess.run(image_out_reshaped)

            coord.request_stop()
            coord.join(threads)

            print(image_dataset.shape, type(image_dataset))

            time.sleep(2)

        except tf.errors.OutOfRangeError:
            print('epoch limit reached')

在完成第一次迭代读取(第一次成功读取tfrecords文件)之后,其余的人告诉我我的纪元限制已达到警告:

OutOfRangeError (see above for traceback): FIFOQueue '_2_input_producer_1' is closed and has insufficient elements (requested 1, current size 0)
     [[Node: ReaderReadV2_1 = ReaderReadV2[_device="/job:localhost/replica:0/task:0/cpu:0"](TFRecordReaderV2_1, input_producer_1)]]

我真的不明白为什么会这样,并且想知道是否有人可以帮助我。

由于

1 个答案:

答案 0 :(得分:0)

尝试将coord.request_stop()从您的主try块中移出,并向您的try中添加一个“ finally”块,如下所示:

    try:
        ...
        #coord.request_stop()
        coord.join(threads)

        print(image_dataset.shape, type(image_dataset))

        #time.sleep(2)

    except tf.errors.OutOfRangeError:
        print('epoch limit reached')
    finally:
        coord.request_stop()

您可能还想尝试将某些代码移到for循环之外,您可以在其他地方设置很多图形结构,例如将tf.train.Coordinator()和tf.train.start_queue_runners放在前面for循环可能更易于管理