带有 Ultralytics YOLO11 的 Triton 推理服务器
Triton 推理服务器(前身为 TensorRT 推理服务器)是由 NVIDIA 开发的开源软件解决方案。它提供了一个针对 NVIDIA GPU 优化的云推理解决方案。Triton 简化了在生产环境中大规模部署 AI 模型的过程。将 Ultralytics YOLO11 与 Triton 推理服务器集成,您可以部署可扩展的、高性能的深度学习推理工作负载。本指南提供了设置和测试集成的步骤。
观看: NVIDIA Triton 推理服务器入门。
什么是 Triton Inference Server?
Triton 推理服务器旨在部署各种 AI 模型到生产环境。它支持各种深度学习和机器学习框架,包括 TensorFlow、PyTorch、ONNX Runtime 等。其主要用例包括:
- 从单个服务器实例提供多个模型
- 无需服务器重启即可动态加载和卸载模型
- 集成推理,允许多个模型一起使用以获得结果
- 用于 A/B 测试和滚动更新的模型版本控制
Triton 推理服务器的主要优势
将 Triton 推理服务器与 Ultralytics YOLO11 结合使用具有以下几个优势:
- 自动批处理: 在处理多个 AI 请求之前,将它们分组在一起,从而减少延迟并提高推理速度
- Kubernetes集成:云原生设计与Kubernetes无缝协作,用于管理和扩展AI应用程序
- 硬件特定优化:充分利用 NVIDIA GPU 以实现最佳性能
- 框架灵活性:支持多种 AI 框架,包括 TensorFlow、PyTorch、ONNX 和 TensorRT
- 开源且可定制:可以修改以适应特定需求,确保各种 AI 应用的灵活性。
准备工作
在继续之前,请确保您具备以下先决条件:
- 您的机器上已安装 Docker
- 安装
tritonclient
:pip install tritonclient[all]
将 YOLO11 导出为 ONNX 格式
在 Triton 上部署模型之前,必须将其导出为 ONNX 格式。ONNX(开放神经网络交换)是一种允许在不同深度学习框架之间传输模型的格式。使用 export
中的 YOLO
函数:
from ultralytics import YOLO
# Load a model
model = YOLO("yolo11n.pt") # load an official model
# Retrieve metadata during export. Metadata needs to be added to config.pbtxt. See next section.
metadata = []
def export_cb(exporter):
metadata.append(exporter.metadata)
model.add_callback("on_export_end", export_cb)
# Export the model
onnx_file = model.export(format="onnx", dynamic=True)
设置 Triton 模型仓库
Triton 模型仓库是 Triton 可以访问和加载模型的存储位置。
-
创建必要的目录结构:
from pathlib import Path # Define paths model_name = "yolo" triton_repo_path = Path("tmp") / "triton_repo" triton_model_path = triton_repo_path / model_name # Create directories (triton_model_path / "1").mkdir(parents=True, exist_ok=True)
-
将导出的 ONNX 模型移动到 Triton 仓库:
from pathlib import Path # Move ONNX model to Triton Model path Path(onnx_file).rename(triton_model_path / "1" / "model.onnx") # Create config file (triton_model_path / "config.pbtxt").touch() data = """ # Add metadata parameters { key: "metadata" value { string_value: "%s" } } # (Optional) Enable TensorRT for GPU inference # First run will be slow due to TensorRT engine conversion optimization { execution_accelerators { gpu_execution_accelerator { name: "tensorrt" parameters { key: "precision_mode" value: "FP16" } parameters { key: "max_workspace_size_bytes" value: "3221225472" } parameters { key: "trt_engine_cache_enable" value: "1" } parameters { key: "trt_engine_cache_path" value: "/models/yolo/1" } } } } """ % metadata[0] # noqa with open(triton_model_path / "config.pbtxt", "w") as f: f.write(data)
运行 Triton 推理服务器
使用 Docker 运行 Triton 推理服务器:
import contextlib
import subprocess
import time
from tritonclient.http import InferenceServerClient
# Define image https://catalog.ngc.nvidia.com/orgs/nvidia/containers/tritonserver
tag = "nvcr.io/nvidia/tritonserver:24.09-py3" # 8.57 GB
# Pull the image
subprocess.call(f"docker pull {tag}", shell=True)
# Run the Triton server and capture the container ID
container_id = (
subprocess.check_output(
f"docker run -d --rm --gpus 0 -v {triton_repo_path}:/models -p 8000:8000 {tag} tritonserver --model-repository=/models",
shell=True,
)
.decode("utf-8")
.strip()
)
# Wait for the Triton server to start
triton_client = InferenceServerClient(url="localhost:8000", verbose=False, ssl=False)
# Wait until model is ready
for _ in range(10):
with contextlib.suppress(Exception):
assert triton_client.is_model_ready(model_name)
break
time.sleep(1)
然后使用 Triton Server 模型运行推理:
from ultralytics import YOLO
# Load the Triton Server model
model = YOLO("http://localhost:8000/yolo", task="detect")
# Run inference on the server
results = model("path/to/image.jpg")
清理容器:
# Kill and remove the container at the end of the test
subprocess.call(f"docker kill {container_id}", shell=True)
TensorRT 优化(可选)
为了获得更高的性能,您可以将 TensorRT 与 Triton Inference Server 结合使用。TensorRT 是专为 NVIDIA GPU 构建的高性能深度学习优化器,可以显著提高推理速度。
将 TensorRT 与 Triton 结合使用的主要优势包括:
- 与未优化的模型相比,推理速度最高可提高 36 倍
- 针对特定硬件的优化,可最大程度地利用 GPU
- 支持降低精度格式(INT8、FP16),同时保持准确性
- 层融合以减少计算开销
要直接使用 TensorRT,您可以将您的 YOLO11 模型导出为 TensorRT 格式:
from ultralytics import YOLO
# Load the YOLO11 model
model = YOLO("yolo11n.pt")
# Export the model to TensorRT format
model.export(format="engine") # creates 'yolo11n.engine'
有关 TensorRT 优化的更多信息,请参阅TensorRT 集成指南。
通过遵循上述步骤,您可以高效地在 Triton Inference Server 上部署和运行 Ultralytics YOLO11 模型,从而为深度学习推理任务提供可扩展且高性能的解决方案。如果您遇到任何问题或有其他疑问,请参阅 Triton 官方文档 或联系 Ultralytics 社区以获得支持。
常见问题
如何设置 Ultralytics YOLO11 与 NVIDIA Triton 推理服务器?
使用 NVIDIA Triton Inference Server 设置 Ultralytics YOLO11 涉及几个关键步骤:
-
将 YOLO11 导出为 ONNX 格式:
from ultralytics import YOLO # Load a model model = YOLO("yolo11n.pt") # load an official model # Export the model to ONNX format onnx_file = model.export(format="onnx", dynamic=True)
-
设置 Triton 模型仓库:
from pathlib import Path # Define paths model_name = "yolo" triton_repo_path = Path("tmp") / "triton_repo" triton_model_path = triton_repo_path / model_name # Create directories (triton_model_path / "1").mkdir(parents=True, exist_ok=True) Path(onnx_file).rename(triton_model_path / "1" / "model.onnx") (triton_model_path / "config.pbtxt").touch()
-
运行 Triton Server:
import contextlib import subprocess import time from tritonclient.http import InferenceServerClient # Define image https://catalog.ngc.nvidia.com/orgs/nvidia/containers/tritonserver tag = "nvcr.io/nvidia/tritonserver:24.09-py3" subprocess.call(f"docker pull {tag}", shell=True) container_id = ( subprocess.check_output( f"docker run -d --rm --gpus 0 -v {triton_repo_path}:/models -p 8000:8000 {tag} tritonserver --model-repository=/models", shell=True, ) .decode("utf-8") .strip() ) triton_client = InferenceServerClient(url="localhost:8000", verbose=False, ssl=False) for _ in range(10): with contextlib.suppress(Exception): assert triton_client.is_model_ready(model_name) break time.sleep(1)
此设置可以帮助您在 Triton Inference Server 上高效地大规模部署 YOLO11 模型,以实现高性能的 AI 模型推理。
将 Ultralytics YOLO11 与 NVIDIA Triton 推理服务器结合使用有哪些好处?
将 Ultralytics YOLO11 与 NVIDIA Triton Inference Server 集成提供了以下几个优势:
- 可扩展的 AI 推理:Triton 允许从单个服务器实例提供多个模型,支持动态模型加载和卸载,使其对于各种 AI 工作负载具有高度的可扩展性。
- 高性能:Triton Inference Server 针对 NVIDIA GPU 进行了优化,可确保高速推理操作,非常适合 目标检测 等实时应用。
- 集成和模型版本控制:Triton 的集成模式能够组合多个模型以改进结果,并且其模型版本控制支持 A/B 测试和滚动更新。
- 自动批处理: Triton 自动将多个推理请求分组在一起,从而显著提高吞吐量并减少延迟。
- 简化的部署:AI 工作流程的逐步优化,无需彻底改造系统,从而更易于高效扩展。
有关使用 Triton 设置和运行 YOLO11 的详细说明,您可以参考设置指南。
为什么在使用 Triton 推理服务器之前,我应该将 YOLO11 模型导出为 ONNX 格式?
在将 Ultralytics YOLO11 模型部署到 NVIDIA Triton Inference Server 之前,使用 ONNX(开放神经网络交换)格式可以带来以下几个主要优势:
- 互操作性:ONNX 格式支持不同深度学习框架(如 PyTorch、TensorFlow)之间的传输,确保更广泛的兼容性。
- 优化:包括 Triton 在内的许多部署环境都针对 ONNX 进行了优化,从而能够实现更快的推理和更好的性能。
- 易于部署:ONNX 受到框架和平台的广泛支持,从而简化了在各种操作系统和硬件配置中的部署过程。
- 框架独立性:一旦转换为 ONNX,您的模型不再受限于其原始框架,从而使其更具可移植性。
- 标准化:ONNX 提供了一种标准化表示,有助于克服不同 AI 框架之间的兼容性问题。
要导出您的模型,请使用:
from ultralytics import YOLO
model = YOLO("yolo11n.pt")
onnx_file = model.export(format="onnx", dynamic=True)
您可以按照 ONNX 集成指南 中的步骤完成该过程。
我可以在 Triton 推理服务器上使用 Ultralytics YOLO11 模型运行推理吗?
是的,您可以在 NVIDIA Triton Inference Server 上使用 Ultralytics YOLO11 模型运行推理。一旦您的模型在 Triton Model Repository 中设置好并且服务器正在运行,您可以按如下方式加载并运行您的模型推理:
from ultralytics import YOLO
# Load the Triton Server model
model = YOLO("http://localhost:8000/yolo", task="detect")
# Run inference on the server
results = model("path/to/image.jpg")
这种方法允许您利用 Triton 的优化,同时使用熟悉的 Ultralytics YOLO 接口。有关使用 YOLO11 设置和运行 Triton Server 的深入指南,请参阅运行 triton 推理服务器部分。
在部署方面,Ultralytics YOLO11 与 TensorFlow 和 PyTorch 模型相比如何?
与用于部署的 TensorFlow 和 PyTorch 模型相比,Ultralytics YOLO11 具有以下几个独特的优势:
- 实时性能:YOLO11 针对实时目标检测任务进行了优化,可提供最先进的准确率和速度,使其成为需要实时视频分析的应用程序的理想选择。
- 易于使用:YOLO11 与 Triton Inference Server 无缝集成,并支持各种导出格式(ONNX、TensorRT、CoreML),从而使其可以灵活地适应各种部署方案。
- 高级功能:YOLO11 包含动态模型加载、模型版本控制和集成推理等功能,这对于可扩展且可靠的 AI 部署至关重要。
- 简化的 API:Ultralytics API 在不同的部署目标上提供一致的接口,从而缩短了学习曲线和开发时间。
- 边缘优化: YOLO11 模型在设计时考虑了边缘部署,即使在资源受限的设备上也能提供出色的性能。
有关更多详细信息,请比较模型导出指南中的部署选项。