Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
b3c81580
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
大约 1 年 前同步成功
通知
282
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
b3c81580
编写于
4月 07, 2021
作者:
W
wuzewu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add save_inference_model api.
上级
fe10b94f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
134 addition
and
24 deletion
+134
-24
paddlehub/compat/module/module_v1.py
paddlehub/compat/module/module_v1.py
+15
-2
paddlehub/module/module.py
paddlehub/module/module.py
+119
-22
未找到文件。
paddlehub/compat/module/module_v1.py
浏览文件 @
b3c81580
...
...
@@ -260,13 +260,24 @@ class ModuleV1(object):
dirname
:
str
,
model_filename
:
str
=
None
,
params_filename
:
str
=
None
,
combined
:
bool
=
False
):
combined
:
bool
=
False
,
**
kwargs
):
'''
Export the model to Paddle Inference format.
Args:
dirname(str): The directory to save the paddle inference model.
model_filename(str): The name of the saved model file. Default to `__model__`.
params_filename(str): The name of the saved parameters file, only takes effect when `combined` is True.
Default to `__params__`.
combined(bool): Whether to save all parameters in a combined file. Default to True.
'''
if
hasattr
(
self
,
'processor'
):
if
hasattr
(
self
.
processor
,
'save_inference_model'
):
return
self
.
processor
.
save_inference_model
(
dirname
,
model_filename
,
params_filename
,
combined
)
model_filename
=
'__model__'
if
not
model_filename
else
model_filename
if
combined
:
model_filename
=
'__model__'
if
not
model_filename
else
model_filename
params_filename
=
'__params__'
if
not
params_filename
else
params_filename
place
=
paddle
.
CPUPlace
()
...
...
@@ -282,6 +293,8 @@ class ModuleV1(object):
model_filename
=
model_filename
,
params_filename
=
params_filename
)
log
.
logger
.
info
(
'Paddle Inference model saved in {}.'
.
format
(
dirname
))
@
paddle_utils
.
run_in_static_mode
def
export_onnx_model
(
self
,
dirname
:
str
,
**
kwargs
):
'''
...
...
paddlehub/module/module.py
浏览文件 @
b3c81580
...
...
@@ -169,10 +169,108 @@ class RunModule(object):
return
_sub_modules
def
save_inference_model
(
self
,
dirname
:
str
,
model_filename
:
str
=
None
,
params_filename
:
str
=
None
,
input_spec
:
List
[
paddle
.
static
.
InputSpec
]
=
None
,
include_sub_modules
:
bool
=
True
,
combined
:
bool
=
True
):
'''
Export the model to Paddle Inference format.
Args:
dirname(str): The directory to save the paddle inference model.
model_filename(str): The name of the saved model file. Default to `__model__`.
params_filename(str): The name of the saved parameters file, only takes effect when `combined` is True.
Default to `__params__`.
input_spec(list): Describes the input of the saved model's forward method, which can be described by
InputSpec or example Tensor. If None, all input variables of the original Layer's forward method
would be the inputs of the saved model. Default None.
include_sub_modules(bool): Whether to export sub modules. Default to True.
combined(bool): Whether to save all parameters in a combined file. Default to True.
'''
if
include_sub_modules
:
for
key
,
_sub_module
in
self
.
sub_modules
().
items
():
try
:
sub_dirname
=
os
.
path
.
normpath
(
os
.
path
.
join
(
dirname
,
key
))
_sub_module
.
save_inference_model
(
sub_dirname
,
include_sub_modules
=
include_sub_modules
,
model_filename
=
model_filename
,
params_filename
=
params_filename
,
combined
=
combined
)
except
:
utils
.
record_exception
(
'Failed to save sub module {}'
.
format
(
_sub_module
.
name
))
if
isinstance
(
self
,
paddle
.
nn
.
Layer
):
save_file
=
os
.
path
.
join
(
dirname
,
'{}'
.
format
(
self
.
name
))
if
not
input_spec
:
if
hasattr
(
self
,
'input_spec'
):
input_spec
=
self
.
input_spec
else
:
_type
=
self
.
type
.
lower
()
if
_type
.
startswith
(
'cv/image'
):
input_spec
=
[
paddle
.
static
.
InputSpec
(
shape
=
[
None
,
3
,
None
,
None
],
dtype
=
'float32'
)]
else
:
raise
RuntimeError
(
'Module {} lacks `input_spec`, please specify it when calling `save_inference_model`.'
.
format
(
self
.
name
))
net
=
paddle
.
jit
.
to_static
(
self
,
input_spec
)
paddle
.
jit
.
save
(
net
,
save_file
)
log
.
logger
.
info
(
'Paddle Inference model saved in {}.'
.
format
(
dirname
))
return
if
not
self
.
_pretrained_model_path
:
raise
RuntimeError
(
'Module {} does not support exporting models in Paddle Inference format.'
.
format
(
self
.
name
))
elif
not
os
.
path
.
exists
(
self
.
_pretrained_model_path
):
log
.
logger
.
warning
(
'The model path of Module {} does not exist.'
.
format
(
self
.
name
))
return
model_filename
=
'__model__'
if
not
model_filename
else
model_filename
if
combined
:
params_filename
=
'__params__'
if
not
params_filename
else
params_filename
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
_model_filename
=
None
_params_filename
=
None
if
os
.
path
.
exists
(
os
.
path
.
join
(
self
.
_pretrained_model_path
,
'model'
)):
_model_filename
=
'model'
if
os
.
path
.
exists
(
os
.
path
.
join
(
self
.
_pretrained_model_path
,
'params'
)):
_params_filename
=
'params'
if
os
.
path
.
exists
(
os
.
path
.
join
(
self
.
_pretrained_model_path
,
'__params__'
)):
_params_filename
=
'__params__'
program
,
feeded_var_names
,
target_vars
=
paddle
.
fluid
.
io
.
load_inference_model
(
dirname
=
self
.
_pretrained_model_path
,
executor
=
exe
,
model_filename
=
_model_filename
,
params_filename
=
_params_filename
,
)
paddle
.
fluid
.
io
.
save_inference_model
(
dirname
=
dirname
,
main_program
=
program
,
executor
=
exe
,
feeded_var_names
=
feeded_var_names
,
target_vars
=
target_vars
,
model_filename
=
model_filename
,
params_filename
=
params_filename
)
log
.
logger
.
info
(
'Paddle Inference model saved in {}.'
.
format
(
dirname
))
def
export_onnx_model
(
self
,
dirname
:
str
,
input_spec
:
List
[
paddle
.
static
.
InputSpec
]
=
None
,
export
_sub_modules
:
bool
=
True
,
include
_sub_modules
:
bool
=
True
,
**
kwargs
):
'''
Export the model to ONNX format.
...
...
@@ -182,41 +280,40 @@ class RunModule(object):
input_spec(list): Describes the input of the saved model's forward method, which can be described by
InputSpec or example Tensor. If None, all input variables of the original Layer's forward method
would be the inputs of the saved model. Default None.
export
_sub_modules(bool): Whether to export sub modules. Default to True.
include
_sub_modules(bool): Whether to export sub modules. Default to True.
**kwargs(dict|optional): Other export configuration options for compatibility, some may be removed in
the future. Don't use them If not necessary. Refer to https://github.com/PaddlePaddle/paddle2onnx
for more information.
'''
if
export
_sub_modules
:
if
include
_sub_modules
:
for
key
,
_sub_module
in
self
.
sub_modules
().
items
():
try
:
sub_dirname
=
os
.
path
.
normpath
(
os
.
path
.
join
(
dirname
,
key
))
_sub_module
.
export_onnx_model
(
sub_dirname
,
export_sub_modules
=
export
_sub_modules
,
**
kwargs
)
_sub_module
.
export_onnx_model
(
sub_dirname
,
include_sub_modules
=
include
_sub_modules
,
**
kwargs
)
except
:
utils
.
record_exception
(
'Failed to export sub module {}'
.
format
(
_sub_module
.
name
))
if
not
self
.
_pretrained_model_path
:
if
isinstance
(
self
,
paddle
.
nn
.
Layer
):
save_file
=
os
.
path
.
join
(
dirname
,
'{}'
.
format
(
self
.
name
))
if
not
input_spec
:
if
hasattr
(
self
,
'input_spec'
):
input_spec
=
self
.
input_spec
if
isinstance
(
self
,
paddle
.
nn
.
Layer
):
save_file
=
os
.
path
.
join
(
dirname
,
'{}'
.
format
(
self
.
name
))
if
not
input_spec
:
if
hasattr
(
self
,
'input_spec'
):
input_spec
=
self
.
input_spec
else
:
_type
=
self
.
type
.
lower
()
if
_type
.
startswith
(
'cv/image'
):
input_spec
=
[
paddle
.
static
.
InputSpec
(
shape
=
[
None
,
3
,
None
,
None
],
dtype
=
'float32'
)]
else
:
_type
=
self
.
type
.
lower
()
if
_type
.
startswith
(
'cv/image'
):
input_spec
=
[
paddle
.
static
.
InputSpec
(
shape
=
[
None
,
3
,
None
,
None
],
dtype
=
'float32'
)]
else
:
raise
RuntimeError
(
'Module {} lacks `input_spec`, please specify it when calling `export_onnx_model`.'
.
format
(
self
.
name
))
raise
RuntimeError
(
'Module {} lacks `input_spec`, please specify it when calling `export_onnx_model`.'
.
format
(
self
.
name
))
paddle
.
onnx
.
export
(
self
,
save_file
,
input_spec
=
input_spec
,
**
kwargs
)
return
paddle
.
onnx
.
export
(
self
,
save_file
,
input_spec
=
input_spec
,
**
kwargs
)
return
if
not
self
.
_pretrained_model_path
:
raise
RuntimeError
(
'Module {} does not support exporting models in ONNX format.'
.
format
(
self
.
name
))
if
not
os
.
path
.
exists
(
self
.
_pretrained_model_path
):
log
.
logger
.
warning
(
'The model path of Module {} does not exist'
.
format
(
self
.
name
))
elif
not
os
.
path
.
exists
(
self
.
_pretrained_model_path
):
log
.
logger
.
warning
(
'The model path of Module {} does not exist.'
.
format
(
self
.
name
))
return
place
=
paddle
.
CPUPlace
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录