diff --git a/PaddleSlim/classification/models/resnet.py b/PaddleSlim/classification/models/resnet.py index 09e83675ed53a8c75a80ef53392221b2332043cd..64389e517853dae7cdcd2203e4808e7770cabed4 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 4c47522041187dbbbfcfb72b7fe7a9cbd0510f21..6bbe49df06c681198b9dda9165fd8ad4b04d1038 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 f972bad5f683266cc5d5a7a36ff169034df6ca04..4b4f0f2e7f6f73d5d39c7f6e84cfc0791814f19f 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():