导出模式 (Export)
Ready一站式导出 ONNX、TensorRT 10/11 (.engine)、TorchScript 与 OpenVINO,支持强类型半精度与融合算子插件。
选择部署格式
Detrium 导出器内置 Tracing 上下文保护(导出期间自动将自定义 CUDA 扩展切换为标准算子路径),并支持调用 onnxslim / onnxsim 简化计算图。
from detrium import Detrium
model = Detrium("runs/detect/train/weights/best.pt")
onnx_path = model.export(format="onnx", imgsz=640, dynamic=True, simplify=True)
trt_path = model.export(format="engine", imgsz=640, half=True)支持的导出格式矩阵
format 值 |
产物后缀 | 核心特性与适用场景 |
|---|---|---|
onnx |
.onnx |
通用跨平台中间表示,支持动态轴与 onnxslim/onnxsim 简化 |
engine |
.engine |
TensorRT 10/11 高性能引擎,支持 strongly_typed 与 AOT dfine::MSDeformAttnV2Fused 插件 |
torchscript |
.torchscript |
PyTorch C++ (LibTorch) 原生部署格式 |
openvino |
.xml |
Intel CPU / iGPU / NPU 优化推理格式 |
参数说明
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
format |
str |
"onnx" |
目标格式:onnx、engine、torchscript、openvino |
output |
str | Path | None |
None |
自定义导出路径;默认输出到原权重同级目录 |
imgsz |
int | tuple | None |
None |
静态或动态参考分辨率 |
batch |
int |
1 |
默认导出 batch 维度 |
dynamic |
bool |
True |
是否启用动态 batch / 空间维度 |
simplify |
bool |
True |
优先使用 onnxslim,缺失时回退 onnxsim 简化图结构 |
half |
bool |
False |
启用 FP16 半精度导出 |
precision |
str | None |
None |
强类型目标精度(fp32 / fp16 / bf16) |
strongly_typed |
bool |
False |
为 TensorRT 10/11 生成强类型 ONNX 并开启强类型构建 |
trt_plugin |
bool |
False |
注入 dfine::MSDeformAttnV2Fused AOT TensorRT 插件 |
opset |
int |
17 |
ONNX Opset 版本号 |
workspace_gb |
float |
4.0 |
TensorRT 构建阶段可用显存工作区(GiB) |
builder_optimization_level |
int |
3 |
TensorRT Builder 优化等级(0–5) |
导出后零 YAML 验证与推理
导出产物已自动绑定元数据(names、imgsz、task、model_name),可直接加载运行:
from detrium import Detrium
ort_model = Detrium("runs/detect/train/weights/best.onnx")
results = ort_model("images/", conf=0.3)
print(ort_model.benchmark(imgsz=640))