0
点赞
收藏
分享

微信扫一扫

Windows 10 下 torch模型转换为 OpenVINO需要的IR文件

cnlinkchina 2022-06-01 阅读 45

OpenVINO模型优化器参考:​​https://docs.openvinotoolkit.org/2021.2/openvino_docs_MO_DG_Deep_Learning_Model_Optimizer_DevGuide.html​​

配置环境

  • Windows 10
  • Anaconda 2.0.4
  • Pycharm 2021.1
  • OpenVINO 工具包 2021.2
  • Python 环境 3.6.13
  • torch 1.9.0
  • onnx 1.10.1

准备好torch模型
Windows 10 下 torch模型转换为 OpenVINO需要的IR文件_2d

自定义神经网络模型pth转ONNX代码

import torch
from torch import nn

# 搭建神经网络
class VVcatModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.model = nn.Sequential(
            nn.Conv2d(3, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(64 * 4 * 4, 64),
            nn.Linear(64, 10)
        )

    def forward(self, x):
        x = self.model(x)
        return x

model = torch.load("./vvcat_model.pth", map_location=torch.device('cuda'))  # 加载模型
model.eval()
model.cuda()
dummy_input = torch.randn(1, 3, 32, 32, device='cuda')  # 注意指定输入尺寸
torch.onnx.export(model, dummy_input, "vvcat_model.onnx",  output_names={"output"}, verbose=True)

Windows 10 下 torch模型转换为 OpenVINO需要的IR文件_xml_02
通过执行代码生成了 .onnx 文件。

转IR格式

使用 OpenVINO 工具包提供的 ​​mo_onnx.py​​​文件,对模型进行转换。
Windows 10 下 torch模型转换为 OpenVINO需要的IR文件_2d_03
管理员模式打开Anaconda,启动虚拟环境的终端,cd进openVINO转换工具目录,执行转换代码:

cd C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer

python mo_onnx.py --input_model M:\python\深度学习\OpenVINO\15.模型转换器\Main\vvcat_model.onnx

转换成功后,在转换工具 model_optimizer 目录下生成了bin和xml文件,然后就可以用 OpenVINO部署了。

  • .xml - 描述网络拓扑
  • .bin - 包含权重和偏差二进制数据。

(pytorch) C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer>python mo_onnx.py --input_model M:\python\深度学习\OpenVINO\15.模型转换器\Main\vvcat_model.onnx
Model Optimizer arguments:
Common parameters:
        - Path to the Input Model:      M:\python\深度学习\OpenVINO\15.模型转换器\Main\vvcat_model.onnx
        - Path for generated IR:        C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer\.
        - IR output name:       vvcat_model
        - Log level:    ERROR
        - Batch:        Not specified, inherited from the model
        - Input layers:         Not specified, inherited from the model
        - Output layers:        Not specified, inherited from the model
        - Input shapes:         Not specified, inherited from the model
        - Mean values:  Not specified
        - Scale values:         Not specified
        - Scale factor:         Not specified
        - Precision of IR:      FP32
        - Enable fusing:        True
        - Enable grouped convolutions fusing:   True
        - Move mean values to preprocess section:       None
        - Reverse input channels:       False
ONNX specific parameters:
Model Optimizer version:        2021.2.0-1877-176bdf51370-releases/2021/2

[ SUCCESS ] Generated IR version 10 model.
[ SUCCESS ] XML file: C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer\.\vvcat_model.xml
[ SUCCESS ] BIN file: C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer\.\vvcat_model.bin
[ SUCCESS ] Total execution time: 2.72 seconds.
It's been a while, check for a new version of Intel(R) Distribution of OpenVINO(TM) toolkit here https://software.intel.com/content/www/us/en/develop/tools/openvino-toolkit/choose-download.html?cid=other&source=Prod&campid=ww_2021_bu_IOTG&content=upg_pro&medium=organic_uid_agjj or on the GitHub*

以下为生成的为我们需要的文件。
Windows 10 下 torch模型转换为 OpenVINO需要的IR文件_python_04

踩坑一:

没有安装 onnx、onnxruntime库导致的错误

[ ERROR ]
Detected not satisfied dependencies:
        onnx: not installed, required: >= 1.1.2

Please install required versions of components or use install_prerequisites script
C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer\install_prerequisites\install_prerequisites_onnx.bat
Note that install_prerequisites scripts may install additional components.
[ ERROR ]  check_requirements exit with return code 1

安装 onnx、onnxruntime库即可解决

pip install onnx

pip install onnxruntime

Windows 10 下 torch模型转换为 OpenVINO需要的IR文件_xml_05

Windows 10 下 torch模型转换为 OpenVINO需要的IR文件_2d_06

踩坑二:

因为权限问题,导致无法转换IR文件。
Windows 10 下 torch模型转换为 OpenVINO需要的IR文件_xml_07
解决方法:管理员模式打开Anaconda,启动虚拟环境的终端,cd进openVINO转换工具目录,再执行转换代码
Windows 10 下 torch模型转换为 OpenVINO需要的IR文件_xml_08
Windows 10 下 torch模型转换为 OpenVINO需要的IR文件_2d_09

Windows 10 下 torch模型转换为 OpenVINO需要的IR文件_xml_10

举报

相关推荐

0 条评论