tensorflow cifar10_eval.py错误:RuntimeError:尝试使用封闭的Session.RuntimeError:尝试使用封闭的Session [英] tensorflow cifar10_eval.py error:RuntimeError: Attempted to use a closed Session.RuntimeError: Attempted to use a closed Session

查看:90
本文介绍了tensorflow cifar10_eval.py错误:RuntimeError:尝试使用封闭的Session.RuntimeError:尝试使用封闭的Session的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PC上运行cifar10网络,完成培训并运行eval脚本后,出现以下错误:

I'm running the cifar10 network on my PC and after finishing the training and running eval script the following error appears:

2016-06-01 14:37:14.238317: precision @ 1 = 0.000
Traceback (most recent call last):

  File "<ipython-input-1-adf2ca85bb77>", line 1, in <module>
    runfile('/home/kang/Documents/work_code_PC1/py_tensorflow_learning/cifar10CNN_test/cifar10_eval_test.py', wdir='/home/kang/Documents/work_code_PC1/py_tensorflow_learning/cifar10CNN_test')

  File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

  File "/home/kang/Documents/work_code_PC1/py_tensorflow_learning/cifar10CNN_test/cifar10_eval_test.py", line 107, in <module>
    tf.app.run()

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/platform/default/_app.py", line 30, in run
    sys.exit(main(sys.argv))

  File "/home/kang/Documents/work_code_PC1/py_tensorflow_learning/cifar10CNN_test/cifar10_eval_test.py", line 104, in main
    evaluate()

  File "/home/kang/Documents/work_code_PC1/py_tensorflow_learning/cifar10CNN_test/cifar10_eval_test.py", line 94, in evaluate
    eval_once(saver, summary_writer, top_k_op, summary_op)

  File "/home/kang/Documents/work_code_PC1/py_tensorflow_learning/cifar10CNN_test/cifar10_eval_test.py", line 72, in eval_once
    coord.join(threads, stop_grace_period_secs = 10)

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/training/coordinator.py", line 264, in join
    six.reraise(*self._exc_info_to_raise)

  File "/usr/lib/python3/dist-packages/six.py", line 659, in reraise
    raise value

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/training/queue_runner.py", line 185, in _run
    sess.run(enqueue_op)

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 315, in run
    return self._run(None, fetches, feed_dict)

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 473, in _run
    raise RuntimeError('Attempted to use a closed Session.')

RuntimeError: Attempted to use a closed Session.

闭门会议意味着什么? train.py和eval.py是否应该同时运行?我是tensorflow的新手,并根据演示代码 https:输入我自己: //www.tensorflow.org/versions/r0.8/tutorials/deep_cnn/index.html

what does the closed Session mean? Should the train.py and eval.py run at the same time? I am new in tensorflow and type myself according to the demo codes https://www.tensorflow.org/versions/r0.8/tutorials/deep_cnn/index.html

推荐答案

查看您发布的代码,问题出在第50行和第51行之间在 eval_once()

Looking at the code you posted, the problem is between lines 50 and 51 in eval_once():

with tf.Session() as sess:
    ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
    else:
        print('No checkpoint file found')
        return
# <<< The Session is closed here >>>
coord = tf.train.Coordinator()
try:
    # ...

当代码退出tf.Session()作为sess:的时, sess 为自动关闭,您将无法再使用它。有(至少)两种方法可以解决此问题:

When the code exits a with tf.Session() as sess: block, sess is automatically closed, and you cannot use it any more. There are (at least) two ways to fix this problem:


  1. 缩进第51行通过76 划分4个空格,这样它们也位于中,块。

  1. Indent lines 51 through 76 by 4 spaces, so that they are also inside the with block.

创建会话而不使用 with 块并手动关闭它:

Create the session without using a with block and close it manually:

def eval_once():
    sess = tf.Session()
    ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
    else:
        print('No checkpoint file found')
        sess.close()
        return

    coord = tf.train.Coordinator()
    try:
        # ...
    finally:
        sess.close()


这篇关于tensorflow cifar10_eval.py错误:RuntimeError:尝试使用封闭的Session.RuntimeError:尝试使用封闭的Session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆