tensorflow - 如何在 Tensorflow 2.0 + Keras 中进行并行 GPU 推理?

标签 tensorflow keras predict tensorflow2.0 multi-gpu

让我们从我刚开始接触 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 1.x 是 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/

    相关文章:

    r - Predict.svm 不预测新数据

    python - 如何将 py_func 与返回 dict 的函数一起使用

    python-3.x - 如何在 Keras 中创建自定义损失函数来评估每个时期后的预测?

    r - 使用 R 中的 glmnet 预测二进制结果的函数

    machine-learning - 截断 LSTM 网络中的旧数据点?

    machine-learning - 训练集的颜色配置文件在 CNN 模式识别中发挥作用吗?

    r - 贝叶斯预测,下标越界

    python-3.x - 非 AVX 系统上的 Tensorflow 2.2.0 支持

    python - 如何使用连续列和分类列进行 tf.estimator

    python - while_loop() 获得意外关键字参数 'maximum_iterations' 错误的解决方法