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

Tensorflow: Passing a session to a python multiprocess

我正在使用 tensorflow 预处理一些大图像。我遇到了内存迅速崩溃的问题。我转而在 python 中使用多处理,因此内存会在我需要时完全释放。

问题是,我正在使用 python 的多进程队列,由于某种未知原因,我无法将我的 tensorflow 会话从我的父进程传递给子进程。使用一些高级调试技术(即每隔几行打印一些东西)我注意到 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()

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

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

你只需要分布式tensorflow。

  1. 在父进程中创建图形和会话。在构建图时将一些运算符(尤其是变量)放置到workers中。
  2. 创建子进程,并运行它们