由于某些原因,需要在windows下配置caffe,基本上是按照caffe的windows分支的readme上写的安装的。大致说一下流程:
https://github.com/BVLC/caffe/tree/windows
1、 安装准备
VS2015,python3.5,matlab2017a,cuda8和cudnn。注意python版本不能是3.6,目前仅支持到3.5。我在安装的时候是装的最新的anaconda3,其带的python版本是3.6,导致不匹配。此时可以用conda create再安装一个3.5的环境,或是卸载重新安装anaconda3-4.2.0版本,带的是python3.5.2版本。
Anaconda官网下载很慢,推荐清华镜像源网站:
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
另外安装cmake和ninja,记得添加到path环境变量:
https://cmake.org/
https://ninja-build.org/
2、 下载caffe的windows分支:
git clonehttps://github.com/BVLC/caffe.git
cd caffe
git checkout windows
如果git慢,直接zip下载后解压吧…
3、 确认存在文件%CAFFE_ROOT%/scripts/build_win.cmd,修改69~100行:
) else (
if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14
if NOT DEFINED WITH_NINJA set WITH_NINJA=0
if NOT DEFINED CPU_ONLY set CPU_ONLY=0
if NOT DEFINED CUDA_ARCH_NAME set CUDA_ARCH_NAME=Auto
if NOT DEFINED CMAKE_CONFIG set CMAKE_CONFIG=Release
if NOT DEFINED USE_NCCL set USE_NCCL=0
if NOT DEFINED CMAKE_BUILD_SHARED_LIBS set CMAKE_BUILD_SHARED_LIBS=0
if NOT DEFINED PYTHON_VERSION set PYTHON_VERSION=3
if NOT DEFINED BUILD_PYTHON set BUILD_PYTHON=1
if NOT DEFINED BUILD_PYTHON_LAYER set BUILD_PYTHON_LAYER=1
if NOT DEFINED BUILD_MATLAB set BUILD_MATLAB=1
if NOT DEFINED PYTHON_EXE set PYTHON_EXE=python
if NOT DEFINED RUN_TESTS set RUN_TESTS=0
if NOT DEFINED RUN_LINT set RUN_LINT=0
if NOT DEFINED RUN_INSTALL set RUN_INSTALL=0
)
注意with_ninja一定是0;
根据是否使用gpu设置cpu_only;
python_version如果用3.5则选3,2.7则选2;
设置是否用python和matlab接口等。
另外如果用了cudnn,可以将其三个文件夹放入CUDA/v8.0/的文件夹中合并。
4、 运行编译
\scripts\build_win.cmd
该脚本将自动识别vs、python、matlab、cuda、cudnn等位置,并开始编译。这比微软的caffe-windows配置方案方便很多,不需要自行编辑地址。
编译会先下载一些dependencies,需要网,需要比较长的时间…
5、 编译过程中有可能会报错,可以仔细看看是哪些错,我遇到过两个:
nvcc fatal: unsupported gpu architecture “compute_90”
说我的cuda版本不识别,有道理,因为我最近下了个最新的cuda v9,可能太新了,于是还是老老实实装回v8。
no module named…
这种一般是编译python接口时有些包还没有,根据提示补充即可:
pip install protobuf
pip installpython-gflags
pip installpydot
pip installpydotplus
6、 编译结束,测试:
cd %CAFFE_ROOT%\build\tools\Release
caffe
可以看到控制台输出:
caffe: commandline brew
usage: caffe<command> <args>
commands:
train train or finetune a model
test score a model
device_query show GPU diagnostic information
time benchmark model execution time
No modulesmatched: use -help
说明配置成功。
7、 测试pycaffe:
cd %CAFFE_ROOT%\python\
进入python控制台后import caffe
如果没有报错,说明配置成功
8、 测试matcaffe:
将%CAFFE_ROOT%\matlab\+caffe\private\Release下的caffe_.mexw64拷贝到上一级的private目录下,然后打开matlab进入%CAFFE_ROOT%\caffe-windows\matlab\demo运行classification_demo.m,一般会提示下载模型文件,则说明配置成功,否则会提示undefined caffe。
9、 之后要修改代码重新编译的话,进入build文件夹用vs打开Caffe.sln编译即可。
10、运行pycaffe需要进入%CAFFE_ROOT%\python,而matcaffe需要将编译好的mex(在%CAFFE_ROOT%\matlab\+caffe\private下)放到.\matlab文件夹下,然后加入matlab的搜索空间中(set path)才能找到。
// 补充微软版的caffe-windows配置方法:
1、下载:https://github.com/Microsoft/caffe
2、.\windows\CommonSettings.props.exampleà.\windows\CommonSettings.props
3、用编辑器修改,根据实际情况配置是与否以及matlab和python地址。另外第55行应改为<IncludePath>$(MatlabDir)\extern\include;$(MatlabDir)\toolbox\distcomp\gpu\extern\include;$(IncludePath)</IncludePath>
否则编译matcaffe时会报错。
4、用vs2013打开Caffe.sln,设置release x64编译,先编译libcaffe,项目右键à属性àC/C++,等级警告为3,将警告视为错误为否,确定后右键à重新生成,开始编译会自动联网用Nuget下载依赖;
5、再依次编译各个子项目即可,如caffe、matcaffe、pycaffe右键à生成编译,release后都在%CAFFE_ROOT%\Build\x64\Release下。
6、支持版本比较老,仅支持cuda7.5+cudnn_4/5+python2.7
7、pycaffe需要进入%CAFFE_ROOT%\Build\x64\Release\pycaffe路径,而matcaffe需要将.\Build\x64\Release\matcaffe加入matlab的搜索空间中(set path)才能找到。
// 经测试,发现caffe在win下跑的比在ubuntu下快一倍,什么原因…