windows编译安装(合并pytoch之后的)caffe2教程
注意
- 在windows平台,请不要使用在官方下载的libtorch,虽然里面有caffe2的库,但是有很多核心代码都没有编译。所以直接使用官方下载的libtorch会报错误,
[E ..\caffe2\core\operator.cc:143] Cannot find operator schema for GivenTensorFill. Will skip schema checking
- 请不要在不理解官方教程在干什么的情况下,使用官方教程进行编译,因为基本都编译失败,容我吐槽一下一下官方对caffe2的态度,自从合并到pytorch之后基本就是有啥更新,文档都不跟进了。官方教程
工具
- VS2017 ,一般社区版就够了。(When installing VS 2017, install Desktop Development with C++ (on the right select: C++/CLI support) and v140 (on the right select: VC++ 2015.3 v140 toolset))如下图:
- python环境,推荐使用miniconda,而不是直接使用python环境。
- Cmake,使用miniconda中的或者自己百度安装都可以。
第三方库
- pyyaml,装好miniconda后,请使用
conda install pyyaml
或者pip install pyyaml
安装。
- 其他的如gflags,glog等等之类的。可以自己编译安装。我只是用windows caffe2做部署,并不会做算法研究,算法基本在Linux下完成,代码也是直接在Linux完成,所以不需要编译Google的这些库。
编译
- 先使用git clone 下载编译脚本,这里要感谢peterjc123。他为pytorch windows贡献了很多。
git clone https://github.com/peterjc123/pytorch-scripts.git
- 需要说明的是这里面大部分脚本都是为编译pytorch Windows准备的,编译caffe2的脚本在
pytorch-scripts/caffe2_builders/
路径下面,而子目录
v140/ #vs2015 的编译脚本
v141/ #vs2017 的编译脚本,请使用这个脚本。
- 查看脚本auto.bat,查看是否有功能是你不需要,或者需要的。加入我不需要debug版的caffe2,我就将
rem Debug build enabled by default
if NOT DEFINED BUILD_DEBUG (
set BUILD_DEBUG=1
)
删除或者屏蔽。
rem Remove to original folder after script is finished
set ORIGINAL_DIR=%cd%
rem Build folders are
rem %CAFFE2_ROOT%\build\Debug for Debug and
rem %CAFFE2_ROOT%\build\Release for Release
set CAFFE2_ROOT=%~dp0%\pytorch
cd %CAFFE2_ROOT%\..
rem Cloning repository if it doesn't exist
if not exist pytorch (
call %~dp0%..\..\internal\clone.bat
cd %CAFFE2_ROOT%\..
)
call %~dp0%..\..\internal\check_deps.bat
if errorlevel 1 exit /b 1
rem Should build folder be deleted and build start from scratch?
if NOT DEFINED REBUILD (
set REBUILD=0
)
rem Debug build enabled by default
if NOT DEFINED BUILD_DEBUG (
set BUILD_DEBUG=1
)
rem Release build enabled by default
if NOT DEFINED BUILD_RELEASE (
set BUILD_RELEASE=1
)
rem msbuild /m: option value
if NOT DEFINED MAX_JOBS (
set MAX_JOBS=%NUMBER_OF_PROCESSORS%
)
if %REBUILD% EQU 1 (
if exist %CAFFE2_ROOT%\build (
cd %CAFFE2_ROOT%
python setup.py clean
)
)
rem Visual Studio 15 2017 Win64 is supported by this script
if NOT DEFINED CMAKE_GENERATOR (
set CMAKE_GENERATOR="Visual Studio 15 2017 Win64"
)
rem Building Debug in %CAFFE2_ROOT%\build\Debug
if %BUILD_DEBUG% EQU 1 (
set CONFIG=Debug
call %~dp0%msbuild.bat
if errorlevel 1 exit /b 1
)
rem Building Release in %CAFFE2_ROOT%\build\Release
if %BUILD_RELEASE% EQU 1 (
set CONFIG=Release
call %~dp0%msbuild.bat
if errorlevel 1 exit /b 1
)
cd %ORIGINAL_DIR%
这上面是auto.bat的脚本,下面是msbuild.bat脚本
if not exist %CAFFE2_ROOT%\build\%CONFIG% (
mkdir %CAFFE2_ROOT%\build\%CONFIG%
)
cd %CAFFE2_ROOT%\build\%CONFIG%
if %CMAKE_GENERATOR% EQU "Visual Studio 15 2017 Win64" (
cmake %CAFFE2_ROOT% ^
-G%CMAKE_GENERATOR% ^
-T host=x64 ^
-DUSE_OPENCV=ON ^
-DCMAKE_BUILD_TYPE=%CONFIG% ^
-DCMAKE_INSTALL_PREFIX=%CAFFE2_ROOT%\build\%CONFIG%\install ^
-DBUILD_SHARED_LIBS=ON ^
-DBUILD_BINARY=ON
if errorlevel 1 exit /b 1
cd %CAFFE2_ROOT%\build\%CONFIG%
) else (
echo "Error: Script supports only Visual Studio 15 2017 Win64 generator"
echo "Exiting..."
cd %ORIGINAL_DIR%
exit /b 1
)
"%MSBUILD_EXE%" INSTALL.vcxproj ^
/p:Configuration=%CONFIG% ^
/p:Platform=x64 ^
/p:VisualStudioVersion=15.0 ^
/p:CL_MPCount=%MAX_JOBS%
if errorlevel 1 exit /b 1
当你的电脑里装了cuda了,该脚本默认编译cuda版本的caffe2。如果只编译cpu版的caffe2,请在该脚本的cmake指令后面加入:
-DUSE_CUDA=OFF ^ #不要忘记这个符号 ^
最后的脚本中的cmake指令是这样的,
cmake %CAFFE2_ROOT% ^
-G%CMAKE_GENERATOR% ^
-T host=x64 ^
-DUSE_OPENCV=ON ^
-DCMAKE_BUILD_TYPE=%CONFIG% ^
-DCMAKE_INSTALL_PREFIX=%CAFFE2_ROOT%\build\%CONFIG%\install ^
-DBUILD_SHARED_LIBS=ON ^
-DBUILD_BINARY=OFF ^
-DUSE_CUDA=OFF ^
- 请使用管理员权限点击auto.bat开始运行编译脚本。这里需要说明的是如果一切正常后,你可以在 pytorch-scripts-master\caffe2_builders\v141目录下没有pytorch的源码,脚本就会自己clone最新的源码并编译。如果是编译自己的版本,请将自己的pytorch源码拷贝该目录下。
- 一切编译正常,请在pytorch-scripts-master\caffe2_builders\v141\pytorch\build\Release\install 路径下查看你编译好的caffe2的库。