Ultralytics YOLO27:

Triton 推理服务器与 Ultralytics YOLO26#

Triton 推理服务器(以前称为 TensorRT 推理服务器)是 NVIDIA 开发的开源软件解决方案。它提供了针对 NVIDIA GPU 优化的云端推理解决方案。Triton 简化了生产环境中大规模部署 AI 模型的流程。将 Ultralytics YOLO26 与 Triton 推理服务器集成后,你可以部署可扩展、高性能的深度学习推理工作负载。本指南介绍设置和测试该集成的方法。



Watch: Getting Started with NVIDIA Triton Inference Server.

什么是 Triton 推理服务器?#

Triton 推理服务器旨在将各种 AI 模型部署到生产环境中。它支持广泛的深度学习和机器学习框架,包括 PyTorchTensorFlowONNXOpenVINOTensorRT 以及许多其他框架。其主要使用场景包括:

  • 从单个服务器实例提供多个模型服务
  • 无需重启服务器即可动态加载和卸载模型
  • 集成推理,允许同时使用多个模型以获得结果
  • 用于 A/B 测试和滚动更新的模型版本管理

Triton 推理服务器的主要优势#

将 Triton 推理服务器与 Ultralytics YOLO26 搭配使用可带来多项优势:

  • 自动批处理:在处理前将多个 AI 请求组合在一起,从而降低延迟并提高推理速度
  • Kubernetes 集成:云原生设计可与 Kubernetes 无缝协作,用于管理和扩展 AI 应用
  • 针对硬件的优化:充分利用 NVIDIA GPU,以实现最高性能
  • 框架灵活性:支持多种 AI 框架,包括 PyTorchTensorFlowONNXOpenVINOTensorRT
  • 开源且可定制:可以根据具体需求进行修改,确保适用于各种 AI 应用

前置条件#

继续操作前,请确保你具备以下先决条件:

  • 你的计算机上已安装 Docker(>= 28.2.0,并安装 NVIDIA Container Toolkit >= 1.18 以支持 CDI GPU 访问)或 Podman
  • 安装 ultralytics
    pip install ultralytics
  • 安装 tritonclient
    pip install tritonclient[all]

设置 Triton 推理服务器#

运行此完整设置代码块,将 Ultralytics YOLO26 导出为 ONNX,构建 Triton 模型仓库,并启动 Triton 推理服务器:

注意

在脚本中使用 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)

运行推理#

使用 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)

TensorRT 优化(可选)#

如需进一步提升性能,你可以将 TensorRT 与 Triton 推理服务器搭配使用。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 推理服务器上部署并运行 Ultralytics YOLO26 模型,以实现可扩展的高性能推理。有关更多详情,请参阅 Triton 官方文档,或向 Ultralytics 社区寻求帮助。

常见问题#

  • 设置 Ultralytics YOLO26NVIDIA Triton 推理服务器 的集成主要包括以下几个步骤:

    1. 将 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)
    2. 设置 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()
    3. 运行 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 推理服务器上高效地大规模部署 Ultralytics YOLO26 模型,以实现高性能 AI 模型推理。

  • Ultralytics YOLO26NVIDIA Triton 推理服务器 集成可带来多项优势:

    • 可扩展的 AI 推理:Triton 允许从单个服务器实例提供多个模型服务,支持动态加载和卸载模型,因此能够高度扩展,满足多样化的 AI 工作负载需求。
    • 高性能:Triton 推理服务器针对 NVIDIA GPU 进行了优化,可确保高速推理操作,非常适合目标检测等实时应用。
    • 集成推理和模型版本管理:Triton 的集成模式支持组合多个模型以改善结果,其模型版本管理功能支持 A/B 测试和滚动更新。
    • 自动批处理:Triton 会自动将多个推理请求组合在一起,从而显著提高吞吐量并降低延迟。
    • 简化部署:无需彻底改造整个系统即可逐步优化 AI 工作流,从而更轻松地实现高效扩展。

    有关设置和运行 Ultralytics YOLO26 与 Triton 的详细说明,请参阅设置 Triton 推理服务器运行推理

  • 在将 Ultralytics YOLO26 模型部署到 NVIDIA Triton 推理服务器 之前使用 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 集成指南中的步骤完成此过程。

  • 可以,你可以在 NVIDIA Triton 推理服务器上使用 Ultralytics YOLO26 模型运行推理。将模型设置在 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 的优化功能。

  • 在部署方面,Ultralytics YOLO26 相比 TensorFlow 和 PyTorch 模型具有多项独特优势:

    • 实时性能:针对实时目标检测任务进行了优化,Ultralytics YOLO26 可提供业界领先的准确率和速度,非常适合需要实时视频分析的应用。
    • 易于使用Ultralytics YOLO26 可与 Triton 推理服务器无缝集成,并支持多种导出格式(ONNXTensorRT),因此能够灵活适应各种部署场景。
    • 高级功能:通过 Triton 提供服务可以实现动态模型加载、模型版本控制和集成推理,这对于可扩展且可靠的 AI 部署至关重要。
    • 简化的 API:Ultralytics API 在不同部署目标之间提供一致的接口,降低了学习曲线并缩短了开发时间。
    • 边缘优化Ultralytics YOLO26 模型在设计时考虑了边缘部署,即使在资源受限的设备上也能提供出色性能。

    有关更多详情,请在模型导出指南中比较各种部署选项。

评论