-
Notifications
You must be signed in to change notification settings - Fork 0
/
exebat.py
125 lines (100 loc) · 4.23 KB
/
exebat.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
import subprocess
import os
from subprocess import Popen
import argparse
def update_bat_file(batch_file_path, var_update_dict):
# read the contents of the batch file
with open(batch_file_path, 'r') as f:
content = f.readlines()
# loop through the lines of the batch file and update the variables
for i in range(len(content)):
if content[i].startswith('set MODEL_SRC_DIR='):
content[i] = f'set MODEL_SRC_DIR={var_update_dict["SRC_DIR"]}\n'
elif content[i].startswith('set MODEL_SRC_FILE='):
content[i] = f'set MODEL_SRC_FILE={var_update_dict["SRC_FILE"]}\n'
elif content[i].startswith('set MODEL_OPTIMISE_FILE='):
content[i] = f'set MODEL_OPTIMISE_FILE={var_update_dict["OPTIMISE_FILE"]}\n'
elif content[i].startswith('set GEN_SRC_DIR='):
content[i] = f'set GEN_SRC_DIR={var_update_dict["GEN_DIR"]}\n'
# write the updated content to the batch file
with open(batch_file_path, 'w') as f:
f.writelines(content)
print('Batch file content updated successfully.')
if __name__ == "__main__":
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
parser = argparse.ArgumentParser()
parser.add_argument(
'--SRC_DIR',
type=str,
default='..\\workspace\\catsdogs\\tflite_model',
help='The model source dir')
parser.add_argument(
'--SRC_FILE',
type=str,
default='mobilenet_v2_int8quant.tflite',
help='The model source file name')
parser.add_argument(
'--GEN_DIR',
type=str,
default='..\\workspace\\catsdogs\\tflite_model\\vela',
help='The generated dir for *vela.tflite')
parser.add_argument(
'--VELA_EN',
type=str2bool,
default=False,
help='Vela compiler enable or not, default is normal C.')
args = parser.parse_args()
# Change to ../deployment folder
old_cwd = os.getcwd()
batch_cwd = os.path.join(old_cwd, "deployment")
os.chdir(batch_cwd)
if args.VELA_EN:
# Get the MODEL_OPTIMISE_FILE
if args.SRC_FILE.count(".tflite"):
MODEL_OPTIMISE_FILE = args.SRC_FILE.split(".tflite")[0] + f"_vela.tflite"
else:
raise OSError("Please input .tflite file!")
# Update the variables.bat
batch_file_path = "variables.bat"
var_update_dict = {}
var_update_dict['SRC_DIR']=args.SRC_DIR
var_update_dict['SRC_FILE']=args.SRC_FILE
var_update_dict['OPTIMISE_FILE']=MODEL_OPTIMISE_FILE
var_update_dict['GEN_DIR']=args.GEN_DIR
update_bat_file(batch_file_path, var_update_dict)
# Execute the bat file
print('Executing the {}.'.format(os.path.join(batch_cwd, "gen_model_cpp.bat")))
print('Please wait...')
p = Popen("gen_model_cpp.bat")
stdout, stderr = p.communicate()
#subprocess.call(["gen_model_cpp.bat"])
vela_output_path = os.path.join(old_cwd, args.GEN_DIR.split("..\\")[1], MODEL_OPTIMISE_FILE)
print('Finish, the vela file is at: {}'.format(vela_output_path))
else:
# Get the MODEL_OPTIMISE_FILE
if not args.SRC_FILE.count(".tflite"):
raise OSError("Please input .tflite file!")
# Update the variables.bat
batch_file_path = "variables_no_vela.bat"
var_update_dict = {}
var_update_dict['SRC_DIR']=args.SRC_DIR
var_update_dict['SRC_FILE']=args.SRC_FILE
var_update_dict['GEN_DIR']=args.GEN_DIR
update_bat_file(batch_file_path, var_update_dict)
# Execute the bat file
print('Executing the {}.'.format(os.path.join(batch_cwd, "gen_model_cpp_no_vela.bat")))
print('Please wait...')
p = Popen("gen_model_cpp_no_vela.bat")
stdout, stderr = p.communicate()
#subprocess.call(["gen_model_cpp.bat"])
vela_output_path = os.path.join(old_cwd, args.GEN_DIR.split("..\\")[1], args.SRC_FILE)
print('Finish, the c source file is at: {}'.format(vela_output_path))
os.chdir(old_cwd)