Converting onnx to har to hef for custom yolo11nano-seg model. We have the hailo-8.

Hello, I’m relatively new to this kind of projects and I’m stuck on converting the har file to hef. So, here’s what happened from onnx to har. Hope someone helps.

from ultralytics import YOLO
model = YOLO(‘/content/rice_final_model.pt’)
model.export(format=‘onnx’, imgsz=640, nms=False, opset=12, simplify=True)

and then onnx to har. i used this script

import os
import numpy as np
from PIL import Image
from hailo_sdk_client import ClientRunner

Constants

ONNX_PATH = “rice_final_model_640.onnx”
MODEL_NAME = “rice_model_640”
CALIB_IMAGES_DIR = “calibration_images”
OUTPUT_HAR = “rice_final_model_640_quantized.har”
ALLS_PATH = “model_script.alls”

def load_calibration_data(path, size=(640, 640)):
images = []

if not os.path.exists(path):
    raise FileNotFoundError(f"Calibration directory {path} not found!")

file_list = \[f for f in sorted(os.listdir(path))
             if f.lower().endswith(('.jpg', '.png', '.jpeg'))\]\[:256\]

if len(file_list) == 0:
    raise ValueError("No images found for calibration!")

print(f"Loading {len(file_list)} images for calibration at {size}...")

for filename in file_list:
    img = Image.open(os.path.join(path, filename)).convert('RGB').resize(size)
    images.append(np.array(img))

return np.array(images).astype(np.float32)

runner = ClientRunner(hw_arch=‘hailo8’)

print(“Translating ONNX to HAR…”)

end_node_names = [
“/model.23/Sigmoid”,
“/model.23/proto/cv3/act/Mul”,
“/model.23/Concat”,
“/model.23/Concat_2”
]

runner.translate_onnx_model(
ONNX_PATH,
MODEL_NAME,
net_input_shapes={“images”: [1, 3, 640, 640]},
end_node_names=end_node_names
)

if os.path.exists(ALLS_PATH):
print(f"Loading model script: {ALLS_PATH}")
runner.load_model_script(ALLS_PATH)
else:
print(“No .alls script found. Continuing with defaults…”)

calib_data = load_calibration_data(CALIB_IMAGES_DIR)

print(“Starting Quantization and Resource Allocation…”)
runner.optimize(calib_data)

runner.save_har(OUTPUT_HAR)

print(f"Created quantized archive: {OUTPUT_HAR}")
print(“You can now run ‘hailo compiler’ on this HAR to generate your HEF.”)

then har to hef with this script:

import os
from hailo_sdk_client import ClientRunner

INPUT_HAR = “rice_final_model_640_quantized.har”
OUTPUT_HEF = “rice_final_model_640.hef”

runner = ClientRunner(har=INPUT_HAR, hw_arch=‘hailo8’)

alls_script = “compile_setup.alls”

with open(alls_script, “w”) as f:
f.write(“performance_param(compiler_optimization_level=1)\n”)
f.write(“resources_param(max_memory_utilization=0.9)\n”)

runner.load_model_script(alls_script)

print(f":hammer: Compiling {INPUT_HAR} to {OUTPUT_HEF}…")

try:
hef = runner.compile()

with open(OUTPUT_HEF, "wb") as f:
    f.write(hef)

print("-" \* 30)
print(f"SUCCESS! {OUTPUT_HEF} is ready for deployment.")
print("-" \* 30)

except Exception as e:
print(f"Compilation failed: {e}")

the error are these:

[error] Mapping Failed (allocation time: 14s)
No successful assignments: concat18 errors:
Agent infeasible

[error] Failed to produce compiled graph
:cross_mark: Compilation failed: Compilation failed: No successful assignments: concat18 errors:
Agent infeasible