STDC/tools/kneron/onnx2nefSTDC630_2.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

65 lines
1.9 KiB
Python

import os
import numpy as np
import onnx
import shutil
import cv2
import ktc
onnx_dir = 'work_dirs/kn_stdc1_in1k-pre_512x1024_80k_cityscapes/'
onnx_path = os.path.join(onnx_dir, 'latest.onnx')
data_path = "data512"
imgsz = (512, 512)
os.makedirs(onnx_dir, exist_ok=True)
print("🔄 Loading and optimizing ONNX...")
model = onnx.load(onnx_path)
model = ktc.onnx_optimizer.onnx2onnx_flow(model)
opt_onnx_path = os.path.join(onnx_dir, 'latest.opt.onnx')
onnx.save(model, opt_onnx_path)
print("📐 Configuring model...")
km = ktc.ModelConfig(20008, "0001", "630", onnx_model=model)
# Optional: performance check
print("\n📊 Evaluating model...")
print(km.evaluate())
input_name = model.graph.input[0].name
print("📥 ONNX input name:", input_name)
img_list = []
print("🖼️ Preprocessing images...")
for root, _, files in os.walk(data_path):
for fname in files:
if fname.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp')):
path = os.path.join(root, fname)
img = cv2.imread(path)
img = cv2.resize(img, imgsz)
img = img.astype(np.float32) / 256.0 - 0.5
img = np.transpose(img, (2, 0, 1)) # HWC ➝ CHW
img = np.expand_dims(img, axis=0) # Add batch dim
img_list.append(img)
print("", path)
if not img_list:
raise RuntimeError("❌ No images processed!")
print("📦 Quantizing (BIE)...")
bie_path = km.analysis({input_name: img_list})
bie_save = os.path.join(onnx_dir, os.path.basename(bie_path))
shutil.copy(bie_path, bie_save)
if not os.path.exists(bie_save):
raise RuntimeError("❌ BIE model not saved!")
print("⚙️ Compiling NEF...")
nef_path = ktc.compile([km])
nef_save = os.path.join(onnx_dir, os.path.basename(nef_path))
shutil.copy(nef_path, nef_save)
if not os.path.exists(nef_save):
raise RuntimeError("❌ NEF model not saved!")
print("✅ Compile finished. NEF at:", nef_save)