tensorflow 模型压缩

模型压缩node

为了将tensorflow深度学习模型部署到移动/嵌入式设备上,咱们应该致力于减小模型的内存占用,缩短推断时间,减小耗电。有几种方法能够实现这些要求,如量化、权重剪枝或将大模型提炼成小模型。sql

在这个项目中,我使用了 TensorFlow 中的量化工具来进行模型压缩。目前我只使用权重量化来减少模型大小,由于根据 Mac 上的测试结果,完整 8 位转换没有提供额外的好处,好比缩短推断时间。(因为 requant_range 中的错误,没法在 Pixel 上运行完整的 8 位模型)。因为 8 位量化工具不适合 CPU,时间甚至翻了一倍。若是你有兴趣了解更多关于量化的实用建议,能够阅读 Pete Warden 这篇很棒的文章(https://petewarden.com/2017/06/22/what-ive-learned-about-neural-network-quantization/)。工具

对模型进行权重量化:学习

  1. 将模型写入协议缓冲区文件。
  2. 从源安装和配置 TensorFlow(https://www.tensorflow.org/install/install_sources)。
  3. 在 TensorFlow 目录下运行下列命令行:
  1. bazel build tensorflow/tools/graph_transforms:transform_graph 
  2. bazel-bin/tensorflow/tools/graph_transforms/transform_graph --in_graph=/your/.pb/file  --outputs="output_node_name"  --out_graph=/the/quantized/.pb/file  --transforms='quantize_weights' 

以个人项目为例,在量化权重后,预训练的 WaveNet 模型的大小从 15.5Mb 降低到了 4.0Mb。如今能够将这个模型文件移动到安卓项目中的「assets」文件夹。测试

相关文章
相关标签/搜索