1、下载tensorflow源码,并用bazel编译
git clone https://github.com/tensorflow/tensorflow.git
bazel build tensorflow/tools/graph_transforms:transform_graph
bazel build tensorflow/tools/quantization:quantize_graph
2、transform_graph压缩
pb_in="model_224.pb"
pb_out="model_transgraph.pb"
graph_trans=~/Experiment/tensorflow/bazel-bin/tensorflow/tools/graph_transforms/transform_graph
$graph_trans --in_graph=$pb_in --out_graph=$pb_out --inputs="input_1" --outputs="output_0" --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"
各选项含义:
fold_constants:查找模型中始终为常量的表达式,并用常量替换他们
fold_batch_norms:训练过程中使用批量标准化时可以优化在Conv2D或者MatMul之后引入的Mul