• Pytorch框架的模型pth文件转换成C++ OpenVINO框架的bin和xml文件并运行


    背景

    我的目的是,将Pytorch框架下训练的模型文件.pth转换成oonx,再转换成OpenVINO C++能读取的bin和xml文件。

    步骤

    1. pth文件转ONNX文件

    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格式
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2. ONNX文件转bin和xml文件

    1)安装OpenVINO和Anaconda

    OpenVINO我安装的是 openvino_2021.3.394 版本,这里不讲解如何安装。
    Anaconda是为了方便安装转换脚本所需的环境, 这里不讲解如何安装。

    2)转换模型

    1. 找到 mo_onnx.py文件,如果没修改OpenVINO的安装路径,那么应该在C:\Program Files (x86)\Intel\openvino_2021.3.394\deployment_tools\model_optimizer 下面。可以使用 Everything 去找一下。

    2. 以管理员身份打开 Anaconda Prompt,cd到上面的mo.py的路径下
      在这里插入图片描述

    3. 创建虚拟环境

    conda create -n Sagittal python=3.8
    
    • 1
    -n 后面添加虚拟环境名。
    
    python 一定要是 3.8 版本,3.9会报下面的错误: 
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    1. 激活虚拟环境
    conda activate Sagittal
    
    • 1
    1. 运行脚本
    python mo_onnx.py --input_model C:\Users\ashui\Desktop\model_best.onnx --output_dir C:\Users\ashui\Desktop
    
    • 1
    --input_model  后面带的是 onnx 文件的绝对路径
    
    --output_model 后面带的是bin和xml文件的生成路径
    
    • 1
    • 2
    • 3
    1. 接下来会报缺少一些列的module,也就是python library,只需要逐一安装就好了。我这边缺少的有:

      numpy
      networkx
      defusedxml
      onnx
      
      • 1
      • 2
      • 3
      • 4
    2. 最后再重新运行步骤5的命令就好了。
      在这里插入图片描述

    3. C++读取xml和bin文件

    1)准备项目

    1. 新建一个C++控制台应用程序,我的项目名为 AirQualityDev,项目配置为 x64, Release。注意下面的OpenCV和OpenVINO也是需要相同的配置才可以用。

    2. 项目文件布局如下:

      AirQualityDev
      	---- AirQualityDev.sln
      	---- AirQualityDev
      		---- main.cpp
      		---- VR
      			---- OpenCV
      				---- include
      					---- opencv2
      				---- lib
      				---- bin
      			---- OpenVINO
      				---- include
      				---- lib
      				---- bin
      			---- Model
      				---- model_best.bin
      				---- model_best.xml
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
    3. 设置项目配置
      项目属性 -> 配置属性 -> C/C++ -> 常规 -> 附加包含目录,添加

    $(ProjectDir)VR\OpenCV\include\
    $(ProjectDir)VR\OpenCV\include\opencv2
    $(ProjectDir)VR\OpenVINO\include\
    
    • 1
    • 2
    • 3

    项目属性 -> 配置属性 -> 链接器 -> 常规 -> 附加库目录,添加

    $(ProjectDir)VR\OpenCV\lib
    $(ProjectDir)VR\OpenVINO\lib
    
    • 1
    • 2

    项目属性 -> 配置属性 -> 链接器 -> 输入-> 附加依赖项,添加

    opencv_core452.lib
    opencv_imgcodecs452.lib
    opencv_imgproc452.lib
    opencv_videoio452.lib
    opencv_highgui452.lib
    inference_engine.lib
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    注意都是x64,release版本。
    项目编译,没问题就可以继续了。

    2) 读取bin和xml文件

    1. 模型说明:

      输入:224 * 224 * 1(灰度图)
      
      输出:1(float)
      
      • 1
      • 2
      • 3
    2. 具体代码

    // 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;
    
        };
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    // 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()
    {
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139

    主要关注的是 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() 从本地读取图像也行。

    到此,结束!有问题评论区随时问我,希望能够帮到大家!

  • 相关阅读:
    是什么让.NET7的Min和Max方法性能暴增了45倍?
    【Python&GIS】基于高德Api实现批量地址查询经纬度
    腾讯云TI平台持续升级,TI-ACC训练加速性能较原生框架提升超30%
    CAD如何使插入的块为分解状态?CAD如何绘制五瓣花?
    jdk的安装及环境配置
    第4季1:将AR0130摄像头更换为OV9712摄像头
    本地运行feishu-chatgpt项目结合内网穿透实现无公网IP远程访问
    彻底明白Java的IO系统
    【TiDB】一些很有意思的sql调优案例分享
    一文详解vue-cli2.0与vue-cli3.0之间的区别
  • 原文地址:https://blog.csdn.net/A_water_/article/details/128197563