From 6e332d3f8c4f9f31282019508c62d8528e545054 Mon Sep 17 00:00:00 2001 From: Liufang Sang Date: Tue, 22 Oct 2019 20:55:30 +0800 Subject: [PATCH] [PaddleSlim] add skip_quant test in doc (#3719) --- PaddleSlim/classification/models/resnet.py | 5 +-- .../classification/quantization/README.md | 32 +++++++++++++++++++ .../classification/quantization/compress.py | 10 ++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/PaddleSlim/classification/models/resnet.py b/PaddleSlim/classification/models/resnet.py index 09e83675..64389e51 100644 --- a/PaddleSlim/classification/models/resnet.py +++ b/PaddleSlim/classification/models/resnet.py @@ -44,6 +44,8 @@ class ResNet(): # TODO(wanghaoshuang@baidu.com): # fix name("conv1") conflict between student and teacher in distillation. + + #with fluid.name_scope('skip_quant'): conv = self.conv_bn_layer( input=input, num_filters=64, @@ -51,8 +53,7 @@ class ResNet(): stride=2, act='relu', name=prefix_name + conv1_name) - with fluid.name_scope("skip_quant"): - conv = fluid.layers.pool2d( + conv = fluid.layers.pool2d( input=conv, pool_size=3, pool_stride=2, diff --git a/PaddleSlim/classification/quantization/README.md b/PaddleSlim/classification/quantization/README.md index 4c475220..6bbe49df 100644 --- a/PaddleSlim/classification/quantization/README.md +++ b/PaddleSlim/classification/quantization/README.md @@ -159,6 +159,38 @@ python infer.py \ ### PaddleLite预测 FP32模型可使用Paddle-Lite进行加载预测,可参见教程[Paddle-Lite如何加载运行量化模型](https://github.com/PaddlePaddle/Paddle-Lite/wiki/model_quantization)。 +## 如何进行部分量化 + +通过在定义op时指定 ``name_scope``为 ``skip_quant``可对这个op跳过量化。比如在PaddleSlim/classification/models/resnet.py中,将某个conv的定义作如下改变: + +原定义: +``` +conv = self.conv_bn_layer( + input=input, + num_filters=64, + filter_size=7, + stride=2, + act='relu', + name=prefix_name + conv1_name) + +``` + +跳过量化时的定义: + +``` + +with fluid.name_scope('skip_quant'): + conv = self.conv_bn_layer( + input=input, + num_filters=64, + filter_size=7, + stride=2, + act='relu', + name=prefix_name + conv1_name) + +``` +在脚本 PaddleSlim/classification/quantization/compress.py 中,统计了``conv`` op的数量和以``fake_quantize``开头的量化op的数量,在对一些``conv`` op跳过之后,可发现以``fake_quantize``开头的量化op的数量变少。 + ## 示例结果 diff --git a/PaddleSlim/classification/quantization/compress.py b/PaddleSlim/classification/quantization/compress.py index f972bad5..4b4f0f2e 100644 --- a/PaddleSlim/classification/quantization/compress.py +++ b/PaddleSlim/classification/quantization/compress.py @@ -99,6 +99,16 @@ def compress(args): distiller_optimizer=None) com_pass.config(args.config_file) com_pass.run() + conv_op_num = 0 + fake_quant_op_num = 0 + for op in com_pass.context.eval_graph.ops(): + if op._op.type == 'conv2d': + conv_op_num += 1 + elif op._op.type.startswith('fake_quantize'): + fake_quant_op_num += 1 + print('conv op num {}'.format(conv_op_num)) + print('fake quant op num {}'.format(fake_quant_op_num)) + def main(): -- GitLab