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

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模型
在这里插入图片描述

自定义神经网络模型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)

在这里插入图片描述
通过执行代码生成了 .onnx 文件。

转IR格式

使用 OpenVINO 工具包提供的 mo_onnx.py文件,对模型进行转换。
在这里插入图片描述
管理员模式打开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*

以下为生成的为我们需要的文件。
在这里插入图片描述

踩坑一:

没有安装 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

在这里插入图片描述

在这里插入图片描述

踩坑二:

因为权限问题,导致无法转换IR文件。
在这里插入图片描述
解决方法:管理员模式打开Anaconda,启动虚拟环境的终端,cd进openVINO转换工具目录,再执行转换代码
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值