aboutsummaryrefslogtreecommitdiff
path: root/convert.py
blob: 678a6ba744d3e24923a5ef54d082034d7ffc2771 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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 FACEDET_MODEL_DATA_H", "#define FACEDET_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 facedet_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 facedet_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()