Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
天天代码码天天
PaddleOCR
提交
fec36dd6
P
PaddleOCR
项目概览
天天代码码天天
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
fec36dd6
编写于
7月 05, 2021
作者:
D
Double_V
提交者:
GitHub
7月 05, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'dygraph' into fix_ci
上级
902ee2c4
0ed4aee5
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
44 addition
and
16 deletion
+44
-16
deploy/slim/quantization/export_model.py
deploy/slim/quantization/export_model.py
+32
-11
deploy/slim/quantization/quant.py
deploy/slim/quantization/quant.py
+10
-3
ppocr/utils/save_load.py
ppocr/utils/save_load.py
+2
-2
未找到文件。
deploy/slim/quantization/export_model.py
浏览文件 @
fec36dd6
...
...
@@ -37,6 +37,17 @@ from paddleslim.dygraph.quant import QAT
from
ppocr.data
import
build_dataloader
def
export_single_model
(
quanter
,
model
,
infer_shape
,
save_path
,
logger
):
quanter
.
save_quantized_model
(
model
,
save_path
,
input_spec
=
[
paddle
.
static
.
InputSpec
(
shape
=
[
None
]
+
infer_shape
,
dtype
=
'float32'
)
])
logger
.
info
(
'inference QAT model is saved to {}'
.
format
(
save_path
))
def
main
():
############################################################################################################
# 1. quantization configs
...
...
@@ -76,7 +87,14 @@ def main():
# for rec algorithm
if
hasattr
(
post_process_class
,
'character'
):
char_num
=
len
(
getattr
(
post_process_class
,
'character'
))
config
[
'Architecture'
][
"Head"
][
'out_channels'
]
=
char_num
if
config
[
'Architecture'
][
"algorithm"
]
in
[
"Distillation"
,
]:
# distillation model
for
key
in
config
[
'Architecture'
][
"Models"
]:
config
[
'Architecture'
][
"Models"
][
key
][
"Head"
][
'out_channels'
]
=
char_num
else
:
# base rec model
config
[
'Architecture'
][
"Head"
][
'out_channels'
]
=
char_num
model
=
build_model
(
config
[
'Architecture'
])
# get QAT model
...
...
@@ -97,22 +115,25 @@ def main():
# start eval
metirc
=
program
.
eval
(
model
,
valid_dataloader
,
post_process_class
,
eval_class
,
model_type
,
use_srn
)
logger
.
info
(
'metric eval ***************'
)
for
k
,
v
in
met
ir
c
.
items
():
for
k
,
v
in
met
ri
c
.
items
():
logger
.
info
(
'{}:{}'
.
format
(
k
,
v
))
save_path
=
'{}/inference'
.
format
(
config
[
'Global'
][
'save_inference_dir'
])
infer_shape
=
[
3
,
32
,
100
]
if
config
[
'Architecture'
][
'model_type'
]
!=
"det"
else
[
3
,
640
,
640
]
quanter
.
save_quantized_model
(
model
,
save_path
,
input_spec
=
[
paddle
.
static
.
InputSpec
(
shape
=
[
None
]
+
infer_shape
,
dtype
=
'float32'
)
])
logger
.
info
(
'inference QAT model is saved to {}'
.
format
(
save_path
))
save_path
=
config
[
"Global"
][
"save_inference_dir"
]
arch_config
=
config
[
"Architecture"
]
if
arch_config
[
"algorithm"
]
in
[
"Distillation"
,
]:
# distillation model
for
idx
,
name
in
enumerate
(
model
.
model_name_list
):
sub_model_save_path
=
os
.
path
.
join
(
save_path
,
name
,
"inference"
)
export_single_model
(
quanter
,
model
.
model_list
[
idx
],
infer_shape
,
sub_model_save_path
,
logger
)
else
:
save_path
=
os
.
path
.
join
(
save_path
,
"inference"
)
export_single_model
(
quanter
,
model
,
infer_shape
,
save_path
,
logger
)
if
__name__
==
"__main__"
:
...
...
deploy/slim/quantization/quant.py
浏览文件 @
fec36dd6
...
...
@@ -109,9 +109,18 @@ def main(config, device, logger, vdl_writer):
# for rec algorithm
if
hasattr
(
post_process_class
,
'character'
):
char_num
=
len
(
getattr
(
post_process_class
,
'character'
))
config
[
'Architecture'
][
"Head"
][
'out_channels'
]
=
char_num
if
config
[
'Architecture'
][
"algorithm"
]
in
[
"Distillation"
,
]:
# distillation model
for
key
in
config
[
'Architecture'
][
"Models"
]:
config
[
'Architecture'
][
"Models"
][
key
][
"Head"
][
'out_channels'
]
=
char_num
else
:
# base rec model
config
[
'Architecture'
][
"Head"
][
'out_channels'
]
=
char_num
model
=
build_model
(
config
[
'Architecture'
])
quanter
=
QAT
(
config
=
quant_config
,
act_preprocess
=
PACT
)
quanter
.
quantize
(
model
)
if
config
[
'Global'
][
'distributed'
]:
model
=
paddle
.
DataParallel
(
model
)
...
...
@@ -132,8 +141,6 @@ 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
,
...
...
ppocr/utils/save_load.py
浏览文件 @
fec36dd6
...
...
@@ -91,14 +91,14 @@ def init_model(config, model, optimizer=None, lr_scheduler=None):
def
load_dygraph_params
(
config
,
model
,
logger
,
optimizer
):
ckp
=
config
[
'Global'
][
'checkpoints'
]
if
ckp
and
os
.
path
.
exists
(
ckp
):
if
ckp
and
os
.
path
.
exists
(
ckp
+
".pdparams"
):
pre_best_model_dict
=
init_model
(
config
,
model
,
optimizer
)
return
pre_best_model_dict
else
:
pm
=
config
[
'Global'
][
'pretrained_model'
]
if
pm
is
None
:
return
{}
if
not
os
.
path
.
exists
(
pm
)
or
not
os
.
path
.
exists
(
pm
+
".pdparams"
):
if
not
os
.
path
.
exists
(
pm
)
and
not
os
.
path
.
exists
(
pm
+
".pdparams"
):
logger
.
info
(
f
"The pretrained_model
{
pm
}
does not exists!"
)
return
{}
pm
=
pm
if
pm
.
endswith
(
'.pdparams'
)
else
pm
+
'.pdparams'
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录