change done
This commit is contained in:
+32
-13
@@ -1,26 +1,32 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Step 1: convert best_weld.pt -> best_weld.onnx -> best_weld.engine
|
||||
convert.py - convert best_weld.pt -> best_weld.onnx -> best_weld.engine
|
||||
|
||||
RUN THIS ON THE JETSON. A TensorRT engine only works on the GPU / TensorRT
|
||||
version it was built on, so an engine built in Colab will NOT run on the Orin Nano.
|
||||
|
||||
Project root : /home/jetson/projects_2026/welding
|
||||
models/best_weld.pt (input)
|
||||
models/best_weld.onnx (created)
|
||||
models/best_weld.engine (created)
|
||||
|
||||
USAGE
|
||||
-----
|
||||
Default (FP16, Ultralytics builds the engine):
|
||||
python3 1_convert.py
|
||||
python3 convert.py
|
||||
|
||||
Explicit ONNX -> engine with trtexec (engine gets Ultralytics metadata added
|
||||
so inference scripts still know the class names):
|
||||
python3 1_convert.py --method trtexec
|
||||
Explicit ONNX -> engine with trtexec (class-name metadata is added so the
|
||||
inference script still knows the class names):
|
||||
python3 convert.py --method trtexec
|
||||
|
||||
Other options:
|
||||
python3 1_convert.py --imgsz 640 --precision fp32 --force
|
||||
python3 1_convert.py --precision int8 --data weld_data.yaml # needs calibration data
|
||||
python3 convert.py --imgsz 640 --precision fp32 --force
|
||||
python3 convert.py --precision int8 --data weld_data.yaml # needs calibration data
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -29,7 +35,19 @@ from pathlib import Path
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
from weld_common import ENGINE_PATH, IMGSZ, ONNX_PATH, PT_PATH
|
||||
# ---------------------------------------------------------------------------
|
||||
# CONFIG
|
||||
# ---------------------------------------------------------------------------
|
||||
# Override without editing the file: export WELD_HOME=/other/path
|
||||
BASE_DIR = Path(os.environ.get("WELD_HOME", "/home/jetson/projects_2026/welding"))
|
||||
MODELS_DIR = BASE_DIR / "models"
|
||||
|
||||
PT_PATH = MODELS_DIR / "best_weld.pt"
|
||||
ONNX_PATH = MODELS_DIR / "best_weld.onnx"
|
||||
ENGINE_PATH = MODELS_DIR / "best_weld.engine"
|
||||
|
||||
# The engine is locked to this size. Training used 640.
|
||||
IMGSZ = 640
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -51,7 +69,7 @@ def pt_to_onnx(pt_path: Path, imgsz: int, force: bool) -> Path:
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Step 2a: .onnx/.pt -> .engine via Ultralytics (recommended)
|
||||
# Step 2a: engine via Ultralytics (recommended)
|
||||
# ---------------------------------------------------------------------------
|
||||
def build_engine_ultralytics(pt_path: Path, imgsz: int, precision: str,
|
||||
data: str, workspace: int) -> None:
|
||||
@@ -69,7 +87,7 @@ def build_engine_ultralytics(pt_path: Path, imgsz: int, precision: str,
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Step 2b: .onnx -> .engine via trtexec (explicit route)
|
||||
# Step 2b: engine via trtexec (explicit route)
|
||||
# ---------------------------------------------------------------------------
|
||||
def find_trtexec() -> str:
|
||||
found = shutil.which("trtexec")
|
||||
@@ -102,7 +120,7 @@ def build_engine_trtexec(pt_path: Path, onnx_path: Path, imgsz: int,
|
||||
# trtexec engines have no such header, so class names would be lost.
|
||||
model = YOLO(str(pt_path))
|
||||
meta = {
|
||||
"description": "Weld detection (Bad Weld / Defect / Good Weld)",
|
||||
"description": "Weld detection (Bad Weld / Good Weld / Defect)",
|
||||
"author": "custom",
|
||||
"date": datetime.now().isoformat(),
|
||||
"version": "custom",
|
||||
@@ -128,7 +146,7 @@ def parse_args():
|
||||
help="Input size baked into the engine (use your training size).")
|
||||
p.add_argument("--precision", choices=["fp16", "fp32", "int8"], default="fp16")
|
||||
p.add_argument("--method", choices=["ultralytics", "trtexec"], default="ultralytics",
|
||||
help="How to build the engine from the ONNX/PT.")
|
||||
help="How to build the engine.")
|
||||
p.add_argument("--data", type=str, default=None, help="Dataset yaml (INT8 calibration only).")
|
||||
p.add_argument("--workspace", type=int, default=4, help="TensorRT workspace in GB.")
|
||||
p.add_argument("--force", action="store_true", help="Rebuild even if outputs exist.")
|
||||
@@ -141,6 +159,7 @@ def main():
|
||||
if not pt_path.exists():
|
||||
sys.exit(f"Weights not found: {pt_path}")
|
||||
|
||||
MODELS_DIR.mkdir(parents=True, exist_ok=True)
|
||||
onnx_path = pt_to_onnx(pt_path, args.imgsz, args.force)
|
||||
|
||||
if ENGINE_PATH.exists() and not args.force:
|
||||
@@ -153,7 +172,7 @@ def main():
|
||||
build_engine_trtexec(pt_path, onnx_path, args.imgsz, args.precision, args.workspace)
|
||||
|
||||
print(f"\nDone.\n ONNX : {ONNX_PATH}\n Engine: {ENGINE_PATH}")
|
||||
print("Next: python3 2_infer_image.py or python3 3_infer_video.py")
|
||||
print("Next: python3 infer.py")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user