让我们从我刚开始接触 TensorFlow 和深度学习的前提开始。
我有使用 tf.Model.train()
训练的 TF 2.0 Keras 风格模型,两个可用的 GPU,我正在寻找缩减推理时间。
我使用非常方便的 tf.distribute.MirroredStrategy().scope()
训练了分布在 GPU 上的模型上下文管理器
mirrored_strategy = tf.distribute.MirroredStrategy()
with mirrored_strategy.scope():
model.compile(...)
model.train(...)
两个 GPU 都得到了有效利用(即使我对结果的准确性不太满意)。
我似乎找不到类似的策略来使用
tf.Model.predict()
在 GPU 之间分配推理方法:当我运行时 model.predict()
我(显然)仅从两个 GPU 之一获得使用。是否可以在两个 GPU 上实例化相同的模型并并行提供不同的数据块?
有些帖子建议如何在 TF 1.x 中执行此操作,但我似乎无法在 TF2.0 中复制结果
https://medium.com/@sbp3624/tensorflow-multi-gpu-for-inferencing-test-time-58e952a2ed95
Tensorflow: simultaneous prediction on GPU and CPU
我对这个问题的心理斗争主要是
tf.Session()
基于而 session 在 TF2.0 中是隐含的,如果我正确理解,我阅读的解决方案对每个 GPU 使用单独的 session ,我真的不知道如何在 TF2.0 中复制它 model.predict()
具有特定 session 的方法。 我知道这个问题可能没有很好地表述,但我总结为:
有没有人知道如何运行 Keras 风格的
model.predict()
在 TF2.0 中的多个 GPU 上(以并行方式对每个 GPU 上的不同批次数据进行推理)?在此先感谢您的帮助。
最佳答案
尝试在 tf.distribute.MirroredStrategy
中加载模型并使用更大的batch_size
mirrored_strategy = tf.distribute.MirroredStrategy()
with mirrored_strategy.scope():
model = tf.keras.models.load_model(saved_model_path)
result = model.predict(batch_size=greater_batch_size)
关于tensorflow - 如何在 Tensorflow 2.0 + Keras 中进行并行 GPU 推理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58951331/