Skip to content

Commit bb224bf

Browse files
authored
Add CE test for dygraph qat (#748) (#749)
1 parent a8173f3 commit bb224bf

File tree

8 files changed

+726
-0
lines changed

8 files changed

+726
-0
lines changed

ce_tests/dygraph/qat/readme.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
1. 准备
2+
3+
安装需要测试的Paddle版本和PaddleSlim版本。
4+
5+
准备ImageNet数据集,假定解压到`/dataset/ILSVRC2012`文件夹,该文件夹下有`train文件夹、val文件夹、train_list.txt和val_list.txt文件`
6+
7+
2. 产出量化模型
8+
9+
`run_train.sh`文件中设置`data_path`为上述ImageNet数据集的路径`/dataset/ILSVRC2012`
10+
11+
根据实际情况,在`run_train.sh`文件中设置使用GPU的id等参数。
12+
13+
执行`sh run_train.sh` 会对几个分类模型使用动态图量化训练功能进行量化,其中只执行一个epoch。
14+
执行完后,在`output_models/quant_dygraph`目录下有产出的量化模型。
15+
16+
3. 转换量化模型
17+
18+
在Intel CPU上部署量化模型,需要使用`test/save_quant_model.py`脚本进行模型转换。
19+
20+
如下是对`mobilenet_v1`模型进行转换的示例。
21+
```
22+
python src/save_quant_model.py --load_model_path output_models/quant_dygraph/mobilenet_v1 --save_model_path int8_models/mobilenet_v1
23+
```
24+
25+
4. 测试量化模型
26+
27+
`run_test.sh`脚本中设置`data_path`为上述ImageNet数据集的路径`/dataset/ILSVRC2012`
28+
29+
根据实际情况,在`run_test.sh`文件中设置使用GPU的id等参数。
30+
31+
使用`run_test.sh`脚本测试转换前和转换后的量化模型精度。
32+
33+
比如:
34+
```
35+
sh run_test.sh output_models/quant_dygraph/mobilenet_v1
36+
sh run_test.sh int8_models/mobilenet_v1
37+
```
38+
39+
5. 测试目标
40+
41+
使用动态图量化训练功能,产出`mobilenet_v1`,`mobilenet_v2`,`resnet50`,`vgg16`量化模型,测试转换前后量化模型精度在1%误差范围内。

ce_tests/dygraph/qat/run_test.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
model_path=$1
2+
test_samples=1000 # if set as -1, use all test samples
3+
data_path='/dataset/ILSVRC2012/'
4+
batch_size=16
5+
6+
echo "--------eval model: ${model_name}-------------"
7+
python ./src/eval.py \
8+
--model_path=$model_path \
9+
--data_dir=${data_path} \
10+
--test_samples=${test_samples} \
11+
--batch_size=${batch_size}

ce_tests/dygraph/qat/run_train.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export CUDA_VISIBLE_DEVICES=5
2+
3+
data_path="/dataset/ILSVRC2012"
4+
epoch=1
5+
lr=0.0001
6+
batch_size=32
7+
num_workers=3
8+
output_dir=$PWD/output_models
9+
10+
for model in mobilenet_v1 mobilenet_v2 resnet50 vgg16
11+
do
12+
python ./src/qat.py \
13+
--arch=${model} \
14+
--data=${data_path} \
15+
--epoch=${epoch} \
16+
--batch_size=${batch_size} \
17+
--num_workers=${num_workers} \
18+
--lr=${lr} \
19+
--output_dir=${output_dir} \
20+
--enable_quant
21+
#--use_pact
22+
done

ce_tests/dygraph/qat/src/eval.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
5+
import os
6+
import numpy as np
7+
import time
8+
import sys
9+
import argparse
10+
import functools
11+
import math
12+
13+
import paddle
14+
import paddle.inference as paddle_infer
15+
from utility import add_arguments, print_arguments
16+
import imagenet_dataset as dataset
17+
18+
19+
def eval(args):
20+
model_file = os.path.join(args.model_path, args.model_filename)
21+
params_file = os.path.join(args.model_path, args.params_filename)
22+
config = paddle_infer.Config(model_file, params_file)
23+
config.enable_mkldnn()
24+
25+
predictor = paddle_infer.create_predictor(config)
26+
27+
input_names = predictor.get_input_names()
28+
input_handle = predictor.get_input_handle(input_names[0])
29+
output_names = predictor.get_output_names()
30+
output_handle = predictor.get_output_handle(output_names[0])
31+
32+
val_dataset = dataset.ImageNetDataset(data_dir=args.data_dir, mode='val')
33+
eval_loader = paddle.io.DataLoader(
34+
val_dataset, batch_size=args.batch_size, drop_last=True)
35+
36+
cost_time = 0.
37+
total_num = 0.
38+
correct_1_num = 0
39+
correct_5_num = 0
40+
for batch_id, data in enumerate(eval_loader()):
41+
img_np = np.array([tensor.numpy() for tensor in data[0]])
42+
label_np = np.array([tensor.numpy() for tensor in data[1]])
43+
44+
input_handle.reshape(img_np.shape)
45+
input_handle.copy_from_cpu(img_np)
46+
47+
t1 = time.time()
48+
predictor.run()
49+
t2 = time.time()
50+
cost_time += (t2 - t1)
51+
52+
output_data = output_handle.copy_to_cpu()
53+
54+
for i in range(len(label_np)):
55+
label = label_np[i][0]
56+
result = output_data[i, :]
57+
index = result.argsort()
58+
total_num += 1
59+
if index[-1] == label:
60+
correct_1_num += 1
61+
if label in index[-5:]:
62+
correct_5_num += 1
63+
64+
if batch_id % 10 == 0:
65+
acc1 = correct_1_num / total_num
66+
acc5 = correct_5_num / total_num
67+
avg_time = cost_time / total_num
68+
print(
69+
"batch_id {}, acc1 {:.3f}, acc5 {:.3f}, avg time {:.5f} sec/img".
70+
format(batch_id, acc1, acc5, avg_time))
71+
72+
if args.test_samples > 0 and \
73+
(batch_id + 1)* args.batch_size >= args.test_samples:
74+
break
75+
76+
acc1 = correct_1_num / total_num
77+
acc5 = correct_5_num / total_num
78+
print("End test: test_acc1 {:.3f}, test_acc5 {:.5f}".format(acc1, acc5))
79+
80+
81+
def main():
82+
parser = argparse.ArgumentParser(description=__doc__)
83+
add_arg = functools.partial(add_arguments, argparser=parser)
84+
add_arg('model_path', str, "", "The inference model path.")
85+
add_arg('model_filename', str, "int8_infer.pdmodel", "model filename")
86+
add_arg('params_filename', str, "int8_infer.pdiparams", "params filename")
87+
add_arg('data_dir', str, "/dataset/ILSVRC2012/",
88+
"The ImageNet dataset root dir.")
89+
add_arg('test_samples', int, -1,
90+
"Test samples. If set -1, use all test samples")
91+
add_arg('batch_size', int, 16, "Batch size.")
92+
93+
args = parser.parse_args()
94+
print_arguments(args)
95+
96+
eval(args)
97+
98+
99+
if __name__ == '__main__':
100+
main()

0 commit comments

Comments
 (0)