forked from noahchalifour/rnnt-speech-recognition
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquantize_model.py
44 lines (28 loc) · 1.13 KB
/
quantize_model.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
from argparse import ArgumentParser
import os
import tensorflow as tf
from utils import model as model_utils
def main(args):
hparams = model_utils.load_hparams(args.model_dir)
model, _ = model_utils.load_model(args.model_dir, hparams,
stateful=True)
model.summary()
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.experimental_new_converter = True
# converter.experimental_new_quantizer = True
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
tf.lite.OpsSet.SELECT_TF_OPS]
# converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()
tflite_dir = os.path.join(args.model_dir, 'tflite')
os.makedirs(tflite_dir, exist_ok=True)
with open(os.path.join(tflite_dir, 'model.tflite'), 'wb') as f:
f.write(tflite_quant_model)
def parse_args():
ap = ArgumentParser()
ap.add_argument('-m', '--model_dir', type=str, default='./model',
help='Directory of model.')
return ap.parse_args()
if __name__ == '__main__':
args = parse_args()
main(args)