Skip to content

Commit

Permalink
refine quantization demo (PaddlePaddle#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
slf12 authored and wanghaoshuang committed Jan 22, 2020
1 parent 138fe14 commit 501ab9d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 50 deletions.
56 changes: 27 additions & 29 deletions demo/quant/quant_aware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,29 @@

请参考 <a href='../../../paddleslim/quant/quantization_api_doc.md'>量化API文档</a>。

## 分类模型的离线量化流程
## 分类模型的量化训练流程

### 1. 配置量化参数
### 准备数据

``demo``文件夹下创建``data``文件夹,将``ImageNet``数据集解压在``data``文件夹下,解压后``data``文件夹下应包含以下文件:
- ``'train'``文件夹,训练图片
- ``'train_list.txt'``文件
- ``'val'``文件夹,验证图片
- ``'val_list.txt'``文件

### 准备需要量化的模型

使用以下命令下载训练好的模型并解压。

```
mkdir pretrain
cd pretrain
wget http://paddle-imagenet-models-name.bj.bcebos.com/MobileNetV1_pretrained.tar
tar xf MobileNetV1_pretrained.tar
cd ..
```

### 配置量化参数

```
quant_config = {
Expand All @@ -24,15 +44,15 @@ quant_config = {
}
```

### 2. 对训练和测试program插入可训练量化op
### 对训练和测试program插入可训练量化op

```
val_program = quant_aware(val_program, place, quant_config, scope=None, for_test=True)
compiled_train_prog = quant_aware(train_prog, place, quant_config, scope=None, for_test=False)
```

### 3.关掉指定build策略
### 关掉指定build策略

```
build_strategy = fluid.BuildStrategy()
Expand All @@ -45,32 +65,10 @@ compiled_train_prog = compiled_train_prog.with_data_parallel(
exec_strategy=exec_strategy)
```

### 4. freeze program

```
float_program, int8_program = convert(val_program,
place,
quant_config,
scope=None,
save_int8=True)
```

### 5.保存预测模型
### 训练命令

```
fluid.io.save_inference_model(
dirname=float_path,
feeded_var_names=[image.name],
target_vars=[out], executor=exe,
main_program=float_program,
model_filename=float_path + '/model',
params_filename=float_path + '/params')
fluid.io.save_inference_model(
dirname=int8_path,
feeded_var_names=[image.name],
target_vars=[out], executor=exe,
main_program=int8_program,
model_filename=int8_path + '/model',
params_filename=int8_path + '/params')
python train.py --model MobileNet --pretrained_model ./pretrain/MobileNetV1_pretrained --checkpoint_dir ./output/mobilenetv1 --num_epochs 30
```
运行之后,可看到``best_model``的最后测试结果,和MobileNet量化前的精度top1=70.99%, top5=89.68%非常相近。
52 changes: 35 additions & 17 deletions demo/quant/quant_aware/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import time
import numpy as np
import paddle.fluid as fluid
sys.path.append(sys.path[0] + "../../../")
sys.path.append(sys.path[0] + "../../")
sys.path.append(sys.path[0] + "/../../../")
sys.path.append(sys.path[0] + "/../../")
from paddleslim.common import get_logger
from paddleslim.analysis import flops
from paddleslim.quant import quant_aware, quant_post, convert
Expand Down Expand Up @@ -37,7 +37,7 @@
add_arg('config_file', str, None, "The config file for compression with yaml format.")
add_arg('data', str, "imagenet", "Which data to use. 'mnist' or 'imagenet'")
add_arg('log_period', int, 10, "Log period in batches.")
add_arg('test_period', int, 10, "Test period in epoches.")
add_arg('checkpoint_dir', str, "output", "checkpoint save dir")
# yapf: enable

model_list = [m for m in dir(models) if "__" not in m]
Expand Down Expand Up @@ -192,16 +192,6 @@ def test(epoch, program):
return np.mean(np.array(acc_top1_ns))

def train(epoch, compiled_train_prog):
build_strategy = fluid.BuildStrategy()
build_strategy.memory_optimize = False
build_strategy.enable_inplace = False
build_strategy.fuse_all_reduce_ops = False
build_strategy.sync_batch_norm = False
exec_strategy = fluid.ExecutionStrategy()
compiled_train_prog = compiled_train_prog.with_data_parallel(
loss_name=avg_cost.name,
build_strategy=build_strategy,
exec_strategy=exec_strategy)

batch_id = 0
for data in train_reader():
Expand All @@ -221,14 +211,41 @@ def train(epoch, compiled_train_prog):
end_time - start_time))
batch_id += 1

build_strategy = fluid.BuildStrategy()
build_strategy.memory_optimize = False
build_strategy.enable_inplace = False
build_strategy.fuse_all_reduce_ops = False
build_strategy.sync_batch_norm = False
exec_strategy = fluid.ExecutionStrategy()
compiled_train_prog = compiled_train_prog.with_data_parallel(
loss_name=avg_cost.name,
build_strategy=build_strategy,
exec_strategy=exec_strategy)

############################################################################################################
# train loop
############################################################################################################
best_acc1 = 0.0
best_epoch = 0
for i in range(args.num_epochs):
train(i, compiled_train_prog)
if i % args.test_period == 0:
test(i, val_program)

acc1 = test(i, val_program)
fluid.io.save_persistables(
exe,
dirname=os.path.join(args.checkpoint_dir, str(i)),
main_program=val_program)
if acc1 > best_acc1:
best_acc1 = acc1
best_epoch = i
fluid.io.save_persistables(
exe,
dirname=os.path.join(args.checkpoint_dir, 'best_model'),
main_program=val_program)

fluid.io.load_persistables(
exe,
dirname=os.path.join(args.checkpoint_dir, 'best_model'),
main_program=val_program)
############################################################################################################
# 3. Freeze the graph after training by adjusting the quantize
# operators' order for the inference.
Expand All @@ -237,7 +254,8 @@ def train(epoch, compiled_train_prog):
float_program, int8_program = convert(val_program, place, quant_config, \
scope=None, \
save_int8=True)

print("eval best_model after convert")
final_acc1 = test(best_epoch, float_program)
############################################################################################################
# 4. Save inference model
############################################################################################################
Expand Down
4 changes: 2 additions & 2 deletions demo/quant/quant_post/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

### 准备数据

在当前文件夹下创建``data``文件夹,将``imagenet``数据集解压在``data``文件夹下,解压后``data``文件夹下应包含以下文件:
``demo``文件夹下创建``data``文件夹,将``ImageNet``数据集解压在``data``文件夹下,解压后``data``文件夹下应包含以下文件:
- ``'train'``文件夹,训练图片
- ``'train_list.txt'``文件
- ``'val'``文件夹,验证图片
Expand Down Expand Up @@ -69,4 +69,4 @@ python eval.py --model_path ./quant_model_train/MobileNet
```
top1_acc/top5_acc= [0.70141864 0.89086477]
```
从以上精度对比可以看出,对``mobilenet````imagenet``上的分类模型进行离线量化后 ``top1``精度损失为``0.77%````top5``精度损失为``0.46%``.
从以上精度对比可以看出,对``mobilenet````imagenet``上的分类模型进行离线量化后 ``top1``精度损失为``0.77%````top5``精度损失为``0.46%``.
3 changes: 1 addition & 2 deletions demo/quant/quant_post/quant_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
import numpy as np
import paddle.fluid as fluid

import reader
sys.path.append(sys.path[0] + "/../../../")
from paddleslim.common import get_logger
from paddleslim.quant import quant_post
sys.path.append(sys.path[0] + "/../../")
from utility import add_arguments, print_arguments

import imagenet_reader as reader
_logger = get_logger(__name__, level=logging.INFO)

parser = argparse.ArgumentParser(description=__doc__)
Expand Down

0 comments on commit 501ab9d

Please sign in to comment.