forked from PanJinquan/DL-Converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_keras_to_tflite.py
executable file
·297 lines (266 loc) · 13.3 KB
/
convert_keras_to_tflite.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# -*- coding: utf-8 -*-
"""
# --------------------------------------------------------
# @Author : panjq
# @E-mail : [email protected]
# @Date : 2020-02-05 11:01:49
# --------------------------------------------------------
"""
import sys
import os
sys.path.append(os.getcwd())
import numpy as np
import glob
import cv2
import argparse
import tensorflow as tf
print("TF version:{}".format(tf.__version__))
bin_path = os.path.dirname(sys.executable)
if 'PATH' in os.environ:
os.environ['PATH'] += ':' + bin_path
else:
os.environ['PATH'] = bin_path
def converer_keras_to_tflite_v1(keras_path, outputs_layer=None, out_tflite=None):
"""
:param keras_path: keras *.h5 files
:param outputs_layer
:param out_tflite: output *.tflite file
:return:
"""
model_dir = os.path.dirname(keras_path)
model_name = os.path.basename(keras_path)[:-len(".h5")]
# 加载keras模型, 结构打印
model_keras = tf.keras.models.load_model(keras_path)
print(model_keras.summary())
# 从keras模型中提取fc1层, 需先保存成新keras模型, 再转换成tflite
model_embedding = tf.keras.models.Model(inputs=model_keras.input,
outputs=model_keras.get_layer(outputs_layer).output)
print(model_embedding.summary())
keras_file = os.path.join(model_dir, "{}_{}.h5".format(model_name, outputs_layer))
tf.keras.models.Model.save(model_embedding, keras_file)
# converter = tf.lite.TocoConverter.from_keras_model_file(keras_file)
converter = tf.lite.TFLiteConverter.from_keras_model_file(keras_file) # tf1.3
# converter = tf.lite.TFLiteConverter.from_keras_model(model_keras) # tf2.0
tflite_model = converter.convert()
if not out_tflite:
out_tflite = os.path.join(model_dir, "{}_{}.tflite".format(model_name, outputs_layer))
open(out_tflite, "wb").write(tflite_model)
print("successfully convert to tflite done")
print("save model at: {}".format(out_tflite))
def converer_keras_to_tflite_v2(keras_path,
outputs_layer=None,
out_tflite=None,
optimize=False,
quantization=False):
"""
:param keras_path: keras *.h5 files
:param outputs_layer: default last layer
:param out_tflite: output *.tflite file
:param optimize
:return:
"""
if not os.path.exists(keras_path):
raise Exception("Error:{}".format(keras_path))
model_dir = os.path.dirname(keras_path)
model_name = os.path.basename(keras_path)[:-len(".h5")]
# 加载keras模型, 结构打印
# model = tf.keras.models.load_model(keras_path)
# model = tf.keras.models.load_model(model_path, custom_objects={'tf': tf}, compile=False)
model = tf.keras.models.load_model(model_path, compile=False)
print(model.summary())
if outputs_layer:
# 从keras模型中提取层,转换成tflite
model = tf.keras.models.Model(inputs=model.input, outputs=model.get_layer(outputs_layer).output)
# outputs = [model.output["bbox"],model.output["scores"]]
# model = tf.keras.models.Model(inputs=model.input, outputs=outputs)
print(model.summary())
# converter = tf.lite.TocoConverter.from_keras_model_file(keras_file)
# converter = tf.lite.TFLiteConverter.from_keras_model_file(keras_file) # tf1.3
converter = tf.lite.TFLiteConverter.from_keras_model(model) # tf2.0
prefix = [model_name, outputs_layer]
# converter.allow_custom_ops = True
# converter.experimental_new_converter = True
""""
https://tensorflow.google.cn/lite/guide/ops_select
我们优先推荐使用 TFLITE_BUILTINS 转换模型,然后是同时使用 TFLITE_BUILTINS,SELECT_TF_OPS ,
最后是只使用 SELECT_TF_OPS。同时使用两个选项(也就是 TFLITE_BUILTINS,SELECT_TF_OPS)
会用 TensorFlow Lite 内置的运算符去转换支持的运算符。
有些 TensorFlow 运算符 TensorFlow Lite 只支持部分用法,这时可以使用 SELECT_TF_OPS 选项来避免这种局限性。
"""
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
tf.lite.OpsSet.SELECT_TF_OPS]
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
if optimize:
print("weight quantization")
# Enforce full integer quantization for all ops and use int input/output
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
prefix += ["optimize"]
else:
# "OPTIMIZE_FOR_SIZE","OPTIMIZE_FOR_LATENCY", Does the same as "DEFAULT"
converter.optimizations = [tf.lite.Optimize.DEFAULT]
if quantization == "int8":
# converter.optimizations = [tf.lite.Optimize.DEFAULT]
# converter.representative_dataset = representative_dataset_gen_simple
# converter.representative_dataset = representative_image_normalization
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# converter.inference_input_type = tf.int8 # or tf.uint8
# converter.inference_output_type = tf.int8 # or tf.uint8
# converter.target_spec.supported_types = [tf.int8]
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset_gen_simple
# Ensure that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# Set the input and output tensors to uint8 (APIs added in r2.3)
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
# converter.inference_type = tf.uint8 # tf.lite.constants.QUANTIZED_UINT8
# input_arrays = converter.get_input_arrays()
# converter.quantized_input_stats = {input_arrays[0]: (0, 1.0)} # mean, std_dev
# converter.default_ranges_stats = (0, 255)
elif quantization == "float16":
converter.target_spec.supported_types = [tf.float16]
prefix += [quantization]
if not out_tflite:
prefix = [str(n) for n in prefix if n]
prefix = "_".join(prefix)
out_tflite = os.path.join(model_dir, "{}.tflite".format(prefix))
tflite_model = converter.convert()
open(out_tflite, "wb").write(tflite_model)
print("successfully convert to tflite done")
print("save model at: {}".format(out_tflite))
def converer_tf_pb_to_tflite_v2(saved_model_dir,
outputs_layer=None,
out_tflite=None,
optimize=False,
quantization=False):
"""
:param saved_model_dir: keras *.h5 files
:param outputs_layer: default last layer
:param out_tflite: output *.tflite file
:param optimize
:return:
"""
if not os.path.exists(saved_model_dir):
raise Exception("Error:{}".format(saved_model_dir))
model_name = os.path.basename(saved_model_dir)
converter = tf.lite.TFLiteConverter.from_saved_model(model_path) # tf2.0
prefix = [model_name, outputs_layer]
converter.allow_custom_ops = True
# converter.experimental_new_converter = True
""""
https://tensorflow.google.cn/lite/guide/ops_select
我们优先推荐使用 TFLITE_BUILTINS 转换模型,然后是同时使用 TFLITE_BUILTINS,SELECT_TF_OPS ,
最后是只使用 SELECT_TF_OPS。同时使用两个选项(也就是 TFLITE_BUILTINS,SELECT_TF_OPS)
会用 TensorFlow Lite 内置的运算符去转换支持的运算符。
有些 TensorFlow 运算符 TensorFlow Lite 只支持部分用法,这时可以使用 SELECT_TF_OPS 选项来避免这种局限性。
"""
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
tf.lite.OpsSet.SELECT_TF_OPS]
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
if optimize:
print("weight quantization")
# Enforce full integer quantization for all ops and use int input/output
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
prefix += ["optimize"]
else:
# "OPTIMIZE_FOR_SIZE","OPTIMIZE_FOR_LATENCY", Does the same as "DEFAULT"
converter.optimizations = [tf.lite.Optimize.DEFAULT]
if quantization == "int8":
# converter.representative_dataset = representative_dataset_gen_simple
converter.representative_dataset = representative_dataset_gen
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# converter.inference_input_type = tf.int8 # or tf.uint8
# converter.inference_output_type = tf.int8 # or tf.uint8
converter.target_spec.supported_types = [tf.int8]
elif quantization == "float16":
converter.target_spec.supported_types = [tf.float16]
prefix += [quantization]
if not out_tflite:
prefix = [str(n) for n in prefix if n]
prefix = "_".join(prefix)
out_tflite = os.path.join(os.path.dirname(saved_model_dir), "{}.tflite".format(prefix))
tflite_model = converter.convert()
open(out_tflite, "wb").write(tflite_model)
print("successfully convert to tflite done")
print("save model at: {}".format(out_tflite))
def representative_dataset_gen():
"""
# 生成代表性数据集
:return:
"""
image_dir = './data/finger_images'
imgSet = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if os.path.isfile(os.path.join(image_dir, f))]
for img_path in imgSet:
orig_image = cv2.imread(img_path)
rgb_image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
image_tensor = cv2.resize(rgb_image, dsize=tuple(input_size))
image_tensor = np.asarray(image_tensor / 255.0, dtype=np.float32)
image_tensor = image_tensor[np.newaxis, :]
yield [image_tensor]
def representative_dataset_gen_simple():
for _ in range(250):
data = np.random.uniform(0.0, 1.0, size=(1, input_size[1], input_size[0], 3)).astype(np.float32)
yield [data]
def get_model(model_path):
if not model_path:
model_path = os.path.join(os.getcwd(), "*.h5")
model_list = glob.glob(model_path)
else:
model_list = [model_path]
return model_list
def unsupport_tflite_op():
"""
ValueError: Didn't find op for builtin opcode 'RESIZE_NEAREST_NEIGHBOR' version '3'
ValueError: Didn't find op for builtin opcode 'RESIZE_BILINEAR' version '3'
tf.shape,tf.Size
error: 'tf.Size' op is neither a custom op nor a flex op
error: 'tf.Softmax' op is neither a custom op nor a flex op
===========================================================
tf.Softmax-->tf.nn.softmax
"""
pass
def parse_args():
# weights_path = "../yolov3-micro.h5"
# weights_path = "../../yolov3-micro_freeze_head.h5"
# weights_path = "./yolov3-micro_freeze_head.h5"
# weights_path = "data/yolov3-micro_freeze_head"
# weights_path = "./data/yolov3-micro_freeze_head"
# weights_path = "data/yolov3-micro_freeze_head"
# weights_path = "data/model_152_loss308.8428.h5"
# weights_path = "data/yolov3-micro_freeze_head"
# weights_path = "data/yolov3-micro_freeze_head.h5"
# weights_path = "data/yolov3_micro0.35_320_freeze_head.h5"
# weights_path = "../../data/yolov3_micro0.25_320_320_freeze_head.h5"
# weights_path = "data/yolov3_micro0.25_320_320_freeze_head.h5"
# weights_path = "data/yolov3_simple0.5_320_320_freeze_head.h5"
# weights_path = "/home/dm/panjinquan3/FaceDetector/tf-yolov3-detection/data/yolov3_lite0.25_320_320_freeze_head.h5"
weights_path = "data/yolov3_lite0.25_320_320_freeze_head.h5"
input_size = [320, 320]
parser = argparse.ArgumentParser()
parser.add_argument("--input_size", help="input_size", default=input_size, type=str)
parser.add_argument("-c", "--model_path", help="model_path", default=weights_path, type=str)
parser.add_argument("--outputs_layer", help="outputs_layer", default=None, type=str)
parser.add_argument("-o", "--out_tflite", help="out tflite model path", default=None, type=str)
parser.add_argument("-opt", "--optimize", help="optimize model", default=True, type=bool)
# parser.add_argument("-q", "--quantization", help="quantization model", default=None, type=str)
# parser.add_argument("-q", "--quantization", help="quantization model", default="int8", type=str)
parser.add_argument("-q", "--quantization", help="quantization model", default="float16", type=str)
return parser.parse_args()
if __name__ == '__main__':
from utils import tf_tools
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
tf_tools.set_device_memory(0.9)
args = parse_args()
# outputs_layer = "fc1"
outputs_layer = args.outputs_layer
model_list = get_model(args.model_path)
out_tflite = args.out_tflite
input_size = args.input_size
optimize = args.optimize
quantization = args.quantization
for model_path in model_list:
converer_keras_to_tflite_v2(model_path, outputs_layer, out_tflite, optimize=optimize, quantization=quantization)
# converer_tf_pb_to_tflite_v2(model_path, outputs_layer, out_tflite, optimize=optimize, quantization=quantization)
# converer_keras_to_tflite_v1(keras_model, outputs_layer, out_tflite=None)