Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
eafcc864
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
1 年多 前同步成功
通知
115
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
eafcc864
编写于
7月 26, 2021
作者:
D
dongshuilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add slim support
上级
4452565b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
83 addition
and
20 deletion
+83
-20
tools/slim/config/ResNet50_vd.yaml
tools/slim/config/ResNet50_vd.yaml
+7
-7
tools/slim/train.py
tools/slim/train.py
+76
-13
未找到文件。
tools/slim/config/ResNet50_vd.yaml
浏览文件 @
eafcc864
...
...
@@ -18,12 +18,12 @@ Global:
# for paddleslim
Slim
:
# for quantalization
quant
:
name
:
pact
#
quant:
#
name: pact
## for prune
#
prune:
#
name: fpgm
# prune
_ratio: 0.3
prune
:
name
:
fpgm
pruned
_ratio
:
0.3
# model architecture
Arch
:
...
...
@@ -58,7 +58,7 @@ DataLoader:
dataset
:
name
:
ImageNetDataset
image_root
:
./dataset/ILSVRC2012/
cls_label_path
:
./dataset/ILSVRC2012/train
_list
.txt
cls_label_path
:
./dataset/ILSVRC2012/train.txt
transform_ops
:
-
DecodeImage
:
to_rgb
:
True
...
...
@@ -89,7 +89,7 @@ DataLoader:
dataset
:
name
:
ImageNetDataset
image_root
:
./dataset/ILSVRC2012/
cls_label_path
:
./dataset/ILSVRC2012/val
_list
.txt
cls_label_path
:
./dataset/ILSVRC2012/val.txt
transform_ops
:
-
DecodeImage
:
to_rgb
:
True
...
...
tools/slim/train.py
浏览文件 @
eafcc864
...
...
@@ -18,10 +18,14 @@ import os
import
sys
import
paddle
import
paddleslim
from
paddle.jit
import
to_static
from
paddleslim.analysis
import
dygraph_flops
as
flops
__dir__
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
__dir__
,
'../../'
)))
from
paddleslim.dygraph.quant
import
QAT
from
ppcls.engine.trainer
import
Trainer
from
ppcls.utils
import
config
,
logger
from
ppcls.utils.save_load
import
load_dygraph_pretrain
...
...
@@ -53,9 +57,12 @@ quant_config = {
class
Trainer_slim
(
Trainer
):
def
__init__
(
self
,
config
,
mode
=
"train"
):
super
().
__init__
(
config
,
mode
)
# self.pact = self.config["Slim"].get("pact", False)
self
.
pact
=
True
if
self
.
pact
:
pact
=
self
.
config
[
"Slim"
].
get
(
"quant"
,
False
)
self
.
pact
=
pact
.
get
(
"name"
,
False
)
if
pact
else
pact
if
self
.
pact
and
str
(
self
.
pact
.
lower
())
!=
'pact'
:
raise
RuntimeError
(
"The quantization only support 'PACT'!"
)
if
pact
:
quant_config
[
"activation_preprocess_type"
]
=
"PACT"
self
.
quanter
=
QAT
(
config
=
quant_config
)
self
.
quanter
.
quantize
(
self
.
model
)
...
...
@@ -64,6 +71,31 @@ class Trainer_slim(Trainer):
else
:
self
.
quanter
=
None
prune_config
=
self
.
config
[
"Slim"
].
get
(
"prune"
,
False
)
if
prune_config
:
if
prune_config
[
"name"
].
lower
()
not
in
[
"fpgm"
,
"l1_norm"
]:
raise
RuntimeError
(
"The prune methods only support 'fpgm' and 'l1_norm'"
)
else
:
logger
.
info
(
"FLOPs before pruning: {}GFLOPs"
.
format
(
flops
(
self
.
model
,
[
1
]
+
self
.
config
[
"Global"
][
"image_shape"
])
/
1000000
))
self
.
model
.
eval
()
if
prune_config
[
"name"
].
lower
()
==
"fpgm"
:
self
.
model
.
eval
()
self
.
pruner
=
paddleslim
.
dygraph
.
FPGMFilterPruner
(
self
.
model
,
[
1
]
+
self
.
config
[
"Global"
][
"image_shape"
])
else
:
self
.
pruner
=
paddleslim
.
dygraph
.
L1NormFilterPruner
(
self
.
model
,
[
1
]
+
self
.
config
[
"Global"
][
"image_shape"
])
self
.
prune_model
()
else
:
self
.
pruner
=
None
if
self
.
quanter
is
None
and
self
.
pruner
is
None
:
logger
.
info
(
"Training without slim"
)
def
train
(
self
):
super
().
train
()
if
self
.
config
[
"Global"
].
get
(
"save_inference_dir"
,
None
):
...
...
@@ -86,17 +118,48 @@ class Trainer_slim(Trainer):
raise
RuntimeError
(
"The best_model or pretraine_model should exist to generate inference model"
)
save_path
=
os
.
path
.
join
(
self
.
config
[
"Global"
][
"save_inference_dir"
],
"inference"
)
if
self
.
quanter
:
self
.
quanter
.
save_quantized_model
(
self
.
model
,
save_path
,
input_spec
=
[
paddle
.
static
.
InputSpec
(
shape
=
[
None
]
+
config
[
"Global"
][
"image_shape"
],
dtype
=
'float32'
)
])
else
:
model
=
to_static
(
self
.
model
,
input_spec
=
[
paddle
.
static
.
InputSpec
(
shape
=
[
None
]
+
self
.
config
[
"Global"
][
"image_shape"
],
dtype
=
'float32'
,
name
=
"image"
)
])
paddle
.
jit
.
save
(
model
,
save_path
)
def
prune_model
(
self
):
params
=
[]
for
sublayer
in
self
.
model
.
sublayers
():
for
param
in
sublayer
.
parameters
(
include_sublayers
=
False
):
if
isinstance
(
sublayer
,
paddle
.
nn
.
Conv2D
):
params
.
append
(
param
.
name
)
ratios
=
{}
for
param
in
params
:
ratios
[
param
]
=
self
.
config
[
"Slim"
][
"prune"
][
"pruned_ratio"
]
plan
=
self
.
pruner
.
prune_vars
(
ratios
,
[
0
])
logger
.
info
(
"FLOPs after pruning: {}GFLOPs; pruned ratio: {}"
.
format
(
flops
(
self
.
model
,
[
1
]
+
self
.
config
[
"Global"
][
"image_shape"
])
/
1000000
,
plan
.
pruned_flops
))
for
param
in
self
.
model
.
parameters
():
if
"conv2d"
in
param
.
name
:
logger
.
info
(
"{}
\t
{}"
.
format
(
param
.
name
,
param
.
shape
))
assert
self
.
quanter
self
.
quanter
.
save_quantized_model
(
self
.
model
,
os
.
path
.
join
(
self
.
config
[
"Global"
][
"save_inference_dir"
],
"inference"
),
input_spec
=
[
paddle
.
static
.
InputSpec
(
shape
=
[
None
]
+
config
[
"Global"
][
"image_shape"
],
dtype
=
'float32'
)
])
self
.
model
.
train
()
if
__name__
==
"__main__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录