window下做onnx->ncnn转换

1.首先做代码->onnx的转换

这个各个框架都有现成的代码直接可以export,建议百度

以pytorch为例

torch.onnx.export(model,               # 你训练的的模型
                  model/param,      # 模型输入(或者是一个多输入的元组,如yolo的输入图片大小为[640,640])
                  "path/to/save",       #  保存文件夹
                  export_params=True,        # store the trained parameter weights inside the model file
                
                  do_constant_folding=True,  # whether to execute constant folding for optimization
                  input_names = ['input'],   # 模型输入名
                  output_names = ['output'], # 模型输出名
                 )

得到一个 xxx.onnx的输出,下面以 best.onnx为例。将best.onnx转换成 best-sim.onnx 。这步可以不做。

pip install onnxsim
python -m onnxsim best.onnx best-sim.onnx

2.onxx->ncnn

先给windows装上Cmake地址为:Download CMake,并配置环境变量

下载Visual Studio (必备)  地址为:免费下载 (microsoft.com)

下载完成后 勾选如上的选项等它下载完所有的依赖包。

下载protobuf 源码,地址为protobuf,解压到文件夹 protobuf_dir

下载ncnn源码,下载地址ncnn 解压到文件夹 ncnn_dir

在window开始页下打开 x64 Native Tools Command Prompt for VS (以下的所有操作均是在x64 Native Tools Command Prompt for VS的窗口下执行

进入 protobuf_dir  ( 也就是刚刚解压的protobuf文件夹的根目录)逐行执行下面代码:
 

cd protobuf_dir
mkdir build
cd build

cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF ../cmake
nmake
nmake install

在buid下面会出现以下的文件,如果不对就重新生成。

中间可能会报错一个cmake的错误,是和cmake的版本有关系,,进入protobuf_dir->cmake->Cmakelists.txt 找到下面这行代码 

cmake_minimum_required(VERSION 3.5)
version 为你的cmake版本就行

进入ncnn_dir,逐行执行下面代码 (其中 cmake -G中有三处protobuf_dir,注意修改为你自己的路径 ):

cd ncnn_dir
mkdir -p build
cd build

cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -DProtobuf_INCLUDE_DIR=protobuf_dir/build/install/include -DProtobuf_LIBRARIES=protobuf_dir/build/install/lib/libprotobuf.lib -DProtobuf_PROTOC_EXECUTABLE=protobuf_dir/build/install/bin/protoc.exe -DNCNN_VULKAN=OFF ..

nmake
nmake install

在buid下面会出现以下的文件:

然后转到build下的tools下:

cd tools/onnx
./onnx2ncnn best-sim.onnx best.param best.bin
cd ../
./ncnnoptimize best.param best.bin best-opt.param best-opt.bin 65536

会得到优化前后的两组:

然后在ncnn\examples下找到自己模型的c++ 代码或者在ncnn\python\examples下找到python代码的示例模仿着调用就行了,参考使用vs运行yolov8_onnx-CSDN博客

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值