Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
4107167f
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
1 年多 前同步成功
通知
51
Star
1434
Fork
344
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
53
列表
看板
标记
里程碑
合并请求
16
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleSlim
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
53
Issue
53
列表
看板
标记
里程碑
合并请求
16
合并请求
16
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
4107167f
编写于
1月 05, 2021
作者:
B
Bai Yifan
提交者:
GitHub
1月 05, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
quant quick_start update (#581)
* quant quick_start update * quant quick_start update
上级
1017881f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
124 addition
and
53 deletion
+124
-53
docs/zh_cn/quick_start/quant_aware_tutorial.md
docs/zh_cn/quick_start/quant_aware_tutorial.md
+61
-24
docs/zh_cn/quick_start/quant_post_static_tutorial.md
docs/zh_cn/quick_start/quant_post_static_tutorial.md
+63
-29
未找到文件。
docs/zh_cn/quick_start/quant_aware_tutorial.md
浏览文件 @
4107167f
...
...
@@ -10,25 +10,43 @@
6.
保存量化后的模型
## 1. 导入依赖
PaddleSlim依赖Paddle
1.7
版本,请确认已正确安装Paddle,然后按以下方式导入Paddle和PaddleSlim:
PaddleSlim依赖Paddle
2.0
版本,请确认已正确安装Paddle,然后按以下方式导入Paddle和PaddleSlim:
```
python
import
paddle
import
paddle.fluid
as
fluid
import
paddleslim
as
slim
import
numpy
as
np
paddle
.
enable_static
()
```
## 2. 构建网络
该章节构造一个用于对MNIST数据进行分类的分类模型,选用
`MobileNetV1`
,并将输入大小设置为
`[1, 28, 28]`
,输出类别数为10。 为了方便展示示例,我们在
`paddleslim.models`
下预定义了用于构建分类模型的方法,执行以下代码构建分类模型:
>注意:paddleslim.models下的API并非PaddleSlim常规API,是为了简化示例而封装预定义的一系列方法,比如:模型结构的定义、Program的构建等。
```
python
exe
,
train_program
,
val_program
,
inputs
,
outputs
=
\
slim
.
models
.
image_classification
(
"MobileNet"
,
[
1
,
28
,
28
],
10
,
use_gpu
=
True
)
USE_GPU
=
True
model
=
slim
.
models
.
MobileNet
()
train_program
=
paddle
.
static
.
Program
()
startup
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
train_program
,
startup
):
image
=
paddle
.
static
.
data
(
name
=
'image'
,
shape
=
[
None
,
1
,
28
,
28
],
dtype
=
'float32'
)
label
=
paddle
.
static
.
data
(
name
=
'label'
,
shape
=
[
None
,
1
],
dtype
=
'int64'
)
gt
=
paddle
.
reshape
(
label
,
[
-
1
,
1
])
out
=
model
.
net
(
input
=
image
,
class_dim
=
10
)
cost
=
paddle
.
nn
.
functional
.
loss
.
cross_entropy
(
input
=
out
,
label
=
gt
)
avg_cost
=
paddle
.
mean
(
x
=
cost
)
acc_top1
=
paddle
.
metric
.
accuracy
(
input
=
out
,
label
=
gt
,
k
=
1
)
acc_top5
=
paddle
.
metric
.
accuracy
(
input
=
out
,
label
=
gt
,
k
=
5
)
opt
=
paddle
.
optimizer
.
Momentum
(
0.01
,
0.9
)
opt
.
minimize
(
avg_cost
)
place
=
paddle
.
CUDAPlace
(
0
)
if
USE_GPU
else
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup
)
val_program
=
train_program
.
clone
(
for_test
=
True
)
```
## 3. 训练模型
...
...
@@ -36,17 +54,33 @@ exe, train_program, val_program, inputs, outputs = \
### 3.1 定义输入数据
为了快速执行该示例,我们选取简单的MNIST数据,Paddle框架的
`paddle.
dataset.mnis
t`
包定义了MNIST数据的下载和读取。
为了快速执行该示例,我们选取简单的MNIST数据,Paddle框架的
`paddle.
vision.datase
t`
包定义了MNIST数据的下载和读取。
代码如下:
```
python
import
paddle.dataset.mnist
as
reader
train_reader
=
paddle
.
fluid
.
io
.
batch
(
reader
.
train
(),
batch_size
=
128
,
drop_last
=
True
)
test_reader
=
paddle
.
fluid
.
io
.
batch
(
reader
.
train
(),
batch_size
=
128
,
drop_last
=
True
)
train_feeder
=
fluid
.
DataFeeder
(
inputs
,
fluid
.
CPUPlace
())
import
paddle.vision.transforms
as
T
transform
=
T
.
Compose
([
T
.
Transpose
(),
T
.
Normalize
([
127.5
],
[
127.5
])])
train_dataset
=
paddle
.
vision
.
datasets
.
MNIST
(
mode
=
"train"
,
backend
=
"cv2"
,
transform
=
transform
)
test_dataset
=
paddle
.
vision
.
datasets
.
MNIST
(
mode
=
"test"
,
backend
=
"cv2"
,
transform
=
transform
)
train_loader
=
paddle
.
io
.
DataLoader
(
train_dataset
,
places
=
place
,
feed_list
=
[
image
,
label
],
drop_last
=
True
,
batch_size
=
64
,
return_list
=
False
,
shuffle
=
True
)
test_loader
=
paddle
.
io
.
DataLoader
(
test_dataset
,
places
=
place
,
feed_list
=
[
image
,
label
],
drop_last
=
True
,
batch_size
=
64
,
return_list
=
False
,
shuffle
=
False
)
```
### 3.2 训练和测试
...
...
@@ -54,10 +88,11 @@ train_feeder = fluid.DataFeeder(inputs, fluid.CPUPlace())
```
python
outputs
=
[
acc_top1
.
name
,
acc_top5
.
name
,
avg_cost
.
name
]
def
train
(
prog
):
iter
=
0
for
data
in
train_
re
ader
():
acc1
,
acc5
,
loss
=
exe
.
run
(
prog
,
feed
=
train_feeder
.
feed
(
data
)
,
fetch_list
=
outputs
)
for
data
in
train_
lo
ader
():
acc1
,
acc5
,
loss
=
exe
.
run
(
prog
,
feed
=
data
,
fetch_list
=
outputs
)
if
iter
%
100
==
0
:
print
(
'train iter={}, top1={}, top5={}, loss={}'
.
format
(
iter
,
acc1
.
mean
(),
acc5
.
mean
(),
loss
.
mean
()))
iter
+=
1
...
...
@@ -65,8 +100,8 @@ def train(prog):
def
test
(
prog
):
iter
=
0
res
=
[[],
[]]
for
data
in
t
rain_re
ader
():
acc1
,
acc5
,
loss
=
exe
.
run
(
prog
,
feed
=
train_feeder
.
feed
(
data
)
,
fetch_list
=
outputs
)
for
data
in
t
est_lo
ader
():
acc1
,
acc5
,
loss
=
exe
.
run
(
prog
,
feed
=
data
,
fetch_list
=
outputs
)
if
iter
%
100
==
0
:
print
(
'test iter={}, top1={}, top5={}, loss={}'
.
format
(
iter
,
acc1
.
mean
(),
acc5
.
mean
(),
loss
.
mean
()))
res
[
0
].
append
(
acc1
.
mean
())
...
...
@@ -127,14 +162,16 @@ test(val_quant_program)
```
python
float_prog
,
int8_prog
=
slim
.
quant
.
convert
(
val_quant_program
,
exe
.
place
,
save_int8
=
True
)
target_vars
=
[
float_prog
.
global_block
().
var
(
outputs
[
-
1
])]
fluid
.
io
.
save_inference_model
(
dirname
=
'./inference_model/float'
,
feeded_var_names
=
[
inputs
[
0
].
name
],
target_vars
=
target_vars
,
paddle
.
static
.
save_inference_model
(
path_prefix
=
'./inference_model/float'
,
feed_vars
=
[
image
],
fetch_vars
=
target_vars
,
executor
=
exe
,
main_program
=
float_prog
)
fluid
.
io
.
save_inference_model
(
dirname
=
'./inference_model/int8'
,
feeded_var_names
=
[
inputs
[
0
].
name
],
target_vars
=
target_vars
,
program
=
float_prog
)
paddle
.
static
.
save_inference_model
(
path_prefix
=
'./inference_model/int8'
,
feed_vars
=
[
image
],
fetch_vars
=
target_vars
,
executor
=
exe
,
main_
program
=
int8_prog
)
program
=
int8_prog
)
```
docs/zh_cn/quick_start/quant_post_static_tutorial.md
浏览文件 @
4107167f
...
...
@@ -8,25 +8,42 @@
4.
静态离线量化
## 1. 导入依赖
PaddleSlim依赖Paddle
1.7
版本,请确认已正确安装Paddle,然后按以下方式导入Paddle和PaddleSlim:
PaddleSlim依赖Paddle
2.0
版本,请确认已正确安装Paddle,然后按以下方式导入Paddle和PaddleSlim:
```
python
import
paddle
import
paddle.fluid
as
fluid
import
paddleslim
as
slim
import
numpy
as
np
paddle
.
enable_static
()
```
## 2. 构建网络
该章节构造一个用于对MNIST数据进行分类的分类模型,选用
`MobileNetV1`
,并将输入大小设置为
`[1, 28, 28]`
,输出类别数为10。为了方便展示示例,我们在
`paddleslim.models`
下预定义了用于构建分类模型的方法,执行以下代码构建分类模型:
>注意:paddleslim.models下的API并非PaddleSlim常规API,是为了简化示例而封装预定义的一系列方法,比如:模型结构的定义、Program的构建等。
```
python
exe
,
train_program
,
val_program
,
inputs
,
outputs
=
\
slim
.
models
.
image_classification
(
"MobileNet"
,
[
1
,
28
,
28
],
10
,
use_gpu
=
True
)
USE_GPU
=
True
model
=
slim
.
models
.
MobileNet
()
train_program
=
paddle
.
static
.
Program
()
startup
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
train_program
,
startup
):
image
=
paddle
.
static
.
data
(
name
=
'image'
,
shape
=
[
None
,
1
,
28
,
28
],
dtype
=
'float32'
)
label
=
paddle
.
static
.
data
(
name
=
'label'
,
shape
=
[
None
,
1
],
dtype
=
'int64'
)
gt
=
paddle
.
reshape
(
label
,
[
-
1
,
1
])
out
=
model
.
net
(
input
=
image
,
class_dim
=
10
)
cost
=
paddle
.
nn
.
functional
.
loss
.
cross_entropy
(
input
=
out
,
label
=
gt
)
avg_cost
=
paddle
.
mean
(
x
=
cost
)
acc_top1
=
paddle
.
metric
.
accuracy
(
input
=
out
,
label
=
gt
,
k
=
1
)
acc_top5
=
paddle
.
metric
.
accuracy
(
input
=
out
,
label
=
gt
,
k
=
5
)
opt
=
paddle
.
optimizer
.
Momentum
(
0.01
,
0.9
)
opt
.
minimize
(
avg_cost
)
place
=
paddle
.
CUDAPlace
(
0
)
if
USE_GPU
else
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup
)
val_program
=
train_program
.
clone
(
for_test
=
True
)
```
## 3. 训练模型
...
...
@@ -34,17 +51,33 @@ exe, train_program, val_program, inputs, outputs = \
### 3.1 定义输入数据
为了快速执行该示例,我们选取简单的MNIST数据,Paddle框架的
`paddle.
dataset.mnist
`
包定义了MNIST数据的下载和读取。
为了快速执行该示例,我们选取简单的MNIST数据,Paddle框架的
`paddle.
vision.datasets
`
包定义了MNIST数据的下载和读取。
代码如下:
```
python
import
paddle.dataset.mnist
as
reader
train_reader
=
paddle
.
fluid
.
io
.
batch
(
reader
.
train
(),
batch_size
=
128
,
drop_last
=
True
)
test_reader
=
paddle
.
fluid
.
io
.
batch
(
reader
.
train
(),
batch_size
=
128
,
drop_last
=
True
)
train_feeder
=
fluid
.
DataFeeder
(
inputs
,
fluid
.
CPUPlace
())
import
paddle.vision.transforms
as
T
transform
=
T
.
Compose
([
T
.
Transpose
(),
T
.
Normalize
([
127.5
],
[
127.5
])])
train_dataset
=
paddle
.
vision
.
datasets
.
MNIST
(
mode
=
"train"
,
backend
=
"cv2"
,
transform
=
transform
)
test_dataset
=
paddle
.
vision
.
datasets
.
MNIST
(
mode
=
"test"
,
backend
=
"cv2"
,
transform
=
transform
)
train_loader
=
paddle
.
io
.
DataLoader
(
train_dataset
,
places
=
place
,
feed_list
=
[
image
,
label
],
drop_last
=
True
,
batch_size
=
64
,
return_list
=
False
,
shuffle
=
True
)
test_loader
=
paddle
.
io
.
DataLoader
(
test_dataset
,
places
=
place
,
feed_list
=
[
image
,
label
],
drop_last
=
True
,
batch_size
=
64
,
return_list
=
False
,
shuffle
=
False
)
```
### 3.2 训练和测试
...
...
@@ -53,21 +86,22 @@ train_feeder = fluid.DataFeeder(inputs, fluid.CPUPlace())
```
python
outputs
=
[
acc_top1
.
name
,
acc_top5
.
name
,
avg_cost
.
name
]
def
train
(
prog
):
iter
=
0
for
data
in
train_
re
ader
():
acc1
,
acc5
,
loss
=
exe
.
run
(
prog
,
feed
=
train_feeder
.
feed
(
data
)
,
fetch_list
=
outputs
)
for
data
in
train_
lo
ader
():
acc1
,
acc5
,
loss
=
exe
.
run
(
prog
,
feed
=
data
,
fetch_list
=
outputs
)
if
iter
%
100
==
0
:
print
(
'train
'
,
acc1
.
mean
(),
acc5
.
mean
(),
loss
.
mean
(
))
print
(
'train
iter={}, top1={}, top5={}, loss={}'
.
format
(
iter
,
acc1
.
mean
(),
acc5
.
mean
(),
loss
.
mean
()
))
iter
+=
1
def
test
(
prog
,
outputs
=
outputs
):
iter
=
0
res
=
[[],
[]]
for
data
in
t
rain_re
ader
():
acc1
,
acc5
,
loss
=
exe
.
run
(
prog
,
feed
=
train_feeder
.
feed
(
data
)
,
fetch_list
=
outputs
)
for
data
in
t
est_lo
ader
():
acc1
,
acc5
,
loss
=
exe
.
run
(
prog
,
feed
=
data
,
fetch_list
=
outputs
)
if
iter
%
100
==
0
:
print
(
'test
'
,
acc1
.
mean
(),
acc5
.
mean
(),
loss
.
mean
(
))
print
(
'test
iter={}, top1={}, top5={}, loss={}'
.
format
(
iter
,
acc1
.
mean
(),
acc5
.
mean
(),
loss
.
mean
()
))
res
[
0
].
append
(
acc1
.
mean
())
res
[
1
].
append
(
acc5
.
mean
())
iter
+=
1
...
...
@@ -94,12 +128,12 @@ test(val_program)
```
python
target_vars
=
[
val_program
.
global_block
().
var
(
name
)
for
name
in
outputs
]
fluid
.
io
.
save_inference_model
(
dirname
=
'./inference_model
'
,
feed
ed_var_names
=
[
var
.
name
for
var
in
inputs
],
target_vars
=
target_vars
,
paddle
.
static
.
save_inference_model
(
path_prefix
=
'./inference_model/fp32
'
,
feed
_vars
=
[
image
,
label
],
fetch_vars
=
[
acc_top1
,
acc_top5
,
avg_cost
]
,
executor
=
exe
,
main_
program
=
val_program
)
program
=
val_program
)
```
## 4. 静态离线量化
...
...
@@ -112,7 +146,9 @@ slim.quant.quant_post_static(
executor
=
exe
,
model_dir
=
'./inference_model'
,
quantize_model_path
=
'./quant_post_static_model'
,
sample_generator
=
reader
.
test
(),
sample_generator
=
paddle
.
dataset
.
mnist
.
test
(),
model_filename
=
'fp32.pdmodel'
,
params_filename
=
'fp32.pdiparams'
,
batch_nums
=
10
)
```
...
...
@@ -121,10 +157,8 @@ slim.quant.quant_post_static(
```
python
quant_post_static_prog
,
feed_target_names
,
fetch_targets
=
fluid
.
io
.
load_inference_model
(
dirname
=
'./quant_post_static_model'
,
model_filename
=
'__model__'
,
params_filename
=
'__params__'
,
quant_post_static_prog
,
feed_target_names
,
fetch_targets
=
paddle
.
static
.
load_inference_model
(
path_prefix
=
'./inference_model/fp32'
,
executor
=
exe
)
test
(
quant_post_static_prog
,
fetch_targets
)
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录