Resources

TensorRT Export Function.

Exports a checkpoint to a TensorRT engine at a given precision, skipping export if a valid engine already exists.

def export_engine(model_path, model_name, precision):
    engine_path = OUTPUT_DIR / f'{model_name}_{precision}.engine'

    if engine_path.exists() and engine_path.stat().st_size > 1e6:
        return engine_path

    model = YOLO(str(model_path))
    model.export(
        format='engine',
        imgsz=IMGSZ,
        batch=BATCH,
        half=(precision == 'fp16'),
        int8=False,
        workspace=4.0,
        device=0
    )

    candidates = [
        model_path.with_suffix('.engine'),
        model_path.parent / f'{model_path.stem}.engine',
    ]
    for cand in candidates:
        if cand.exists():
            cand.rename(engine_path)
            return engine_path

    raise FileNotFoundError(f'Engine not found for {model_name}')

TensorRT engines are hardware-specific — compile on the exact GPU you plan to deploy to, not on a development machine.

← Return to ArticleBrowse All Resources →