Skip to content

Commit da3f80a

Browse files
committed
Update README.md
1 parent e90948d commit da3f80a

File tree

1 file changed

+60
-56
lines changed

1 file changed

+60
-56
lines changed

README.md

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
# MgeConvert
22

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 等,如果需要适配其他模型, 可能需要添加更多的算子支持。
46

5-
MgeConvert转换工具位于converters目录下,可直接调用其中的脚本将MegEngine导出的mge/TracedModule模型转换为第三方模型文件。
67
目前,MgeConvert 亦支持将 ONNX 模型转换到 mge/TracedModule 模型。
78

9+
![mgeconvert](https://user-images.githubusercontent.com/15715998/174760137-1252ae3c-4be0-4cdd-9314-ab94edd7e439.png)
10+
811
MgeConvert转换器的结构包含前端、中间表示(IR)、后端三个部分:
912
1. 前端的部分位于 `frontend` 目录下, 支持 mge 和 traced module 模型格式,可以将 MegEngine 序列化出来的计算图转为IR图结构
1013
2. IR部分位于 `converter_ir`目录下,包含图和 IR 算子定义、对计算图做变换的 transform rules 以及对量化模型处理的量化器
1114
3. 后端的部分位于 `backend` 目录下,包含caffe、onnx、tflite的转换器,可以将IR图结构转换为第三方框架的模型文件
1215

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+
- [算子支持列表](#算子支持列表)
1434

1535
## Feature 支持说明
1636

@@ -31,9 +51,7 @@ MgeConvert转换器的结构包含前端、中间表示(IR)、后端三个
3151
| Float32 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
3252

3353

34-
## 安装方式
35-
36-
### 依赖说明
54+
## 依赖说明
3755

3856
MgeConvert 基于 MegEngine 工作,因此确保您的电脑已经安装 MegEngine(>=1.0)。
3957

@@ -50,58 +68,44 @@ MgeConvert 基于 MegEngine 工作,因此确保您的电脑已经安装 MegEng
5068
- Python packages: pybind11==2.6.2
5169
- third party: [flatbuffers](https://github.com/google/flatbuffers.git)==1.12.0
5270

53-
> :warning: 安装时以上依赖覆盖本地版本,建议在虚拟环境中安装mgeconvert
71+
> :warning: 安装时以上依赖覆盖本地版本
5472
55-
如果安装过0.5.0及之前版本的mgeconvert,需要先卸载旧版本:
56-
57-
```bash
58-
sudo pip3 uninstall mgeconvert
59-
```
6073

74+
## 安装方式
6175

62-
### pip 安装
63-
64-
- 以 caffe 为例,下面这条指令将通过``pip``包管理器安装开发版本的 caffe 转换器并处理相关依赖:
76+
> 如果安装过0.5.0及之前版本的mgeconvert,重新安装前请先使用sudo权限卸载旧版本:
77+
>
78+
> ```bash
79+
> sudo pip3 uninstall mgeconvert
80+
> ```
6581
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 开始支持源码包安装:
6984
70-
> 建议指定版本号安装release版本的转换器,如安装0.4.2版本
85+
- 以 caffe 为例,下面这条指令将安装caffe 转换器并处理相关依赖
7186
7287
```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"
7489
```
7590
76-
> :warning: 如果需要转换``TracedModule``模型,请安装0.5.0以上版本
77-
78-
79-
- ``--targets`` 的可选值有 ``caffe````onnx````tflite```all`
91+
> ``--targets`` 的可选值有 caffe、onnx、tflite 和 all。 `all` 代表安装全部转换器。可选值支持组合传入,比如 ``--targets=caffe,tflite``
8092
81-
`all` 代表安装全部转换器。可选值支持组合传入,比如 ``--targets=caffe,tflite``
93+
> ``tflite`` 转换器的schema默认使用r2.3版本,支持使用参数 ``tfversion`` 选择tflite schema的版本, 比如 ``--install-option="--targets=tflite --tfversion=r2.4"``
8294
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-
```
9495

9596
### 源代码安装
9697

97-
安装选项说明同上,以 caffe 为例,下面的命令将安装0.4.2版本的caffe转换器
98+
安装选项说明同上,以 caffe 为例,下面的命令将安装0.5.0版本的caffe转换器
9899

99100
```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
101102
cd mgeconvert
102103
pip3 install . --user --install-option="--targets=caffe"
103104
```
104105

106+
> :warning: 如果需要转换``TracedModule``模型,请安装0.5.0及以上版本
107+
108+
105109
## 使用方式
106110

107111
转换器按输入模型格式主要分为两种:
@@ -126,9 +130,9 @@ convert -h
126130
convert mge_to_caffe -h
127131
```
128132

129-
#### caffe模型转换
133+
### 1.1 :sparkles: caffe模型转换
130134

131-
##### float模型转换
135+
#### 1.1.1 float模型转换
132136

133137
- 转换mge模型的参考命令:
134138

@@ -142,7 +146,7 @@ convert mge_to_caffe -i model.mge -c out.prototxt -b out.caffemodel
142146
convert tracedmodule_to_caffe -i model.tm -c out.prototxt -b out.caffemodel
143147
```
144148

145-
##### QAT模型转换
149+
#### 1.1.2 QAT模型转换
146150
mgeconvert 支持将 QAT TracedModule 模型转换到caffe:
147151
- QAT模型转caffe默认会导出量化参数文件,通过 `quantize_file_path` 指定量化参数文件路径:
148152

@@ -156,18 +160,18 @@ convert tracedmodule_to_caffe -i qat_model.tm -c out.prototxt -b out.caffemodel
156160
convert tracedmodule_to_caffe -i qat_model.tm -c out.prototxt -b out.caffemodel --quantize_file_path quant_params.json --param_fake_quant
157161
```
158162

159-
- 如果QAT模型中没有QuantStub对输入数据进行量化处理,可以在转换时指定输入数据的量化类型、scale和zero_point量化参数 :
163+
- 如果QAT模型中没有QuantStub对输入数据进行量化处理,可以用 **`--input_data_type --input_scales --input_zero_points`** 在转换时指定输入数据的量化类型、scale和zero_point量化参数,如果有多个scale、zero point用逗号隔开
160164

161165
```bash
162166
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
163167
```
164168

165169

166-
#### tflite模型转换
170+
### 1.2 :sparkles: tflite模型转换
167171

168172
TFlite转换器支持 float32 和量化的 TracedModule 转换。
169173

170-
##### float模型转换
174+
#### 1.2.1 float模型转换
171175

172176
转换float模型的命令参考:
173177

@@ -179,33 +183,33 @@ convert mge_to_tflite -i model.mge -o out.tflite
179183
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite
180184
```
181185

182-
##### QAT模型转换
186+
#### 1.2.2 QAT模型转换
183187

184188
- 对于QAT模型,可以通过添加tracedmodule_to_tflite转换器中的 `require_quantize` 选项,转换出tflite支持的量化数据类型(int8/uint8/int16/int32)量化后的Quantized 模型:
185189

186190
```bash
187191
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --require_quantize
188192
```
189193

190-
也可不设置 `require_quantize` 选项,转换出float32模型和量化参数文件。
194+
也可不设置 **`--require_quantize`** 选项,转换出float32模型和量化参数文件。
191195

192196
```bash
193197
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --quantize_file_path quant_params.json
194198
```
195199

196-
- 对于QAT模型,还可以通过设置 `param_fake_quant` 参数来选择是否对参数进行假量化。
200+
- 对于QAT模型,还可以通过设置 **`--param_fake_quant`** 参数来选择是否对参数进行假量化。
197201

198202
```bash
199203
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --quantize_file_path quant_params.json --param_fake_quant
200204
```
201205

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用逗号隔开:
203207

204208
```bash
205209
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
206210
```
207211

208-
#### onnx模型互转
212+
### 1.3 :sparkles: onnx模型互转
209213

210214
mgeconvert 转 onnx 模型支持 [opset](https://github.com/onnx/onnx/blob/master/docs/Operators.md) 7~12 的转换。
211215
onnx 转 mge/TracedModule 对各时期的 opset 变更均进行了适配,理论上没有 opset 的限制。
@@ -232,7 +236,7 @@ convert onnx_to_tracedmodule -i tracedmodule.onnx -o out.tm
232236

233237
可参考[wiki](https://github.com/MegEngine/mgeconvert/wiki/Mgeconvert-Python-Api-Doc)中的例子。
234238

235-
## 常见问题说明
239+
## FAQ 常见问题说明
236240

237241
1. 安装时出现类似报错:
238242

@@ -243,9 +247,9 @@ error removing /home/user/.local/lib/python3.6/site-packages/mgeconvert-0.5.0-py
243247

244248
这是使用sudo安装过旧版本出现的权限问题,先卸载旧版本再安装:
245249

246-
```bash
247-
sudo pip3 uninstall mgeconvert
248-
```
250+
> ```bash
251+
> sudo pip3 uninstall mgeconvert
252+
> ```
249253
250254
2. 使用`tflite`转换器时`fbconverter.so`出现 `undefined symbol`错误:
251255
@@ -254,9 +258,9 @@ ImportError: /home//lib/python3.6/site-packages/mgeconvert/backend/ir_to_tflite/
254258
```
255259
这是链接的`libflatbuffers.so`版本和依赖版本不一致导致的问题,执行以下命令使用`mgeconvert`编译的`libflatbuffers.so`
256260

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+
>```
260264
261265
## 算子支持列表
262266

0 commit comments

Comments
 (0)