[ubuntu][C++]onnxruntime安装cpu版本后测试代码

下载官方预编译包后,怎么用呢。可以参考这个源码跑

测试环境:

ubuntu22.04

onnxruntime==1.18.0

测试代码:

CMakeLists.txt

复制代码
cmake_minimum_required(VERSION 3.12)
project(onnx_test)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
SET (ONNXRUNTIME_DIR /home/limrobot/onnxruntime-linux-x64-1.18.0)
# 查找ONNX Runtime
#find_package(ONNXRuntime REQUIRED)

# 添加可执行文件
add_executable(onnx_test main.cpp)

# 链接ONNX Runtime库
target_link_libraries(onnx_test PRIVATE ${ONNXRUNTIME_DIR}/lib/libonnxruntime.so)

# 包含目录
target_include_directories(onnx_test PRIVATE ${ONNXRUNTIME_DIR}/include)

# 复制模型文件到构建目录
configure_file(yolov8n.onnx ${CMAKE_BINARY_DIR}/yolov8n.onnx COPYONLY)

main.cpp

复制代码
#include <onnxruntime_cxx_api.h>
#include <iostream>
#include <vector>
#include <stdexcept>

int main() {
    try {
        // 初始化ONNX Runtime环境
        Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "test");
        Ort::SessionOptions session_options;
        
        // 设置线程数
        session_options.SetIntraOpNumThreads(1);
        session_options.SetInterOpNumThreads(1);
        
        // 加载模型
        Ort::Session session(env, "yolov8n.onnx", session_options);
        
        // 使用Allocator
        Ort::AllocatorWithDefaultOptions allocator;
        
        // 打印输入信息
        size_t num_input_nodes = session.GetInputCount();
        std::cout << "Number of inputs: " << num_input_nodes << std::endl;
        
        for(size_t i = 0; i < num_input_nodes; i++) {
            // 获取输入名称
            auto input_name = session.GetInputNameAllocated(i, allocator);
            auto input_type_info = session.GetInputTypeInfo(i);
            auto input_tensor_info = input_type_info.GetTensorTypeAndShapeInfo();
            
            auto input_dims = input_tensor_info.GetShape();
            std::cout << "Input " << i << " name: " << input_name.get() << std::endl;
            std::cout << "Input shape: ";
            for(auto dim : input_dims) {
                std::cout << dim << " ";
            }
            std::cout << std::endl;
            std::cout << "Input type: " << input_tensor_info.GetElementType() << std::endl;
        }
        
        // 打印输出信息
        size_t num_output_nodes = session.GetOutputCount();
        std::cout << "Number of outputs: " << num_output_nodes << std::endl;
        
        for(size_t i = 0; i < num_output_nodes; i++) {
            // 获取输出名称
            auto output_name = session.GetOutputNameAllocated(i, allocator);
            auto output_type_info = session.GetOutputTypeInfo(i);
            auto output_tensor_info = output_type_info.GetTensorTypeAndShapeInfo();
            
            auto output_dims = output_tensor_info.GetShape();
            std::cout << "Output " << i << " name: " << output_name.get() << std::endl;
            std::cout << "Output shape: ";
            for(auto dim : output_dims) {
                std::cout << dim << " ";
            }
            std::cout << std::endl;
            std::cout << "Output type: " << output_tensor_info.GetElementType() << std::endl;
        }
        
        std::cout << "ONNX Runtime test completed successfully!" << std::endl;
        
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}

运行结果:

相关推荐
冉佳驹5 小时前
C++ ——— 异常处理的核心机制和智能指针管理
c++·异常捕获·异常继承体与多态·重载抛异常·raii思想·智能指针shared_ptr·weak_ptr指针
C++ 老炮儿的技术栈5 小时前
Qt 编写 TcpClient 程序 详细步骤
c语言·开发语言·数据库·c++·qt·算法
陌上花开缓缓归以5 小时前
linux mtd-utils使用源码分析(ubuntu测试版)
linux·arm开发·ubuntu
yuuki2332335 小时前
【C++】继承
开发语言·c++·windows
梵刹古音5 小时前
【C++】 析构函数
开发语言·c++
wangjialelele5 小时前
Linux下的IO操作以及ext系列文件系统
linux·运维·服务器·c语言·c++·个人开发
打工哪有不疯的6 小时前
使用 MSYS2 为 Qt (MinGW 32/64位) 完美配置 OpenSSL
c++·qt
HypoxiaDream6 小时前
LINUX-Ext系列⽂件系统
linux·运维·服务器
小毛驴8506 小时前
Linux curl 命令用法
linux·运维·chrome
李斯啦果6 小时前
【Linux】Linux目录配置
linux·运维·服务器