简体   繁体   English

在多处理中使用 model.predict (Keras + TF)

[英]Using model.predict (Keras + TF) in multiprocessing

I have following problem.我有以下问题。 I'm using a Tensorflow Keras model to evaluate continuous sensor data.我正在使用 Tensorflow Keras 模型来评估连续传感器数据。 My input for my model consists of 15 sensor data frames.我的模型输入包含 15 个传感器数据帧。 Because the function model.predict() takes near 1 second I wanted to execute this function asynchronous so that I can collect the next data frames in this time period.因为函数 model.predict() 需要将近 1 秒的时间,所以我想异步执行这个函数,以便我可以在这段时间内收集下一个数据帧。 To accomplish this I created a Pool with the multiprocessing libary and a function to for model.predict.为了实现这一点,我创建了一个带有多处理库的池和一个用于 model.predict 的函数。 My code looks something like this:我的代码看起来像这样:

def predictData(data): 
   return model.predict(data)

global model
model = tf.keras.models.load_model("Network.h5")
model._make_predict_function()

p = Pool(processes = 4)
...
res = p.apply_async(predictData, ([[iinput]],))
print(res.get(timeout = 10))

Now I always get a timeout-error when calling predictData().现在我在调用 predictData() 时总是收到超时错误。 It seems like model.predict() is not working right.似乎 model.predict() 工作不正常。 What am I making wrong?我做错了什么?

It is possible to run multiple predictions in multiple concurrent python processes, only you have to build inside each independent process its own tensorflow computational graph and then call the keras.model.predict可以在多个并发 python 进程中运行多个预测,只需要在每个独立进程内部构建自己的 tensorflow 计算图,然后调用 keras.model.predict

Write a function which you will use with the multiprocessing module (with the Process or Pool class), within this function you should build your model, tensorflow graph and whatever you need, set all tensorflow and keras variables, then you can call the predict method on it, and then pipe the result back to your master process.编写一个将与多处理模块(使用 Process 或 Pool 类)一起使用的函数,在此函数中,您应该构建模型、张量流图以及您需要的任何内容,设置所有 tensorflow 和 keras 变量,然后您可以调用 predict 方法在它上面,然后将结果通过管道返回到您的主进程。

for example:例如:

    def f(data):

          import tensorflow, keras

          configure your tensorflow and keras settings (e.g.  GPU/CPU usage)

          keras_model = build_your_keras_model()

          result = keras_model.predict(data)

          return result

    if __main__ = '__main__':

          p = Pool(processes = 4)

          res = p.apply_async(f, (data,))

          print(res.get(timeout = 10))

The reason is that each process you spawn will require a new initialized version of your model which it uses to make predictions.原因是您生成的每个进程都需要一个新的模型初始化版本,用于进行预测。 Therefore you have to make sure you instantiate/load your model for every spawned process.因此,您必须确保为每个生成的进程实例化/加载模型。 This is defiantly not optimal.这显然不是最佳选择。

This is a known caveat with multiprocessing machine learning training and/or inference.这是多处理机器学习训练和/或推理的已知警告。 Some libraries come with multiprocessing features out-of-the-box and provide parallizable calls to their models.一些库带有开箱即用的多处理功能,并为其模型提供可并行调用。 However, in most libraries once you want to do multiprocessing, you are on your own!但是,在大多数库中,一旦您想要进行多处理,就只能靠自己了!

Make sure you instantiate your model once and then find a way to share that model across processes.确保您将模型实例化一次,然后找到一种跨进程共享该模型的方法。 One basic way to do that, is to serve your model as a flask service then make predictions against that service to your hearts content.做到这一点的一种基本方法是将您的模型作为烧瓶服务提供,然后对该服务进行预测,以满足您的需求。 Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM