一. 模型转换
流程:.pt -> .onnx -> onnxsim.onnx -> .param, .bin
1.export onnx
参考 export.py , 建议使用默认的输入输出节点名(数字),否则后续ncnn推理可能遇到问题。
2.onnxsim
参考use-ncnn-with-pytorch-or-onnx
python3 -m onnxsim resnet18.onnx resnet18-sim.onnx
3.simonnx->ncnn
一键转换 Caffe, ONNX, TensorFlow 到 NCNN, MNN, Tengine4
4.修改param reshape 为-1,可以动态输入
二.ncnn编译
本篇为linux端环境配置介绍。
测试环境:
系统环境:
ubuntu 18.04
软件环境:
opencv: 3.2.0
cmake: 3.10.2
gcc: 7.5.0
g++: 7.5.0
protoc: 3.19.1
1.编译NCNN
操作步骤:
(1)下载ncnn源码 https://github.com/Tencent/ncnn
*注意不要漏掉glslang文件夹下内容。
(2)离线安装vulkan
# 下载压缩文件:https://sdk.lunarg.com/sdk/download/1.2.189.0/linux/vulkansdk-linux-x86_64-1.2.189.0.tar.gz
tar -xf vulkansdk-linux-x86_64-1.2.189.0.tar.gz
# 添加vulkan到环境变量
vim ~/.bashrc
# 最下面添加 :export VULKAN_SDK=<path>/1.2.189.0/x86_64
source ~/.bashrc
(3)离线安装protobuf
# 下载protobuf安装包 https://github.com/google/protobuf/releases
tar -zxvf protobuf-all-3.19.1.tar.gz
cd protobuf-3.19.1
./configure
make
make check
sudo make install
protoc --version
# 默认安装到/usr/local/lib
# 若提示没有,则在环境变量中添加路径/usr/local/lib
vim ~/.bashrc
# 添加如下
LD_LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH
# 退出后运行下面一行
source ~/.bashrc
(4)编译NCNN源码
cd ncnn
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DNCNN_VULKAN=ON -DNCNN_SYSTEM_GLSLANG=ON -DNCNN_BUILD_EXAMPLES=ON ..
make -j4
make install
(5)检查是否编译完成
cd ../example
../build/examples/squeezenet ../images/256-ncnn.png
# 结果如下:
[0 AMD RADV FIJI (LLVM 10.0.1)] queueC=1[4] queueG=0[1] queueT=0[1]
[0 AMD RADV FIJI (LLVM 10.0.1)] bugsbn1=0 buglbia=0 bugcopc=0 bugihfa=0
[0 AMD RADV FIJI (LLVM 10.0.1)] fp16p=1 fp16s=1 fp16a=0 int8s=1 int8a=1
532 = 0.163452
920 = 0.093140
716 = 0.061584
2.添加NCNN和Opencv环境
参考CMakeLists.txt
project(name)
cmake_minimum_required(VERSION 2.8)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set (CMAKE_CXX_FLAGS "${CAMKE_CXX_FLAGS} -fopenmp")
add_executable(name name.cpp main.cpp name.h)
set(ncnn_DIR "/path/ncnn/build/install/lib/cmake/ncnn" CACHE PATH "Directory that contains ncnnConfig.cmake")
find_package(ncnn REQUIRED)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
target_link_libraries(name ${OpenCV_LIBS} ncnn)