-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
145 lines (127 loc) · 5.33 KB
/
main.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
import os
import sys
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import tensorflow as tf
import json
import random
import argparse
# import orjson
# from tensorflow import keras
from model_assembler import model_assembler
from model_parser import *
from utils.utils import *
from dinamic_config import dinamic_config
parser = argparse.ArgumentParser()
parser.add_argument('--model_name', type=str, default='lenet', help='name of the model')
parser.add_argument('--free_unused_data', action='store_true', help='free unused intermediate data')
parser.add_argument('--executable', action='store_true', help='generate executable file')
opt = parser.parse_args()
def reduce_size_json(json_file):
with fileinput.input(files=json_file, inplace=True) as f:
keep_sign = True
for line in f:
if 'buffers:' in line:
keep_sign = False
print('}', end="")
if keep_sign:
print(line, end="")
model_path = './tflite_model/'
model_name = opt.model_name + '.tflite'
if opt.model_name == 'gpt2':
enable_sig = False
else:
enable_sig = True
interpreter = tf.lite.Interpreter(
os.path.join(model_path, model_name)
)
interpreter.allocate_tensors()
# --------------------------------------------------
# parse the TFLite model and generate code
# --------------------------------------------------
os.system('flatc -t schema.fbs -- %s' % os.path.join(model_path, model_name))
reduce_size_json(os.path.splitext(model_name)[0] + '.json')
os.system('jsonrepair %s.json --overwrite' % os.path.splitext(model_name)[0])
# for op in interpreter._get_ops_details():
# print(op)
with open('%s.json' % os.path.splitext(model_name)[0],'r') as f:
model_json_f = f.read()
model_json = json.loads(model_json_f)
# op_details = interpreter._get_ops_details()
# print(op_details)
# for tensor_details in interpreter.get_tensor_details():
# print(tensor_details)
tensor_list = []
for input in interpreter.get_input_details():
tensor_list.append(input['index'])
for tensor_details in interpreter.get_tensor_details():
tensor_list.append(tensor_details["index"])
tensor_list.sort()
inout_list = []
for i in range(len(model_json['subgraphs'][0]["operators"])):
# print(model_json['subgraphs'][0]["operators"][i])
for j in range(len(model_json['subgraphs'][0]["operators"][i]['outputs'])):
inout_list.append(model_json['subgraphs'][0]["operators"][i]['outputs'][j])
for input in interpreter.get_input_details():
inout_list.append(input['index'])
# for output in interpreter.get_output_details():
# inout_list.append(output['index'])
jsontext, unknown_config = lib_generator(model_json, interpreter, inout_list)
# --------------------------------------------------
# dinamic config & build
# --------------------------------------------------
currentPath = os.getcwd().replace('\\','/')
# os.chdir('./tensorflow-2.9.1/')
# os.system("bash build.sh")
os.chdir(currentPath)
# print(inout_list)
for op in jsontext['oplist']:
del_list = []
# print("input:", op['input'])
for i in range(len(op['input'])):
if not (op['input'][i] in inout_list):
# print("not in inout_list:", op['input'][i])
del_list.append(op['input'][i])
# print("del:", del_list)
for j in range(len(del_list)):
op['input'].remove(del_list[j])
out_node = op['output'][0]
try:
model_json['subgraphs'][0]["tensors"][out_node]["type"]
except:
op['type'] = "FLOAT32"
else:
op['type'] = model_json['subgraphs'][0]["tensors"][out_node]["type"]
try:
model_json['subgraphs'][0]["tensors"][out_node]["quantization"]
except:
op["quantization"] = {}
else:
op["quantization"] = model_json['subgraphs'][0]["tensors"][out_node]["quantization"]
input_list = model_json['subgraphs'][0]['inputs']
jsontext['inputs'] = []
for i in range(len(input_list)):
try:
tensor_type = model_json['subgraphs'][0]["tensors"][input_list[i]]["type"]
except:
tensor_type = "FLOAT32"
else:
tensor_type = model_json['subgraphs'][0]["tensors"][input_list[i]]["type"]
jsontext['inputs'].append({'name': 'serving_default_x:'+str(i), 'type': tensor_type, 'quantization': model_json['subgraphs'][0]["tensors"][input_list[i]]["quantization"]})
output_list = model_json['subgraphs'][0]['outputs']
jsontext['outputs'] = []
for i in range(len(output_list)):
try:
tensor_type = model_json['subgraphs'][0]["tensors"][input_list[i]]["type"]
except:
tensor_type = "FLOAT32"
else:
tensor_type = model_json['subgraphs'][0]["tensors"][input_list[i]]["type"]
jsontext['outputs'].append({'name': 'PartitionedCall:'+str(i), 'type': tensor_type, 'quantization': model_json['subgraphs'][0]["tensors"][output_list[i]]["quantization"]})
jsondata = json.dumps(jsontext,indent=4,separators=(',', ': '))
file = open('./obfjson/model' + '_' + opt.model_name + '.json', 'w')
file.write(jsondata)
file.close()
model_assembler(interpreter, './obfjson/model' + '_' + opt.model_name + '.json', opt.free_unused_data, enable_sig=enable_sig, executable=opt.executable)
dinamic_config(unknown_config, './obfjson/model' + '_' + opt.model_name + '.json', os.path.join(model_path, model_name), enable_sig=enable_sig, executable=opt.executable)
os.system('python eval.py --model_name=' + opt.model_name + ' --latency=True')