Link to this section使用 Triton Inference Server 和 Ultralytics YOLO26#
Triton Inference Server(前身为 TensorRT Inference Server)是由 NVIDIA 开发的开源软件解决方案。它提供了一种针对 NVIDIA GPU 优化的云端推理解决方案。Triton 简化了生产环境中大规模 AI 模型的部署。将 Ultralytics YOLO26 与 Triton Inference Server 集成,使你能够部署可扩展的高性能深度学习推理工作负载。本指南提供了设置和测试该集成的步骤。
Watch: Getting Started with NVIDIA Triton Inference Server.
Link to this section什么是 Triton Inference Server?#
Triton Inference Server 旨在生产环境中部署各种 AI 模型。它支持广泛的深度学习和机器学习框架,包括 PyTorch、TensorFlow、ONNX、OpenVINO、TensorRT 等。其主要应用场景包括:
- 从单个服务器实例提供多个模型服务
- 无需重启服务器即可动态加载和卸载模型
- 集成推理,允许将多个模型组合使用以实现结果
- 用于 A/B 测试和滚动更新的模型版本控制
Link to this sectionTriton Inference Server 的主要优势#
将 Triton Inference Server 与 Ultralytics YOLO26 结合使用具有以下几项优势:
- 自动批处理:在处理前将多个 AI 请求分组,从而降低延迟并提高推理速度
- Kubernetes 集成:云原生设计,可与 Kubernetes 无缝协作,用于管理和扩展 AI 应用程序
- 特定硬件优化:充分利用 NVIDIA GPU 以获得最佳性能
- 框架灵活性:支持多种 AI 框架,包括 PyTorch、TensorFlow、ONNX、OpenVINO 和 TensorRT
- 开源且可自定义:可以根据特定需求进行修改,确保了各种 AI 应用程序的灵活性
Link to this section前提条件#
在继续之前,请确保你已具备以下先决条件:
- 在你的机器上安装 Docker (>= 28.2.0, 并带有用于 CDI GPU 访问的 NVIDIA Container Toolkit >= 1.18) 或 Podman
- 安装
ultralytics:pip install ultralytics - 安装
tritonclient:pip install tritonclient[all]
Link to this section设置 Triton Inference Server#
运行此完整的设置代码块,将 Ultralytics YOLO26 导出为 ONNX,构建 Triton 模型仓库,并启动 Triton Inference Server:
在脚本中使用 runtime 开关来选择你的容器引擎:
- 对于 Docker,设置
runtime = "docker" - 对于 Podman,设置
runtime = "podman"
import contextlib
import subprocess
import time
from pathlib import Path
from tritonclient.http import InferenceServerClient
from ultralytics import YOLO
runtime = "docker" # set to "podman" to use Podman
# 1) Exporting YOLO26 to ONNX Format
# Load a model
model = YOLO("yolo26n.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)
# 2) Setting Up Triton Model Repository
# 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)
# 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"
}
}
# Enable TensorRT acceleration (requires a GPU and TensorRT-enabled Triton; remove this block for CPU-only serving)
# The 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)
# 3) Running Triton Inference Server
# Define image https://catalog.ngc.nvidia.com/orgs/nvidia/containers/tritonserver
tag = "nvcr.io/nvidia/tritonserver:26.02-py3" # 16.17 GB (Compressed Size)
subprocess.call(f"{runtime} pull {tag}", shell=True)
# CDI GPU request works identically on Docker and Podman
gpu_flags = "--device nvidia.com/gpu=all"
container_name = "triton_server"
# Note: The :z flag on the volume mount is necessary for systems with SELinux (like Fedora/RHEL)
subprocess.call(
f"{runtime} run -d --rm --name {container_name} {gpu_flags} -v {triton_repo_path.absolute()}:/models:z -p 8000:8000 {tag} tritonserver --model-repository=/models",
shell=True,
)
# Wait for the Triton server to start
triton_client = InferenceServerClient(url="127.0.0.1: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)Link to this section运行推理#
使用 Triton Server 模型运行推理:
from ultralytics import YOLO
# Load the Triton Server model
model = YOLO("http://127.0.0.1:8000/yolo", task="detect")
# Run inference on the server
results = model("path/to/image.jpg")清理容器:
import subprocess
runtime = "docker" # set to "podman" to use Podman
container_name = "triton_server" # Kill the named container
subprocess.call(f"{runtime} kill {container_name}", shell=True)Link to this sectionTensorRT 优化(可选)#
为了获得更高的性能,你可以将 TensorRT 与 Triton Inference Server 结合使用。TensorRT 是专为 NVIDIA GPU 构建的高性能深度学习优化器,可以显著提高推理速度。
将 TensorRT 与 Triton 结合使用的主要优势包括:
- 与未优化的模型相比,推理速度最高可提升 36 倍
- 特定硬件优化以实现最大 GPU 利用率
- 支持低精度格式(INT8,FP16),同时保持准确性
- 层融合以减少计算开销
若要直接使用 TensorRT,你可以将 Ultralytics YOLO26 模型导出为 TensorRT 格式:
from ultralytics import YOLO
# Load the YOLO26 model
model = YOLO("yolo26n.pt")
# Export the model to TensorRT format
model.export(format="engine") # creates 'yolo26n.engine'有关 TensorRT 优化的更多信息,请参阅 TensorRT 集成指南。
现在你可以在 Triton Inference Server 上部署并运行 Ultralytics YOLO26 模型,以实现可扩展的高性能推理。如需了解更多详情,请参阅 Triton 官方文档 或向 Ultralytics 社区 寻求帮助。
Link to this section常见问题解答#
Link to this section如何设置 Ultralytics YOLO26 与 NVIDIA Triton Inference Server?#
设置 Ultralytics YOLO26 与 NVIDIA Triton Inference Server 涉及几个关键步骤:
-
将 YOLO26 导出为 ONNX 格式:
from ultralytics import YOLO # Load a model model = YOLO("yolo26n.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 服务器:
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:26.02-py3" runtime = "docker" # set to "podman" to use Podman subprocess.call(f"{runtime} pull {tag}", shell=True) # CDI GPU request works identically on Docker and Podman gpu_flags = "--device nvidia.com/gpu=all" container_name = "triton_server" subprocess.call( f"{runtime} run -d --rm --name {container_name} {gpu_flags} -v {triton_repo_path.absolute()}:/models:z -p 8000:8000 {tag} tritonserver --model-repository=/models", shell=True, ) triton_client = InferenceServerClient(url="127.0.0.1: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 上大规模地高效部署 Ultralytics YOLO26 模型,以实现高性能 AI 模型推理。
Link to this section将 Ultralytics YOLO26 与 NVIDIA Triton Inference Server 结合使用有什么好处?#
集成 Ultralytics YOLO26 与 NVIDIA Triton Inference Server 提供了多项优势:
- 可扩展的 AI 推理:Triton 允许从单个服务器实例服务多个模型,并支持动态模型加载和卸载,使其对于各种 AI 工作负载具有高度的可扩展性。
- 高性能:Triton Inference Server 针对 NVIDIA GPU 进行了优化,确保了高速推理操作,非常适合目标检测等实时应用。
- 集成和模型版本控制:Triton 的集成模式允许组合多个模型以改进结果,其模型版本控制支持 A/B 测试和滚动更新。
- 自动批处理:Triton 会自动将多个推理请求组合在一起,显著提高了吞吐量并减少了延迟。
- 简化的部署:逐步优化 AI 工作流程,无需彻底改变整个系统,从而更容易实现高效扩展。
有关设置和运行 Ultralytics YOLO26 与 Triton 的详细说明,请参阅 设置 Triton Inference Server 和 运行推理。
Link to this section为什么在将 YOLO26 模型用于 Triton Inference Server 之前需要将其导出为 ONNX 格式?#
在将 Ultralytics YOLO26 模型部署到 NVIDIA Triton Inference Server 之前使用 ONNX(开放神经网络交换)格式具有以下几项关键优势:
- 互操作性:ONNX 格式支持在不同的深度学习框架(例如 PyTorch、TensorFlow)之间进行转换,从而确保了更广泛的兼容性。
- 优化:包括 Triton 在内的许多部署环境都针对 ONNX 进行了优化,从而实现了更快的推理和更好的性能。
- 易于部署:ONNX 在各种框架和平台上得到广泛支持,简化了不同操作系统和硬件配置下的部署过程。
- 框架无关性:一旦转换为 ONNX,你的模型将不再绑定到其原始框架,从而使其更具可移植性。
- 标准化:ONNX 提供了一种标准化的表示方式,有助于克服不同 AI 框架之间的兼容性问题。
要导出模型,请使用:
from ultralytics import YOLO
model = YOLO("yolo26n.pt")
onnx_file = model.export(format="onnx", dynamic=True)你可以按照 ONNX 集成指南 中的步骤来完成整个流程。
Link to this section我可以在 Triton Inference Server 上使用 Ultralytics YOLO26 模型运行推理吗?#
是的,你可以使用 Ultralytics YOLO26 模型在 NVIDIA Triton Inference Server 上运行推理。一旦你在 Triton 模型仓库中设置好模型并启动服务器,你就可以按如下方式加载并运行模型推理:
from ultralytics import YOLO
# Load the Triton Server model
model = YOLO("http://127.0.0.1:8000/yolo", task="detect")
# Run inference on the server
results = model("path/to/image.jpg")这种方法允许你在使用熟悉的 Ultralytics YOLO 接口的同时,利用 Triton 的优化功能。
Link to this sectionUltralytics YOLO26 在部署方面与 TensorFlow 和 PyTorch 模型相比如何?#
Ultralytics YOLO26 在部署方面与 TensorFlow 和 PyTorch 模型相比具有几项独特的优势:
- 实时性能:针对实时目标检测任务进行了优化,Ultralytics YOLO26 提供了业界领先的准确性和速度,使其成为需要实时视频分析的应用程序的理想选择。
- 易用性:Ultralytics YOLO26 与 Triton Inference Server 无缝集成,并支持多种导出格式(ONNX、TensorRT),使其在各种部署场景中都非常灵活。
- 高级功能:Ultralytics YOLO26 包含动态模型加载、模型版本控制和集成推理等功能,这些对于可扩展且可靠的 AI 部署至关重要。
- 简化的 API:Ultralytics API 在不同的部署目标上提供了一致的接口,减少了学习曲线和开发时间。
- 边缘优化:Ultralytics YOLO26 模型专为边缘部署而设计,即使在资源受限的设备上也能提供出色的性能。
有关更多详细信息,请在模型导出指南中比较部署选项。