将pytorch 模型转为 ncnn模型(vs 2019 )
文章目录
step 1 pth文件转为onnx文件
具体步骤可以参考:https://github.com/ultralytics/yolov5/blob/master/export.py
和https://blog.csdn.net/weixin_44753371/article/details/119328952的step1
step2:.onnx转ncnn模型
编译protobuf-3.4.0
下载 protobuf-3.4.0 https://github.com/google/protobuf/archive/v3.4.0.zip
cd <protobuf-root-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
Build ncnn library
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 ..
nmake
nmake install
或:
cd <ncnn-root-dir>
mkdir -p build
cd build
cmake -G"Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -DProtobuf_INCLUDE_DIR=<protobuf-root-dir>/src -DProtobuf_LIBRARIES=<protobuf-root-dir>/build-vs2019/Release/libprotobuf.lib -DProtobuf_PROTOC_EXECUTABLE=<protobuf-root-dir>/build-vs2019/Release/protoc.exe ..
cmake --build . --config Release
[option] -DNCNN_VULKAN=ON …
\build\tools\onnx\Release 路径下可以找到onnx2ncnn.exe文件.
onnx模型转为ncnn
cd <ncnn-root-dir>\build\tools\onnx\Release
onnx2nccn.exe <onnxfilepath>
# 当前目录下就会出现ncnn.bin,ncnn.param文件
step2 报错处理
- 1:nmake not find
找到nmake.exe文件的位置然后加入PATH环境变量:
D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64
- 2: RC Pass 1: command “rc /fo CMakeFiles\cmTC_deb0b.dir/manifest.res CMakeFiles\cmTC_deb0b.dir/manifest.rc” failed (exit code 0) with the following output:
系统找不到指定的文件。NMAKE : fatal error U1077: ““D:\Program Files\CMake\bin\cmake.exe””: 返回代码“0xffffffff”
Stop.
NMAKE : fatal error U1077: ““D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64\nmake.exe””: 返回代码“0x2”
Stop.
找到rc.exe文件的位置加入path
C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x64\rc.exe
将step2 的build目录清空并且将命令 替换为:
参考自:https://github.com/Tencent/ncnn/issues/2549
cd <protobuf-root-dir>
mkdir build
cd build
cmake -G"Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF ../cmake
cmake --build . --config Release
cmake -G"Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF …/cmake
运行结果如下所示:
cmake --build . --config Release 运行结果如下:
Visual Studio 17 2022 = Generates Visual Studio 2022 project files.
Use -A option to specify architecture.
* Visual Studio 16 2019 = Generates Visual Studio 2019 project files.
Use -A option to specify architecture.
Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files.
Optional [arch] can be "Win64" or "IA64".
Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files.
Optional [arch] can be "Win64" or "IA64".
-
报错信息:CMake Error at CMakeLists.txt:265 (message):
The submodules were not downloaded! Please update submodules with “git
submodule update --init” and try again.# 不要使用download zip 下载 ncnn,使用git clone 下载 git clone https://github.com.cnpmjs.org/Tencent/ncnn.git cd ncnn git submodule update --init # 然后执行step2 ncnn编译操作
ncnn编译成功后如下图所示:
cmake --build . --config Release 运行结果如下:
参考链接:
- https://github.com/Tencent/ncnn/issues/2549
- https://blog.csdn.net/weixin_44753371/article/details/119328952
- https://github.com/ultralytics/yolov5/blob/master/export.py
- https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-windows-x64-using-visual-studio-community-2017