YOLO26 模型的 TensorRT 导出
TensorRT 是 NVIDIA 为高速深度学习推理提供的 SDK。将 Ultralytics YOLO26 模型与 format="engine" 一起导出,会生成 .engine 文件。该文件针对构建它时使用的 NVIDIA GPU 进行了优化,支持 FP32、FP16 或 INT8 精度。
TensorRT 会针对构建引擎所用的 GPU 调优引擎。请在部署 GPU 上构建引擎,或在型号相同的 GPU 上使用相同的 TensorRT 版本构建;.engine 文件不是可移植的模型格式。对于边缘部署,Ultralytics Platform 提供八种 Jetson 目标配置,并记录了每种配置的实机构建和验证状态;你也可以在目标设备上本地导出。
概览#
| 属性 | 详情 |
|---|---|
format 参数 | engine(别名:tensorrt、trt) |
| 输出 | yolo26n.engine,一个以 JSON 元数据头(类别名称、任务、图像尺寸)开头的文件。中间文件 yolo26n.onnx 会保存在它旁边 |
| 运行平台 | NVIDIA GPU:数据中心、工作站和 Jetson,包括 Jetson DLA 核心(TensorRT 11 不支持) |
| 运行时 | TensorRT 7.0 或更高版本(不支持 10.2.0) |
| 精确率 | FP32、FP16、INT8 |
| 导出主机 | 配备 NVIDIA GPU 和 CUDA 的机器。请在部署 GPU 上构建,或在型号相同的 GPU 上构建 |
Ultralytics Platform 可以在你选择的 GPU 类型上为你构建 TensorRT 引擎,包括 NVIDIA Jetson 目标设备,因此你无需设置本地导出环境。
快速入门#
在配备 NVIDIA GPU 和 CUDA 的机器上安装 Ultralytics 软件包。在 Linux 上,如果缺少匹配 CUDA 版本的 TensorRT Python 软件包,首次导出时会自动安装。在 NVIDIA Jetson 上,请使用 JetPack 随附的 TensorRT 版本。在 Windows 上,请在导出前自行安装 TensorRT。
pip install ultralytics有关其他安装方法,请参阅安装指南。然后导出模型,并加载导出的引擎来运行推理或验证其准确率。推理和验证都需要 NVIDIA GPU。在 NVIDIA T4 上构建 YOLO26n 引擎需要一到三分钟。
from ultralytics import YOLO
# 加载 YOLO26 模型
model = YOLO("yolo26n.pt")
# 将模型导出为 TensorRT 格式
model.export(format="engine") # 创建 'yolo26n.engine'
# 或者改为导出 FP16 引擎
model.export(format="engine", quantize=16) # 创建 'yolo26n.engine'from ultralytics import YOLO
# 加载导出的 TensorRT 模型
model = YOLO("yolo26n.engine")
# 运行推理
results = model("https://ultralytics.com/images/bus.jpg")from ultralytics import YOLO
# 加载导出的 TensorRT 模型
model = YOLO("yolo26n.engine")
# 在 COCO8 数据集上验证准确率
metrics = model.val(data="coco8.yaml")支持的任务#
TensorRT 导出支持 YOLOv8、YOLO11 和 YOLO26 模型的全部七种 Ultralytics 任务:检测、实例分割、语义分割、深度估计、分类、姿态估计 和 OBB。语义分割和深度估计仅适用于 YOLO26;只有 YOLO26 系列提供这两种检测头。
导出参数#
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
format | str | 'engine' | 导出模型的目标格式,用于定义与各种部署环境的兼容性。 |
imgsz | int 或 tuple | 640 | 模型输入所需的图像尺寸。正方形图像可使用整数;若要指定具体尺寸,则可使用元组 (height, width)。 |
quantize | int 或 str | None | 量化精度:16(FP16)或 8(INT8/PTQ;需要校准 data/fraction);32/未设置时使用 FP32。使用 quantize=8 训练的检查点会根据自身携带的范围导出 INT8,无需校准;即使未设置 quantize 也是如此。设置 quantize=16 或 quantize=32 会引发错误。此参数取代已弃用的 half/int8 标志。 |
dynamic | bool | False | 构建的引擎可接受从 1 到 batch 的批次大小,以及从 32 像素到 imgsz 两倍的图像尺寸;对于较旧 GPU 上的动态 INT8 引擎,最小值会更高。设置 False 后,引擎只接受导出时的 batch 和 imgsz。 |
simplify | bool | True | 使用 onnxslim 简化模型图,可能提升性能和兼容性。 |
opset | int | None | 指定中间 ONNX 图的 ONNX opset 版本。如果未设置,则使用 opset 18(PyTorch < 2.4 时使用较低版本),且不高于已安装的 onnx 版本。 |
workspace | float 或 None | None | TensorRT 构建引擎时可使用的内存上限,单位为 GiB。保留 None,让 TensorRT 自动分配内存。 |
nms | bool,可选 | None | 选择原始输出(None,默认值)、嵌入式 NMS(True)或无 NMS 检测头(False)。设置 nms=True 后,conf(默认值 0.25)、iou、max_det 和 agnostic_nms 会在导出时固定;对于语义分割和深度模型,nms=True 会被忽略;对于分类模型,则会引发错误。 |
batch | int | 1 | 引擎的批次大小;使用 dynamic=True 时,此值为最大批次大小。它也是 INT8 校准时使用的批次大小。 |
data | str | None | 用于 INT8 校准的数据集 YAML 文件路径;分类任务则接受数据集目录或内置数据集名称。除非设置了 split,否则校准会使用 val 拆分。如果使用 quantize=8 时未指定该参数,Ultralytics 会使用适用于模型任务的小型默认数据集,例如检测任务使用 coco8.yaml;使用 quantize=8 训练的检查点则不需要数据集。 |
fraction | float、int 或 list | 1.0 | 校准子集,可按比例、图像数量或 [train, val, test] 比例/数量指定。包含两个项目的列表会让 test 保持完整,而 0 会跳过它。 |
device | str | None | 指定导出设备:GPU(device=0,未设置时使用),或 NVIDIA Jetson 的 DLA(device=dla:0 或 device=dla:1,搭配 quantize=16 或 quantize=8)。 |
在 NVIDIA Jetson 上,Ultralytics 使用 JetPack 版本附带的 TensorRT 运行时进行导出。在 JetPack 7 上,导出需要 TensorRT 10.15 或更高版本且低于 11。TensorRT 11 上的 device=dla:0 和 device=dla:1 会引发错误。NVIDIA 指出 TensorRT 10.7 是最后一个支持 DLA 的 10.x 版本,而 TensorRT 11.4.1 新增了 JetPack 7.2.2 支持,但 Ultralytics 目前尚未使用该版本。
所有格式共有的参数请参阅导出模式文档。
使用 INT8 量化导出 TensorRT#
quantize=8 使用训练后量化(PTQ)构建 INT8 引擎:导出时会将校准图像输入模型,以测量每个张量的值范围。通过量化感知训练使用 quantize=8 微调的检查点已经包含 INT8 范围,因此导出时会跳过校准。
在所有 TensorRT 版本中,quantize 和 data 参数都相同,但对于未使用 quantize=8 训练的检查点,底层导出流程有所不同:
| TensorRT 7-10 | TensorRT 11 | |
|---|---|---|
| INT8 方法 | 通过 IInt8Calibrator 接口进行隐式量化 | 显式量化:NVIDIA ModelOpt 会在 ONNX 图中插入 Q/DQ 节点 |
| 校准算法 | GPU 上使用 MINMAX_CALIBRATION,DLA 使用 ENTROPY_CALIBRATION_2 | ModelOpt max,在 CPU 上运行 |
| 使用的校准图像 | 该数据拆分中的每个完整批次 | 按随机顺序选取约 512 张图像;如果数据拆分较小,则使用其中的每个完整批次 |
使用 dynamic=True 和 quantize=8 | 未经测试;导出时不使用 dynamic=True | 支持 |
FP16(quantize=16) | TensorRT 构建器标志 | ModelOpt 自动转换为混合精度 |
首次使用时会安装 ModelOpt。Ultralytics 不会重复使用校准缓存,因此每次导出都会重新校准。关于这两种方法的详细信息,请参阅 TensorRT 文档中的使用量化类型。
对于未使用 quantize=8 训练的检查点,请使用 data 传入校准图像,例如 data="my_dataset.yaml"。除非设置了 split,否则校准会使用该数据集的 val 拆分;fraction 用于限制图像数量。NVIDIA 建议至少使用 500 张能代表部署数据的图像;少于 300 张时,导出过程会记录警告。如果没有设置 data,导出过程会记录警告,并使用适用于模型任务的小型示例数据集,例如检测任务使用 coco8.yaml。其验证拆分包含 4 张图像,足以测试导出,但不足以校准部署模型。
from ultralytics import YOLO
model = YOLO("yolo26n.pt")
# 使用 COCO 验证图像的 10%(500 张图像)进行校准;将 coco.yaml 替换为你自己的数据集
model.export(format="engine", quantize=8, data="coco.yaml", fraction=0.1)
# 加载导出的 INT8 引擎并运行推理
model = YOLO("yolo26n.engine")
result = model.predict("https://ultralytics.com/images/bus.jpg")在基准测试 GPU 上,INT8 引擎的运行速度比 FP32 引擎快 1.04 到 2.3 倍,但在十个模型中,只有三个模型的速度超过 FP16 引擎。YOLO26n 到 YOLO26l 的 INT8 精度降低了 1.7 到 2.9 mAP,YOLO26x 和 YOLO26n-pose 的精度损失则大得多。YOLO26n INT8 引擎比其 FP32 引擎更大(30 MB 对 11 MB),而大型模型的体积则会缩小(YOLO26l 为 53 MB 对 103 MB)。INT8 也可能改变置信度值,因此请根据 INT8 引擎自身的 F1 曲线选择置信度阈值。所有模型的表现请参阅基准测试。
输出格式#
该引擎接受一个名为 images 的输入,并返回与其构建所依据的 ONNX 导出相同的输出,但有一个例外:在低于 10 的 TensorRT 版本上,语义模型会返回形状为 (batch, classes, height, width) 的浮点类别分数,而不是 uint8 类别映射。有关各任务和每种 nms 设置对应的输出形状,请参阅 ONNX 输出格式。
在 Ultralytics 外部加载引擎#
Ultralytics 的 .engine 文件并不是纯 TensorRT 引擎。文件开头是一个 4 字节的小端长度值,后面紧跟相应字节数的 JSON 元数据,再后面才是序列化引擎。向 TensorRT 的 deserialize_cuda_engine 传入整个文件时,它会返回 None。如需使用 TensorRT API 加载引擎,请跳过文件头:
import json
import tensorrt as trt
with open("yolo26n.engine", "rb") as f:
n = int.from_bytes(f.read(4), byteorder="little") # 元数据头的长度
metadata = json.loads(f.read(n)) # 类别名称、任务、imgsz、批次、导出参数
engine = trt.Runtime(trt.Logger(trt.Logger.WARNING)).deserialize_cuda_engine(f.read())如果其他工具需要纯 TensorRT 引擎文件,请将文件头之后的字节写入新文件,或者使用导出时保存在引擎旁边的 yolo26n.onnx 文件,在该工具中构建引擎。在 TensorRT 11 上,FP16 和经过校准的 INT8 导出还会生成 yolo26n.fp16.onnx 和 yolo26n.int8.onnx;在其他工具中使用 .int8.onnx 文件构建 INT8 引擎。对于使用 quantize=8 训练的检查点,yolo26n.onnx 已包含 Q/DQ 节点,因此不会写入 .int8.onnx 文件;在 TensorRT 11 上,此类导出会生成 yolo26n.fp16.onnx,其中包含相同的 Q/DQ 图,其他层则采用 FP16。
部署#
YOLO("yolo26n.engine") 使用 TensorRT Python 运行时加载引擎。默认情况下,引擎在 GPU 0 上运行,即使传入 device="cpu" 也是如此。传入 device=1 可在另一块同型号 GPU 上运行引擎。使用 device=dla:0 或 device=dla:1 导出的引擎会在相应的 DLA 核心上运行。
未使用 dynamic=True 导出的引擎具有固定输入形状。predict 和 val 使用导出时的 imgsz,并忽略不同的 imgsz 参数。Ultralytics 会将较大的批次拆分为多个导出尺寸的批次,并对较小的批次进行填充,因此任意数量的图像都可以处理。使用 dynamic=True 导出的引擎可接受范围内的所有批次大小和图像尺寸,超出范围的形状会触发 ValueError: input size ... is outside the TensorRT optimization profile。使用 nms=True 时,超出范围的批次会改为拆分成多个导出尺寸的批次。
部署指南#
-
NVIDIA Triton Inference Server:使用 Triton 的 TensorRT 加速器提供 ONNX 导出服务,该加速器会在服务器 GPU 上构建引擎。
-
NVIDIA Jetson 快速入门:在 Jetson 设备上设置 Ultralytics,并使用 JetPack 导出 TensorRT 引擎,包括 DLA。
-
NVIDIA DGX Spark 快速入门:在 NVIDIA DGX Spark 上设置 Ultralytics 和 TensorRT。
-
NVIDIA Jetson 上的 DeepStream:在 NVIDIA DeepStream 视频流水线中运行导出的模型。
-
GitHub 上的 NVIDIA TensorRT:TensorRT 的源代码、示例和工具。
限制和故障排查#
- 引擎不可移植:引擎针对特定 GPU 型号和 TensorRT 版本构建。更换 GPU、TensorRT 版本或 JetPack 版本时,请重新构建引擎。无法加载引擎时,Ultralytics 会记录
TensorRT model exported with a different version than expected。 - 需要 GPU:未设置
device时,导出会使用device=0;对于device=cpu,则会因export running on CPU but must be on GPU而失败。 - 引擎无法在其他工具中加载:
.engine文件以元数据头开头,因此 TensorRT API 无法直接读取。请参阅在 Ultralytics 外部加载引擎。 - TensorRT 版本:Ultralytics 需要 TensorRT 7.0.0 或更高版本。不支持 TensorRT 10.2.0,自动安装也会跳过该版本。在 JetPack 7 和 NVIDIA DGX 系统上,导出需要 TensorRT 10.15 或更高版本(Jetson 上还必须低于 11);如果已安装的版本不在此范围内,导出会尝试安装符合要求的版本。
- 旧版 TensorRT 上的无 NMS 检测头:低于 8.5.0 的 TensorRT 版本无法构建 YOLO26 无 NMS 检测头。导出过程会记录警告并写入需要执行 NMS 的原始预测结果。请将 TensorRT 升级至 8.5.0 或更高版本,以便使用
nms=False导出。 - JetPack 6 上的 INT8:在 JetPack 6 上使用 TensorRT 10.3.0 时,带有
nms=True的 INT8 导出会失败,而带有nms=False的 INT8 导出会退回到原始预测结果。解决方法请参阅 Jetson 指南。 - TensorRT 10.16 上的 INT8:在 NVIDIA RTX PRO 6000 Blackwell 和 RTX 6000D GPU 上,使用 TensorRT 10.16.1 进行 INT8 导出时,因
Could not find any implementation for node而失败。在 RTX PRO 6000 Blackwell 上,TensorRT 10.13、10.14、10.15 和 11.4 均成功构建了 INT8 引擎。 - DLA:
device=dla:0和device=dla:1仅适用于 NVIDIA Jetson,且只能搭配quantize=16或quantize=8使用。在 TensorRT 11 上导出会引发错误。DLA 不支持的层会在 GPU 上运行。 - 动态形状:
dynamic=True需要最大批次大小,因此还要设置batch,例如batch=8。如果不设置,导出过程会记录警告,最大批次大小默认为 1。引擎可接受从 1 到batch的批次大小,以及从 32 像素到导出时imgsz两倍的图像尺寸。例如,使用imgsz=320 batch=4导出的引擎可在 640 像素下运行,但对 704 像素或批次大小为 6 的输入会触发input size ... is outside the TensorRT optimization profile。仅在 TensorRT 11 上组合使用dynamic=True和quantize=8。对于计算能力为 7.5 或更低的 GPU(例如 T4),此类动态 INT8 引擎的最小尺寸为 64 像素(P6 模型为 128 像素)。 - 固定输入尺寸:未使用
dynamic=True导出的引擎始终以导出时的imgsz运行。向 predict 或 val 传入其他imgsz不会产生任何效果;如需更改输入尺寸,请重新导出。 - 未使用
data的 INT8 导出:除非检查点使用quantize=8训练,否则导出会使用该任务的小型默认数据集进行校准。对于检测任务,该数据集只有 4 张验证图像,且导出过程会记录警告。请通过data传入有代表性的图像;少于 300 张时,导出过程会发出警告。 - 首次推理较慢:使用新引擎时,或在动态引擎上首次使用新的
imgsz时,前几次调用所需时间会比后续调用更长。
其他错误请参阅常见问题指南。
基准测试#
YOLO26 模型页面列出了所有模型在 NVIDIA T4 GPU 上使用 TensorRT 10 运行各项任务的速度,语义分割除外。
FP32、FP16 和 INT8 对比#
下表对比了所有 YOLO26 检测模型,以及其他五种任务中的 YOLO26n 模型在 PyTorch 和 FP32、FP16、INT8 TensorRT 11.4 引擎上的表现。所有数值均在一块 NVIDIA RTX PRO 6000 Blackwell Server Edition GPU 上测得。
每张图像耗时(毫秒),数值越低越好。每个模型速度最快的格式以粗体标出;由于 YOLO26x INT8 引擎的准确率不稳定,因此未列入比较。
| 模型 | imgsz | PyTorch | TensorRT FP32 | TensorRT FP16 | TensorRT INT8 |
|---|---|---|---|---|---|
| YOLO26n | 640 | 2.85 | 0.67 | 0.48 | 0.53 |
| YOLO26s | 640 | 2.98 | 1.15 | 0.61 | 0.66 |
| YOLO26m | 640 | 3.29 | 2.22 | 1.03 | 1.08 |
| YOLO26l | 640 | 4.70 | 3.03 | 1.49 | 1.43 |
| YOLO26x | 640 | 5.93 | 4.31 | 2.09 | 1.84 |
| YOLO26n-seg | 640 | 3.18 | 0.93 | 0.65 | 0.72 |
| YOLO26n-sem | 1024 | 1.74 | 0.74 | 0.45 | 0.71 |
| YOLO26n-cls | 224 | 1.75 | 0.29 | 0.24 | 0.20 |
| YOLO26n-pose | 640 | 3.39 | 0.76 | 0.51 | 0.59 |
| YOLO26n-obb | 1024 | 4.55 | 1.03 | 0.64 | 0.88 |
- FP16 在所有模型上的准确率与 FP32 相差不超过 0.1 个百分点。在检测模型上,它的运行速度是 FP32 引擎的 1.4 到 2.2 倍,是 PyTorch 的 2.8 到 5.9 倍。
- INT8 在检测模型上的延迟不超过 FP16 的 12%,但 YOLO26n、YOLO26s、YOLO26m 和 YOLO26l 的 mAP 下降了 1.7 到 2.9。在这块 GPU 上,只有 YOLO26l、YOLO26x 和 YOLO26n-cls 的运行速度比 FP16 快。
- YOLO26x INT8 的准确率在三个构建版本中降至 25.3 到 38.5 mAP,而 FP32 为 57.5。YOLO26n-pose INT8 的姿态 mAP 从 56.7 降至 33.4。部署前,请在你自己的数据上验证每个 INT8 引擎。
- TensorRT 10.16 的 YOLO26n 和 YOLO26s FP32、FP16 引擎准确率与 TensorRT 11.4 相同。其 FP16 引擎速度约快 7%,FP32 引擎速度则比 TensorRT 11.4 慢 4% 到 9%。
关于 NVIDIA Jetson 和 DGX Spark,请参阅 Jetson 基准测试 和 DGX Spark 指南。
测量环境:ultralytics 8.4.211、TensorRT 11.4.0、PyTorch 2.15.0 和 CUDA 13.2,测量日期为 2026 年 11 月 13 日,测量位置为 batch=1。延迟是 val 报告的推理阶段耗时,不包括预处理或后处理。
关于 TensorRT#
TensorRT 通过融合层、为每一层选择构建 GPU 上速度最快的内核,并可选择将精度降至 FP16 或 INT8 来构建引擎。完整参考资料请参阅 TensorRT 文档。
常见问题#
可以,但只能在型号相同且 TensorRT 版本相同的 GPU 上运行。引擎是针对一种 GPU 型号和一个 TensorRT 版本构建的。要在不同硬件之间共享同一个文件,请导出为 ONNX,然后在每台设备上构建引擎。对于 NVIDIA Jetson,Ultralytics Platform 可以为八种 Jetson 目标设备构建引擎。
从 FP16(
quantize=16)开始。它不需要校准数据,并且能保持 FP32 的准确率。在 NVIDIA RTX PRO 6000 Blackwell 和 TensorRT 11.4 上进行的基准测试中,FP16 将 YOLO26s 的延迟从每张图像 1.15 ms 降至 0.61 ms,同时保持 mAP验证 50-95 为 48.5。INT8(quantize=8)的运行时间为 0.66 ms,mAP验证 50-95 为 46.3,因此在这块 GPU 上并不比 FP16 快;除非检查点使用quantize=8训练,否则还需要有代表性的校准数据。INT8 在其他硬件上可能更有优势:Jetson 基准测试列出了嵌入式设备上的 FP16 和 INT8 结果。选择 INT8 前,请在你的部署设备上测量这两种精度的性能。使用
dynamic=True导出,并将batch设置为你计划运行的最大批次。这样,引擎便可接受从 1 到batch的批次,以及从 32 像素到导出imgsz两倍的图像尺寸;较旧 GPU 上的动态 INT8 引擎则有更高的最小尺寸。不使用dynamic=True时,引擎只有一个固定输入形状,Ultralytics 会拆分或填充每个批次以适配该形状。动态引擎from ultralytics import YOLO model = YOLO("yolo26n.pt") model.export(format="engine", dynamic=True, batch=8, quantize=16) model = YOLO("yolo26n.engine") results = model(["https://ultralytics.com/images/bus.jpg"] * 4, batch=4) # 一批 4 张图像在 Jetson 设备本机上,使用 JetPack 版本附带的 TensorRT 版本运行导出。要使用 DLA 核心,请添加
device=dla:0和quantize=16或quantize=8。有关设置和基准测试,请参阅 NVIDIA Jetson 指南。在 Jetson 上导出from ultralytics import YOLO model = YOLO("yolo26n.pt") model.export(format="engine", quantize=16) # Jetson GPU 上的 FP16 引擎 # 或者改为为 DLA 核心 0 构建 FP16 引擎 model.export(format="engine", quantize=16, device="dla:0")Triton 推理服务器指南会将模型导出为 ONNX,并在
config.pbtxt中启用 Triton 的 TensorRT 加速器。随后,Triton 会在首次加载时于服务器 GPU 上构建 TensorRT 引擎并将其缓存,因此引擎始终与服务器的 GPU 和 TensorRT 版本匹配。本指南涵盖模型仓库布局、服务器容器,以及使用 Ultralytics 进行客户端推理。
评论116
提出问题、分享结果,或帮助其他人。
AgbajeAyomipo
Hi Ultralytics team, I’ve trained a YOLOv11 model on (1024x1024) images from a camera feed and now I’m trying to run real-time inference on live streams using a Jetson Orin Nano. I want to maintain full resolution for inference because of accuracy requirements, but currently I'm getting only 1-2 FPS. When I reduce the input size, the FPS improves significantly, but it's not acceptable due to missed detections at lower resolutions. I attempted to export the model to TensorRT using:
model.export(format="engine", imgsz=(2592, 1944), half=True) but it fails silently. If I use a dynamic=true without specifying the imgsz parameter, the export succeeds and uses (1024, 1024). My questions:
Is TensorRT export for such high-resolution input supported/recommended with YOLOv11? If not, is there any known limitation or workaround? Are there ways to improve performance with high-res inference on Jetson Orin Nano? What hardware would you recommend for real-time inference on 1-3 camera streams with 5MP inputs, assuming we need at least ~15-20 FPS per stream? Device: AAEON BOXER-8621AI Module: NVIDIA® Jetson Orin Nano™ (4GB LPDDR5, Super Mode Enabled) Memory: 4 GB LPDDR5 I/O: USB 3.2 Gen 2, M.2 Expansion Operating System: JetPack 5.1.4 (L4T 35.6.0, Ubuntu 20.04) Use Case: Real-time multi-stream 5MP video inference
1martinmclee
Have you gotten any workaround? Perhaps with the YOLO26?
AgbajeAyomipo
Hi Ultralytics team, I’ve trained a YOLOv11 model on full-size 5MP (2592x1944) images from a camera feed and now I’m trying to run real-time inference on live streams using a Jetson Orin Nano. I want to maintain full resolution for inference because of accuracy requirements, but currently I'm getting only 1-2 FPS. When I reduce the input size, the FPS improves significantly, but it's not acceptable due to missed detections at lower resolutions. I attempted to export the model to TensorRT using:
model.export(format="engine", imgsz=(2592, 1944), half=True)but it fails silently. If I use a dynamic=true, the export succeeds and uses (1024, 1024). My questions:- Is TensorRT export for such high-resolution input supported/recommended with YOLOv11? If not, is there any known limitation or workaround?
- Are there ways to improve performance with high-res inference on Jetson Orin Nano?
- What hardware would you recommend for real-time inference on 1-3 camera streams with 5MP inputs, assuming we need at least ~15-20 FPS per stream? Device: AAEON BOXER-8621AI Module: NVIDIA® Jetson Orin Nano™ (4GB LPDDR5, Super Mode Enabled) Memory: 4 GB LPDDR5 I/O: USB 3.2 Gen 2, M.2 Expansion Operating System: JetPack 5.1.4 (L4T 35.6.0, Ubuntu 20.04) Use Case: Real-time multi-stream 5MP video inference
1020423lzl
m的模型大小比n大的多,速度也慢了很多,输入图像尺寸越大,越吃显存,速度也越慢
李子乐控制工程 @.***
---Original--- From: @.> Date: Sat, May 9, 2026 10:30 AM To: @.>; Cc: @.@.>; Subject: Re: [ultralytics/ultralytics] zh/integrations/tensorrt/ (Discussion #9868)
现在有一个奇怪的现象,我导出的tensorrt模型的推理速度几乎和原来的.pt几乎没有差别甚至更慢,并且导出的tensorrt模型的推理结果和.pt模型的差别很大。 我的设备是i914900k+64G RAM+RTX A6000 我的环境是cuda12.4.131+tensorrt10.10.0.31+torch2.5.1+ultralytics8.3.252 导出命令: yolo export model=yolo11m.pt imgsz=1280 format='engine 推理代码为: model.predict(stream_path, imgsz=1280, classes=[14], conf=0.2, iou=0.5, stream=True, task='detect')
另外我试了下yolo11n.pt的640输入尺寸导出,推理速度就有明显提升,当然推理结果差别仍然较大。 请问这是什么道理,有什么解决办法?
— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you commented.Message ID: @.***>
User能不能读完再说话,肯定是m和m比,n和n比,同样尺寸输入和同样尺寸输入的比啊。 该显卡的RT core算力是普通fp32算力的近两倍。
Glenn JocherUltralytics是的,性能应当在同一模型、同一
imgsz、同一batch、同一后处理参数下比较;m比n更大、1280比640更吃显存且更慢是正常现象。对 RTX A6000 来说,FP32的 TensorRT 往往不会比.pt明显更快,建议先按 TensorRT 导出文档 用half=True重新导出,并在相同输入上预热后再统计平均耗时;如果engine与.pt结果仍明显不一致,请贴出!yolo checks、完整控制台日志文本和一个最小可复现示例,我们好继续排查环境或后处理差异。
User现在有一个奇怪的现象,我导出的tensorrt模型的推理速度几乎和原来的.pt几乎没有差别甚至更慢,并且导出的tensorrt模型的推理结果和.pt模型的差别很大。 我的设备是i914900k+64G RAM+RTX A6000 我的环境是cuda12.4.131+tensorrt10.10.0.31+torch2.5.1+ultralytics8.3.252 导出命令:
yolo export model=yolo11m.pt imgsz=1280 format='engine推理代码为:model.predict(stream_path, imgsz=1280, classes=[14], conf=0.2, iou=0.5, stream=True, task='detect')另外我试了下yolo11n.pt的640输入尺寸导出,推理速度就有明显提升,当然推理结果差别仍然较大。 请问这是什么道理,有什么解决办法?
020423lzl
etson@yahboom:~/ultralytics/ultralytics$ yolo predict model=best.engine source=0 save=False show WARNING ⚠️ Unable to automatically guess model task, assuming 'task=detect'. Explicitly define task for your model, i.e. 'task=detect', 'segment', 'classify','pose' or 'obb'. Ultralytics 8.3.172 🚀 Python-3.10.12 torch-2.5.0a0+872d972e41.nv24.08 CUDA:0 (Orin, 7620MiB) Loading best.engine for TensorRT inference... [12/18/2025-17:11:05] [TRT] [I] Loaded engine size: 75 MiB [12/18/2025-17:11:05] [TRT] [I] [MS] Running engine with multi stream info [12/18/2025-17:11:05] [TRT] [I] [MS] Number of aux streams is 7 [12/18/2025-17:11:05] [TRT] [I] [MS] Number of total worker streams is 8 [12/18/2025-17:11:05] [TRT] [I] [MS] The main stream provided by execute/enqueue calls is the first worker stream [12/18/2025-17:11:05] [TRT] [I] [MemUsageChange] TensorRT-managed allocation in IExecutionContext creation: CPU +1, GPU +88, now: CPU 1, GPU 158 (MiB)
[ WARN:0@2.771] global cap_gstreamer.cpp:1777 open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1 1/1: 0... Success ✅ (inf frames of shape 1920x1080 at 5.00 FPS)
Traceback (most recent call last): File "/home/jetson/.local/bin/yolo", line 7, in sys.exit(entrypoint()) File "/home/jetson/Downloads/ultralytics-main/ultralytics/cfg/init.py", line 983, in entrypoint getattr(model, mode)(**overrides) # default args from model File "/home/jetson/Downloads/ultralytics-main/ultralytics/engine/model.py", line 555, in predict return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream) File "/home/jetson/Downloads/ultralytics-main/ultralytics/engine/predictor.py", line 247, in predict_cli for _ in gen: # sourcery skip: remove-empty-nested-block, noqa File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 36, in generator_context response = gen.send(None) File "/home/jetson/Downloads/ultralytics-main/ultralytics/engine/predictor.py", line 351, in stream_inference s[i] += self.write_results(i, Path(paths[i]), im, s) File "/home/jetson/Downloads/ultralytics-main/ultralytics/engine/predictor.py", line 434, in write_results string += f"{result.verbose()}{result.speed['inference']:.1f}ms" File "/home/jetson/Downloads/ultralytics-main/ultralytics/engine/results.py", line 694, in verbose return "".join(f"{n} {self.names[i]}{'s' * (n > 1)}, " for i, n in enumerate(counts) if n > 0) File "/home/jetson/Downloads/ultralytics-main/ultralytics/engine/results.py", line 694, in return "".join(f"{n} {self.names[i]}{'s' * (n > 1)}, " for i, n in enumerate(counts) if n > 0) KeyError: 171 terminate called without an active exception Aborted (core dumped) jetson@yahboom:~/ultralytics/ultralytics$ How to solve it
Glenn JocherUltralyticsKeyError: 171表示模型输出了类别171,但当前names里没有这个索引(通常是自定义数据集的标签类别编号不在0..nc-1范围内、data.yaml里的names数量与nc不匹配,或导出/推理用的模型与数据配置不一致导致的元数据错配);请先把所有标注.txt的 class id 修正为连续的0..nc-1,同步修正data.yaml的nc/names,修正后删除数据集labels/下的*.cache,再用同一套环境从best.pt重新导出.engine(参考 TensorRT 导出文档),并在推理时显式加上task=detect。另外请把下面命令的完整输出(文本)贴在这个 discussion 里,以及你当时的导出命令,方便我们确认是否有多环境/路径冲突:yolo checks020423lzl
Thank you for your reply. The issue has been resolved. I was using the RT-DETR model, but I mistakenly used "from ultralytics import YOLO" when exporting
Glenn JocherUltralyticsGlad you found it — RT-DETR models should be loaded/exported with
RTDETR(...)(mixing them withYOLO(...)can produce mismatched metadata likenames), see the RT-DETR model docs for details:from ultralytics import RTDETR model = RTDETR("best.pt") model.export(format="engine") # creates best.engine
fly2skyToEnjoy
May I ask : The following command exports the file, whether the box and mask are both nmsed at the same time ???
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'
Load the exported TensorRT model
tensorrt_model = YOLO("yolo11n.engine")
Run inference
results = tensorrt_model("https://ultralytics.com/images/bus.jpg")
AK993
这个方法适用于rt-detr吗
dilanpatel195
I am trying to prediction model for the YOLOv11n-seg, I have exported the .py to .engine to back it run faster but keep getting a KEY error message:
KeyError Traceback (most recent call last) Cell In[4], line 109 106 # Inside the main loop after grabbing a frame 107 if inference_active and not inpainting_active: 108 # Perform YOLO inference --> 109 results = model(rgb_frame)[0] 110 masked_image = rgb_frame.copy() # Make a copy for masking 112 # Process YOLO masks (original functionality remains unchanged)
File c:\ProgramData\miniconda3\Lib\site-packages\ultralytics\engine\model.py:176, in Model.call(self, source, stream, **kwargs) 147 def call( 148 self, 149 source: Union[str, Path, int, Image.Image, list, tuple, np.ndarray, torch.Tensor] = None, 150 stream: bool = False, 151 **kwargs, 152 ) -> list: 153 """ 154 Alias for the predict method, enabling the model instance to be callable for predictions. 155 (...) 174 ... print(f"Detected {len(r)} objects in image") 175 """ --> 176 return self.predict(source, stream, **kwargs)
File c:\ProgramData\miniconda3\Lib\site-packages\ultralytics\engine\model.py:554, in Model.predict(self, source, stream, predictor, **kwargs) 552 if prompts and hasattr(self.predictor, "set_prompts"): # for SAM-type models 553 self.predictor.set_prompts(prompts) --> 554 return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
File c:\ProgramData\miniconda3\Lib\site-packages\ultralytics\engine\predictor.py:168, in BasePredictor.call(self, source, model, stream, *args, **kwargs) 166 return self.stream_inference(source, model, *args, **kwargs) 167 else: --> 168 return list(self.stream_inference(source, model, *args, **kwargs))
File c:\ProgramData\miniconda3\Lib\site-packages\torch\utils_contextlib.py:36, in _wrap_generator..generator_context(*args, **kwargs) 33 try: 34 # Issuing None to a generator fires it up 35 with ctx_factory(): ---> 36 response = gen.send(None) 38 while True: 39 try: 40 # Forward the response to our caller and get its next request
File c:\ProgramData\miniconda3\Lib\site-packages\ultralytics\engine\predictor.py:274, in BasePredictor.stream_inference(self, source, model, *args, **kwargs) 268 self.results[i].speed = { 269 "preprocess": profilers[0].dt * 1e3 / n, 270 "inference": profilers[1].dt * 1e3 / n, 271 "postprocess": profilers[2].dt * 1e3 / n, 272 } 273 if self.args.verbose or self.args.save or self.args.save_txt or self.args.show: --> 274 s[i] += self.write_results(i, Path(paths[i]), im, s) 276 # Print batch results 277 if self.args.verbose:
File c:\ProgramData\miniconda3\Lib\site-packages\ultralytics\engine\predictor.py:334, in BasePredictor.write_results(self, i, p, im, s) 332 result = self.results[i] 333 result.save_dir = self.save_dir.str() # used in other locations --> 334 string += f"{result.verbose()}{result.speed['inference']:.1f}ms" 336 # Add predictions to image 337 if self.args.save or self.args.show:
File c:\ProgramData\miniconda3\Lib\site-packages\ultralytics\engine\results.py:663, in Results.verbose(self) 661 for c in boxes.cls.unique(): 662 n = (boxes.cls == c).sum() # detections per class --> 663 log_string += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " 664 return log_string
KeyError: 2
Any help would be greatly appreciated!
Ranjith3436
Now, My TensorRT Sengment model used for track. if any detections not there the processing FPS is 75-80 in jetson. But once the person or people detected suddenly reused to 15-20. processing time going to 40ms. only when the object detected. What's the issue in model.track(), could i run all the tracking thing on GPU.
Glenn JocherUltralytics(已编辑)@Ranjith3436 the performance drop when objects are detected is expected behavior, as tracking introduces additional computational overhead. When objects are detected, the model.track() method performs several CPU-bound operations including feature extraction, association, and trajectory management.
To improve performance:
- Try using a smaller model variant (like YOLO26n-seg instead of larger variants)
- Consider reducing the image resolution for tracking
- Optimize tracking parameters by setting
persist=Trueand adjustingconfthresholds higher to track fewer objects - Use
half=True(FP16) orint8=Truewhen exporting your model to TensorRT for maximum GPU acceleration
For the tracking components specifically, the BoT-SORT or ByteTrack algorithms used by Ultralytics do have some CPU-bound operations, but you can minimize their impact with the above optimizations. For more details on tracking optimization, check our tracking documentation.
1
Ranjith3436
I convert Classification model that i retrained. But it throw the error "amax(): Expected reduction dim 1 to have non-zero size."
Glenn JocherUltralytics@Ranjith3436 this error typically occurs when TensorRT encounters an empty dimension during model conversion. For classification models, this could happen if:
- Your model architecture was modified or if you're using a custom architecture
- There might be an issue with batch normalization layers during export
- The class count in your retrained model might be causing dimension mismatches
Try these solutions:
- Ensure your model's forward pass works correctly before export
- Try exporting with
simplify=Falseto preserve the original graph structure - Check if your classification model has the correct output dimension for your number of classes
If the issue persists, share more details about your model architecture and export command for better assistance. For more guidance, refer to our TensorRT integration documentation.
Ranjith3436
The model trained use yolov8l-cls.pt and the error come from jetson device.
(model): ClassificationModel( (model): Sequential( (0): Conv( (conv): Conv2d(3, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (1): Conv( (conv): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (2): C2f( (cv1): Conv( (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False) (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (cv2): Conv( (conv): Conv2d(320, 128, kernel_size=(1, 1), stride=(1, 1), bias=False) (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (m): ModuleList( (0-2): 3 x Bottleneck( (cv1): Conv( (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (cv2): Conv( (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) ) ) ) (3): Conv( (conv): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (4): C2f( (cv1): Conv( (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False) (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (cv2): Conv( (conv): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False) (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (m): ModuleList( (0-5): 6 x Bottleneck( (cv1): Conv( (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (cv2): Conv( (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) ) ) ) (5): Conv( (conv): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (6): C2f( (cv1): Conv( (conv): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1), bias=False) (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (cv2): Conv( (conv): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False) (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (m): ModuleList( (0-5): 6 x Bottleneck( (cv1): Conv( (conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (cv2): Conv( (conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) ) ) ) (7): Conv( (conv): Conv2d(512, 1024, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) (bn): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (8): C2f( (cv1): Conv( (conv): Conv2d(1024, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False) (bn): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (cv2): Conv( (conv): Conv2d(2560, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False) (bn): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (m): ModuleList( (0-2): 3 x Bottleneck( (cv1): Conv( (conv): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (cv2): Conv( (conv): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) ) ) ) (9): Classify( (conv): Conv( (conv): Conv2d(1024, 1280, kernel_size=(1, 1), stride=(1, 1), bias=False) (bn): BatchNorm2d(1280, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (act): SiLU(inplace=True) ) (pool): AdaptiveAvgPool2d(output_size=1) (drop): Dropout(p=0.0, inplace=True) (linear): Linear(in_features=1280, out_features=2, bias=True) ) ) ) )
model architecture
Ranjith3436
This error sorted out