将pytorch 模型转为 ncnn模型

将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

运行结果如下所示:

image-20210826173143846.png

cmake --build . --config Release 运行结果如下:

image-20210826174418843-1629971268515.png

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编译成功后如下图所示:

image-20210827111116194-1630033879029.png

cmake --build . --config Release 运行结果如下:

image-20210827111836387.png

参考链接:

  • 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值