ultralytics/yolov5 · error · RuntimeError
failed to load ONNX file: {onnx}
Error message
failed to load ONNX file: {onnx} What it means
export.py's TensorRT builder raises RuntimeError when trt.OnnxParser.parse_from_file() returns False, meaning the TensorRT parser could not digest the ONNX file. This happens inside export_engine after the network is created, so TensorRT itself is installed and working; the ONNX graph is the problem (corrupt file, unsupported op, or opset/IR version too new for the installed TensorRT).
Source
Thrown at export.py:666
if verbose:
logger.min_severity = trt.Logger.Severity.VERBOSE
builder = trt.Builder(logger)
config = builder.create_builder_config()
if is_trt10:
config.set_memory_pool_limit(trt.MemoryPoolType.WORKSPACE, workspace << 30)
else: # TensorRT versions 7, 8
config.max_workspace_size = workspace * 1 << 30
if cache: # enable timing cache
Path(cache).parent.mkdir(parents=True, exist_ok=True)
buf = Path(cache).read_bytes() if Path(cache).exists() else b""
timing_cache = config.create_timing_cache(buf)
config.set_timing_cache(timing_cache, ignore_mismatch=True)
flag = 1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)
network = builder.create_network(flag)
parser = trt.OnnxParser(network, logger)
if not parser.parse_from_file(str(onnx)):
raise RuntimeError(f"failed to load ONNX file: {onnx}")
inputs = [network.get_input(i) for i in range(network.num_inputs)]
outputs = [network.get_output(i) for i in range(network.num_outputs)]
for inp in inputs:
LOGGER.info(f'{prefix} input "{inp.name}" with shape{inp.shape} {inp.dtype}')
for out in outputs:
LOGGER.info(f'{prefix} output "{out.name}" with shape{out.shape} {out.dtype}')
if dynamic:
if im.shape[0] <= 1:
LOGGER.warning(f"{prefix} --dynamic model requires maximum --batch-size argument")
profile = builder.create_optimization_profile()
for inp in inputs:
profile.set_shape(inp.name, (1, *im.shape[1:]), (max(1, im.shape[0] // 2), *im.shape[1:]), im.shape)
config.add_optimization_profile(profile)
LOGGER.info(f"{prefix} building FP{16 if builder.platform_has_fast_fp16 and half else 32} engine as {f}")
if builder.platform_has_fast_fp16 and half:View on GitHub (pinned to 20d1d78a08)
Solutions
- Regenerate the ONNX from scratch in the same run so versions match: python export.py --weights yolov5s.pt --include onnx engine.
- Validate the file first: python -c "import onnx; m=onnx.load('yolov5s.onnx'); onnx.checker.check_model(m)".
- Downgrade the ONNX opset to one your TensorRT supports (export.py --opset 12) or upgrade TensorRT.
- If the file is corrupt, delete the .onnx and re-export; never hand-copy partial files.
Example fix
# before python export.py --weights yolov5s.pt --include engine # reuses stale/corrupt yolov5s.onnx # after rm yolov5s.onnx && python export.py --weights yolov5s.pt --include onnx engine --opset 12
Defensive patterns
Strategy: validation
Validate before calling
import onnx
def onnx_is_parsable(path: str) -> bool:
model = onnx.load(path) # raises if corrupt
onnx.checker.check_model(model)
return True Try / catch
try:
export_engine(...) # or run export.py --include engine
except RuntimeError as e:
if 'failed to load ONNX' in str(e):
onnx.checker.check_model(onnx.load(onnx_path)) # diagnose
re_export_onnx() # regenerate and retry once Prevention
- Always export ONNX and engine in the same export.py invocation.
- Pin onnx/opset versions to what your TensorRT supports.
- Checksum exported artifacts when copying them between machines.
When it happens
Trigger: Running export.py --include engine against an ONNX file produced by a newer onnx/onnxruntime than the installed TensorRT supports; a truncated/corrupt .onnx from an interrupted export; custom layers or ops not supported by trt.OnnxParser; pointing --weights at a hand-edited ONNX file.
Common situations: Mixed toolchain versions (torch 2.x + onnx opset 17 with TensorRT 7.x); reusing an old .onnx after upgrading TensorRT; CI runners where the ONNX export step partially failed but left a file; concatenating exports across machines.
Related errors
- TensorRT engine deserialization failed. Re-export the engine
- ERROR: YOLOv5 TF.js inference is not supported
- no matching TensorFlow activation found for PyTorch activati
AI-assisted analysis of ultralytics/yolov5@20d1d78a08 (2026-08-15).
Data as JSON: /api/errors/f12092af1205a199.
Report an issue: GitHub.