[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;
}

运行结果:

相关推荐
樱木Plus1 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
0xDevNull1 天前
Linux切换JDK版本详细教程
linux
进击的丸子1 天前
虹软人脸服务器版SDK(Linux/ARM Pro)多线程调用及性能优化
linux·数据库·后端
字节逆旅2 天前
ubuntu应用深度守护
ubuntu
Johny_Zhao3 天前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw
blasit3 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_4 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
chlk1234 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑4 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件4 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux