让我们从这样一个前提开始:我刚刚接触到TensorFlow和一般的深入学习。
我有TF-2.0 Keras风格的模型,使用tf.Model.train()
(两个可用的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上建立相同的模型,并并行地向它们提供不同的数据块?
有些帖子建议如何在TF1.x中做这件事,但我似乎无法复制TF2.0中的结果
https://medium.com/@sbp3624/tensorflow-multi-gpu-for-inferencing-test-time-58e952a2ed95
Tensorflow: simultaneous prediction on GPU and CPU
我对这个问题的思考主要是
tf.Session()
的,而会话在TF2.0中是隐式的,如果我正确地得到了它,我所读的解决方案将为每个GPU使用单独的会话,我不知道如何在TF2.0 model.predict()
方法。我知道这个问题可能没有很好的表述,但我总结如下:
有没有人知道在TF2.0中如何在多个GPU上运行Keras样式的model.predict()
(以并行方式推断每个GPU上的不同批数据)?
提前感谢您的帮助。
发布于 2020-11-26 01:55:56
尝试在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)
https://stackoverflow.com/questions/58951331
复制相似问题