当前位置:   article > 正文

pytorch下yolov5模型迁移至android_pytorch打包好的yolo模型怎么导入到android项目

pytorch打包好的yolo模型怎么导入到android项目

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

1、pt->onnx

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

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

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

在这里插入图片描述

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

3、onnx->ncnn

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

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

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

Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

then 输入以下命令:

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

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

Input layer images without shape info, shape_inference aborted
Input layer images without shape info, estimate_memory_footprint aborted
  • 1
  • 2

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/894126
推荐阅读
相关标签