版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xueyingxue001/article/details/72726421
PS1:Quantize Neural Networks 的内容见此文章:
https://petewarden.com/2016/05/03/how-to-quantize-neural-networks-with-tensorflow/
PS2:我使用的是 tensorflow 1.1
1,安装
1.1 下载源码(之后用 tensorflow_root 来代指进入源码的第一层目录)
git clone https://github.com/tensorflow/tensorflow.git
1.2 进入 tensorflow_root 目录
cd tensorflow
1.3 编译 label_image
./configure
bazel build --config=mkl --copt="-DEIGEN_USE_VML" -c opt tensorflow/examples/label_image:label_image
1.4 编译 quantize(与 PS 中文章的步骤不同,这里要使用这个命令)
bazel build --config=mkl --copt="-DEIGEN_USE_VML" -c opt tensorflow/tools/quantization:quantize_graph
1.5 编译 tensorflow
bazel build --config=mkl --copt="-DEIGEN_USE_VML" -c opt //tensorflow/tools/pip_package:build_pip_package
1.6 生成 pip 包
mkdir -p /tmp/tensorflow/
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow/
1.7 安装
pip install /tmp/tensorflow/tensorflow*.whl
2,压缩 pb
2.1 curl http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz -o /tmp/inceptionv3.tgz
2.2 tar xzf /tmp/inceptionv3.tgz -C /tmp/
2.3 在 tensorflow_root 中执行如下命令:
bazel-bin/tensorflow/tools/quantization/quantize_graph \
--input=/tmp/classify_image_graph_def.pb \
--output_node_names="softmax" --output=/tmp/quantized_graph.pb \
--mode=eightbit
2.4 结果验证:
运行命令 ls -lh /tmp 发现多了个 quantized_graph.pb,对比大小如下:
classify_image_graph_def.pb : 92M
quantized_graph.pb : 24M
真的小了好多。
3, 验证新模型
在 tensorflow_root 目录下
3.1 执行如下命令使用新模型
bazel-bin/tensorflow/examples/label_image/label_image \
--graph=/tmp/quantized_graph.pb \
--labels=/tmp/imagenet_synset_to_human_label_map.txt \
--input_width=299 \
--input_height=299 \
--input_mean=128 \
--input_std=128 \
--input_layer="Mul:0" \
--output_layer="softmax:0"
3.2 执行如下命令使用原模型
bazel-bin/tensorflow/examples/label_image/label_image \
--graph=/tmp/classify_image_graph_def.pb \
--labels=/tmp/imagenet_synset_to_human_label_map.txt \
--input_width=299 \
--input_height=299 \
--input_mean=128 \
--input_std=128 \
--input_layer="Mul:0" \
--output_layer="softmax:0"
3.3 对比输出
结果略有不同,但还是挺接近的。
https://petewarden.com/2016/05/03/how-to-quantize-neural-networks-with-tensorflow/
PS2:我使用的是 tensorflow 1.1
1,安装
1.1 下载源码(之后用 tensorflow_root 来代指进入源码的第一层目录)
git clone https://github.com/tensorflow/tensorflow.git
1.2 进入 tensorflow_root 目录
cd tensorflow
1.3 编译 label_image
./configure
bazel build --config=mkl --copt="-DEIGEN_USE_VML" -c opt tensorflow/examples/label_image:label_image
1.4 编译 quantize(与 PS 中文章的步骤不同,这里要使用这个命令)
bazel build --config=mkl --copt="-DEIGEN_USE_VML" -c opt tensorflow/tools/quantization:quantize_graph
1.5 编译 tensorflow
bazel build --config=mkl --copt="-DEIGEN_USE_VML" -c opt //tensorflow/tools/pip_package:build_pip_package
1.6 生成 pip 包
mkdir -p /tmp/tensorflow/
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow/
1.7 安装
pip install /tmp/tensorflow/tensorflow*.whl
2,压缩 pb
2.1 curl http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz -o /tmp/inceptionv3.tgz
2.2 tar xzf /tmp/inceptionv3.tgz -C /tmp/
2.3 在 tensorflow_root 中执行如下命令:
bazel-bin/tensorflow/tools/quantization/quantize_graph \
--input=/tmp/classify_image_graph_def.pb \
--output_node_names="softmax" --output=/tmp/quantized_graph.pb \
--mode=eightbit
2.4 结果验证:
运行命令 ls -lh /tmp 发现多了个 quantized_graph.pb,对比大小如下:
classify_image_graph_def.pb : 92M
quantized_graph.pb : 24M
真的小了好多。
3, 验证新模型
在 tensorflow_root 目录下
3.1 执行如下命令使用新模型
bazel-bin/tensorflow/examples/label_image/label_image \
--graph=/tmp/quantized_graph.pb \
--labels=/tmp/imagenet_synset_to_human_label_map.txt \
--input_width=299 \
--input_height=299 \
--input_mean=128 \
--input_std=128 \
--input_layer="Mul:0" \
--output_layer="softmax:0"
3.2 执行如下命令使用原模型
bazel-bin/tensorflow/examples/label_image/label_image \
--graph=/tmp/classify_image_graph_def.pb \
--labels=/tmp/imagenet_synset_to_human_label_map.txt \
--input_width=299 \
--input_height=299 \
--input_mean=128 \
--input_std=128 \
--input_layer="Mul:0" \
--output_layer="softmax:0"
3.3 对比输出
结果略有不同,但还是挺接近的。