Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
4bb194dd
X
X2Paddle
项目概览
PaddlePaddle
/
X2Paddle
大约 1 年 前同步成功
通知
328
Star
698
Fork
167
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
26
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
X
X2Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
26
Issue
26
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
4bb194dd
编写于
9月 17, 2020
作者:
S
SunAhong1993
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix the convert.py
上级
2ed1bcd8
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
43 addition
and
20 deletion
+43
-20
x2paddle/convert.py
x2paddle/convert.py
+43
-20
未找到文件。
x2paddle/convert.py
浏览文件 @
4bb194dd
...
@@ -13,7 +13,6 @@
...
@@ -13,7 +13,6 @@
# limitations under the License.
# limitations under the License.
from
six
import
text_type
as
_text_type
from
six
import
text_type
as
_text_type
from
x2paddle
import
program
import
argparse
import
argparse
import
sys
import
sys
...
@@ -67,8 +66,8 @@ def arg_parser():
...
@@ -67,8 +66,8 @@ def arg_parser():
parser
.
add_argument
(
parser
.
add_argument
(
"--without_data_format_optimization"
,
"--without_data_format_optimization"
,
"-wo"
,
"-wo"
,
action
=
"store_true"
,
type
=
_text_type
,
default
=
False
,
default
=
"True"
,
help
=
"tf model conversion without data format optimization"
)
help
=
"tf model conversion without data format optimization"
)
parser
.
add_argument
(
parser
.
add_argument
(
"--define_input_shape"
,
"--define_input_shape"
,
...
@@ -94,13 +93,11 @@ def arg_parser():
...
@@ -94,13 +93,11 @@ def arg_parser():
action
=
'append'
,
action
=
'append'
,
default
=
None
,
default
=
None
,
help
=
"define the inputs' shape"
)
help
=
"define the inputs' shape"
)
return
parser
return
parser
def
tf2paddle
(
model_path
,
def
tf2paddle
(
model_path
,
save_dir
,
save_dir
,
without_data_format_optimization
=
False
,
without_data_format_optimization
,
define_input_shape
=
False
,
define_input_shape
=
False
,
params_merge
=
False
):
params_merge
=
False
):
# check tensorflow installation and version
# check tensorflow installation and version
...
@@ -127,10 +124,29 @@ def tf2paddle(model_path,
...
@@ -127,10 +124,29 @@ def tf2paddle(model_path,
print
(
"Now translating model from tensorflow to paddle."
)
print
(
"Now translating model from tensorflow to paddle."
)
model
=
TFDecoder
(
model_path
,
define_input_shape
=
define_input_shape
)
model
=
TFDecoder
(
model_path
,
define_input_shape
=
define_input_shape
)
if
not
without_data_format_optimization
:
mapper
=
TFOpMapperNHWC
(
model
)
mapper
=
TFOpMapper
(
model
)
program
.
build
()
optimizer
=
TFOptimizer
(
mapper
)
program
.
gen_model
(
save_dir
)
# neccesary optimization
optimizer
.
delete_redundance_code
()
# optimizer below is experimental
optimizer
.
optimize_elementwise_op
()
optimizer
.
merge_activation
()
optimizer
.
merge_bias
()
optimizer
.
optimize_sub_graph
()
# optimizer.merge_batch_norm()
# optimizer.merge_prelu()
else
:
mapper
=
TFOpMapperNHWC
(
model
)
optimizer
=
TFOptimizer
(
mapper
)
optimizer
.
delete_redundance_code
()
optimizer
.
strip_graph
()
optimizer
.
merge_activation
()
optimizer
.
merge_bias
()
optimizer
.
make_nchw_input_output
()
optimizer
.
remove_transpose
()
mapper
.
save_inference_model
(
save_dir
,
params_merge
)
def
caffe2paddle
(
proto
,
weight
,
save_dir
,
caffe_proto
,
params_merge
=
False
):
def
caffe2paddle
(
proto
,
weight
,
save_dir
,
caffe_proto
,
params_merge
=
False
):
...
@@ -158,8 +174,8 @@ def onnx2paddle(model_path, save_dir, params_merge=False):
...
@@ -158,8 +174,8 @@ def onnx2paddle(model_path, save_dir, params_merge=False):
try
:
try
:
import
onnx
import
onnx
version
=
onnx
.
version
.
version
version
=
onnx
.
version
.
version
if
version
!=
'1.6.0'
:
if
version
<
'1.6.0'
:
print
(
"[ERROR] onnx
=
=1.6.0 is required"
)
print
(
"[ERROR] onnx
>
=1.6.0 is required"
)
return
return
except
:
except
:
print
(
"[ERROR] onnx is not installed, use
\"
pip install onnx==1.6.0
\"
."
)
print
(
"[ERROR] onnx is not installed, use
\"
pip install onnx==1.6.0
\"
."
)
...
@@ -178,8 +194,8 @@ def onnx2paddle(model_path, save_dir, params_merge=False):
...
@@ -178,8 +194,8 @@ def onnx2paddle(model_path, save_dir, params_merge=False):
print
(
"Paddle model and code generating ..."
)
print
(
"Paddle model and code generating ..."
)
mapper
.
save_inference_model
(
save_dir
,
params_merge
)
mapper
.
save_inference_model
(
save_dir
,
params_merge
)
print
(
"Paddle model and code generated."
)
print
(
"Paddle model and code generated."
)
def
pytorch2paddle
(
model_path
,
save_dir
,
input_shapes
):
def
pytorch2paddle
(
model_path
,
save_dir
,
input_shapes
):
# check pytorch installation and version
# check pytorch installation and version
try
:
try
:
...
@@ -222,9 +238,14 @@ def pytorch2paddle(model_path, save_dir, input_shapes):
...
@@ -222,9 +238,14 @@ def pytorch2paddle(model_path, save_dir, input_shapes):
def
paddle2onnx
(
model_path
,
save_dir
,
opset_version
=
10
):
def
paddle2onnx
(
model_path
,
save_dir
,
opset_version
=
10
):
from
x2paddle.decoder.paddle_decoder
import
PaddleDecoder
from
x2paddle.decoder.paddle_decoder
import
PaddleDecoder
from
x2paddle.op_mapper.paddle2onnx.paddle_op_mapper
import
PaddleOpMapper
from
x2paddle.op_mapper.paddle2onnx.paddle_op_mapper
import
PaddleOpMapper
import
paddle.fluid
as
fluid
model
=
PaddleDecoder
(
model_path
,
'__model__'
,
'__params__'
)
model
=
PaddleDecoder
(
model_path
,
'__model__'
,
'__params__'
)
mapper
=
PaddleOpMapper
()
mapper
=
PaddleOpMapper
()
mapper
.
convert
(
model
.
program
,
save_dir
,
opset_number
=
opset_version
)
mapper
.
convert
(
model
.
program
,
save_dir
,
scope
=
fluid
.
global_scope
(),
opset_version
=
opset_version
)
def
main
():
def
main
():
...
@@ -262,11 +283,12 @@ def main():
...
@@ -262,11 +283,12 @@ def main():
if
args
.
framework
==
"tensorflow"
:
if
args
.
framework
==
"tensorflow"
:
assert
args
.
model
is
not
None
,
"--model should be defined while translating tensorflow model"
assert
args
.
model
is
not
None
,
"--model should be defined while translating tensorflow model"
without_data_format_optimization
=
False
assert
args
.
without_data_format_optimization
in
[
"True"
,
"False"
],
"--the param without_data_format_optimization should be defined True or False"
define_input_shape
=
False
define_input_shape
=
False
params_merge
=
False
params_merge
=
False
if
args
.
without_data_format_optimization
:
without_data_format_optimization
=
True
if
args
.
without_data_format_optimization
==
"True"
else
False
without_data_format_optimization
=
True
if
args
.
define_input_shape
:
if
args
.
define_input_shape
:
define_input_shape
=
True
define_input_shape
=
True
if
args
.
params_merge
:
if
args
.
params_merge
:
...
@@ -288,13 +310,14 @@ def main():
...
@@ -288,13 +310,14 @@ def main():
if
args
.
params_merge
:
if
args
.
params_merge
:
params_merge
=
True
params_merge
=
True
onnx2paddle
(
args
.
model
,
args
.
save_dir
,
params_merge
)
onnx2paddle
(
args
.
model
,
args
.
save_dir
,
params_merge
)
elif
args
.
framework
==
"pytorch"
:
elif
args
.
framework
==
"pytorch"
:
assert
args
.
model
is
not
None
,
"--model should be defined while translating pytorch model"
assert
args
.
model
is
not
None
,
"--model should be defined while translating pytorch model"
pytorch2paddle
(
args
.
model
,
args
.
save_dir
,
args
.
input_shapes
)
pytorch2paddle
(
args
.
model
,
args
.
save_dir
,
args
.
input_shapes
)
elif
args
.
framework
==
"paddle2onnx"
:
elif
args
.
framework
==
"paddle2onnx"
:
assert
args
.
model
is
not
None
,
"--model should be defined while translating paddle model to onnx"
assert
args
.
model
is
not
None
,
"--model should be defined while translating paddle model to onnx"
paddle2onnx
(
args
.
model
,
args
.
save_dir
,
args
.
onnx_opset
)
paddle2onnx
(
args
.
model
,
args
.
save_dir
,
opset_version
=
args
.
onnx_opset
)
else
:
else
:
raise
Exception
(
raise
Exception
(
...
@@ -302,4 +325,4 @@ def main():
...
@@ -302,4 +325,4 @@ def main():
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
main
()
main
()
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录