onnx.md 1.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Introduction of `onnx` module in MMCV (Experimental)

## register_extra_symbolics

Some extra symbolic functions need to be registered before exporting PyTorch model to ONNX.

### Example

```python
import mmcv
from mmcv.onnx import register_extra_symbolics

opset_version = 11
register_extra_symbolics(opset_version)
```

## ONNX simplify

### Intention

`mmcv.onnx.simplify` is based on [onnx-simplifier](https://github.com/daquexian/onnx-simplifier), which is a useful tool to make exported ONNX models slimmer by performing a series of optimization. However, for Pytorch models with custom op from `mmcv`, it would break down. Thus, custom ops for ONNX Runtime should be registered.

23
24
25
26
27
28
29
30
### Prerequisite

`mmcv.onnx.simplify` has three dependencies: `onnx`, `onnxoptimizer`, `onnxruntime`. After installation of `mmcv`, you have to install them manually using pip.

```bash
pip install onnx onnxoptimizer onnxruntime
```

31
32
33
34
35
36
37
### Usage

```python
import onnx
import numpy as np

import mmcv
38
39
from mmcv.onnx.simplify import simplify

40
41
42
43
44
45
46
47
48
49
dummy_input = np.random.randn(1, 3, 224, 224).astype(np.float32)
input = {'input':dummy_input}
input_file = 'sample.onnx'
output_file = 'slim.onnx'
model = simplify(input_file, [input], output_file)
```

### FAQs

- None