Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_41840029
PaddleOCR
提交
fb8e883f
P
PaddleOCR
项目概览
weixin_41840029
/
PaddleOCR
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleOCR
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleOCR
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
fb8e883f
编写于
5月 12, 2021
作者:
L
LDOUBLEV
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine deploy slim
上级
c64e235a
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
37 addition
and
43 deletion
+37
-43
deploy/slim/prune/sensitivity_anal.py
deploy/slim/prune/sensitivity_anal.py
+34
-20
deploy/slim/quantization/quant.py
deploy/slim/quantization/quant.py
+3
-23
未找到文件。
deploy/slim/prune/sensitivity_anal.py
浏览文件 @
fb8e883f
...
...
@@ -24,6 +24,14 @@ sys.path.append(__dir__)
sys
.
path
.
append
(
os
.
path
.
join
(
__dir__
,
'..'
,
'..'
,
'..'
))
sys
.
path
.
append
(
os
.
path
.
join
(
__dir__
,
'..'
,
'..'
,
'..'
,
'tools'
))
import
json
import
cv2
import
paddle
from
paddle
import
fluid
import
paddleslim
as
slim
from
copy
import
deepcopy
from
tools
import
program
import
paddle
import
paddle.distributed
as
dist
from
ppocr.data
import
build_dataloader
...
...
@@ -38,13 +46,27 @@ import tools.program as program
dist
.
get_world_size
()
def
get_pruned_params
(
parameters
):
def
get_pruned_params
(
parameters
,
mode
=
"det"
):
if
mode
==
"det"
:
skip_prune_params
=
[
"conv2d_56.w_0"
,
"conv2d_54.w_0"
,
"conv2d_51.w_0"
,
"conv_last_weights"
,
"conv14_linear_weights"
,
"conv13_expand_weights"
,
"conv12_linear_weights"
,
"conv12_expand_weights"
,
"conv7_expand_weights"
,
"conv8_expand_weights"
,
"conv8_linear_weights"
,
"conv5_linear_weights"
,
"conv5_expand_weights"
,
"conv3_linear_weights"
]
skip_prune_params
=
skip_prune_params
+
[
'conv2d_53.w_0'
]
else
:
skip_prune_params
=
None
params
=
[]
for
param
in
parameters
:
if
len
(
param
.
shape
)
==
4
and
'depthwise'
not
in
param
.
name
and
'transpose'
not
in
param
.
name
and
"conv2d_57"
not
in
param
.
name
and
"conv2d_56"
not
in
param
.
name
:
if
param
.
name
not
in
skip_prune_params
:
params
.
append
(
param
.
name
)
return
params
...
...
@@ -75,7 +97,7 @@ def main(config, device, logger, vdl_writer):
model
=
build_model
(
config
[
'Architecture'
])
flops
=
paddle
.
flops
(
model
,
[
1
,
3
,
640
,
640
])
logger
.
info
(
f
"FLOPs before pruning:
{
flops
}
"
)
print
(
f
"FLOPs before pruning:
{
flops
}
"
)
from
paddleslim.dygraph
import
FPGMFilterPruner
model
.
train
()
...
...
@@ -96,11 +118,6 @@ def main(config, device, logger, vdl_writer):
# load pretrain model
pre_best_model_dict
=
init_model
(
config
,
model
,
logger
,
optimizer
)
logger
.
info
(
'train dataloader has {} iters, valid dataloader has {} iters'
.
format
(
len
(
train_dataloader
),
len
(
valid_dataloader
)))
# build metric
eval_class
=
build_metric
(
config
[
'Metric'
])
logger
.
info
(
'train dataloader has {} iters, valid dataloader has {} iters'
.
format
(
len
(
train_dataloader
),
len
(
valid_dataloader
)))
...
...
@@ -110,32 +127,29 @@ def main(config, device, logger, vdl_writer):
logger
.
info
(
f
"metric['hmean']:
{
metric
[
'hmean'
]
}
"
)
return
metric
[
'hmean'
]
p
arams_sensitive
=
p
runer
.
sensitive
(
pruner
.
sensitive
(
eval_func
=
eval_fn
,
sen_file
=
"./sen.pickle"
,
skip_vars
=
[
"conv2d_57.w_0"
,
"conv2d_transpose_2.w_0"
,
"conv2d_transpose_3.w_0"
])
logger
.
info
(
"The sensitivity analysis results of model parameters saved in sen.pickle"
)
# calculate pruned params's ratio
params_sensitive
=
pruner
.
_get_ratios_by_loss
(
params_sensitive
,
loss
=
0.02
)
for
key
in
params_sensitive
.
keys
():
logger
.
info
(
f
"
{
key
}
,
{
params_sensitive
[
key
]
}
"
)
params
=
get_pruned_params
(
model
.
parameters
())
ratios
=
{}
# set the prune ratio is 0.2
for
param
in
params
:
ratios
[
param
]
=
0.2
plan
=
pruner
.
prune_vars
(
params_sensitive
,
[
0
])
plan
=
pruner
.
prune_vars
(
ratios
,
[
0
])
for
param
in
model
.
parameters
():
if
(
"weights"
in
param
.
name
and
"conv"
in
param
.
name
)
or
(
"w_0"
in
param
.
name
and
"conv2d"
in
param
.
name
):
logger
.
info
(
f
"
{
param
.
name
}
:
{
param
.
shape
}
"
)
print
(
f
"
{
param
.
name
}
:
{
param
.
shape
}
"
)
flops
=
paddle
.
flops
(
model
,
[
1
,
3
,
640
,
640
])
logger
.
info
(
f
"FLOPs after pruning:
{
flops
}
"
)
print
(
f
"FLOPs after pruning:
{
flops
}
"
)
# start train
program
.
train
(
config
,
train_dataloader
,
valid_dataloader
,
device
,
model
,
loss_class
,
optimizer
,
lr_scheduler
,
post_process_class
,
eval_class
,
pre_best_model_dict
,
logger
,
vdl_writer
)
...
...
deploy/slim/quantization/quant.py
浏览文件 @
fb8e883f
...
...
@@ -112,10 +112,6 @@ def main(config, device, logger, vdl_writer):
config
[
'Architecture'
][
"Head"
][
'out_channels'
]
=
char_num
model
=
build_model
(
config
[
'Architecture'
])
# prepare to quant
quanter
=
QAT
(
config
=
quant_config
,
act_preprocess
=
PACT
)
quanter
.
quantize
(
model
)
if
config
[
'Global'
][
'distributed'
]:
model
=
paddle
.
DataParallel
(
model
)
...
...
@@ -136,31 +132,15 @@ def main(config, device, logger, vdl_writer):
logger
.
info
(
'train dataloader has {} iters, valid dataloader has {} iters'
.
format
(
len
(
train_dataloader
),
len
(
valid_dataloader
)))
quanter
=
QAT
(
config
=
quant_config
,
act_preprocess
=
PACT
)
quanter
.
quantize
(
model
)
# start train
program
.
train
(
config
,
train_dataloader
,
valid_dataloader
,
device
,
model
,
loss_class
,
optimizer
,
lr_scheduler
,
post_process_class
,
eval_class
,
pre_best_model_dict
,
logger
,
vdl_writer
)
def
test_reader
(
config
,
device
,
logger
):
loader
=
build_dataloader
(
config
,
'Train'
,
device
,
logger
)
import
time
starttime
=
time
.
time
()
count
=
0
try
:
for
data
in
loader
():
count
+=
1
if
count
%
1
==
0
:
batch_time
=
time
.
time
()
-
starttime
starttime
=
time
.
time
()
logger
.
info
(
"reader: {}, {}, {}"
.
format
(
count
,
len
(
data
[
0
]),
batch_time
))
except
Exception
as
e
:
logger
.
info
(
e
)
logger
.
info
(
"finish reader: {}, Success!"
.
format
(
count
))
if
__name__
==
'__main__'
:
config
,
device
,
logger
,
vdl_writer
=
program
.
preprocess
(
is_train
=
True
)
main
(
config
,
device
,
logger
,
vdl_writer
)
# test_reader(config, device, logger)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录