关于 ubuntu ncnn 环境的安装, protobuf、pnnx

11 篇文章 0 订阅
8 篇文章 0 订阅

安装protobuf

ncnn环境安装

sudo apt-get install build-essential
sudo apt-get install cmake 
sudo apt-get install autoconf automake libtool curl make g++ unzip

编译

./autogen.sh
sudo ./configure --prefix=$INSTALL_DIR  #--prefix指定安装目录
sudo make
sudo make check
sudo make install

将模型转成ncnn 的格式

方式1:直接用ncnn 自带的工具转

  • 在编译后的build tools有个onnx文件可以把模型转成ncnn格式
./onnx2ncnn lw50.onnx lw50.param lw50.bin

在这里插入图片描述
不过基本都会报错

Unsupported slice axes !
Unsupported slice axes !
Unsupported slice axes !
Unsupported slice axes !
Unsupported slice axes !
Unsupported slice axes !

或者
![在这里插入图片描述](https://img-blog.csdnimg.cn/e785e5fec95141b2afbfb62afaa6d4f7.png
如果成功会生成两个文件

yolov5s.param
yolov5s.bin

这个可以参考修改,原因是有的层实现不兼容https://zhuanlan.zhihu.com/p/275989233

方式二

用pnnx 进行转换
这个有编译好的版本
https://github.com/pnnx/pnnx/releases

直接下载运行就可以了

pnnx resnet18.pt inputshape=[1,3,224,224]

如果不支持的话, 只能自己编译

pnnx 源码:
https://github.com/pnnx/pnnx

直接去pytorch 下c++ 源码
https://pytorch.org/

旧版本参考
https://blog.csdn.net/qq_41375318/article/details/126800667

在这里插入图片描述

这里需要修改 编译路径,找到LibTorch 的make路径,相当于是软链接路径文件
在这里插入图片描述
同时需要一个python 的软链接路径

找个地方新建python3 文件夹

  • 新建一个CMakeLists.txt,
cmake_minimum_required(VERSION 3.5)
project(Python3Config)

message(STATUS "Python3_EXECUTABLE: ${Python3_EXECUTABLE}")
message(STATUS "Python3_INCLUDE_DIRS: ${Python3_INCLUDE_DIRS}")
message(STATUS "Python3_LIBRARIES: ${Python3_LIBRARIES}")
message(STATUS "Python3_VERSION: ${Python3_VERSION}")
message(STATUS "NumPy_INCLUDE_DIRS: ${NumPy_INCLUDE_DIRS}")

configure_file(Python3Config.cmake.in Python3Config.cmake @ONLY)

另外新建一个变量文件Python3Config.cmake.in

set(Python3_EXECUTABLE "/home/cdm/anaconda3/bin/python")
set(Python3_INCLUDE_DIRS "/home/cdm/anaconda3/lib/python3.9/Python3_INCLUDE_DIRS")
set(Python3_LIBRARIES "/home/cdm/anaconda3/lib/python3.9/Python3_LIBRARIES")
set(Python3_VERSION "3.9.13")
set(NumPy_INCLUDE_DIRS "/home/cdm/anaconda3/lib/python3.9/site-packages/numpy")

查看命令

import os
import numpy

numpy_path = os.path.dirname(numpy.__file__)
print(numpy_path)
print(os.path.join(os.path.dirname(os.__file__), "Python3_LIBRARIES"))
print(os.environ.get('Python3_INCLUDE_DIRS'))
dir_path = os.environ.get('Python3_INCLUDE_DIRS')
if dir_path:
    for file_name in os.listdir(dir_path):
        print(file_name)
else:
    print('Python3_INCLUDE_DIRS环境变量未设置')
python_executable = os.popen('which python3').read().strip()
print(python_executable)

然后执行 cmake .

编译生成
Python3Config.cmake

set(Python3_EXECUTABLE "/home/cdm/anaconda3/bin/python")
set(Python3_INCLUDE_DIRS "/home/cdm/anaconda3/lib/python3.9/Python3_INCLUDE_DIRS")
set(Python3_LIBRARIES "/home/cdm/anaconda3/lib/python3.9/Python3_LIBRARIES")
set(Python3_VERSION "3.9.13")
set(NumPy_INCLUDE_DIRS "/home/cdm/anaconda3/lib/python3.9/site-packages/numpy")

其实直接输入也行

然后修改pnnx 的CMakeLists.txt

find_package(Python3 COMPONENTS Interpreter Development)
find_package(Torch REQUIRED)

加上路径
就是修改查找编译的路径地址

set(Python3_DIR "/home/cdm/env/ncnn/tools/pnnx")
set(Torch_DIR "/home/cdm/env/libtorch/share/cmake/Torch")

接下来根据这个官方来就行

mkdir ncnn/tools/pnnx/build
cd ncnn/tools/pnnx/build
cmake -DCMAKE_INSTALL_PREFIX=install -DTorch_INSTALL_DIR=<your libtorch dir> ..
cmake --build . --config Release -j 2
cmake --build . --config Release --target install

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东哥爱编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值