Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
AI_人工智能
TFLite
关于 tensorflow lite 量化的一点思考(简)
发布于 2021-06-30 15:51:34 浏览:1363
订阅该版
[tocm] > tensorflow lite 官网:https://tensorflow.google.cn/lite/performance/post_training_integer_quant **本文仅涉及到模型训练好之后的量化工作** Tensorflow Lite 官网提供了三种量化方式:  ## 1 (未量化) 模型转 tflite 格式 ```python import tensorflow as tf tflite_model = tf.keras.models.load_model(path) # 如果是tf1: tf.compat.v1.lite.TFLiteConverter.from_saved_model() # Convert the model converter = tf.lite.TFLiteConverter.from_saved_model(tflite_model) # path to the SavedModel directory tflite_model = converter.convert() # Convert the keras model. converter = tf.lite.TFLiteConverter.from_keras_model(tflite_model) tflite_model = converter.convert() # Save the model. with open('model.tflite', 'wb') as f: f.write(tflite_model) ``` ## 2 Float 16 量化 将权重与激活函数均转换为16位浮点数。 - 模型减小1/2。 - 量化中精度损失最少 缺点:float16 量化模型在 CPU 上运行时会将权重值“反量化”为 float32。 ```python import tensorflow as tf converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) converter.optimizations = [tf.lite.Optimize.DEFAULT] converter.target_spec.supported_types = [tf.float16] tflite_quant_model = converter.convert() ``` ## 3 动态范围量化 Dynamic range quantization 只能用于CPU加速, “动态范围”:根据激活函数的范围动态的将其转换为8bit整数 仅量化权重,从float32量化为int8,激活保持不变,模型减小了3/4 。 在推理的时候,把int8转回fp32,**输入和输出都是浮点数** ```python import tensorflow as tf converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) converter.optimizations = [tf.lite.Optimize.DEFAULT] tflite_quant_model = converter.convert() ```  ## 4 全整型量化 需要校准或估计模型中所有浮点张量的范围,即 (min, max),所以需要一部分的数据集。 将数据集送进去的函数: ```python def representative_dataset(): for sample in samples: yield [sample.image] ``` - 量化权重和偏置,输入输出是浮点型 ```python import tensorflow as tf converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) converter.optimizations = [tf.lite.Optimize.DEFAULT] # 定义示例数据生成器 def representative_dataset_gen(): for _ in range(num_calibration_steps): # Get sample input data as a numpy array in a method of your choosing. yield [input] # 为转换器提供示例数据 converter.representative_dataset = representative_dataset_gen tflite_quant_model = converter.convert() ``` - 输入输出是整型 ```python import tensorflow as tf converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) converter.optimizations = [tf.lite.Optimize.DEFAULT] def representative_dataset_gen(): for _ in range(num_calibration_steps): # Get sample input data as a numpy array in a method of your choosing. yield [input] 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 tflite_quant_model = converter.convert() ``` 可以看到输入和输出张量现在是整数格式: ```python interpreter = tf.lite.Interpreter(model_content=tflite_model_quant) input_type = interpreter.get_input_details()[0]['dtype'] print('input: ', input_type) output_type = interpreter.get_output_details()[0]['dtype'] print('output: ', output_type) # input:
# output:
```
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
lebhoryi
这家伙很懒,什么也没写!
文章
30
回答
6
被采纳
1
关注TA
发私信
相关文章
1
RT-Thread AI Kit 相关资料和教程在哪里?
2
20号的开发者大会上,人脸识别AI
3
2020 开发者大会演示的 AI 套件什么时候开源?
4
请问RT-AK有没有部署MobileNet的例子呢
5
请问RT-AK会支持paddlepaddle的模型吗
6
嵌入式比赛中要求的 RT-Thread ai toolkit 能介绍一下吗?
7
使用rt_ai_tools转换模型时报错
8
求一个识别人有没有带口罩的模型?
9
RT-AK的人物检测例子下载后编译失败
10
cube ai部署后报错
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
UART
ESP8266
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
RTT_逍遥
10
个答案
3
次被采纳
xiaorui
3
个答案
2
次被采纳
winfeng
2
个答案
2
次被采纳
三世执戟
8
个答案
1
次被采纳
KunYi
8
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
lizimu
2
篇文章
9
次点赞
swet123
1
篇文章
4
次点赞
Days
1
篇文章
4
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部