Tensorflow:将会话传递给python多进程

时间:2016-01-20 12:34:49

标签: python python-2.7 python-multiprocessing tensorflow

我使用tensorflow预处理一些大图像。我遇到了一个内存迅速崩溃的问题。我转向在python中使用多处理,这样内存就可以随时释放。

问题是,我使用的是python的多进程队列,由于某种原因未知,我无法将我的张量流会话从父进程传递给子进程。使用一些高级调试技术(即每隔几行打印一些东西),我注意到python在我使用会话的行内空闲,它不会抛出错误信息。

我的代码看起来像这样:

def subprocess(some_image, sess, q):
    with sess.as_default():
        # ... use sess and q ...
        print "All good and well" #This is printed
        some_image.eval() #Nothing happens here in console
        print "Still all good and well" #This is not printed

if __name__ == '__main__':
    # ... some initial operations ...
    some_image = read_some_image()

    sess = tf.Session()

    q = Queue()
    q.put(something)
    p = Process(target=subprocess, args=(some_image, sess, q))
    p.start()
    p.join()

可能是什么问题? 非常感谢!

2 个答案:

答案 0 :(得分:2)

我认为您不能像tf.Session()那样在$rootscope.allowpage=true之间共享"state"。 我认为每个过程都需要它自己的会话。

答案 1 :(得分:-1)

您所需要的只是分布式张量流。

  1. 在父进程中创建图表和会话。在构造图形时,将一些操作符(尤其是变量)放在工作者身上。
  2. 创建子流程并运行它们
相关问题