1
1
# MgeConvert
2
2
3
- 适用于 [ MegEngine] ( https://github.com/MegEngine/MegEngine ) 的各种转换器, 目前支持的框架有 [ Caffe] ( https://github.com/BVLC/caffe ) 、[ ONNX] ( https://github.com/onnx/onnx ) 和 TFLite。
3
+ MgeConvert 是适用于 [ MegEngine] ( https://github.com/MegEngine/MegEngine ) 模型的转换器, 可将MegEngine导出的[ mge静态图模型] ( https://www.megengine.org.cn/doc/stable/zh/user-guide/model-development/serialization/index.html#dump-traced-model ) 或[ TracedModule模型] ( https://www.megengine.org.cn/doc/stable/zh/user-guide/model-development/traced_module/quick-start.html#tracedmodule ) 转换为第三方模型文件。
4
+
5
+ 目前支持转换的第三方框架有 [ Caffe] ( https://github.com/BVLC/caffe ) 、[ ONNX] ( https://github.com/onnx/onnx ) 和 [ TFLite] ( https://www.tensorflow.org/lite/guide ) ,支持的模型包括 ResNet、ResNext、ShuffleNet 等,如果需要适配其他模型, 可能需要添加更多的算子支持。
4
6
5
- MgeConvert转换工具位于converters目录下,可直接调用其中的脚本将MegEngine导出的mge/TracedModule模型转换为第三方模型文件。
6
7
目前,MgeConvert 亦支持将 ONNX 模型转换到 mge/TracedModule 模型。
7
8
9
+ ![ mgeconvert] ( https://user-images.githubusercontent.com/15715998/174760137-1252ae3c-4be0-4cdd-9314-ab94edd7e439.png )
10
+
8
11
MgeConvert转换器的结构包含前端、中间表示(IR)、后端三个部分:
9
12
1 . 前端的部分位于 ` frontend ` 目录下, 支持 mge 和 traced module 模型格式,可以将 MegEngine 序列化出来的计算图转为IR图结构
10
13
2 . IR部分位于 ` converter_ir ` 目录下,包含图和 IR 算子定义、对计算图做变换的 transform rules 以及对量化模型处理的量化器
11
14
3 . 后端的部分位于 ` backend ` 目录下,包含caffe、onnx、tflite的转换器,可以将IR图结构转换为第三方框架的模型文件
12
15
13
- 目前支持的模型包括 ResNet、ResNext、ShuffleNet 等,如果需要适配其他模型, 可能需要添加更多的算子支持。
16
+ - [ MgeConvert] ( #mgeconvert )
17
+ - [ Feature 支持说明] ( #feature-支持说明 )
18
+ - [ 依赖说明] ( #依赖说明 )
19
+ - [ 安装方式] ( #安装方式 )
20
+ - [ pip 安装(推荐)] ( #pip-安装推荐 )
21
+ - [ 源代码安装] ( #源代码安装 )
22
+ - [ 使用方式] ( #使用方式 )
23
+ - [ 1. 命令行使用] ( #1-命令行使用 )
24
+ - [ 1.1 :sparkles : caffe模型转换] ( #11-sparkles-caffe模型转换 )
25
+ - [ 1.1.1 float模型转换] ( #111-float模型转换 )
26
+ - [ 1.1.2 QAT模型转换] ( #112-qat模型转换 )
27
+ - [ 1.2 :sparkles : tflite模型转换] ( #12-sparkles-tflite模型转换 )
28
+ - [ 1.2.1 float模型转换] ( #121-float模型转换 )
29
+ - [ 1.2.2 QAT模型转换] ( #122-qat模型转换 )
30
+ - [ 1.3 :sparkles : onnx模型互转] ( #13-sparkles-onnx模型互转 )
31
+ - [ 2. python接口使用] ( #2-python接口使用 )
32
+ - [ FAQ 常见问题说明] ( #faq-常见问题说明 )
33
+ - [ 算子支持列表] ( #算子支持列表 )
14
34
15
35
## Feature 支持说明
16
36
@@ -31,9 +51,7 @@ MgeConvert转换器的结构包含前端、中间表示(IR)、后端三个
31
51
| Float32 | :white_check_mark : | :white_check_mark : | :white_check_mark : |
32
52
33
53
34
- ## 安装方式
35
-
36
- ### 依赖说明
54
+ ## 依赖说明
37
55
38
56
MgeConvert 基于 MegEngine 工作,因此确保您的电脑已经安装 MegEngine(>=1.0)。
39
57
@@ -50,58 +68,44 @@ MgeConvert 基于 MegEngine 工作,因此确保您的电脑已经安装 MegEng
50
68
- Python packages: pybind11==2.6.2
51
69
- third party: [ flatbuffers] ( https://github.com/google/flatbuffers.git ) ==1.12.0
52
70
53
- > :warning : 安装时以上依赖覆盖本地版本,建议在虚拟环境中安装mgeconvert
71
+ > :warning : 安装时以上依赖覆盖本地版本
54
72
55
- 如果安装过0.5.0及之前版本的mgeconvert,需要先卸载旧版本:
56
-
57
- ``` bash
58
- sudo pip3 uninstall mgeconvert
59
- ```
60
73
74
+ ## 安装方式
61
75
62
- ### pip 安装
63
-
64
- - 以 caffe 为例,下面这条指令将通过`` pip `` 包管理器安装开发版本的 caffe 转换器并处理相关依赖:
76
+ > 如果安装过0.5.0及之前版本的mgeconvert,重新安装前请先使用sudo权限卸载旧版本:
77
+ >
78
+ > ``` bash
79
+ > sudo pip3 uninstall mgeconvert
80
+ > ` ` `
65
81
66
- ``` bash
67
- python3 -m pip install git+https://github.com/MegEngine/mgeconvert.git --user --install-option=" --targets=caffe"
68
- ```
82
+ # ## pip 安装(推荐)
83
+ mgeconvert v1.0.0 开始支持源码包安装:
69
84
70
- > 建议指定版本号安装release版本的转换器,如安装0.4.2版本 :
85
+ - 以 caffe 为例,下面这条指令将安装caffe 转换器并处理相关依赖 :
71
86
72
87
` ` ` bash
73
- python3 -m pip install
git+https://github.com/MegEngine/ mgeconvert
[email protected] --user --install-option=
" --targets=caffe"
88
+ pip3 install mgeconvert --user --install-option=" --targets=caffe"
74
89
```
75
90
76
- > :warning : 如果需要转换`` TracedModule `` 模型,请安装0.5.0以上版本
77
-
78
-
79
- - `` --targets `` 的可选值有 `` caffe `` 、`` onnx `` 、`` tflite `` 和 ` all ` 。
91
+ > `` --targets `` 的可选值有 caffe、onnx、tflite 和 all。 ` all ` 代表安装全部转换器。可选值支持组合传入,比如 `` --targets=caffe,tflite `` 。
80
92
81
- ` all ` 代表安装全部转换器。可选值支持组合传入, 比如 `` --targets=caffe, tflite `` 。
93
+ > `` tflite `` 转换器的schema默认使用r2.3版本,支持使用参数 `` tfversion `` 选择tflite schema的版本, 比如 `` --install-option="-- targets=tflite --tfversion=r2.4" ``
82
94
83
- - `` tflite `` 转换器的schema默认使用r2.3版本,支持使用参数 `` tfversion `` 选择tflite schema的版本, 例如:
84
-
85
- ``` bash
86
- --install-option=" --targets=tflite --tfversion=r2.4"
87
- ```
88
-
89
- ` tflite ` 转换器依赖的 ` libflatbuffers.so ` 位于 ` $HOME/.local/lib ` 下,使用前需要执行:
90
-
91
- ``` bash
92
- export LD_LIBRARY_PATH=$HOME /.local/lib:$LD_LIBRARY_PATH
93
- ```
94
95
95
96
### 源代码安装
96
97
97
- 安装选项说明同上,以 caffe 为例,下面的命令将安装0.4.2版本的caffe转换器 :
98
+ 安装选项说明同上,以 caffe 为例,下面的命令将安装0.5.0版本的caffe转换器 :
98
99
99
100
``` bash
100
- git clone https://github.com/MegEngine/mgeconvert.git@v0.4.2
101
+ git clone https://github.com/MegEngine/mgeconvert.git@v0.5.0
101
102
cd mgeconvert
102
103
pip3 install . --user --install-option=" --targets=caffe"
103
104
```
104
105
106
+ > :warning : 如果需要转换`` TracedModule `` 模型,请安装0.5.0及以上版本
107
+
108
+
105
109
## 使用方式
106
110
107
111
转换器按输入模型格式主要分为两种:
@@ -126,9 +130,9 @@ convert -h
126
130
convert mge_to_caffe -h
127
131
```
128
132
129
- #### caffe模型转换
133
+ ### 1.1 : sparkles : caffe模型转换
130
134
131
- ##### float模型转换
135
+ #### 1.1.1 float模型转换
132
136
133
137
- 转换mge模型的参考命令:
134
138
@@ -142,7 +146,7 @@ convert mge_to_caffe -i model.mge -c out.prototxt -b out.caffemodel
142
146
convert tracedmodule_to_caffe -i model.tm -c out.prototxt -b out.caffemodel
143
147
```
144
148
145
- ##### QAT模型转换
149
+ #### 1.1.2 QAT模型转换
146
150
mgeconvert 支持将 QAT TracedModule 模型转换到caffe:
147
151
- QAT模型转caffe默认会导出量化参数文件,通过 ` quantize_file_path ` 指定量化参数文件路径:
148
152
@@ -156,18 +160,18 @@ convert tracedmodule_to_caffe -i qat_model.tm -c out.prototxt -b out.caffemodel
156
160
convert tracedmodule_to_caffe -i qat_model.tm -c out.prototxt -b out.caffemodel --quantize_file_path quant_params.json --param_fake_quant
157
161
```
158
162
159
- - 如果QAT模型中没有QuantStub对输入数据进行量化处理,可以在转换时指定输入数据的量化类型 、scale和zero_point量化参数 :
163
+ - 如果QAT模型中没有QuantStub对输入数据进行量化处理,可以用 ** ` --input_data_type --input_scales --input_zero_points ` ** 在转换时指定输入数据的量化类型 、scale和zero_point量化参数,如果有多个scale、zero point用逗号隔开 :
160
164
161
165
``` bash
162
166
convert tracedmodule_to_caffe -i qat_model.tm -c out.prototxt -b out.caffemodel --quantize_file_path quant_params.json --input_data_type quint8 --input_scales 0.125 --input_zero_points 128
163
167
```
164
168
165
169
166
- #### tflite模型转换
170
+ ### 1.2 : sparkles : tflite模型转换
167
171
168
172
TFlite转换器支持 float32 和量化的 TracedModule 转换。
169
173
170
- ##### float模型转换
174
+ #### 1.2.1 float模型转换
171
175
172
176
转换float模型的命令参考:
173
177
@@ -179,33 +183,33 @@ convert mge_to_tflite -i model.mge -o out.tflite
179
183
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite
180
184
```
181
185
182
- ##### QAT模型转换
186
+ #### 1.2.2 QAT模型转换
183
187
184
188
- 对于QAT模型,可以通过添加tracedmodule_to_tflite转换器中的 ` require_quantize ` 选项,转换出tflite支持的量化数据类型(int8/uint8/int16/int32)量化后的Quantized 模型:
185
189
186
190
``` bash
187
191
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --require_quantize
188
192
```
189
193
190
- 也可不设置 ` require_quantize ` 选项,转换出float32模型和量化参数文件。
194
+ 也可不设置 ** ` -- require_quantize` ** 选项,转换出float32模型和量化参数文件。
191
195
192
196
``` bash
193
197
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --quantize_file_path quant_params.json
194
198
```
195
199
196
- - 对于QAT模型,还可以通过设置 ` param_fake_quant ` 参数来选择是否对参数进行假量化。
200
+ - 对于QAT模型,还可以通过设置 ** ` -- param_fake_quant` ** 参数来选择是否对参数进行假量化。
197
201
198
202
``` bash
199
203
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --quantize_file_path quant_params.json --param_fake_quant
200
204
```
201
205
202
- - 如果QAT模型中没有QuantStub对输入数据进行量化处理,可以在转换时指定输入数据的量化类型 、scale和zero_point量化参数, 如果有多个scale、zero point用逗号隔开:
206
+ - 如果QAT模型中没有QuantStub对输入数据进行量化处理,可以用 ** ` --input_data_type --input_scales --input_zero_points ` ** 在转换时指定输入数据的量化类型 、scale和zero_point量化参数, 如果有多个scale、zero point用逗号隔开:
203
207
204
208
``` bash
205
209
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --input_data_type quint8 --input_scales 0.125,0.125 --input_zero_points 128,128 --require_quantize
206
210
```
207
211
208
- #### onnx模型互转
212
+ ### 1.3 : sparkles : onnx模型互转
209
213
210
214
mgeconvert 转 onnx 模型支持 [ opset] ( https://github.com/onnx/onnx/blob/master/docs/Operators.md ) 7~ 12 的转换。
211
215
onnx 转 mge/TracedModule 对各时期的 opset 变更均进行了适配,理论上没有 opset 的限制。
@@ -232,7 +236,7 @@ convert onnx_to_tracedmodule -i tracedmodule.onnx -o out.tm
232
236
233
237
可参考[ wiki] ( https://github.com/MegEngine/mgeconvert/wiki/Mgeconvert-Python-Api-Doc ) 中的例子。
234
238
235
- ## 常见问题说明
239
+ ## FAQ 常见问题说明
236
240
237
241
1 . 安装时出现类似报错:
238
242
@@ -243,9 +247,9 @@ error removing /home/user/.local/lib/python3.6/site-packages/mgeconvert-0.5.0-py
243
247
244
248
这是使用sudo安装过旧版本出现的权限问题,先卸载旧版本再安装:
245
249
246
- ``` bash
247
- sudo pip3 uninstall mgeconvert
248
- ```
250
+ > ``` bash
251
+ > sudo pip3 uninstall mgeconvert
252
+ > ` ` `
249
253
250
254
2. 使用` tflite` 转换器时` fbconverter.so` 出现 ` undefined symbol` 错误:
251
255
@@ -254,9 +258,9 @@ ImportError: /home//lib/python3.6/site-packages/mgeconvert/backend/ir_to_tflite/
254
258
```
255
259
这是链接的` libflatbuffers.so ` 版本和依赖版本不一致导致的问题,执行以下命令使用` mgeconvert ` 编译的` libflatbuffers.so ` :
256
260
257
- ``` bash
258
- export LD_LIBRARY_PATH=$HOME /.local /lib:$LD_LIBRARY_PATH
259
- ```
261
+ > ``` bash
262
+ > export LD_LIBRARY_PATH=$MGECONVERT_PATH /backend/ir_to_tflite/pyflexbuffers /lib:$LD_LIBRARY_PATH
263
+ > ` ` `
260
264
261
265
# # 算子支持列表
262
266
0 commit comments