使用tensorflow读取TFRecords时出错:tensorflow.python.framework.errors.NotFoundError:FetchOutputs节点ParseSingleExample / Squeeze

时间:2016-07-19 15:24:30

标签: python python-3.x tensorflow

我对张量流非常新,我确信我在这里做错了。我的问题是,当我从文件中读取记录时,代码会运行一段时间,但大部分时间都会失败:

import tensorflow as tf
import numpy as np
import time

def readFrame(inQueue, reader):
  frameWidth = int(1920/16)+1
  frameHeight = int(1080/16)+1
  frameItemsCount = frameWidth*frameHeight

  _, serialized_frame = reader.read(inQueue)
  features = tf.parse_single_example(
      serialized_frame,
      features= {
        'x'        : tf.FixedLenFeature([frameItemsCount], tf.float32)
        , 'y'        : tf.FixedLenFeature([frameItemsCount], tf.float32)
        , 'direction': tf.FixedLenFeature([frameItemsCount], tf.float32)
        # , 'force'    : tf.FixedLenFeature([frameItemsCount], tf.float32)
        # , 'sad'      : tf.FixedLenFeature([frameItemsCount], tf.float32)
      })

  return features

# pretty much copy paste from fully_connected_reader.py
def main(_):
  with tf.Graph().as_default():
    inputFramesQueue = tf.train.string_input_producer(["tfrecords.out"], num_epochs=100)
    reader = tf.TFRecordReader()

    init_op = tf.initialize_all_variables()
    sess = tf.Session()
    sess.run(init_op)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
      step = 0
      while not coord.should_stop() and step < 3:
        start_time = time.time()
        frameDict = readFrame(inputFramesQueue, reader)
        print(frameDict)
        frame = sess.run([
          frameDict['x']
          , frameDict['y']
          , frameDict['direction'] 
          # , frameDict['force'] 
          # , frameDict['sad']
        ])
        print(frame[2])

        duration = time.time() - start_time
        if step % 100 == 0: print('Step %d: (%.3f sec)' % (step, duration))
        step += 1
    except tf.errors.OutOfRangeError:
      print('Done training for %d epochs, %d steps.' % (FLAGS.num_epochs, step))
    finally:
      coord.request_stop()

    coord.join(threads)
    sess.close()

if __name__ == '__main__':
  tf.app.run()

例如,当我运行它时,这是我得到的两个不同的输出:

(venv) bash-3.2$ python src/readframes.py
{'x': <tf.Tensor 'ParseSingleExample/Squeeze_x:0' shape=(8228,) dtype=float32>, 'direction': <tf.Tensor 'ParseSingleExample/Squeeze_direction:0' shape=(
8228,) dtype=float32>, 'y': <tf.Tensor 'ParseSingleExample/Squeeze_y:0' shape=(8228,) dtype=float32>}
Traceback (most recent call last):
  File "/Users/mikaelle/Projects/Omat/tensorflow-grouping/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 715, in _do_call
    return fn(*args)
  File "/Users/mikaelle/Projects/Omat/tensorflow-grouping/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 697, in _run_fn
    status, run_metadata)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/Users/mikaelle/Projects/Omat/tensorflow-grouping/venv/lib/python3.5/site-packages/tensorflow/python/framework/errors.py", line 450, in raise_ex
ception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.NotFoundError: FetchOutputs node ParseSingleExample/Squeeze_y:0: not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "src/readframes.py", line 62, in <module>
    tf.app.run()
  File "/Users/mikaelle/Projects/Omat/tensorflow-grouping/venv/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 30, in run
    sys.exit(main(sys.argv))
  File "src/readframes.py", line 44, in main
    , frameDict['direction']
  File "/Users/mikaelle/Projects/Omat/tensorflow-grouping/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 372, in run
    run_metadata_ptr)
  File "/Users/mikaelle/Projects/Omat/tensorflow-grouping/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 636, in _run
    feed_dict_string, options, run_metadata)
  File "/Users/mikaelle/Projects/Omat/tensorflow-grouping/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 708, in _do_run
    target_list, options, run_metadata)
  File "/Users/mikaelle/Projects/Omat/tensorflow-grouping/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 728, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.NotFoundError: FetchOutputs node ParseSingleExample/Squeeze_y:0: not found
(venv) bash-3.2$

(venv) bash-3.2$ python src/readframes.py
{'y': <tf.Tensor 'ParseSingleExample/Squeeze_y:0' shape=(8228,) dtype=float32>, 'direction': <tf.Tensor 'ParseSingleExample/Squeeze_direction:0' shape=(
8228,) dtype=float32>, 'x': <tf.Tensor 'ParseSingleExample/Squeeze_x:0' shape=(8228,) dtype=float32>}
[ 0.  0.  0. ...,  0.  0.  0.]
Step 0: (0.014 sec)
{'y': <tf.Tensor 'ParseSingleExample_1/Squeeze_y:0' shape=(8228,) dtype=float32>, 'direction': <tf.Tensor 'ParseSingleExample_1/Squeeze_direction:0' sha
pe=(8228,) dtype=float32>, 'x': <tf.Tensor 'ParseSingleExample_1/Squeeze_x:0' shape=(8228,) dtype=float32>}
[ 0.  0.  0. ...,  0.  0.  0.]
{'y': <tf.Tensor 'ParseSingleExample_2/Squeeze_y:0' shape=(8228,) dtype=float32>, 'direction': <tf.Tensor 'ParseSingleExample_2/Squeeze_direction:0' sha
pe=(8228,) dtype=float32>, 'x': <tf.Tensor 'ParseSingleExample_2/Squeeze_x:0' shape=(8228,) dtype=float32>}
[ 0.          2.24240255  2.24240255 ...,  0.          0.          0.        ]
(venv) bash-3.2$

当尝试从那里获取项目时,似乎无法访问文件......

1 个答案:

答案 0 :(得分:0)

我在开始线程之后和开始阅读记录之前暂停了一段时间后摆脱了这个错误。

threads = tf.train.start_queue_runners(sess=sess, coord=coord)
time.sleep(1)

如果我在启动线程之前添加了sleep,它没有任何效果......我最好的猜测是,可能有一些预读缓冲区由队列线程填充,但解析数据不等待缓冲区在开始读取之前有足够的数据......

我仍然相信我在这里做错了什么。即使在添加睡眠之后,我似乎无法比服务更快地读取数据。