当前位置:   article > 正文

tensorflow-模型训练后tflite量化(tensorflow1.15)_tf.lite.optimize.default

tf.lite.optimize.default

 1.方式一: converter.post_training_quantize=True

  1. def convert_model_qtv1():
  2. converter = tf.lite.TFLiteConverter.from_frozen_graph('model_convert/ckpt_new/densenet_ocr.pb', input_arrays=["Placeholder"],
  3. input_shapes={"Placeholder": [1, 32, 280, 1]}, output_arrays=[
  4. "sequence_rnn_module/transpose_time_major"])
  5. converter.post_training_quantize=True
  6. tflite_model = converter.convert()
  7. open("model_convert/ckpt_new/densenet_ocr_metal_v1.tflite", "wb").write(tflite_model)

2.方式二:Dynamic range quantization

The simplest form of post-training quantization statically quantizes only the weights from floating point to integer, which has 8-bits of precision:

  1. def convert_model_qtv1():
  2. converter = tf.lite.TFLiteConverter.from_frozen_graph('model_convert/ckpt_new/densenet_ocr.pb', input_arrays=["Placeholder"],
  3. input_shapes={"Placeholder": [1, 32, 280, 1]}, output_arrays=[
  4. "sequence_rnn_module/transpose_time_major"])
  5. converter.optimizations = [tf.lite.Optimize.DEFAULT]
  6. tflite_model = converter.convert()
  7. open("model_convert/ckpt_new/densenet_ocr_metal-DY_v1.tflite", "wb").write(tflite_model)

3.方式三:Full integer quantization

Integer with float fallback (using default float input/output):

  1. def representative_dataset():
  2. for _ in range(100):
  3. #这里一定记得要修改data的输入shape与模型的输入shape吻合
  4. data = np.random.rand(1, 32, 280, 1)
  5. yield [data.astype(np.float32)]
  6. def convert_model_qtv1():
  7. converter = tf.lite.TFLiteConverter.from_frozen_graph('model_convert/ckpt_new/densenet_ocr.pb', input_arrays=["Placeholder"],
  8. input_shapes={"Placeholder": [1, 32, 280, 1]}, output_arrays=[
  9. "sequence_rnn_module/transpose_time_major"])
  10. converter.optimizations = [tf.lite.Optimize.DEFAULT]
  11. converter.representative_dataset = representative_dataset
  12. tflite_model = converter.convert()
  13. open("model_convert/ckpt_new/densenet_ocr_metal-int_v1.tflite", "wb").write(tflite_model)

4.方式四:Float16 quantization

To enable float16 quantization of weights, use the following steps:

  1. def convert_model_qtv1():
  2. converter = tf.lite.TFLiteConverter.from_frozen_graph('model_convert/ckpt_new/densenet_ocr.pb', input_arrays=["Placeholder"],
  3. input_shapes={"Placeholder": [1, 32, 280, 1]}, output_arrays=[
  4. "sequence_rnn_module/transpose_time_major"])
  5. converter.optimizations = [tf.lite.Optimize.DEFAULT]
  6. converter.target_spec.supported_types = [tf.float16]
  7. tflite_model = converter.convert()
  8. open("model_convert/ckpt_new/densenet_ocr_metal-fl16_v1.tflite", "wb").write(tflite_model)

转换之后的tflite大小对比:

 ---没量化前

 ---方式一

 ---方式二

---方式三

---方式四

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/393070
推荐阅读
相关标签
  

闽ICP备14008679号