python-3.x - 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
,但不确定它是否也适用于这种情况,以及用于推理(而不是训练)
解决方案
我建议您使用Tensorflow Serving解决方案,因为它实现了服务器端批处理策略,可优化推理速度和 GOU 利用率。此外,如果您想加快管道速度,您应该将模型转换为TensorRT模型,该模型将模型操作优化到特定的 GPU(它还可以做更多)。
推荐阅读
- git - 如何在git中删除之前的推送
- graphql - 为什么 apollo 客户端网络请求取消了两次?
- android - 列表适配器与回收视图适配器
- ios - 无法接收应用内购买退款服务器到服务器通知
- swift - Swift 应用内购买沙盒测试器错误
- java - 如何更改另一个对象的颜色并同时从另一个 Java 中删除
- c++ - 如何显式调用转换类型 ID 包含占位符说明符的转换函数
- php - 我在 PHP 中制作了一个代理抓取工具,但我不知道如何检查代理是否处于活动状态
- python - (Python) 如何获取列表的组合
- ubuntu - 是否可以在 google colab 的虚拟机中挂载 squashfs 文件?