1 准备工作
windows版caffe:https://github.com/Microsoft/caffe
VS2013(其他版本不行!!!)
python2.7(其他版本不行!!!)
2 详细步骤
修改配置:
将 caffe-windows\windows\CommonSettings.props.example备份,并改名为CommonSettings.props
修改如下:
</pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros">
<BuildDir>$(SolutionDir)..\Build</BuildDir>
<!--NOTE: CpuOnlyBuild and UseCuDNN flags can't be set at the same time.-->
<CpuOnlyBuild>false</CpuOnlyBuild><!--注释里说的很清楚,这两个值不能同时设为true。若没有GPU就把CpuOnlyBuild设为true-->
<UseCuDNN>true</UseCuDNN>
<CudaVersion>7.5</CudaVersion>
<!-- NOTE: If Python support is enabled, PythonDir (below) needs to be
set to the root of your Python installation. If your Python installation
does not contain debug libraries, debug build will not work. -->
<PythonSupport>false</PythonSupport><!--设置是否支持python接口,若想支持,需要改后面的PythonDir的值-->
<!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be
set to the root of your Matlab installation. -->
<MatlabSupport>false</MatlabSupport><!--设置是否支持matlab接口,若想支持,需要改后面的MatlabDir的值-->
<CudaDependencies></CudaDependencies>
<!-- Set CUDA architecture suitable for your GPU.
Setting proper architecture is important to mimize your run and compile time. -->
<CudaArchitecture>compute_35,sm_35;compute_52,sm_52</CudaArchitecture>
<!-- CuDNN 3 and 4 are supported -->
<CuDnnPath></CuDnnPath>
<ScriptsDir>$(SolutionDir)\scripts</ScriptsDir>
</PropertyGroup>
<PropertyGroup Condition="'$(CpuOnlyBuild)'=='false'">
<CudaDependencies>cublas.lib;cuda.lib;curand.lib;cudart.lib</CudaDependencies>
</PropertyGroup>
<PropertyGroup Condition="'$(UseCuDNN)'=='true'">
<CudaDependencies>cudnn.lib;$(CudaDependencies)</CudaDependencies>
</PropertyGroup>
<PropertyGroup Condition="'$(UseCuDNN)'=='true' And $(CuDnnPath)!=''">
<LibraryPath>$(CuDnnPath)\cuda\lib\x64;$(LibraryPath)</LibraryPath>
<IncludePath>$(CuDnnPath)\cuda\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup>
<OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup>
<LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(LibraryPath)</LibraryPath>
<IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(PythonSupport)'=='true'"><!--与前面python接口设置对应-->
<PythonDir>C:\Miniconda2\</PythonDir>
<LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>
<IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(MatlabSupport)'=='true'"><!--与前面的matlab接口设置对应-->
<MatlabDir>C:\Program Files\MATLAB\R2014b</MatlabDir>
<LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>
<IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(CpuOnlyBuild)'=='true'">
3 编译caffe
编译用vs2013打开.\windows\Caffe.sln 并将解决方案的配置改为release。先编译libcaffe,右键生成解决方案。如果没报错,点击项目,生成全部解决方案。如果一切顺利,则会在bulid/x64/下生成release版本或者debug版本。之后,将该版本下的caffe复制到python2.7的libs/site-pakages/目录下即可。
当然,实际操作中往往不会这么顺利。。。。下面是一些常见错误:
4 常见错误
1 error C2220: 警告被视为错误 - 没有生成“object”文件 (..\..\src\caffe\util\math_functions.cpp)
打开VS->项目->属性->配置属性->C/C++->常规->将警告视为错误修改为“否”,重新编译就好。
2 error LNK1104: 无法打开文件“python27_d.lib”
修改编译变量debug为release 或者将python2.7下的include/pyconfig.h下的python27_d.lib修改为python27.lib
#ifdef MS_COREDLL
# ifndef Py_BUILD_CORE /* not building the core - must be an ext */
# if defined(_MSC_VER)
/* So MSVC users need not specify the .lib file in
their Makefile (other compilers are generally
taken care of by distutils.) */
# ifdef _DEBUG
# pragma comment(lib,"python27.lib")
# else
# pragma comment(lib,"python27.lib")
# endif /* _DEBUG */
# endif /* _MSC_VER */
# endif /* Py_BUILD_CORE */
#endif /* MS_COREDLL */
3 找不到python27.dll
将此python27.dll文件复制到 C:\Windows\System32 下
4 __import__Pyxxx等等的错误
将python27 /include下的object.h的#define Py_TRACE_REFS注释掉
/* Py_DEBUG implies Py_TRACE_REFS. */
#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
//#define Py_TRACE_REFS change by ZHANGUO
#endif