我的目的是,将Pytorch框架下训练的模型文件.pth转换成oonx,再转换成OpenVINO C++能读取的bin和xml文件。
import torch
model_path = "../Model/MyMobileNetV3_220_1.0.pth" # 模型参数路径
dummy_input = torch.randn(1, 1, 224, 224) # 先随机一个模型输入的数据
model = torch.load(model_path, 'cpu') # 导入模型参数
torch.onnx.export(model, dummy_input, "MobileNetV3.onnx", verbose=True) # 将模型保存成.onnx格式
OpenVINO我安装的是 openvino_2021.3.394 版本,这里不讲解如何安装。
Anaconda是为了方便安装转换脚本所需的环境, 这里不讲解如何安装。
找到 mo_onnx.py文件,如果没修改OpenVINO的安装路径,那么应该在C:\Program Files (x86)\Intel\openvino_2021.3.394\deployment_tools\model_optimizer 下面。可以使用 Everything 去找一下。
创建虚拟环境
conda create -n Sagittal python=3.8
-n 后面添加虚拟环境名。
python 一定要是 3.8 版本,3.9会报下面的错误:
conda activate Sagittal
python mo_onnx.py --input_model C:\Users\ashui\Desktop\model_best.onnx --output_dir C:\Users\ashui\Desktop
--input_model 后面带的是 onnx 文件的绝对路径
--output_model 后面带的是bin和xml文件的生成路径
接下来会报缺少一些列的module,也就是python library,只需要逐一安装就好了。我这边缺少的有:
numpy
networkx
defusedxml
onnx
新建一个C++控制台应用程序,我的项目名为 AirQualityDev,项目配置为 x64, Release。注意下面的OpenCV和OpenVINO也是需要相同的配置才可以用。
项目文件布局如下:
AirQualityDev
---- AirQualityDev.sln
---- AirQualityDev
---- main.cpp
---- VR
---- OpenCV
---- include
---- opencv2
---- lib
---- bin
---- OpenVINO
---- include
---- lib
---- bin
---- Model
---- model_best.bin
---- model_best.xml
设置项目配置
项目属性 -> 配置属性 -> C/C++ -> 常规 -> 附加包含目录,添加
$(ProjectDir)VR\OpenCV\include\
$(ProjectDir)VR\OpenCV\include\opencv2
$(ProjectDir)VR\OpenVINO\include\
项目属性 -> 配置属性 -> 链接器 -> 常规 -> 附加库目录,添加
$(ProjectDir)VR\OpenCV\lib
$(ProjectDir)VR\OpenVINO\lib
项目属性 -> 配置属性 -> 链接器 -> 输入-> 附加依赖项,添加
opencv_core452.lib
opencv_imgcodecs452.lib
opencv_imgproc452.lib
opencv_videoio452.lib
opencv_highgui452.lib
inference_engine.lib
注意都是x64,release版本。
项目编译,没问题就可以继续了。
模型说明:
输入:224 * 224 * 1(灰度图)
输出:1(float)
具体代码
// AutoSagittal.h
#pragma once
#include
#include
#include
#include "../UVFReading/ScanFileStructure.h"
#define CLASS_DECLSPEC __declspec(dllexport)
using namespace std;
namespace AI_Module
{
class CLASS_DECLSPEC AutoSagittalByDL {
public:
bool Initialize(const std::string exePath, std::string& error);
bool Run(CScanImage* pScanImage, std::vector<int>& vSagittalDepth, std::string& error);
void Destroy();
private:
InferenceEngine::CNNNetwork model;
InferenceEngine::InferRequest infer_request;
InferenceEngine::ExecutableNetwork executable_network;
InferenceEngine::Core ie;
private:
int modelInputWidth = 224;
int modelInputHeight = 224;
};
}
// AutoSagittal.cpp
#include "pch.h"
#include "AutoSagittalByDL.h"
#include
bool AI_Module::AutoSagittalByDL::Initialize(const std::string exePath, std::string& error)
{
std::string xmlPath = exePath + ("\\MobileNetV3.xml");
std::string binPath = exePath + ("\\MobileNetV3.bin");
try
{
model = ie.ReadNetwork(xmlPath, binPath);
//load model to device
executable_network = ie.LoadNetwork(model, "CPU");
//create infer request
infer_request = executable_network.CreateInferRequest();
// Get input image size
std::string input_name = (model.getInputsInfo().begin())->first;
InferenceEngine::Blob::Ptr input = infer_request.GetBlob(input_name);
modelInputWidth = input->getTensorDesc().getDims()[2];
modelInputHeight = input->getTensorDesc().getDims()[3];
//prepare input blobs
InferenceEngine::InputInfo::Ptr input_info = model.getInputsInfo().begin()->second;
input_info->setLayout(InferenceEngine::Layout::NCHW);
//input_info->setPrecision(InferenceEngine::Precision::FP32);
prepare output blobs
//InferenceEngine::DataPtr output_info = model.getOutputsInfo().begin()->second;
//std::string output_name = model.getOutputsInfo().begin()->first;
//output_info->setPrecision(InferenceEngine::Precision::FP32);
}
catch (InferenceEngine::Exception e)
{
error = e.what();
return false;
}
catch (...)
{
error = "Lamina Match model init unknown error!";
return false;
}
return true;
}
bool AI_Module::AutoSagittalByDL::Run(CScanImage* pScanImage, std::vector<int>& vSagittalDepth, std::string& error)
{
try
{
clock_t startTime = clock();
clock_t span;
if (pScanImage)
{
// Clear the depth vector
vSagittalDepth.clear();
// Get image count
int iImageNum = pScanImage->m_iIndex;
// Get source image size
int iBmpWidth = pScanImage->m_iSliceWidth;
int iBmpHeight = pScanImage->m_iSliceHeight;
for (int i = 0; i < iImageNum; ++i)
{
// Get the current B-mode image
auto const bImage = pScanImage->m_vFrame[i];
if (!bImage)
{
ostringstream errorStream;
errorStream << "Could not get the bmode image, index = " << i << std::endl;
error = errorStream.str();
return false;
}
// Resize image to input size
cv::Mat image(cv::Size(iBmpWidth, iBmpHeight), CV_8UC1, bImage);
resize(image, image, cv::Size(modelInputWidth, modelInputHeight));
std::string input_name = model.getInputsInfo().begin()->first;
InferenceEngine::Blob::Ptr input = infer_request.GetBlob(input_name);
auto input_image_ptr = input->buffer().as<InferenceEngine::PrecisionTrait<InferenceEngine::Precision::FP32>::value_type*>();
// Set the input image to the model
for (size_t pid = 0, image_size = modelInputWidth * modelInputHeight; pid < image_size; ++pid)
{
input_image_ptr[pid] = ((float)image.at<uchar>(pid) / 255.0f - 0.254f) / 0.295;
}
// Model inference
infer_request.Infer();
// Get model result
std::string output_name = model.getOutputsInfo().begin()->first;
InferenceEngine::Blob::Ptr output = infer_request.GetBlob(output_name);
auto output_ratio = output->buffer().as<InferenceEngine::PrecisionTrait<InferenceEngine::Precision::FP32>::value_type*>();
vSagittalDepth.push_back(iBmpWidth * output_ratio[0]);
}
span = clock() - startTime;
printf("span = %d ms, ", span);
return true;
}
else
{
error = "CScanImage pointer is empty\n";
return false;
}
}
catch (InferenceEngine::Exception e)
{
error = e.what();
return false;
}
catch (...)
{
error = "Lamina Match model init unknown error!";
return false;
}
return true;
}
void AI_Module::AutoSagittalByDL::Destroy()
{
}
主要关注的是 bool AI_Module::AutoSagittalByDL::Initialize()
和 bool AI_Module::AutoSagittalByDL::Run()
中从 cv::Mat image(cv::Size(iBmpWidth, iBmpHeight), CV_8UC1, bImage);
开始的部分。关于CScanImage class 是我自己的class,里面存储了我想要预测的图像,我需要先读到图像,再resize到224 * 224 *1。你们要用的话可以删掉这部分,直接用 cv::imread() 从本地读取图像也行。
到此,结束!有问题评论区随时问我,希望能够帮到大家!