pytorch转onnx的c++调用

5 篇文章 0 订阅
5 篇文章 0 订阅

搞了两天,记录一下学习过程。

pytorch模型转为onnx:

Pytorch网络模型转Onnx格式,多种方法(opencv、onnxruntime、c++)调用Onnx_努力的袁的博客-CSDN博客_c++调用onnx模型

c++使用onnxruntime或者opencv调用onnx模型进行推理:

C++使用onnxruntime/opencv对onnx模型进行推理(附代码)_努力的袁的博客-CSDN博客_c++调用onnx模型

记录一下遇到的坑:

1、opencv安装

2、cmake安装 

3、onnxruntime编译,参考:

ONNXRuntime C++ CMake 工程编译及安装_qq_1041357701的博客-CSDN博客

onnxruntime (C++/CUDA) 编译安装及部署_白色小靴的博客-CSDN博客

注意编译的时候采用:

./build.sh --skip_tests --use_cuda --config Release --build_shared_lib --parallel --cuda_home /usr/local/cuda --cudnn_home /usr/local/cuda

4、编译自己的代码

最后在ubuntu编译的时候加上链接符号(黑体字),在vs里面非常智能都给你做好了,Linux就得靠自己去加链接符号:

g++ models_dog.cpp -I/usr/local/include/opencv4 -I/usr/local/include/onnxruntime/core/session/  -L/usr/local/lib  -lopencv_stitching -lopencv_objdetect  -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core -lonnxruntime -lopencv_dnn

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ONNX (Open Neural Network Exchange) is an open standard for representing machine learning models that enables models to be trained in one framework and transferred to another for inference. C is a high-level programming language that is widely used for developing various applications. There is no specific "ONNX C demo", but you can find examples of how to use ONNX in C-based projects. One approach would be to use a library such as ONNX Runtime, which provides an inference engine for ONNX models. You can find some examples and tutorials on the ONNX Runtime GitHub repository, including a "getting started" guide and code samples in C. Keep in mind that while C is a powerful language, it may not be the best choice for developing complex machine learning models. More commonly, machine learning models are developed in Python using popular libraries like TensorFlow, PyTorch, or Keras, and then exported to ONNX for deployment to production environments. ### 回答2: ONNX C demo 是一个演示用于使用ONNX模型的C语言代码示例。ONNX是一种开放的模型格式,使用ONNX模型可以在不同的深度学习框架之间传递和使用模型,比如TensorFlow、PyTorch等。这个demo提供了一个使用ONNX模型的C语言实现的示例,可以帮助开发者理解和学习如何在C语言中加载和使用ONNX模型。 ONNX C demo 主要包括以下几个部分: 1. 模型加载:首先,我们需要加载ONNX模型。通过使用ONNX Runtime库中的函数,我们可以从磁盘上的ONNX文件中加载模型,并将其存储在内存中。这样,我们就可以在C语言代码中使用这个模型了。 2. 输入数据:然后,我们需要准备输入数据。通过使用ONNX Runtime库中的函数,我们可以创建一个输入张量,并将需要的输入数据写入到这个张量中。 3. 模型推断:接下来,我们可以使用ONNX Runtime库中的函数进行模型推断。通过将加载的模型和输入数据传递给这些函数,我们可以获得模型的输出结果。 4. 输出结果:最后,我们可以获取模型的输出结果,并将其在C语言代码中使用或展示。 通过这个demo,我们可以学习如何使用ONNX C库进行模型的加载、输入数据的准备、模型推断和结果获取。这对于开发者来说将是一个很有帮助的学习工具,特别是对于那些使用C语言开发深度学习应用的人来说。 ### 回答3: ONNX (Open Neural Network Exchange) 是一个开放、跨平台的神经网络模型交换格式。ONNX C Demo 是一个使用 ONNX 进行推断的示例程序。 ONNX C Demo可以用于加载、解析和运行 ONNX 模型。它提供了一个接口,可以接收输入数据,传递给 ONNX 模型进行推断,并返回模型的输出结果。 在使用 ONNX C Demo 进行推断之前,需要先将训练好的模型换为 ONNX 格式。换后的模型包含了网络的结构和权重参数。接下来,可以使用 ONNX C Demo 加载该模型,并将待推断的数据传递给模型。 ONNX C Demo 通过调用 ONNX Runtime 的 API 来实现推断过程。ONNX Runtime 是一个高性能的推断引擎,支持多种硬件平台和操作系统。它可以根据不同的硬件配置和算法优化,自动选择最优的推断方式,提供快速和准确的推断结果。 ONNX C Demo 还可以用于对 ONNX 模型进行性能评测和优化。通过分析模型的推断耗时和资源占用情况,可以针对性地对模型进行调优,以提升推断的速度和效率。 总之,ONNX C Demo 提供了一个简单而强大的工具,可以方便地加载和运行 ONNX 模型,实现神经网络的推断功能。无论是对于开发者还是研究人员来说,都是一个非常有用的工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值