首页 > 解决方案 > 为什么 Tensorflow Serving 在 Keras 股票模型上运行的模型比 Tensorflow 慢

问题描述

我一直在尝试在嵌入式设备上部署带有 Tensorflow Serving 的机器学习解决方案(Jetson Xavier [ARMv8])

该解决方案使用的一个模型是股票 Xception 网络,其生成方式为:

xception = tf.keras.applications.Xception(include_top=False, input_shape=(299, 299, 3), pooling=None
xception.save("./saved_xception_model/1", save_format="tf")

在设备上运行 Xception 模型会产生合理的性能 - 大约 0.1 秒进行预测,忽略所有处理:

xception = tf.keras.models.load_model("saved_xception_model/1", save_format="tf")
image = get_some_image() # image is numpy.ndarray
image.astype("float32")
image /= 255
image = cv2.resize(image, (299, 299))
# Tensorflow predict takes ~0.1s
xception.predict([image])

然而,一旦模型通过 Nvidia-Docker 在 Tensorflow Serving GPU 容器中运行,模型就会慢得多——预测大约需要 3 秒。

我一直试图找出性能不佳的原因,但我已经没有想法了。

到目前为止,我已经测试过:

  1. 调整 TF Serving 的批处理参数以全力降低延迟 ( batch_timeout_micros: 0, max_batch_size: 1, , 并注意到性能有 0.5 秒的适度增长。
  2. 通过 TensorRT 优化模型saved_model_cli
  3. 单独运行 Xception 模型,作为 TF Serving 提供的唯一模型。
  4. 尝试将每个 TF 进程分配的内存加倍。
  5. 尝试完全启用和禁用批处理。
  6. 尝试启用和禁用模型预热。

我希望 TF Serving 能够提供与 TF 相同(或多或少,允许 GRPC 编码和解码)的预测时间,并且它们对我正在运行的其他模型也是如此。我的任何努力都没有让 Xception 达到我预期的 ~0.1s 性能。

我安装的 Tensorflow 是由 Nvidia 从 TF 版本 2.0 构建的。我的 TF Serving 容器是从 TF-Serving 2.0 源代码自行构建的,支持 GPU。

我按如下方式启动 TensorFlow Serving 容器:

tf_serving_cmd = "docker run --runtime=nvidia -d"
tf_serving_cmd += " --name my-container"
tf_serving_cmd += " -p=8500:8500 -p=8501:8501"
tf_serving_cmd += " --mount=type=bind,source=/home/xception_model,target=/models/xception_model"
tf_serving_cmd += " --mount=type=bind,source=/home/model_config.pb,target=/models/model_config.pb"
tf_serving_cmd += " --mount=type=bind,source=/home/batching_config.pb,target=/models/batching_config.pb"

# Self built TF serving image for Jetson Xavier, ARMv8.
tf_serving_cmd += " ${MY_ORG}/serving" 
# I have tried 0.5 with no performance difference. 

# TF-Serving does not complain it wants more memory in either case.
tf_serving_cmd += " --per_process_gpu_memory_fraction:0.25"
tf_serving_cmd += " --model_config_file=/models/model_config.pb"
tf_serving_cmd += " --flush_filesystem_caches=true"
tf_serving_cmd += " --enable_model_warmup=true"
tf_serving_cmd += " --enable_batching=true"
tf_serving_cmd += " --batching_parameters_file=/models/batching_config.pb"

我开始怀疑这是否是 TF-Serving 中的错误,尽管我不知道在哪里(是的,我知道这从来都不是错误,它始终是用户......)

谁能提出为什么 TF-Serving 与 TF 相比可能表现不佳?

标签: tensorflowkerastensorflow-servingnvidia-docker

解决方案


推荐阅读