首页 > 解决方案 > tensorflow 和 keras - 从 SQS 进行生产中的快速推理

问题描述

我的模型是在 Keras 中编写和训练的。我正在尝试将其用于生产中的推理。我收到包含(path_in, path_out).

我显然可以使用:

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,但不确定它是否也适用于这种情况,以及用于推理(而不是训练)

标签: python-3.xtensorflowamazon-sqs

解决方案


我建议您使用Tensorflow Serving解决方案,因为它实现了服务器端批处理策略,可优化推理速度和 GOU 利用率。此外,如果您想加快管道速度,您应该将模型转换为TensorRT模型,该模型将模型操作优化到特定的 GPU(它还可以做更多)。


推荐阅读