From a7d14023c1dad24516275e127c6195ad76069d18 Mon Sep 17 00:00:00 2001 From: SunAhong1993 <48579383+SunAhong1993@users.noreply.github.com> Date: Fri, 23 Apr 2021 23:23:56 +0800 Subject: [PATCH] Fix readme and converter.py (#551) --- README.md | 13 +++++++------ x2paddle/convert.py | 42 ++++++++++++++++++------------------------ 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 8e37546..210f276 100644 --- a/README.md +++ b/README.md @@ -21,14 +21,14 @@ X2Paddle的架构设计着重考虑了对多深度学习框架的的支持以及 ## 环境依赖 -python >= 3.5 -paddlepaddle 2.0.0-rc1 或者 develop +> python >= 3.5 +> paddlepaddle 2.0.0+ **按需安装以下依赖** -tensorflow : tensorflow == 1.14.0 -caffe : 无 -onnx : onnx >= 1.6.0 -pytorch:torch >=1.5.0 (script方式暂不支持1.7.0) +> tensorflow : tensorflow == 1.14.0 +> caffe : 无 +> onnx : onnx >= 1.6.0 +> pytorch:torch >=1.5.0 (script方式暂不支持1.7.0) ## 安装 ### 方式一:源码安装(推荐) @@ -90,6 +90,7 @@ X2Paddle提供了工具解决如下问题,详见[tools/README.md](tools/README 3. [PyTorch模型导出为ONNX模型](./docs/user_guides/pytorch2onnx.md) 4. [X2Paddle添加内置的Caffe自定义层](./docs/user_guides/add_caffe_custom_layer.md) 5. [转换后PaddlePaddle预测模型简介](./docs/user_guides/pd_folder_introduction.py) +6. [Paddle到ONNX的转换](https://github.com/PaddlePaddle/Paddle2ONNX) ## 支持列表文档 1. [X2Paddle测试模型库](./docs/introduction/x2paddle_model_zoo.md) diff --git a/x2paddle/convert.py b/x2paddle/convert.py index 9e2567a..613ba65 100644 --- a/x2paddle/convert.py +++ b/x2paddle/convert.py @@ -41,16 +41,15 @@ def arg_parser(): parser.add_argument( "--save_dir", "-s", + required=True, type=_text_type, default=None, help="path to save translated model") parser.add_argument( "--framework", "-f", - type=_text_type, - default=None, - help="define which deeplearning framework(tensorflow/caffe/onnx/paddle2onnx)" - ) + choices=['tensorflow', 'caffe', 'onnx'], + help="define which deeplearning framework(tensorflow/caffe/onnx)") parser.add_argument( "--caffe_proto", "-c", @@ -73,11 +72,10 @@ def arg_parser(): parser.add_argument( "--paddle_type", "-pt", - type=_text_type, + choices=['dygraph', 'static'], default="dygraph", - help="define the paddle model type after converting(dygraph/static)" - ) - + help="define the paddle model type after converting(dygraph/static)") + return parser @@ -101,14 +99,13 @@ def tf2paddle(model_path, "[ERROR] Tensorflow is not installed, use \"pip install tensorflow\"." ) return - + from x2paddle.decoder.tf_decoder import TFDecoder if paddle_type == "dygraph": from x2paddle.op_mapper.dygraph.tf2paddle.tf_op_mapper import TFOpMapper else: from x2paddle.op_mapper.static.tf2paddle.tf_op_mapper import TFOpMapper - - + print("Now translating model from tensorflow to paddle.") model = TFDecoder(model_path, define_input_shape=define_input_shape) mapper = TFOpMapper(model) @@ -122,7 +119,6 @@ def tf2paddle(model_path, graph_opt = GraphOptimizer(source_frame="tf", paddle_type=paddle_type) graph_opt.optimize(mapper.paddle_graph) mapper.paddle_graph.gen_model(save_dir) - def caffe2paddle(proto, weight, save_dir, caffe_proto, paddle_type): @@ -190,7 +186,7 @@ def pytorch2paddle(module, save_dir, jit_type="trace", input_examples=None): ) return print("Now translating model from pytorch to paddle.") - + from x2paddle.decoder.pytorch_decoder import ScriptDecoder, TraceDecoder from x2paddle.op_mapper.dygraph.pytorch2paddle.pytorch_op_mapper import PyTorchOpMapper @@ -202,7 +198,8 @@ def pytorch2paddle(module, save_dir, jit_type="trace", input_examples=None): mapper.paddle_graph.build() print("Model optimizing ...") from x2paddle.optimizer.optimizer import GraphOptimizer - graph_opt = GraphOptimizer(source_frame="pytorch", paddle_type="dygraph", jit_type=jit_type) + graph_opt = GraphOptimizer( + source_frame="pytorch", paddle_type="dygraph", jit_type=jit_type) graph_opt.optimize(mapper.paddle_graph) print("Model optimized.") mapper.paddle_graph.gen_model(save_dir, jit_type=jit_type) @@ -224,14 +221,10 @@ def main(): x2paddle.__version__)) return - assert args.framework is not None, "--framework is not defined(support tensorflow/caffe/onnx)" - assert args.save_dir is not None, "--save_dir is not defined" - assert args.paddle_type in ["dygraph", "static"], "--paddle_type must be 'dygraph' or 'static'" - try: import platform v0, v1, v2 = platform.python_version().split('.') - if not(int(v0) >= 3 and int(v1) >= 5): + if not (int(v0) >= 3 and int(v1) >= 5): print("[ERROR] python>=3.5 is required") return import paddle @@ -252,8 +245,8 @@ def main(): define_input_shape = False if args.define_input_shape: define_input_shape = True - tf2paddle(args.model, args.save_dir, - define_input_shape, args.paddle_type) + tf2paddle(args.model, args.save_dir, define_input_shape, + args.paddle_type) elif args.framework == "caffe": assert args.prototxt is not None and args.weight is not None, "--prototxt and --weight should be defined while translating caffe model" @@ -263,11 +256,12 @@ def main(): assert args.model is not None, "--model should be defined while translating onnx model" onnx2paddle(args.model, args.save_dir, args.paddle_type) elif args.framework == "paddle2onnx": - print("Paddle to ONNX tool has been migrated to the new github: https://github.com/PaddlePaddle/paddle2onnx") + print( + "Paddle to ONNX tool has been migrated to the new github: https://github.com/PaddlePaddle/paddle2onnx" + ) else: - raise Exception( - "--framework only support tensorflow/caffe/onnx now") + raise Exception("--framework only support tensorflow/caffe/onnx now") if __name__ == "__main__": -- GitLab