onnx_convertor.md 4.1 KB
Newer Older
Y
Yibing Liu 已提交
1 2
### Backgroud

Y
Yibing Liu 已提交
3 4 5 6
[ONNX (Open Neural Network Exchange)](https://github.com/onnx/onnx) bridges different deep learning frameworks by providing an open source format for models. The models trained in other frameworks can be converted into the ONNX format to execute inference by utilizing the built-in operators in ONNX. With the converse conversion, different frameworks can share any models supported by ONNX in pinciple. Now most mainstream frameworks have joined the ONNX community, e.g. Caffe2, TensorFlow, and MXNet etc. And there is a trendency that more and more vendors begin to support ONNX or even choose ONNX as the only machine learning engine in their devices. 

Therefore, it is necessary to enable the conversion between PaddlePaddle and ONNX. This design doc aims to implement the convertor, mainly for the ONNX conversion of models in Fluid and possibly including some important models in V2 format in the future. A complete convertor should be bidirectional, but  considering the importance, the conversion from Fluid to ONNX will be implemented preferentially.

Y
Yibing Liu 已提交
7 8

### How it works
Y
Yibing Liu 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

As the first step, Fluid must cover [all the listed operators](https://github.com/onnx/onnx/blob/master/docs/Operators.md) in ONNX. The complement is being carried out and only a few minor operators need to be newly added or enhanced, which would not postpone the convertor and the test of common models.

About the convertor, several things need to be considered: 

- OP-level conversion 
   - How to map the inputs, attributes, weights, and outputs each operator.
- Data type mapping
- Network representation adapation 
   - The model in Fluid is represented by nested `Block`, how to parse and reconstruct it in ONNX graph format, and vice versa;

- Model validation
   - To assure the correctness of conversion. A simple way may be to generate some dummy data as the input and compare the inference results.
- Long term support
   - As ONNX keeps evolving, a mechanism to make sure long term support is needed.
Y
Yibing Liu 已提交
24 25

### Project structure
26 27 28 29 30

<p align="center">
<img src="./images/project_structure.png"/>
</p>

Y
Yibing Liu 已提交
31
The project contains four important parts: 
32

Y
Yibing Liu 已提交
33
* **fluid**: The directory that contains wrappers for fluid related APIs. Fluid has provided some low-level APIs to parse or generate the inference model. However, directly using these low-level APIs makes the code tediously long. This module wraps low-level APIs to provide simplied interfaces.
34

Y
Yibing Liu 已提交
35
* **onnx**: ONNX uses protobuf to save computation flow and model weights. This directory consists of scripts responsible for parsing and generating an ONNX binary model.
36

Y
Yibing Liu 已提交
37
* **onnx_fluid**: Concepts in fluid like ```program```, ```block``` etc. don't have direct corresponding concepts in ONNX. Even though both contain the operator concept, the adaption is also necessary for many operators. This directory consists of the most important modules responsible for acutal converting. Adaption for different level concepts should be provided like fluid ```program/block``` to ONNX graph, fluid operators to ONNX operators etc.
38

Y
Yibing Liu 已提交
39
* **convert.py**: The interface exposed to users. 
Y
Yibing Liu 已提交
40 41

### Usage
Y
Yibing Liu 已提交
42
The converter is designed to very easy-to-use. Bidirectional conversion between Fluid inference model and ONNX binary model is supported. Model validation is also provided to verify the correctness of converted model.
43

Y
Yibing Liu 已提交
44
* Fluid inference model to ONNX binary model
45

Y
Yibing Liu 已提交
46 47 48
```
python convert.py --input <fluid inference model> --output <ONNX model> --to_validate True
```
49

Y
Yibing Liu 已提交
50
The conversion and model validation will be completed consecutively, finally output a readable model structure description. And for the converse conversion, users only need to exchange the input and output. 
51

Y
Yibing Liu 已提交
52 53

### Supported models
Y
Yibing Liu 已提交
54 55 56 57 58 59

Potential risks may come from the conversion of sequence-related models, including the LodTensor, ```if/else``` and ```while``` operator.
So a good choice is to focus on some important feedforward models first, then implement some simple recurrent models.
 
- Feedforward models: common models selected in PaddleBook, e.g. VGG, ResNet and some other models proposed by application teams.
- Recurrent models: language model, stacked LSTMs etc.