fix: export TensorRT engine from .pt source, not ONNX

This commit is contained in:
jetson committed 2026-11-04 11:08:09 +07:00
1 parent 7a14672b71
commit ad702faccc
2 files changed
+22 -4

No files matched your search

+4 -2
View File
@@ -15,8 +15,10 @@ version tags are cut in this repo — `main` is the release line).
can be converted to a TensorRT `.engine` from the UI or via
`/api/models/<stem>/convert` (aliases accept the stem in the URL or body);
conversion runs in a background thread in two stages (`.pt` -> `.onnx` via
Ultralytics `export(format="onnx", imgsz=640)`, then `.onnx` -> `.engine`
via `export(format="engine", device=0, half=True, imgsz=640)`), with status
Ultralytics `export(format="onnx", imgsz=640)`, then `.pt` -> `.engine`
via `export(format="engine", device=0, half=True, imgsz=640)`; Ultralytics
cannot export an engine from an ONNX file, so a sibling `.pt` is required
for the engine stage), with status
polling on `/api/models/conversions/<id>` (list `/api/models/conversions`,
cancel `/.../cancel`). Known classes are derived from model metadata when
the stem is not in `KNOWN_MODEL_CLASSES` and cached by path/mtime/size.
+18 -2
View File
@@ -127,6 +127,10 @@ class ConversionManager:
stem = source_path.stem
onnx_path = models_dir / f"{stem}.onnx"
engine_path = models_dir / f"{stem}.engine"
# Keep the original source for the engine export: Ultralytics can
# only export a TensorRT engine from a PyTorch (.pt) model, not
# from an already-exported ONNX file.
engine_source = source_path
# Stage 1: .pt -> .onnx
if source_path.suffix.lower() == ".pt":
@@ -146,7 +150,19 @@ class ConversionManager:
if not source_path.is_file():
raise FileNotFoundError(f"onnx export did not produce {source_path}")
# Stage 2: .onnx -> .engine
# Stage 2: .pt -> .engine (Ultralytics cannot export from ONNX).
# If the original source was .onnx, fall back to a sibling .pt
# with the same stem when one exists.
if engine_source.suffix.lower() != ".pt":
pt_candidate = models_dir / f"{engine_source.stem}.pt"
if pt_candidate.is_file():
engine_source = pt_candidate
if engine_source.suffix.lower() != ".pt" or not engine_source.is_file():
raise RuntimeError(
"TensorRT engine export requires a .pt source; "
f"no .pt found for {stem!r}"
)
if engine_path.exists() and not force:
job.stage = "engine (exists)"
job.progress = 100.0
@@ -155,7 +171,7 @@ class ConversionManager:
job.progress = 50.0
from ultralytics import YOLO
model = YOLO(str(source_path))
model = YOLO(str(engine_source))
model.export(format="engine", device=0, half=True, imgsz=640)
job.progress = 95.0