-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconvert_tf_model.py
More file actions
33 lines (26 loc) · 1.36 KB
/
convert_tf_model.py
File metadata and controls
33 lines (26 loc) · 1.36 KB
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
import tensorflow as tf
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--post_train_quantize', default=False, type=bool, help='do post training quantization')
parser.add_argument('--model_path', type=str, help='path to the trained model')
parser.add_argument('--model_type', default='pb', type=str, help='type of the saved model, pb/chkp/keras')
parser.add_argument('--input', type=str, help='name of the input, needed only in case of model_type == pb')
parser.add_argument('--output', type=str, help='name of the output, needed only in case of the model_type == pb')
args = parser.parse_args()
tf.enable_eager_execution()
# create object of the TFLiteConverter type, depending on saved model
if args.model_type == 'pb':
input = [args.input]
output = [args.output]
converter = tf.lite.TFLiteConverter.from_frozen_graph(args.model_path, input, output)
elif args.model_type == 'chkp':
converter = tf.contrib.lite.TFLiteConverter.from_saved_model(args.model_path)
elif args.model_type == 'keras':
converter = tf.lite.TFLiteConverter.from_keras_model_file(args.model_path)
# do quantization of the weights
if args.post_train_quantize == True:
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# convert the model
tflite_model = converter.convert()
tflite_model_quant_file = "./test.tflite"
tflite_model_quant_file.write_bytes(tflite_model)