-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_converter.py
49 lines (40 loc) · 1.64 KB
/
auto_converter.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
#-*-coding:utf-8-*-
import os
from convert import Converter
from utils.utils import init_model_list
class AutoConverter:
def __init__(self, model_folder, onnx=True, libtorch=True, onnx_sim=True):
self.model_folder = model_folder
self.onnx = onnx
self.libtorch = libtorch
self.onnx_sim = onnx_sim
self.model_ls, self.cfg_ls, _, _ = init_model_list(self.model_folder)
@staticmethod
def get_superior_path(path):
return "/".join(path.replace("\\", "/").split("/")[:-1])
def get_onnx_path(self, model_path):
if self.onnx:
return os.path.join(self.get_superior_path(model_path), "model.onnx")
else:
return None
def get_libtorch_path(self, model_path):
if self.libtorch:
return os.path.join(self.get_superior_path(model_path), "model.pt")
else:
return None
def get_onnx_sim_path(self, model_path):
if self.onnx_sim:
return os.path.join(self.get_superior_path(model_path), "model_sim.onnx")
else:
return None
def run(self):
total_num = len(self.model_ls)
for idx, (model, cfg) in enumerate(zip(self.model_ls, self.cfg_ls)):
print("-------------------[{}/{}]: Begin Converting {}--------------".format(idx+1, total_num, model))
converter = Converter(model, cfg, self.get_onnx_path(model), self.get_libtorch_path(model),
self.get_onnx_sim_path(model))
converter.convert()
if __name__ == '__main__':
model_folder = "exp/test_init"
convert = AutoConverter(model_folder)
convert.run()