Tensorflow:使用多线程加载/运行多个模型

时间:2017-06-03 05:04:33

标签: python multithreading tensorflow

我已经搜索了这个问题的答案,但没有运气。

在许多应用程序中,我们在一个GPU上部署了多个训练有素的NN模型。此外,我们必须使用多线程技术才能获得GPU的全部计算能力。例如,我的应用程序中有两种不同的tensorflow CNN模型,我想在两个不同的线程中运行它们(即推断/部署)。

这是我做的:

## define my cnn model
class my_cnn_model:
    ...

## initialize two graphs 
g1 = tf.Graph()
g2 = tf.Graph()

## define two sessions and bind one cnn model to each session
sess1 = tf.InteractiveSession(graph=g1, config=cnn1_cfg)
cnn1 = my_cnn_model(..., sess1)

sess2 =tf.InteractiveSession(graph=g2, config=cnn2_cfg)
cnn2 = my_cnn_model(..., sess2)

## define two function model runs
def cnn1_model_run(g1, sess1):
    ...
    sess1.run();

def cnn2_model_run(g2, sess2):
    ...
    sess2.run();


## put the functions into two separate threads
thread_0 = threading.Thread(target= cnn1_model_run,args=...)
thread_1 = threading.Thread(target= cnn2_model_run,args=...)

## now issue the two threads
thread_0.start()
thread_1.start()

结果显示,两个cnn模型相互干扰并输出错误。使用上述机制的问题在哪里? python多线程函数对多个tensorflow模型部署不安全吗?

0 个答案:

没有答案