我的模型是在 Keras 中编写和训练的。我正在尝试将其用于生产中的推理。我收到包含 (path_in, path_out)
元组的 SQS“任务”消息.
我显然可以使用:
BATCH_SIZE = 10
batch_messages = []
while True:
while len(batch_messages) < BATCH_SIZE:
msg = sqs.read_messsage()
batch_messages.apend(msg)
assert len(batch_messages) == BATCH_SIZE
batch = np.array([read_image(msg.path_in) for msg in batch_messages])
output_batch = model.predict(batch)
for i in range(BATCH_SIZE):
write_output(output_batch[i], path=batch_messages[i].path_out)
batch_messages = []
问题是代码浪费了大部分时间从 SQS 读取,从磁盘读取图像并在最后写回。这意味着 GPU 在这段时间内一直处于空闲状态。
我知道 Keras 的
Sequence
,但不确定它是否也适用于这种情况,以及用于推理(而不是训练)
最佳答案
我建议你使用 Tensorflow Serving解决方案,因为它实现了服务器端批处理策略,可优化推理速度和 GOU 利用率。此外,如果您想加快管道速度,您应该将模型转换为 TensorRT将模型操作优化到特定 GPU 的模型(并且它做得更多)。
关于python-3.x - tensorflow 和 keras - SQS 生产中的快速推理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61562123/