Coder Social home page Coder Social logo

paddlepaddle / paddle2onnx Goto Github PK

View Code? Open in Web Editor NEW
655.0 74.0 151.0 4.45 MB

ONNX Model Exporter for PaddlePaddle

License: Apache License 2.0

Python 57.77% Shell 0.31% CMake 0.85% C++ 41.06%
paddlepaddle ocr deploy onnx detection classification ppocr picodet ppyoloe onnxruntime

paddle2onnx's Issues

转stnet报错

将stnet训练的模型,想转onnx,但是却报错了
onnx.onnx_cpp2py_export.checker.ValidationError: Nodes in a graph must be topologically sorted, however input 'conv3d_0.tmp_0' of node:
input: "conv3d_0.tmp_0" input: "conv3d_0.tmp_1@reshape_y" output: "conv3d_0.tmp_1" op_type: "Add"
is not output of any previous nodes.

环境:ubuntu 16.04
paddle版本: paddlepaddle-gpu 1.6.1.post107
onnx版本: onnx 1.5.0

转yolov3 paddle1.5.1

aistudio@jupyter-83877-140942:~$ paddle2onnx --fluid_model freeze_model/ --onnx_model onnxyolov3.onnx/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
----------- Configuration Arguments -----------
check_task: image_classification
debug: False
fluid_model: freeze_model/
fluid_model_name:
fluid_params_name:
image_path:
name_prefix:
onnx_model: onnxyolov3.onnx
return_variable: False
to_print_model: False

load the model parameter done.
The operator sets to run test case.
{'nearest_interp', 'yolo_box', 'shape', 'batch_norm', 'multiclass_nms', 'conv2d', 'transpose2', 'cast', 'concat', 'fill_constant', 'leaky_relu', 'slice', 'elementwise_mul', 'scale', 'elementwise_add'}
Traceback (most recent call last):
File "/opt/conda/envs/python35-paddle120-env/bin/paddle2onnx", line 10, in
sys.exit(main())
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/fluid_onnx/fluid_to_onnx.py", line 230, in main
convert(args)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/fluid_onnx/fluid_to_onnx.py", line 194, in convert
checker.check_model(onnx_model)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/onnx/checker.py", line 91, in check_model
C.check_model(model.SerializeToString())
onnx.onnx_cpp2py_export.checker.ValidationError: Node () has input size 2 not in range [min=3, max=4].

==> Context: Bad node spec: input: "leaky_relu_58.tmp_0" input: "nearest_interp_0.tmp_0@scales" output: "nearest_interp_0.tmp_0" op_type: "Resize" attribute { name: "mode" s: "nearest" type: STRING }

Issues using paddle onnx convert models

We ran into the following issues and would like to get direction from paddle-onnx dev.

1. assertion error : dims.nbDims == 3

I can convert paddle paddle models to onnx using the fluid_to_onnx.py script, e.g.

(venv) root@c33e4b787188:/paddle-onnx# python fluid_to_onnx.py --fluid_model extras/fit_a_line.inference.model --onnx_model extras/fit_a_line.onnx.inference.model --to_print_model > extras/fit_a_line_onnx.inference.model.out
(venv) root@c33e4b787188:
/paddle-onnx# cd extras
(venv) root@c33e4b787188:~/paddle-onnx/extras# ls
fit_a_line.inference.model fit_a_line.onnx.inference.model fit_a_line_onnx.inference.model.out

but when I try to validate the resulting onnx model using the validate.py script I get the following error when using the tensorrt backend:

(venv) root@c33e4b787188:/paddle-onnx# deactivate
root@c33e4b787188:
/paddle-onnx# python validate.py --fluid_model extras/fit_a_line.inference.model --onnx_model extras/fit_a_line.onnx.inference.model --backend tensorrt
----------- Configuration Arguments -----------
a: 0.0
b: 1.0
backend: tensorrt
batch_size: 10
expected_decimal: 5
fluid_model: extras/fit_a_line.inference.model
onnx_model: extras/fit_a_line.onnx.inference.model

Inference results for fluid model:
[array([[15.874767],
[17.174097],
[14.951813],
[14.069194],
[13.003316],
[17.782452],
[16.204231],
[13.260891],
[15.537827],
[13.720056]], dtype=float32)]

Traceback (most recent call last):
File "validate.py", line 129, in
validate(args)
File "validate.py", line 113, in validate
rep = backend.prepare(onnx_model, device='CUDA:0')
File "build/bdist.linux-x86_64/egg/onnx_tensorrt/backend.py", line 166, in prepare
File "build/bdist.linux-x86_64/egg/onnx_tensorrt/backend.py", line 71, in init
RuntimeError: While parsing node number 1:
/root/onnx-tensorrt/builtin_op_importers.cpp:539 In function importFlatten:
[8] Assertion failed: dims.nbDims == 3

2. Issue 2 : dim_value 0

We see some dim_value of 0 in the generated model. Is this expected and how those should be interpreted.

When I look at the human readable onnx model, i.e. fit_a_line_onnx.inference.model.out that was generated by the fluid_to_onnx.py script there are nodes with dim_value of 0:

(venv) root@c33e4b787188:~/paddle-onnx/extras# cat fit_a_line_onnx.inference.model.out | more

----------- Configuration Arguments -----------
fluid_model: extras/fit_a_line.inference.model
onnx_model: extras/fit_a_line.onnx.inference.model
to_print_model: True

The converted model is:
ir_version: 3
producer_name: "PaddlePaddle"
graph {
node {
input: "x"
output: "x@flatten_0"
op_type: "Flatten"
attribute {
name: "axis"
i: 1
type: INT
}
}
node {
input: "fc_0.w_0"
output: "fc_0.w_0@flatten_0"
op_type: "Flatten"
attribute {
name: "axis"
i: 1
type: INT
}
}
node {
input: "x@flatten_0"
input: "fc_0.w_0@flatten_0"
output: "fc_0.tmp_0@matmul_0"
op_type: "MatMul"
}
node {
output: "fc_0.tmp_0@shape_0"
op_type: "Constant"
attribute {
name: "value"
t {
dims: 2
data_type: INT64
int64_data: 0
int64_data: 1
name: fc_0.tmp_0@shape_0
}
type: TENSOR
}
}
node {
input: "fc_0.tmp_0@matmul_0"
input: "fc_0.tmp_0@shape_0"
output: "fc_0.tmp_0"
op_type: "Reshape"
}
node {
input: "fc_0.tmp_0"
input: "fc_0.b_0"
output: "fc_0.tmp_1"
op_type: "Add"
attribute {
name: "axis"
i: 1
type: INT
}
attribute {
name: "broadcast"
i: 1
type: INT
}
}
name: "fit_a_line"
initializer {
dims: 1
data_type: FLOAT
float_data: 19.3055801392
name: "fc_0.b_0"
}
initializer {
dims: 13
dims: 1
data_type: FLOAT
float_data: -0.235716566443
float_data: 1.50793659687
float_data: -1.37839913368
float_data: 0.587660908699
float_data: -1.62691628933
float_data: 1.94002008438
float_data: -1.5584435463
float_data: 1.01809895039
float_data: -2.47688126564
float_data: -2.48663592339
float_data: -2.72155380249
float_data: 1.01887917519
float_data: -2.73560881615
name: "fc_0.w_0"
}
input {
name: "x"
type {
tensor_type {
elem_type: FLOAT
shape {
dim {
dim_value: 0
}
dim {
dim_value: 13
}
}
}
}
}
input {
name: "fc_0.b_0"
type {
tensor_type {
elem_type: FLOAT
shape {
dim {
dim_value: 1
}
}
}
}
}
input {
name: "fc_0.w_0"
type {
tensor_type {
elem_type: FLOAT
shape {
dim {
dim_value: 13
}
dim {
dim_value: 1
}
}
}
}
}
output {
name: "fc_0.tmp_1"
type {
tensor_type {
elem_type: FLOAT
shape {
dim {
dim_value: 0
}
dim {
dim_value: 1
}
}
}
}
}
}
opset_import {
version: 7
}

Saved converted model to path: extras/fit_a_line.onnx.inference.model

I get the same results after converting the recognize_digits_mlp.inference.model from the paddle paddle repo’s /path-to-repo/paddle-paddle/python/paddle/fluid/tests/book directory…

Need consider compatibility

Please add the following import at the head of each python file.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

We need distinguish persistable vars and const op

Currently, we simply use a Constant operator to handle persistable variables. I think instead we should use
initializes field of onnx graph proto. Since in fluid, we have constant operators, please consider this case:

        cell_init = fluid.layers.fill_constant_batch_size_like(
            input=decoder_boot,
            value=0.0,
            shape=[-1, decoder_size],
            dtype='float32')
        cell_init.stop_gradient = False

        with rnn.block():
            current_word = rnn.step_input(target_embedding)
            encoder_vec = rnn.static_input(encoder_vec)
            encoder_proj = rnn.static_input(encoder_proj)

cell_init should be the constant op which is not trainable.

Seems we need add unit test to make sure onnx work properly.

I execute sh setup.sh and all python dependencies are installed successfully. However, I can't run convert.py and the error message is:

Traceback (most recent call last):
  File "convert.py", line 18, in <module>
    from onnx import helper, checker
  File "/usr/local/lib/python2.7/dist-packages/onnx/__init__.py", line 10, in <module>
    import onnx.helper  # noqa
  File "/usr/local/lib/python2.7/dist-packages/onnx/helper.py", line 15, in <module>
    import onnx.defs as defs
  File "/usr/local/lib/python2.7/dist-packages/onnx/defs/__init__.py", line 6, in <module>
    import onnx.onnx_cpp2py_export.defs as C
ImportError: /usr/local/lib/python2.7/dist-packages/onnx/onnx_cpp2py_export.so: undefined symbol: _ZNK6google8protobuf7Message13SpaceUsedLongEv

paddle2onnx ssd_mobilnet_v1

使用paddleDetection的ssd_mobilnet_v1训练获得的模型,使用此工具转换为onnx模型后,使用onnxruntime调用onnx模型检测不到正确的框,概率分数也较低,与真实相差很远。

Operator unit tests broken

Many unit tests seem to be broken for me on the develop branch.

In particular, the hard-coding of output var type is problematic:

    def append_input_output(self, block, op_proto, np_list, persistable_list, is_input):
        ...
        def create_var(block, name, np_list, var_proto):
            ...
            return block.create_var(
                dtype='float32',
                shape=shape,
                persistable=persistable,
                lod_level=lod_level,
                name=name)

However, making the type from np_val will fix some tests but break many others.

paddle转onnx出错

Python:Python 3.7
转换框架版本:PaddlePaddle 1.5.1
paddledetecton0.1: darnet yolov3 训练的模型
paddle2onnx: 0.2
*训练参数和模型:*https://github.com/PaddlePaddle/PaddleDetection/blob/release/0.1/configs/yolov3_darknet_voc.yml

使用转换命令:paddle2onnx --fluid_model mj_yolov3_darknet/ --fluid_model_name mj_yolov3_darknet/model --fluid_params_name mj_yolov3_darknet/params --onnx_model mjy3
得到的结果,出错信息

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
----------- Configuration Arguments -----------
check_task: image_classification
debug: False
fluid_model: mj_yolov3_darknet/
fluid_model_name: mj_yolov3_darknet/model
fluid_params_name: mj_yolov3_darknet/params
image_path:
name_prefix:
onnx_model: mjy3
return_variable: False
to_print_model: False

load the model parameter done.
The operator sets to run test case.
{'concat', 'multiclass_nms', 'scale', 'conv2d', 'leaky_relu', 'batch_norm', 'yolo_box', 'nearest_interp', 'elementwise_add', 'transpose2'}
Traceback (most recent call last):
File "/opt/conda/envs/python35-paddle120-env/bin/paddle2onnx", line 10, in
sys.exit(main())
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/fluid_onnx/fluid_to_onnx.py", line 230, in main
convert(args)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/fluid_onnx/fluid_to_onnx.py", line 194, in convert
checker.check_model(onnx_model)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/onnx/checker.py", line 91, in check_model
C.check_model(model.SerializeToString())
onnx.onnx_cpp2py_export.checker.ValidationError: Node () has input size 0 not in range [min=1, max=1].

==> Context: Bad node spec: output: "nearest_interp_0.tmp_0@out_size_f" op_type: "Cast" attribute { name: "to" i: 1 type: INT }
@cjt222

fluid_to_onnx show error

i don't know what happens and how to solve it

-----------  Configuration Arguments -----------
check_task: image_classification
debug: False
fluid_model: ../mask_detector/
fluid_model_name: __model__
fluid_params_name: __param__
image_path: 
name_prefix: 
onnx_model: ./model.onnx
return_variable: False
to_print_model: False
------------------------------------------------
load the model parameter done.
Traceback (most recent call last):
  File "fluid_to_onnx.py", line 234, in <module>
    main()
  File "fluid_to_onnx.py", line 230, in main
    convert(args)
  File "fluid_to_onnx.py", line 141, in convert
    block=block)
  File "/home/tu/anaconda3/envs/occlusion_face_paddle/lib/python3.7/site-packages/fluid_onnx/ops.py", line 192, in conv2d_op
    kernel_shape = block.vars[get_old_name(inputs['Filter'][0])].shape
KeyError: ''

依赖的环境问题

根据requirements.txt尝试了几种环境组合,依然报错,请问拿release的1.0版本来说,下面的环境是哪个版本啊
protobuf==?
onnx==?
paddlepaddle==?

import paddle.fluid as fluid ImportError: No module named fluid

Using pip install paddlepaddle, I get the paddlepaddle (0.11.0).
After that I tried python convert.py --modeldir inception_v1/, I get the error: import paddle.fluid as fluid ImportError: No module named fluid.

Then I tried to change import paddle.v2.fluid as fluid, it works.

Rerunning the converter and get the following error:

  File "/home/haifeng/Synopsys/paddle-onnx/convert.py", line 21, in <module>
    import fluid_onnx.ops as ops
  File "/home/haifeng/Synopsys/paddle-onnx/fluid_onnx/ops.py", line 17, in <module>
    from paddle.fluid.executor import fetch_var
ImportError: No module named fluid.executor

It seems the converter use different paddlepaddle version.
Could you look into it?

Thanks

paddle模型转换为onnx,上线出现错误。

现象:一个最基本的乘法,转换为onnx文件后,上线报错。错误信息:

441188a12e273f99aa4c9c2cf

环境:paddle环境是1.5 ,paddle2onnx 是0.2

paddle部分的代码:

import paddle.fluid.layers as layers
dataX = fluid.layers.data(name="dataX", append_batch_size = False, shape=[2, 5],dtype="float32")
w = layers.create_parameter(shape=[5, 3], dtype='float32', is_bias=False)
output = fluid.layers.mul(dataX, w,
                          x_num_col_dims = 1,
                          y_num_col_dims = 1)

place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
infer_path = os.path.join('./', 'ckp-infer2')
if not os.path.isdir(infer_path):
    os.makedirs(infer_path)
fluid.io.save_inference_model(main_program=fluid.default_main_program(),
                              dirname=infer_path,
                              feeded_var_names=[dataX.name],
                              target_vars=[output],
                              executor=exe)

然后转换为onnx的命令:

paddle2onnx --fluid_model ckp-infer2/  --onnx_model paddle2onnx_test

看onnx文件,发现里面确实有一个reshape的操作。不清楚为什么
image

Some important feedforward models to be supported at first stage

We are going to support the conversion of models as follows at first stage. They all can be found in our models bank, some maybe only need to be verified after #19 merged, and some lack of necessary operators.

Even all the models above are supported, only a small subset of operators are used and verified. So for the rest operators, we may need another task:

  • Design the unit test framework to test every operator's conversion (#29 @kuke)

Hints:

  • Choose one task every time and assign yourself when you are really ready for working on it;
  • Please append the model to the supported models section in README like #19 after its conversion being validated.

个别op目前暂不支持

我把ernie的inference model存下来后, 作为paddle-onnx的输入, 然后就报了这样的错:
image
请问有什么解决方案吗? 十分感谢!

Fluid2ONNX conversion on the example

Hello, I met an issue on conversion from fluid to ONNX model on the example. Anyone can help?

----------- Configuration Arguments -----------
fluid_model: extras/fit_a_line.inference.model/
onnx_model: test.onnx
to_print_model: False

Traceback (most recent call last):
File "fluid_to_onnx.py", line 143, in
convert(args)
File "fluid_to_onnx.py", line 67, in convert
var=var, scope=inference_scope)
File "/home/test/paddle-onnx/fluid_onnx/variables.py", line 39, in paddle_onnx_weight
data = _fetch_var(var.name, scope)
File "/home/test/.local/lib/python2.7/site-packages/paddle/fluid/executor.py", line 191, in _fetch_var
assert isinstance(name, str)
AssertionError

ONNX version: 1.2.2+ (pip install on centos 7.2)
Paddle: github Aug 28th

ONNX operators used in image classification models

  • recognize_digits: Conv, Add, Relu, MaxPool, BatchNormalization, Reshape, MatMul, Softmax, Tanh

  • ResNet: Conv, Add, Relu, AveragePool, Reshape, MatMul, Softmax

  • VGG: Conv, Add, Relu, Dropout, Constant, Mul, MaxPool, Reshape, MatMul, BatchNormalization, Softmax

  • MobileNet: Conv, BatchNormalization, Relu, GlobalAveragePool, Reshape, MatMul, Add, Softmax

  • SE_ResNeXt: Conv, BatchNormalization, Relu, MaxPool, GlobalAveragePool, Reshape, MatMul, Add, Sigmoid, Mul, Dropout, Constant, Softmax

  • Inception V4: Conv, BatchNormalization, MaxPool, Concat, AveragePool, Dropout, Constant, Mul, Reshape, MatMul, Add, Softmax

ONNX == 1.0.1, Data layout == NCHW

转换yolov3报错的问题

使用paddlepaddle1.5版本,下载了model_zoo中的yolov3_darknet_voc,使用PaddlePaddle中PaddleDetection中的infer.py生成__model__和参数文件,再使用paddle2onnx进行转换,碰到了leaky_relu层的输入参数不匹配的问题,可能是什么原因?
image

Need develop branch.

New feature or hot-fix should be pushed to develop branch first. The master branch should maintain the stable version. We can polish the conversion for fit_a_line model and when the model is well supported we can ship it to master branch. Please vote for this proposal.

Loading Paddle parameters

We need to figure out whether Paddle params will be read:

  • in C++ using an executor run,
  • in C++ using a new interface just for reading
  • in Python, with logic identical to C++ logic

Additionally, we need to get on the same page with populating the graph with parameters

Undefined shape value in paddle generated onnx model

Paddle-ONNX generated model set dim_value=0 as placeholder for parameters to be set by the user, such as batch size. It seems there is no facility in ONNX to store undefined shape value, such as 0.

The undefined value broke ngraph-onnx importer and this thread is to open a discuss to seek a solution.

There is patch developed by @arogowie-intel which can be submitted to review. That is one the option.

See more info here: #52

CC @kuke @varunarora @postrational @raramer01

转化yolov3 mobilenet backbone 出错。

使用configs/yolov3_mobilenet_v1.yml 配置文件,其中按照自己数据集的相关信息,修改配置文件,训练好的模型采用 paddledetection库中的tools/export_model.py把模型进行导出,而后使用paddle2onnx
准备导出onnx模型,出错信息如下:
使用的相关软件包的版本为:
paddlepaddle-gpu 1.6.1.post107
paddle2onnx 0.2

Screenshot from 2019-12-23 16-39-24-2

FileNotFoundError: [Errno 2] No such file or directory: '/home/work/******params/__model__'

----------- Configuration Arguments -----------
check_task: image_classification
debug: False
fluid_model: /home/work/luqiankun/tools_for_renfeng/qt_batchlist_31w_bak/params/
fluid_model_name:
fluid_params_name:
image_path:
name_prefix:
onnx_model: ERNIE2.0
return_variable: False
to_print_model: False

Traceback (most recent call last):
File "/ssd1/share/python36/bin/paddle2onnx", line 11, in
load_entry_point('paddle2onnx==0.1', 'console_scripts', 'paddle2onnx')()
File "/ssd1/share/python36/lib/python3.6/site-packages/paddle2onnx-0.1-py3.6.egg/fluid_onnx/fluid_to_onnx.py", line 230, in main
File "/ssd1/share/python36/lib/python3.6/site-packages/paddle2onnx-0.1-py3.6.egg/fluid_onnx/fluid_to_onnx.py", line 99, in convert
File "/ssd1/share/python36/lib/python3.6/site-packages/paddle/fluid/io.py", line 1199, in load_inference_model
with open(model_filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: '/home/work/******/params/model'

运行代码报错 __model__文件是什么?我再paddle里下的ernie模型里也没有这个文件呀

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.