{"id":13443260,"url":"https://github.com/MegEngine/mgeconvert","last_synced_at":"2025-03-20T16:30:49.247Z","repository":{"id":37094462,"uuid":"312239793","full_name":"MegEngine/mgeconvert","owner":"MegEngine","description":"MegEngine到其他框架的转换器","archived":false,"fork":false,"pushed_at":"2023-04-27T05:16:03.000Z","size":44164,"stargazers_count":69,"open_issues_count":7,"forks_count":16,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-09T00:47:36.833Z","etag":null,"topics":["megengine"],"latest_commit_sha":null,"homepage":"https://megengine.org.cn/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MegEngine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-11-12T10:18:14.000Z","updated_at":"2024-12-26T07:39:21.000Z","dependencies_parsed_at":"2024-01-18T14:41:46.384Z","dependency_job_id":"95dbdd91-62d1-4f1d-bbb7-bcf8296e4163","html_url":"https://github.com/MegEngine/mgeconvert","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MegEngine%2Fmgeconvert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MegEngine%2Fmgeconvert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MegEngine%2Fmgeconvert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MegEngine%2Fmgeconvert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MegEngine","download_url":"https://codeload.github.com/MegEngine/mgeconvert/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244649731,"owners_count":20487479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["megengine"],"created_at":"2024-07-31T03:01:58.258Z","updated_at":"2025-03-20T16:30:48.564Z","avatar_url":"https://github.com/MegEngine.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# MgeConvert\n\nMgeConvert 是适用于 [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)转换为第三方模型文件。\n\n目前支持转换的第三方框架有 [Caffe](https://github.com/BVLC/caffe)、[ONNX](https://github.com/onnx/onnx) 和 [TFLite](https://www.tensorflow.org/lite/guide)，支持的模型包括 ResNet、ResNext、ShuffleNet 等，如果需要适配其他模型, 可能需要添加更多的算子支持。\n\n目前，MgeConvert 亦支持将 ONNX 模型转换到 mge/TracedModule 模型。\n\n![mgeconvert](https://user-images.githubusercontent.com/15715998/174760137-1252ae3c-4be0-4cdd-9314-ab94edd7e439.png)\n\nMgeConvert转换器的结构包含前端、中间表示（IR）、后端三个部分：\n1. 前端的部分位于 `frontend` 目录下, 支持 mge 和 traced module 模型格式，可以将 MegEngine 序列化出来的计算图转为IR图结构\n2. IR部分位于 `converter_ir`目录下，包含图和 IR 算子定义、对计算图做变换的 transform rules 以及对量化模型处理的量化器\n3. 后端的部分位于 `backend` 目录下，包含caffe、onnx、tflite的转换器，可以将IR图结构转换为第三方框架的模型文件\n\n- [MgeConvert](#mgeconvert)\n  - [Feature 支持说明](#feature-支持说明)\n  - [依赖说明](#依赖说明)\n  - [安装方式](#安装方式)\n    - [pip 安装(推荐)](#pip-安装推荐)\n    - [源代码安装](#源代码安装)\n  - [使用方式](#使用方式)\n    - [1. 命令行使用](#1-命令行使用)\n    - [1.1 :sparkles: caffe模型转换](#11-sparkles-caffe模型转换)\n      - [1.1.1 float模型转换](#111-float模型转换)\n      - [1.1.2 QAT模型转换](#112-qat模型转换)\n    - [1.2 :sparkles: tflite模型转换](#12-sparkles-tflite模型转换)\n      - [1.2.1 float模型转换](#121-float模型转换)\n      - [1.2.2 QAT模型转换](#122-qat模型转换)\n    - [1.3 :sparkles: onnx模型互转](#13-sparkles-onnx模型互转)\n    - [2. python接口使用](#2-python接口使用)\n  - [FAQ 常见问题说明](#faq-常见问题说明)\n  - [算子支持列表](#算子支持列表)\n\n## Feature 支持说明\n\n- :white_check_mark: 已支持，并完成测试\n- :memo: 未支持，或尚未测试完全\n- :boom: 明确不支持\n\n| TracedModule        | tflite             | caffe              | onnx               |torchscript|\n|---------------------|--------------------|--------------------|--------------------|-----------|\n| QAT                 | :white_check_mark: | :white_check_mark: | :memo:             |:white_check_mark:|\n| Quantized           | :white_check_mark: | :boom:             | :memo:             | :boom: |\n| Float32             | :white_check_mark: | :white_check_mark: | :white_check_mark: |:white_check_mark:|\n\n| Mge                 | tflite             | caffe              | onnx               |\n|---------------------|--------------------|--------------------|--------------------|\n| QAT                 | :boom:             | :boom:             | :boom:             |\n| Quantized           | :memo:             | :boom:             | :memo:             |\n| Float32             | :white_check_mark: | :white_check_mark: | :white_check_mark: |\n\n\n## 依赖说明\n\nMgeConvert 基于 MegEngine 工作，因此确保您的电脑已经安装 MegEngine(\u003e=1.0)。\n\n1. caffe\n\n - Python packages: protobuf\u003e=3.11.0\n\n2. onnx\n\n - Python packages: protobuf, onnx\u003e=1.8.0, onnxruntime, onnxoptimizer==0.2.7, onnx-simplifier==0.3.6\n\n3. tflite\n\n - Python packages: pybind11==2.6.2\n - third party: [flatbuffers](https://github.com/google/flatbuffers.git)==1.12.0\n\n4. torchscript\n\n - torch \u003e= 1.10\n\n\u003e :warning: 安装时以上依赖覆盖本地版本\n\n\n## 安装方式\n\n\u003e 如果安装过0.5.0及之前版本的mgeconvert，重新安装前请先使用sudo权限卸载旧版本：\n\u003e \n\u003e ```bash\n\u003e sudo pip3 uninstall mgeconvert\n\u003e ```\n\n### pip 安装(推荐)\nmgeconvert v1.0.0 开始支持源码包安装:\n\n- 以 caffe 为例，下面这条指令将安装caffe 转换器并处理相关依赖：\n\n```bash\npip3 install mgeconvert --user --install-option=\"--targets=caffe\"\n```\n\n\u003e ``--targets`` 的可选值有 caffe、onnx、tflite 和 all。 `all` 代表安装全部转换器。可选值支持组合传入，比如 ``--targets=caffe,tflite`` 。\n\n\u003e ``tflite`` 转换器的schema默认使用r2.3版本，支持使用参数 ``tfversion`` 选择tflite schema的版本, 比如 ``--install-option=\"--targets=tflite --tfversion=r2.4\"``\n\n\n### 源代码安装\n\n安装选项说明同上，以 caffe 为例，下面的命令将安装0.5.0版本的caffe转换器：\n\n```bash\ngit clone https://github.com/MegEngine/mgeconvert.git@v0.5.0\ncd mgeconvert\npip3 install . --user --install-option=\"--targets=caffe\"\n```\n\n\u003e :warning: 如果需要转换``TracedModule``模型，请安装0.5.0及以上版本\n\n\n## 使用方式\n\n转换器按输入模型格式主要分为两种：\n1. 使用megengine jit.trace [dump](https://megengine.org.cn/doc/stable/zh/user-guide/model-development/jit/dump.html#dump) 出来的序列化模型，这类模型的转换器以 `mge_to` 命名\n2. [TracedModule](https://megengine.org.cn/doc/stable/zh/user-guide/deployment/traced_module/index.html#) 导出的序列化模型，这类模型的转换器以 `tracedmodule_to` 命名\n\n### 1. 命令行使用\n\n执行脚本位于 ``~/.local/bin`` 文件夹内，使用前需要将此路径加入到环境变量 ``PATH`` 中。\n\n命令行支持命令补全，执行 `convert --init` 即可使用。\n\n查询支持的转换框架，结果取决于安装时的 ``--install-option``：\n\n```bash\nconvert -h\n```\n\n以 mge模型转 caffe 为例，查询转换参数：\n\n```bash\nconvert mge_to_caffe -h\n```\n\n### 1.1 :sparkles: caffe模型转换\n\n#### 1.1.1 float模型转换\n\n- 转换mge模型的参考命令：\n\n```bash\nconvert mge_to_caffe -i model.mge -c out.prototxt -b out.caffemodel\n```\n\n- 转换 TracedModule 模型的参考命令：\n\n```bash\nconvert tracedmodule_to_caffe -i model.tm -c out.prototxt -b out.caffemodel\n```\n\n#### 1.1.2 QAT模型转换\nmgeconvert 支持将 QAT TracedModule 模型转换到caffe：\n- QAT模型转caffe默认会导出量化参数文件，通过 `quantize_file_path` 指定量化参数文件路径：\n\n```bash\nconvert tracedmodule_to_caffe -i qat_model.tm -c out.prototxt -b out.caffemodel --quantize_file_path quant_params.json\n```\n\n- 添加 `param_fake_quant` 参数可选择对模型参数进行假量化：\n\n```bash\nconvert tracedmodule_to_caffe -i qat_model.tm -c out.prototxt -b out.caffemodel --quantize_file_path quant_params.json --param_fake_quant\n```\n\n- 如果QAT模型中没有QuantStub对输入数据进行量化处理，可以用 **`--input_data_type --input_scales --input_zero_points`** 在转换时指定输入数据的量化类型、scale和zero_point量化参数，如果有多个scale、zero point用逗号隔开 ：\n\n```bash\nconvert 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\n```\n\n\n### 1.2 :sparkles: tflite模型转换\n\nTFlite转换器支持 float32 和量化的 TracedModule 转换。\n\n#### 1.2.1 float模型转换\n\n转换float模型的命令参考：\n\n```bash\nconvert mge_to_tflite -i model.mge -o out.tflite\n```\n\n```bash\nconvert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite\n```\n\n#### 1.2.2 QAT模型转换\n\n- 对于QAT模型，可以通过添加tracedmodule_to_tflite转换器中的 `require_quantize` 选项，转换出tflite支持的量化数据类型（int8/uint8/int16/int32）量化后的Quantized 模型:\n\n```bash\nconvert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --require_quantize\n```\n\n也可不设置 **`--require_quantize`** 选项，转换出float32模型和量化参数文件。\n\n```bash\nconvert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --quantize_file_path quant_params.json\n```\n\n- 对于QAT模型，还可以通过设置 **`--param_fake_quant`** 参数来选择是否对参数进行假量化。\n\n```bash\nconvert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --quantize_file_path quant_params.json --param_fake_quant\n```\n\n- 如果QAT模型中没有QuantStub对输入数据进行量化处理，可以用 **`--input_data_type --input_scales --input_zero_points`** 在转换时指定输入数据的量化类型、scale和zero_point量化参数，如果有多个scale、zero point用逗号隔开：\n\n```bash\nconvert 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\n```\n\n### 1.3 :sparkles: onnx模型互转\n\nmgeconvert 转 onnx 模型支持 [opset](https://github.com/onnx/onnx/blob/master/docs/Operators.md) 7~12 的转换。\nonnx 转 mge/TracedModule 对各时期的 opset 变更均进行了适配，理论上没有 opset 的限制。\n\n目前只支持float模型的互转，转换命令参考：\n\n```bash\nconvert mge_to_onnx -i model.mge -o out.onnx\n```\n\n```bash\nconvert tracedmodule_to_onnx -i tracedmodule.tm -o out.onnx\n```\n\n```bash\nconvert onnx_to_mge -i model.onnx -o out.mge\n```\n\n```bash\nconvert onnx_to_tracedmodule -i tracedmodule.onnx -o out.tm\n```\n\n### 2. python接口使用\n\n可参考[wiki](https://github.com/MegEngine/mgeconvert/wiki/Mgeconvert-Python-Api-Doc)中的例子。\n\n## FAQ 常见问题说明\n\n1. 安装时出现类似报错：\n\n```\nerror removing /home/user/.local/lib/python3.6/site-packages/mgeconvert-0.5.0-py3.6.egg-info: \n[Errno 13] Permission denied: '/home/user/.local/lib/python3.6/site-packages/mgeconvert-0.5.0-py3.6.egg-info/PKG-INFO'\n```\n\n这是使用sudo安装过旧版本出现的权限问题，先卸载旧版本再安装：\n\n\u003e ```bash\n\u003e sudo pip3 uninstall mgeconvert\n\u003e ```\n\n2. 使用`tflite`转换器时`fbconverter.so`出现 `undefined symbol`错误：\n\n```\nImportError: /home//lib/python3.6/site-packages/mgeconvert/backend/ir_to_tflite/pyflexbuffers/fbconverter.so: undefined symbol: _ZN11flatbuffers13ClassicLocale9instance_E\n```\n这是链接的`libflatbuffers.so`版本和依赖版本不一致导致的问题，执行以下命令使用`mgeconvert`编译的`libflatbuffers.so` ：\n\n\u003e ```bash\n\u003e export LD_LIBRARY_PATH=$MGECONVERT_PATH/backend/ir_to_tflite/pyflexbuffers/lib:$LD_LIBRARY_PATH\n\u003e```\n\n## 算子支持列表\n\n### tracedmodule -\u003e {tflite, caffe, onnx}\n\n| tracedmodule:rocket:\u003cbr/\u003emgo:fire:      | TFLite  | Caffe   | ONNX    |\n|--------------------------|---------|---------|---------|\n| abs                      | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| average pool2d           | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| batchnorm                | ×\u003cbr/\u003e× | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| broadcast                | ×\u003cbr/\u003e× | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| ceil                     | ×\u003cbr/\u003e× | ×\u003cbr/\u003e× | ✓\u003cbr/\u003e✓ |\n| concat                   | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| conv2d                   | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| convtranspose2d          | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| div(true_div)            | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| exp                      | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| elemwise max             | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| floor                    | ×\u003cbr/\u003e× | ×\u003cbr/\u003e× | ✓\u003cbr/\u003e✓ |\n| log                      | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| matrix mul               | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| max pool2d               | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| mul                      | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| pad                      | ✓\u003cbr/\u003e× | ×\u003cbr/\u003e× | ×\u003cbr/\u003e× |\n| pow                      | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| reduce max               | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| reduce min               | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| reduce mean              | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| reduce sum               | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| relu                     | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| relu6                    | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| reshape                  | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| resize                   | ✓\u003cbr/\u003e✓ | ×\u003cbr/\u003e× | ✓\u003cbr/\u003e✓ |\n| sigmoid(logistic)        | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| softmax                  | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| leaky_relu               | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| sub                      | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| slice(subtensor)         | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| squeeze(axis_add_remove) | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| tanh                     | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| typecvt                  | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| transpose(dimshuffle)    | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| AdaptiveAvgPool2d        | ×\u003cbr/\u003e× | ✓\u003cbr/\u003e✓ | ✓\u003cbr/\u003e✓ |\n| flatten                  | ×\u003cbr/\u003e× | ×\u003cbr/\u003e× | ✓\u003cbr/\u003e✓ |\n\n### onnx -\u003e {tracedmodule, mge}\n\n| ONNX                     | tracedmodule:rocket:\u003cbr/\u003emge:fire:  |\n|--------------------------|---------|\n| Abs                      | ✓\u003cbr/\u003e✓ |\n| AveragePool              | ✓\u003cbr/\u003e✓ |\n| Cast                     | ✓\u003cbr/\u003e✓ |\n| Conv                     | ✓\u003cbr/\u003e✓ |\n| Clip                     | ✓\u003cbr/\u003e✓ |\n| Concat                   | ✓\u003cbr/\u003e✓ |\n| Div                      | ✓\u003cbr/\u003e✓ |\n| Dropout                  | ✓\u003cbr/\u003e✓ |\n| Flatten                  | ✓\u003cbr/\u003e✓ |\n| Gather                   | ✓\u003cbr/\u003e✓ |\n| Gemm                     | ✓\u003cbr/\u003e✓ |\n| GlobalAveragePool        | ✓\u003cbr/\u003e✓ |\n| GlobalMaxPool            | ✓\u003cbr/\u003e✓ |\n| Hardsigmoid              | ✓\u003cbr/\u003e✓ |\n| LSTM                     | ✓\u003cbr/\u003e✓ |\n| MaxPool                  | ✓\u003cbr/\u003e✓ |\n| Mul                      | ✓\u003cbr/\u003e✓ |\n| Pow                      | ✓\u003cbr/\u003e✓ |\n| Reduce                   | ✓\u003cbr/\u003e✓ |\n| Relu                     | ✓\u003cbr/\u003e✓ |\n| Reshape                  | ✓\u003cbr/\u003e✓ |\n| Resize                   | ✓\u003cbr/\u003e✓ |\n| Shape                    | ✓\u003cbr/\u003e✓ |\n| Sigmoid                  | ✓\u003cbr/\u003e✓ |\n| Slice                    | ✓\u003cbr/\u003e✓ |\n| Softmax                  | ✓\u003cbr/\u003e✓ |\n| Sqrt                     | ✓\u003cbr/\u003e✓ |\n| Sub                      | ✓\u003cbr/\u003e✓ |\n| Transpose                | ✓\u003cbr/\u003e✓ |\n| Unsqueeze                | ✓\u003cbr/\u003e✓ |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMegEngine%2Fmgeconvert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMegEngine%2Fmgeconvert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMegEngine%2Fmgeconvert/lists"}