簡體   English   中英

嘗試從多個tfrecord文件讀取數據

[英]Trying to read data from multiple tfrecord files

我有一個.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)]]

我真的不明白為什么會這樣,並且想知道是否有人可以幫助我。

謝謝

嘗試將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之前循環可能更易於管理

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM