tensorflow模型量化压缩

在使用tensorflow这个功能时候需要先下载tensorflow的源代码:

git clone https://github.com/tensorflow/tensorflow.git

进入tensorflow根目录,这里使用tools文件下的两个工具进行量化压缩:graph_transforms、quantization。
安装bazel进行tensorflow工具包的编译。
这里需要注意的是,bazel最好使用最新的,这样编译tensorflow就不会报接口未部署的一些错误。
Linux安装bazel:
https://github.com/bazelbuild/bazel/release
找到bazel-x.x.x-installer-linux-x86_64.sh下载到本地并安装。可以按照git的安装方法进行安装。
安装完毕后开始进行编译tensorflow:

bazel build tensorflow/tools/graph_transforms:transform_graph
bazel build tensorflow/tools/quantization:quantize_graph

编译需要占据很多内存以及cpu资源,建议在性能好点的机器上编译。
编译完成后使用:

pb_in=$pb_path
pb_out=$pb_path_out

graph_trans=tensorflow/bazel-bin/tensorflow/tools/graph_transforms/transform_graph

$graph_trans --in_graph=$pb_in --out_graph=$pb_in --inputs='input_data' --outputs='softmax' --transforms='
  add_default_attributes
  fold_constants(ignore_errors=true)
  fold_batch_norms
  fold_old_batch_norms
  quantize_weights
  quantize_nodes
  strip_unused_nodes
  sort_by_execution_order'

亲测21M的模型,量化压缩后,模型大小缩小为5.3M。将近4倍的压缩比率。其实在大型的vgg网络上进行压缩,效果更明显,91M模型减小到21M。
对于第二个工具quantize_graph。

bazel-bin/tensorflow/tools/quantization/quantize_graph \  
--input=input.pb  
--output_node_names="softmax2"   
--print_nodes  
--output=out.pb \  
--mode=eightbit  
--logtostderr  

发现执行上述命令后,模型大小变化不大????
实验结果表示,建议使用transform_graph进行模型量化压缩
参考:
https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API

https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/graph_transforms#shrinking-file-size
LSTM模型压缩问题:

Node ‘rY’ expects to be colocated with unknown node ‘logits/biases’

https://github.com/tensorflow/tensorflow/issues/9494
–transforms=’remove_attribute(attribute_name=_class)’

requested_output_max must be >= requested_output_min, but got nan and 0

https://github.com/tensorflow/tensorflow/issues/10739
**The results are very similar that I get when I remove “quantize_nodes” from “transforms”.
This behavior is same also for inceptionV3 and inceptionV4 as that for inception_resnet_v2.**

评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值