STDC/tools/optimize_onnx_kneron.py
charlie880624 7716a0060f
Some checks failed
deploy / build-n-publish (push) Has been cancelled
lint / lint (push) Has been cancelled
build / build_cpu (3.7, 1.5.1, torch1.5, 0.6.1) (push) Has been cancelled
build / build_cpu (3.7, 1.6.0, torch1.6, 0.7.0) (push) Has been cancelled
build / build_cpu (3.7, 1.7.0, torch1.7, 0.8.1) (push) Has been cancelled
build / build_cpu (3.7, 1.8.0, torch1.8, 0.9.0) (push) Has been cancelled
build / build_cpu (3.7, 1.9.0, torch1.9, 0.10.0) (push) Has been cancelled
build / build_cuda101 (3.7, 1.5.1+cu101, torch1.5, 0.6.1+cu101) (push) Has been cancelled
build / build_cuda101 (3.7, 1.6.0+cu101, torch1.6, 0.7.0+cu101) (push) Has been cancelled
build / build_cuda101 (3.7, 1.7.0+cu101, torch1.7, 0.8.1+cu101) (push) Has been cancelled
build / build_cuda101 (3.7, 1.8.0+cu101, torch1.8, 0.9.0+cu101) (push) Has been cancelled
build / build_cuda102 (3.6, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / build_cuda102 (3.7, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / build_cuda102 (3.8, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / build_cuda102 (3.9, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / test_windows (windows-2022, cpu, 3.8) (push) Has been cancelled
build / test_windows (windows-2022, cu111, 3.8) (push) Has been cancelled
feat: add golf dataset, kneron configs, and tools
- Add golf1/2/4/7/8 dataset classes for semantic segmentation
- Add kneron-specific configs (meconfig series, kn_stdc1_golf4class)
- Organize scripts into tools/check/ and tools/kneron/
- Add kneron_preprocessing module
- Update README with quick-start guide
- Update .gitignore to exclude data dirs, onnx, nef outputs

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-18 13:14:30 +08:00

44 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import sys
import onnx
# === 動態加入 optimizer_scripts 模組路徑 ===
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(current_dir, 'tools'))
from optimizer_scripts.pytorch_exported_onnx_preprocess import torch_exported_onnx_flow
def main():
# === 設定路徑 ===
onnx_path = r'C:\Users\rd_de\kneronstdc\work_dirs\meconfig8\latest.onnx'
optimized_path = r'C:\Users\rd_de\kneronstdc\work_dirs\meconfig8\latest_optimized.onnx'
if not os.path.exists(onnx_path):
print(f'❌ 找不到 ONNX 檔案: {onnx_path}')
return
# === 載入 ONNX 模型 ===
print(f'🔄 載入 ONNX: {onnx_path}')
m = onnx.load(onnx_path)
# === 修正 ir_version避免 opset11 時報錯)===
if m.ir_version == 7:
print(f'⚠️ 調整 ir_version 7 → 6相容性修正')
m.ir_version = 6
# === 執行 Kneron 優化流程 ===
print('⚙️ 執行 Kneron 優化 flow...')
try:
m = torch_exported_onnx_flow(m, disable_fuse_bn=False)
except Exception as e:
print(f'❌ 優化失敗: {type(e).__name__}{e}')
return
# === 儲存結果 ===
os.makedirs(os.path.dirname(optimized_path), exist_ok=True)
onnx.save(m, optimized_path)
print(f'✅ 已儲存最佳化 ONNX: {optimized_path}')
if __name__ == '__main__':
main()