forked from espressif/esp-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack_model.py
186 lines (163 loc) · 5.17 KB
/
pack_model.py
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
import argparse
import shutil
import struct
from pathlib import Path
def struct_pack_string(string, max_len=None):
"""
pack string to binary data.
if max_len is None, max_len = len(string) + 1
else len(string) < max_len, the left will be padded by struct.pack('x')
string: input python string
max_len: output
"""
if max_len == None:
max_len = len(string)
else:
assert len(string) <= max_len
left_num = max_len - len(string)
out_bytes = None
for char in string:
if out_bytes == None:
out_bytes = struct.pack("b", ord(char))
else:
out_bytes += struct.pack("b", ord(char))
for i in range(left_num):
out_bytes += struct.pack("x")
return out_bytes
def get_model_format(filename):
"""
Get model format, EDL1 or EDL2
"""
with open(filename, "rb") as f:
data = f.read(4)
format = data.decode("utf-8")
if format != "EDL1" and format != "EDL2":
raise RuntimeError("Wrong model format.")
return format
def read_data(filename, format):
"""
Read binary data, like index and mndata
"""
data = None
with open(filename, "rb") as f:
data = f.read()
if format == "EDL2" and len(data) % 16 != 0:
padding = 16 - len(data) % 16
data += struct.pack("x") * padding
return data
def pack_models(model_path_or_dir, out_file="models.espdl"):
"""
Pack all models into one binary file by the following format:
{
"PDL1": char[4]
model_num: uint32
model1_data_offset: uint32
model1_name_offset: uint32
model1_name_length: uint32
model2_data_offset: uint32
model2_name_offset: uint32
model2_name_length: uint32
...
model1_name,
model2_name,
...
model1_data,
model2_data,
...
}model_pack_t
or
{
"PDL2": char[4]
model_num: uint32
model1_data_offset: uint32
model1_name_offset: uint32
model1_name_length: uint32
model2_data_offset: uint32
model2_name_offset: uint32
model2_name_length: uint32
...
model1_name,
model2_name,
...
zero padding
model1_data
zero padding
model2_data
zero padding
}
model_path: the path of models
out_file: the ouput binary filename
"""
if len(model_path_or_dir) == 1:
model_path_or_dir = Path(model_path_or_dir[0])
if model_path_or_dir.is_file():
shutil.copyfile(model_path_or_dir, out_file)
return
else:
model_files = sorted(list(model_path_or_dir.glob("*.espdl")))
else:
model_files = []
for model_path in sorted(model_path_or_dir):
model_path = Path(model_path)
assert model_path.is_file(), "invalid model_path."
model_files.append(model_path)
model_formats = [get_model_format(file) for file in model_files]
format = model_formats[0]
for i in range(1, len(model_formats)):
if format != model_formats[i]:
raise RuntimeError("All packed model format should be same.")
model_names = []
model_bins = []
name_length = 0
for model_file in model_files:
model_names.append(model_file.name)
model_bins.append(read_data(model_file, format))
name_length += len(model_file.name)
print(model_file.name)
model_num = len(model_names)
if format == "EDL1":
header_bin = struct_pack_string("PDL1", 4)
else:
header_bin = struct_pack_string("PDL2", 4)
header_bin += struct.pack("I", model_num)
name_offset = 4 + 4 + model_num * 12
if format == "EDL1":
data_offset = name_offset + name_length
padding_bin = b""
else:
data_offset = (name_offset + name_length + 15) & ~15
padding_bin = struct.pack("x") * (data_offset - name_offset - name_length)
name_bin = None
data_bin = None
for idx, name in enumerate(model_names):
if not name_bin:
name_bin = struct_pack_string(name, len(name)) # + model name
else:
name_bin += struct_pack_string(name, len(name))
name_offset += len(model_names[idx - 1])
if not data_bin:
data_bin = model_bins[idx]
else:
data_bin += model_bins[idx]
data_offset += len(model_bins[idx - 1])
header_bin += struct.pack("I", data_offset)
header_bin += struct.pack("I", name_offset)
header_bin += struct.pack("I", len(name))
out_bin = header_bin + name_bin + padding_bin + data_bin
with open(out_file, "wb") as f:
f.write(out_bin)
if __name__ == "__main__":
# input parameter
parser = argparse.ArgumentParser(description="esp-dl model package tool")
parser.add_argument(
"-m", "--model_path", type=str, nargs="+", help="the path of model files"
)
parser.add_argument(
"-o",
"--out_file",
type=str,
default="models.espdl",
help="the path of binary file",
)
args = parser.parse_args()
pack_models(args.model_path, out_file=args.out_file)