diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-10-20 11:44:30 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-10-20 11:44:30 +0200 |
| commit | 9b9005101e6170a226acb265428aac429be6edbf (patch) | |
| tree | f89c76f4a5c2a00a72abe9b92468ac9f15083b94 /convert.py | |
| download | facedet-9b9005101e6170a226acb265428aac429be6edbf.tar.gz facedet-9b9005101e6170a226acb265428aac429be6edbf.zip | |
Init
Diffstat (limited to 'convert.py')
| -rw-r--r-- | convert.py | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/convert.py b/convert.py new file mode 100644 index 0000000..429d122 --- /dev/null +++ b/convert.py @@ -0,0 +1,192 @@ +import argparse +import hashlib +import re +import sys +from pathlib import Path + +import numpy as np +import onnx + +ROOT = Path(__file__).resolve().parent +ONNX_PATH = ROOT / "models/face_detection_yunet_2026may.onnx" +IN_W = IN_H = 32 + +OP_CONV = 1 +OP_MAXPOOL = 2 +OP_RESIZE2X = 3 +OP_ADD = 4 + +def load_graph(path=ONNX_PATH): + model = onnx.load(str(path)) + g = model.graph + init = {t.name: onnx.numpy_helper.to_array(t) for t in g.initializer} + nodes = [] + for n in g.node: + a = {x.name: x for x in n.attribute} + nodes.append({ + "op": n.op_type, + "in": [x for x in n.input if x not in init], + "w": [x for x in n.input if x in init], + "out": n.output[0], + "group": a["group"].i if "group" in a else 1, + "k": list(a["kernel_shape"].ints) if "kernel_shape" in a else None, + "s": list(a["strides"].ints) if "strides" in a else [1, 1], + "p": list(a["pads"].ints) if "pads" in a else [0, 0, 0, 0], + }) + return model, nodes, init + + +def head_map(nodes): + prod = {n["out"]: n for n in nodes} + heads = {} + for out in ["cls_8", "cls_16", "cls_32", "obj_8", "obj_16", "obj_32", + "bbox_8", "bbox_16", "bbox_32", "kps_8", "kps_16", "kps_32"]: + cur, sigmoid = out, False + while cur in prod and prod[cur]["op"] in ("Sigmoid", "Reshape", + "Transpose"): + sigmoid |= prod[cur]["op"] == "Sigmoid" + cur = prod[cur]["in"][0] + heads[out] = (cur, sigmoid) + return heads + + +def compile_plan(nodes, init, heads): + relu_after = {n["in"][0] for n in nodes if n["op"] == "Relu"} + rename = {n["out"]: n["in"][0] for n in nodes if n["op"] == "Relu"} + + def resolve(name): + return rename.get(name, name) + + shapes = {"input": (3, IN_H, IN_W)} + plan = [] + weights = [] + keep = {resolve(t) for t, _ in heads.values()} + + for n in nodes: + if n["op"] in ("Relu", "Transpose", "Reshape", "Sigmoid"): + continue + ins = [resolve(x) for x in n["in"]] + out = n["out"] + if n["op"] == "Conv": + wname = n["w"][0] + w = init[wname] + b = init[n["w"][1]] if len(n["w"]) > 1 else np.zeros(w.shape[0], + np.float32) + cin_h, cin_w = shapes[ins[0]][1], shapes[ins[0]][2] + kh, kw = n["k"] + sh, sw = n["s"] + ph = n["p"][0] + oh = (cin_h + 2 * ph - kh) // sh + 1 + ow = (cin_w + 2 * ph - kw) // sw + 1 + shapes[out] = (w.shape[0], oh, ow) + wi = len(weights) + weights.append((f"w{wi}", w.astype(np.float32))) + weights.append((f"b{wi}", b.astype(np.float32))) + plan.append({"op": OP_CONV, "in": [ins[0]], "out": out, + "cin": shapes[ins[0]][0], "cout": w.shape[0], + "group": n["group"], "k": kh, "s": sh, "pad": ph, + "relu": 1 if out in relu_after else 0, "widx": wi}) + elif n["op"] == "MaxPool": + c, h, w_ = shapes[ins[0]] + shapes[out] = (c, h // 2, w_ // 2) + plan.append({"op": OP_MAXPOOL, "in": [ins[0]], "out": out}) + elif n["op"] == "Resize": + c, h, w_ = shapes[ins[0]] + shapes[out] = (c, h * 2, w_ * 2) + plan.append({"op": OP_RESIZE2X, "in": [ins[0]], "out": out}) + elif n["op"] == "Add": + shapes[out] = shapes[ins[0]] + plan.append({"op": OP_ADD, "in": ins, "out": out}) + else: + sys.exit(f"unsupported op {n['op']}") + + last_use = {} + for i, p in enumerate(plan): + for x in p["in"]: + last_use[x] = i + for t in keep: + last_use[t] = len(plan) + + nbytes = {t: int(np.prod(s)) * 4 for t, s in shapes.items()} + offsets = {"input": 0} + free = [] + arena = nbytes["input"] + for i, p in enumerate(plan): + need = (nbytes[p["out"]] + 63) & ~63 + slot = None + for j, (off, size, until) in enumerate(free): + if size >= need and until < i: + slot = (off, size) + free.pop(j) + break + if slot is None: + off = (arena + 63) & ~63 + arena = off + need + slot = (off, need) + offsets[p["out"]] = slot[0] + free.append((slot[0], slot[1], last_use.get(p["out"], i))) + p["in_off"] = [offsets[x] for x in p["in"]] + p["out_off"] = offsets[p["out"]] + p["in_shape"] = shapes[p["in"][0]] + p["out_shape"] = shapes[p["out"]] + return plan, weights, shapes, offsets, arena + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--model", type=Path, default=ONNX_PATH) + parser.add_argument("--sha256") + parser.add_argument("--prefix", default="model") + parser.add_argument("--out", type=Path, default=ROOT / "model_data.h") + args = parser.parse_args() + if not re.fullmatch(r"[a-z][a-z0-9_]*", args.prefix): + parser.error("prefix must be a lowercase C identifier") + if args.prefix != "model" and args.out.resolve() == (ROOT / "model_data.h").resolve(): + parser.error("a custom prefix requires a separate output header") + if args.model.resolve() != ONNX_PATH.resolve(): + if not args.sha256 or args.out.resolve() == (ROOT / "model_data.h").resolve(): + parser.error("experimental models require --sha256 and a separate --out header") + expected = args.sha256 or "ebafce4e3c118d6554634be5c27ab333b4c047a9a8c3faf1d7cf93101c22f0f0" + if hashlib.sha256(args.model.read_bytes()).hexdigest() != expected: + sys.exit("ONNX checksum does not match the expected model") + model, nodes, init = load_graph(args.model) + onnx.checker.check_model(model) + heads = head_map(nodes) + plan, weights, shapes, offsets, arena = compile_plan(nodes, init, heads) + lines = ["#ifndef YUNET_MODEL_DATA_H", "#define YUNET_MODEL_DATA_H", "", + "static const float model_weight_data[] = {"] + weight_offsets = [] + pos = 0 + for _, arr in weights: + weight_offsets.append(pos) + flat = arr.ravel() + for start in range(0, len(flat), 8): + lines.append(" " + ", ".join(float(v).hex() + "f" + for v in flat[start:start + 8]) + ",") + pos += len(flat) + lines += ["};", "", "static const float *const model_weights[] = {"] + lines += [f" model_weight_data + {off}," for off in weight_offsets] + lines += ["};", "", "static const yunet_op model_ops[] = {"] + for p in plan: + fields = [p["op"], p.get("group", 0), p.get("k", 0), p.get("s", 0), + p.get("pad", 0), p.get("relu", 0), p.get("widx", 0), + *p["in_shape"], *p["out_shape"], p["in_off"][0], + p["in_off"][1] if len(p["in_off"]) > 1 else 0, p["out_off"]] + lines.append(" {" + ", ".join(map(str, fields)) + "},") + lines += ["};", "", "static const yunet_map_info model_maps[] = {"] + for name, (tensor, sigmoid) in heads.items(): + c, h, w = shapes[tensor] + lines.append(f' {{"{name}", {offsets[tensor]}, {c}, {h}, {w}, {int(sigmoid)}}},') + lines += ["};", "", f"enum {{ MODEL_ARENA_BYTES = {arena},", + f" MODEL_OP_COUNT = {len(plan)}, MODEL_MAP_COUNT = {len(heads)} }};", + "", "#endif", ""] + text = "\n".join(lines) + if args.prefix != "model": + text = text.replace("model_", args.prefix + "_").replace("MODEL_", args.prefix.upper() + "_") + args.out.write_text(text) + print(f"YuNet 2026may: {len(plan)} operations, {len(weights) // 2} convolutions, {pos} weights") + + +if __name__ == "__main__": + main() + |
