pytorch下yolov5模型迁移至android

流程:pt->onnx->ncnn->android
本人系统:win10

1、pt->onnx

①打开 volov5/models/export.py
在这里插入图片描述
红框内改为.pt模型的路径,然后运行
转换结果如图:
在这里插入图片描述

②需要去掉模型冗余的维度
打开anaconda prompt,去到 .onnx文件夹下

conda activate 你的虚拟环境
python -m onnxsim best.onnx best-sim.onnx

在这里插入图片描述

2、编译ncnn

①、配置相关环境(前方高能!!!!建议此步骤多参考几篇文章)
ncnn官网问题讨论
Build for Windows x64 using Visual Studio Community 2017
需要这些库,没有的结合教程安装一下
在这里插入图片描述
②构建ncnn库(替换为正确的路径):

> cd <ncnn-root-dir>
> mkdir -p build
> cd build
> cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -DProtobuf_INCLUDE_DIR=<protobuf-root-dir>/build/install/include -DProtobuf_LIBRARIES=<protobuf-root-dir>/build/install/lib/libprotobuf.lib -DProtobuf_PROTOC_EXECUTABLE=<protobuf-root-dir>/build/install/bin/protoc.exe -DNCNN_VULKAN=ON ..
> nmake
> nmake install

ncnn / build文件夹里会出现一大堆东西,我们只需要onnx2ncnn.exe与ncnnoptimize.exe,把上述两个程序copy过来:
在这里插入图片描述

3、onnx->ncnn

在上图的目录下输入命令:

onnx2ncnn best-sim.onnx best.param best.bin

生成:
在这里插入图片描述
同时报错:

Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !

解决:更改best.param
改前:
在这里插入图片描述
改后:
在这里插入图片描述
删除多余结构+增加结构+更改部分数字(Exp的699对应着convolution的699,模型不同,参数也会有轻微的变化)

then 输入以下命令:

ncnnoptimize best.param best.bin best-opt.param best-opt.bin 65536

生成
在这里插入图片描述
同时又又又报错:

Input layer images without shape info, shape_inference aborted
Input layer images without shape info, estimate_memory_footprint aborted

解决:更改best-opt.param
第四行的Exp改成 YoloV5Focus,最终得到:
在这里插入图片描述在这里插入图片描述
🙃🙃🙃🙃🙃🙃🙃🙃🙃🙃🙃🙃🙃🙃

4、ncnn->android

官网提供了现成的模块:ncnn-android-yolov5
在这里插入图片描述
把模型替换成自己的,记得改一下调用模型的名字

the end

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值