openvino 将onnx转为IR并进行int8量化

环境

- Ubuntu 22.04
- python 3.10

安装环境

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-venv build-essential python3-dev git-all -y
sudo apt-get install intel-opencl-icd -y

编译 mo

下载 openvino

git clone https://github.com/openvinotoolkit/openvino

编译 mo

cd openvino/tools
python3 -m pip install mo

编译成功输出如下信息:

Collecting mo
  Downloading mo-0.3.0-py2.py3-none-any.whl (12 kB)
Requirement already satisfied: PyYAML in /usr/local/lib/python3.10/dist-packages (from mo) (6.0)
Collecting colorama (from mo)
  Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Requirement already satisfied: toml in /usr/local/lib/python3.10/dist-packages (from mo) (0.10.2)
Installing collected packages: colorama, mo
Successfully installed colorama-0.4.6 mo-0.3.0

安装

pip install openvino-dev

查看帮助

mo -h

onnx 转为 IR

mo --input_model onnx/model.onnx --compress_to_fp16 --output_dir ir_model

这里压缩为fp16

输出信息:

[ INFO ] Generated IR will be compressed to FP16. If you get lower accuracy, please consider disabling compression explicitly by adding argument --compress_to_fp16=False.
Find more information about compression to FP16 at https://docs.openvino.ai/2023.0/openvino_docs_MO_DG_FP16_Compression.html
[ INFO ] The model was converted to IR v11, the latest model format that corresponds to the source DL framework input/output format. While IR v11 is backwards compatible with OpenVINO Inference Engine API v1.0, please use API v2.0 (as of 2022.1) to take advantage of the latest improvements in IR v11.
Find more information about API v2.0 and IR v11 at https://docs.openvino.ai/2023.0/openvino_2_0_transition_guide.html
[ SUCCESS ] Generated IR version 11 model.
[ SUCCESS ] XML file: /workspace/bert/ir_model/model.xml
[ SUCCESS ] BIN file: /workspace/bert/ir_model/model.bin

转换成功了!

输出文件对比:

# ls -lh ir_model/
total 321M
-rw-r--r-- 1 root root 319M Sep 22 11:27 model.bin
-rw-r--r-- 1 root root 1.8M Sep 22 11:27 model.xml

# ls -lh onnx/
total 640M
-rw-r--r-- 1 root root 640M Sep 21 20:23 model.onnx

IR 模型量化为 int8

编译 Post-Training Optimization Tool

cd openvino/tools/pot/
python3 setup.py install

bert模型量化步骤参考:
https://github.com/openvinotoolkit/openvino_notebooks/tree/main/notebooks/105-language-quantize-bert

Quantization of Image Classification model参考:
https://github.com/openvinotoolkit/openvino_notebooks/tree/main/notebooks/301-tensorflow-training-openvino

参考

  1. https://github.com/openvinotoolkit/openvino_notebooks#-installation-guide
  2. https://docs.openvino.ai/2022.3/notebooks/102-pytorch-onnx-to-openvino-with-output.html
  3. https://github.com/openvinotoolkit/openvino/tree/master/tools/mo
  4. https://github.com/openvinotoolkit/openvino
  5. https://github.com/openvinotoolkit/openvino/tree/master/tools/pot
### OpenVINO 2022 中 ONNXIR 的转换与最佳实践 OpenVINO 2022 版本提供了强大的工具链来支持 ONNX 模型的导入和优化。通过 `mo.py`(Model Optimizer)脚本,可以轻松实现从 ONNX 格式到 OpenVINO Intermediate Representation (IR) [.xml 和 .bin] 的转换[^1]。 #### 转换流程概述 为了将 ONNX 模型成功转换为 OpenVINO IR 格式,需遵循以下技术细节: - **安装依赖项**:确保已正确配置 OpenVINO 环境完成必要的 Python 库安装。例如,在 Windows 平台上可以通过官方文档指导完成安装过程[^3]。 - **调用 Model Optimizer**:使用命令行接口运行 `mo.py` 脚本来执行转换操作。以下是典型的命令结构: ```bash mo --input_model <path_to_onnx_model> --output_dir <output_directory> ``` 此命令会读取指定路径下的 ONNX 文件,将其转化为 `.xml` 和 `.bin` 文件形式存储于目标目录下[^2]。 - **参数调整**:依据具体应用场景可能需要额外设置一些选项以满足特定需求,比如输入形状定义 (`--input_shape`) 或数据类型精度控制 (`--data_type FP16|FP32`) 等。 #### 示例代码展示 下面给出一段完整的Python脚本用于自动化上述转换步骤: ```python import os from subprocess import run, PIPE def convert_onnx_to_ir(onnx_path, ir_output_dir): command = [ 'mo', '--input_model', onnx_path, '--output_dir', ir_output_dir ] result = run(command, stdout=PIPE, stderr=PIPE) if result.returncode != 0: raise Exception(f"Conversion failed with error {result.stderr.decode()}") if __name__ == "__main__": current_dir = os.getcwd() model_file = os.path.join(current_dir, "model.onnx") output_folder = os.path.join(current_dir, "ir_models") try: convert_onnx_to_ir(model_file, output_folder) print("ONNX to IR conversion completed successfully.") except Exception as e: print(e) ``` 以上脚本封装了一个函数 `convert_onnx_to_ir()` 来简化批量处理多个模型文件的过程。 #### 性能考量与建议 当考虑部署效率时,推荐采用较低精度的数据表示方式如 INT8 或者混合量化策略,这样可以在保持较高准确性的同时显著减少计算资源消耗以及加速推理速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szZack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值