C++:Opencv读取ONNX模型,通俗易懂

1. 准备 ONNX 模型

假设你已经有一个训练好的 ONNX 模型文件。可以从各类深度学习框架(如 PyTorch、TensorFlow)中导出 ONNX 模型。例如,下面是一个简单的 PyTorch 模型导出为 ONNX 文件的示例:

cpp 复制代码
import torch
import torchvision.models as models

# Load a pre-trained model (e.g., ResNet18)
model = models.resnet18(pretrained=True)
model.eval()

# Dummy input for tracing
dummy_input = torch.randn(1, 3, 224, 224)

# Export the model to ONNX format
torch.onnx.export(model, dummy_input, "resnet18.onnx")

2. 读取 ONNX 模型

在 OpenCV 中,你可以使用 cv::dnn::readNetFromONNX 函数加载 ONNX 模型。

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>

int main() {
    // 模型文件路径
    std::string modelFile = "resnet18.onnx";

    // 从 ONNX 文件中读取模型
    cv::dnn::Net net = cv::dnn::readNetFromONNX(modelFile);

    // 检查模型是否成功加载
    if (net.empty()) {
        std::cerr << "Failed to load network!" << std::endl;
        return -1;
    }

    return 0;
}

3. 预处理输入图像

在进行推理之前,需要将输入图像预处理成模型所需的格式。通常,这包括调整图像大小、归一化等。

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>

int main() {
    // 模型文件路径
    std::string modelFile = "resnet18.onnx";
    cv::dnn::Net net = cv::dnn::readNetFromONNX(modelFile);

    if (net.empty()) {
        std::cerr << "Failed to load network!" << std::endl;
        return -1;
    }

    // 读取输入图像
    cv::Mat img = cv::imread("image.jpg");

    if (img.empty()) {
        std::cerr << "Failed to read image!" << std::endl;
        return -1;
    }

    // 将图像调整为模型所需的大小和格式
    cv::Mat blob = cv::dnn::blobFromImage(img, 1.0, cv::Size(224, 224), cv::Scalar(104.0, 117.0, 123.0), true, false);

    // 设置网络的输入
    net.setInput(blob);

    // 执行前向传播以获得输出
    cv::Mat output = net.forward();

    // 输出处理
    std::cout << "Output size: " << output.size << std::endl;

    return 0;
}

4. 进行推理

在前面的代码中,已经包含了执行推理的步骤。net.forward() 函数会返回模型的输出结果。

5. 处理和显示结果

通常,推理结果需要根据模型的输出进行处理。例如,如果是图像分类模型,你可能需要将输出的向量映射到类别标签

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>

int main() {
    // 模型文件路径
    std::string modelFile = "resnet18.onnx";
    cv::dnn::Net net = cv::dnn::readNetFromONNX(modelFile);

    if (net.empty()) {
        std::cerr << "Failed to load network!" << std::endl;
        return -1;
    }

    // 读取输入图像
    cv::Mat img = cv::imread("image.jpg");

    if (img.empty()) {
        std::cerr << "Failed to read image!" << std::endl;
        return -1;
    }

    // 将图像调整为模型所需的大小和格式
    cv::Mat blob = cv::dnn::blobFromImage(img, 1.0, cv::Size(224, 224), cv::Scalar(104.0, 117.0, 123.0), true, false);

    // 设置网络的输入
    net.setInput(blob);

    // 执行前向传播以获得输出
    cv::Mat output = net.forward();

    // 处理输出
    cv::Point classId;
    double confidence;
    cv::minMaxLoc(output, 0, &confidence, 0, &classId);
    
    std::cout << "Predicted class ID: " << classId.x << std::endl;
    std::cout << "Confidence: " << confidence << std::endl;

    return 0;
}
相关推荐
巴里巴气1 小时前
安装GPU版本的Pytorch
人工智能·pytorch·python
「、皓子~1 小时前
后台管理系统的诞生 - 利用AI 1天完成整个后台管理系统的微服务后端+前端
前端·人工智能·微服务·小程序·go·ai编程·ai写作
说私域2 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的抖音渠道力拓展与多渠道利润增长研究
人工智能·小程序·开源
笑衬人心。2 小时前
初学Spring AI 笔记
人工智能·笔记·spring
luofeiju2 小时前
RGB下的色彩变换:用线性代数解构色彩世界
图像处理·人工智能·opencv·线性代数
测试者家园2 小时前
基于DeepSeek和crewAI构建测试用例脚本生成器
人工智能·python·测试用例·智能体·智能化测试·crewai
张较瘦_2 小时前
[论文阅读] 人工智能 + 软件工程 | Call Me Maybe:用图神经网络增强JavaScript调用图构建
论文阅读·人工智能·软件工程
大模型真好玩2 小时前
准确率飙升!Graph RAG如何利用知识图谱提升RAG答案质量(四)——微软GraphRAG代码实战
人工智能·python·mcp
Baihai_IDP2 小时前
vec2text 技术已开源!一定条件下,文本嵌入向量可“近乎完美地”还原
人工智能·面试·llm
江太翁2 小时前
Pytorch torch
人工智能·pytorch·python