This repository was archived by the owner on Apr 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_onnx.py
More file actions
208 lines (181 loc) · 7.94 KB
/
Copy pathextract_onnx.py
File metadata and controls
208 lines (181 loc) · 7.94 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""Minimal ONNX protobuf parser — extract graph structure + weight tensors.
Only parses what we need: NodeProto, TensorProto, AttributeProto.
Pure Python, no deps beyond stdlib + numpy.
ONNX proto3 wire format:
tag = (field_num << 3) | wire_type
wire_type 0 = varint, 2 = length-delimited (strings/submessages), 5 = fixed32
"""
import struct
import numpy as np
def read_varint(buf, pos):
v = 0
shift = 0
while True:
b = buf[pos]; pos += 1
v |= (b & 0x7F) << shift
if b < 0x80: return v, pos
shift += 7
def read_tag(buf, pos):
tag, pos = read_varint(buf, pos)
return (tag >> 3), (tag & 7), pos
def read_bytes(buf, pos):
n, pos = read_varint(buf, pos)
return bytes(buf[pos:pos+n]), pos + n
def skip_field(buf, pos, wire):
if wire == 0: _, pos = read_varint(buf, pos); return pos
if wire == 1: return pos + 8
if wire == 2: n, pos = read_varint(buf, pos); return pos + n
if wire == 5: return pos + 4
raise ValueError(f"unknown wire {wire}")
# ---- Parsers ----
def parse_attribute(buf):
"""Returns dict {name, type, ints, floats, i, f, s}."""
a = {"name": "", "type": 0, "ints": [], "floats": [], "i": 0, "f": 0.0, "s": b""}
pos = 0
while pos < len(buf):
fn, wire, pos = read_tag(buf, pos)
if fn == 1 and wire == 2: # name
n, pos = read_varint(buf, pos); a["name"] = buf[pos:pos+n].decode(); pos += n
elif fn == 2 and wire == 5: # f (fixed32 float, per ONNX proto)
a["f"] = struct.unpack_from("<f", buf, pos)[0]; pos += 4
elif fn == 3 and wire == 0: # i (varint int64, per ONNX proto)
v, pos = read_varint(buf, pos)
if v >= (1 << 63): v -= (1 << 64)
a["i"] = v
elif fn == 4 and wire == 2: # s
n, pos = read_varint(buf, pos); a["s"] = bytes(buf[pos:pos+n]); pos += n
elif fn == 7 and wire == 2: # floats (packed)
n, pos = read_varint(buf, pos)
end = pos + n
while pos < end:
a["floats"].append(struct.unpack_from("<f", buf, pos)[0]); pos += 4
elif fn == 8 and wire == 2: # ints (packed)
n, pos = read_varint(buf, pos)
end = pos + n
while pos < end:
v, pos = read_varint(buf, pos)
if v >= (1 << 63): v -= (1 << 64)
a["ints"].append(v)
elif fn == 8 and wire == 0: # single int repeated
v, pos = read_varint(buf, pos)
if v >= (1 << 63): v -= (1 << 64)
a["ints"].append(v)
elif fn == 20 and wire == 0: # type (AttributeType enum)
a["type"], pos = read_varint(buf, pos)
else:
pos = skip_field(buf, pos, wire)
return a
def parse_node(buf):
"""Returns dict {inputs, outputs, name, op_type, attrs}."""
n = {"inputs": [], "outputs": [], "name": "", "op_type": "", "attrs": []}
pos = 0
while pos < len(buf):
fn, wire, pos = read_tag(buf, pos)
if fn == 1 and wire == 2: # input
sbuf, pos = read_bytes(buf, pos); n["inputs"].append(sbuf.decode())
elif fn == 2 and wire == 2: # output
sbuf, pos = read_bytes(buf, pos); n["outputs"].append(sbuf.decode())
elif fn == 3 and wire == 2: # name
sbuf, pos = read_bytes(buf, pos); n["name"] = sbuf.decode()
elif fn == 4 and wire == 2: # op_type
sbuf, pos = read_bytes(buf, pos); n["op_type"] = sbuf.decode()
elif fn == 5 and wire == 2: # attribute
sbuf, pos = read_bytes(buf, pos); n["attrs"].append(parse_attribute(sbuf))
else:
pos = skip_field(buf, pos, wire)
return n
def parse_tensor(buf):
"""Returns dict with dims, data_type, name, numpy_data."""
t = {"dims": [], "data_type": 0, "name": "", "raw_data": b"", "float_data": [], "int32_data": [], "int64_data": []}
pos = 0
while pos < len(buf):
fn, wire, pos = read_tag(buf, pos)
if fn == 1 and wire == 0: # dims single
v, pos = read_varint(buf, pos)
if v >= (1 << 63): v -= (1 << 64)
t["dims"].append(v)
elif fn == 1 and wire == 2: # dims packed
n, pos = read_varint(buf, pos); end = pos + n
while pos < end:
v, pos = read_varint(buf, pos)
if v >= (1 << 63): v -= (1 << 64)
t["dims"].append(v)
elif fn == 2 and wire == 0: # data_type
t["data_type"], pos = read_varint(buf, pos)
elif fn == 8 and wire == 2: # name
sbuf, pos = read_bytes(buf, pos); t["name"] = sbuf.decode()
elif fn == 9 and wire == 2: # raw_data
n, pos = read_varint(buf, pos); t["raw_data"] = bytes(buf[pos:pos+n]); pos += n
elif fn == 4 and wire == 2: # float_data packed
n, pos = read_varint(buf, pos); end = pos + n
while pos < end:
t["float_data"].append(struct.unpack_from("<f", buf, pos)[0]); pos += 4
else:
pos = skip_field(buf, pos, wire)
# Materialize as numpy
DTYPE_MAP = {1: np.float32, 2: np.uint8, 3: np.int8, 4: np.uint16, 5: np.int16,
6: np.int32, 7: np.int64, 9: np.bool_, 10: np.float16, 11: np.float64}
dt = DTYPE_MAP.get(t["data_type"])
if t["raw_data"] and dt is not None:
t["numpy"] = np.frombuffer(t["raw_data"], dtype=dt).copy().reshape(t["dims"]) if t["dims"] else np.frombuffer(t["raw_data"], dtype=dt).copy()
elif t["float_data"]:
t["numpy"] = np.array(t["float_data"], dtype=np.float32).reshape(t["dims"]) if t["dims"] else np.array(t["float_data"], dtype=np.float32)
else:
t["numpy"] = None
return t
def parse_graph(buf):
"""Returns dict with nodes, initializers, inputs, outputs."""
g = {"nodes": [], "initializers": [], "name": ""}
pos = 0
while pos < len(buf):
fn, wire, pos = read_tag(buf, pos)
if fn == 1 and wire == 2: # node
sbuf, pos = read_bytes(buf, pos); g["nodes"].append(parse_node(sbuf))
elif fn == 2 and wire == 2: # name
sbuf, pos = read_bytes(buf, pos); g["name"] = sbuf.decode()
elif fn == 5 and wire == 2: # initializer
sbuf, pos = read_bytes(buf, pos); g["initializers"].append(parse_tensor(sbuf))
else:
pos = skip_field(buf, pos, wire)
return g
def parse_model(path):
"""ModelProto.graph is field 7."""
with open(path, "rb") as f:
buf = memoryview(f.read())
g = None
pos = 0
while pos < len(buf):
fn, wire, pos = read_tag(buf, pos)
if fn == 7 and wire == 2: # graph
sbuf, pos = read_bytes(buf, pos)
g = parse_graph(sbuf)
else:
pos = skip_field(buf, pos, wire)
return g
if __name__ == "__main__":
import sys
path = sys.argv[1] if len(sys.argv) > 1 else "models/w600k_r50.onnx"
print(f"Parsing {path}...")
g = parse_model(path)
print(f"Graph: {g['name']}")
print(f"Nodes: {len(g['nodes'])}")
print(f"Initializers: {len(g['initializers'])}")
# Op histogram
op_counts = {}
for n in g["nodes"]:
op_counts[n["op_type"]] = op_counts.get(n["op_type"], 0) + 1
print(f"\nOp histogram:")
for op, c in sorted(op_counts.items(), key=lambda x: -x[1]):
print(f" {op}: {c}")
# List Conv layers with their weight shapes
print(f"\nConv layers:")
init_by_name = {t["name"]: t for t in g["initializers"]}
convs = [n for n in g["nodes"] if n["op_type"] == "Conv"]
for i, n in enumerate(convs[:15]):
wname = n["inputs"][1]
w = init_by_name.get(wname)
wshape = w["dims"] if w else "?"
attrs = {a["name"]: (a["ints"] if a["ints"] else (a["i"] if a["i"] else a["f"])) for a in n["attrs"]}
print(f" Conv{i}: w={wshape} strides={attrs.get('strides')} pads={attrs.get('pads')}")
print(f"\nTotal Conv layers: {len(convs)}")
print(f"Initializer total params: {sum(np.prod(t['dims']) if t['dims'] else 0 for t in g['initializers']):,}")