-
Notifications
You must be signed in to change notification settings - Fork 123
/
pack_model.py
136 lines (117 loc) · 3.73 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
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 read_data(filename):
"""
Read binary data, like index and mndata
"""
data = None
with open(filename, "rb") as f:
data = f.read()
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
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_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))
name_length += len(model_file.name)
print(model_file.name)
model_num = len(model_names)
header_bin = struct_pack_string("PDL1", 4)
header_bin += struct.pack("I", model_num)
name_offset = 4 + 4 + model_num * 12
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 + 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)