PaddlePaddle to ONNX model converter
简体中文 | English
Paddle2ONNX enables users to convert models from PaddlePaddle to ONNX.
python >= 2.7 static computational graph: paddlepaddle >= 1.8.0 dynamic computational graph: paddlepaddle >= 2.0.0 onnx == 1.7.0 | Optional
pip install paddle2onnx
git clone https://github.com/PaddlePaddle/paddle2onnx.git python setup.py install
Uncombined PaddlePaddle model(parameters saved in different files)
paddle2onnx --model_dir paddle_model --save_file onnx_file --opset_version 10 --enable_onnx_checker True
Combined PaddlePaddle model(parameters saved in one binary file)
paddle2onnx --model_dir paddle_model --model_filename model_filename --params_filename params_filename --save_file onnx_file --opset_version 10 --enable_onnx_checker True
| Parameters | Description | |----------|--------------| |--modeldir | The directory path of the paddlepaddle model saved by `paddle.fluid.io.saveinferencemodel`| |--modelfilename |[Optional] The model file name under the directory designated by
--model_dir. Only needed when all the model parameters saved in one binary file. Default value None| |--paramsfilename |[Optonal] the parameter file name under the directory designated by`--modeldir`. Only needed when all the model parameters saved in one binary file. Default value None| |--savefile | the directory path for the exported ONNX model| |--opsetversion | [Optional] To configure the ONNX Opset version. Opset 9-11 are stably supported. Default value is 9.| |--enableonnxchecker| [Optional] To check the validity of the exported ONNX model. It is suggested to turn on the switch. If set to True, onnx>=1.7.0 is required. Default value is False| |--version |[Optional] check the version of paddle2onnx |
import paddle from paddle import nn from paddle.static import InputSpec import paddle2onnx as p2oclass LinearNet(nn.Layer): def init(self): super(LinearNet, self).init() self._linear = nn.Linear(784, 10)
def forward(self, x): return self._linear(x)
layer = LinearNet()
configure model inputs
x_spec = InputSpec([None, 784], 'float32', 'x')
convert model to inference mode
layer.eval()
save_path = 'onnx.save/linear_net' p2o.dygraph2onnx(layer, save_path + '.onnx', input_spec=[x_spec])
when paddlepaddle>2.0.0, you can try:
paddle.onnx.export(layer, save_path, input_spec=[x_spec])