https://blog.csdn.net/fengtaoO08/article/details/83023428
本文将在Win10系统上安装和配置caffe。与网络上大多数文章不同,本文将在VS2017环境下编译CUDA9.0版本的caffe。事实上,本文配置流程对于更高版本的CUDA其安装流程是类似的,读者可进行参考。
本文中用%CAFFE_DIR%
代表准备安装caffe的目录路径,请读者自行替换为实际路径,如D:\Caffe\
。
本文中用%USER_NAME%
代表Windows用户名,因此Windows用户文件夹路径为C:\Users\%USER_NAME%
,请读者自行替换为实际路径。
在准备安装caffe的目录下运行以下命令
git clone https://github.com/BVLC/caffe.git
cd caffe
git checkout windows
本节是最为繁琐的一步。
新的VS2017 15.8自带的VC++工具集版本与CUDA 9.0有严重的兼容性问题,直接使用该版本的工具集编译CUDA 9.0会出现许多莫名其妙的错误,且在网上很难找到解决方法。但我们并不需要因此重新安装旧版本的VS2017,而只需安装并使用旧的VC++ 2017 版本 15.4 v14.11工具集编译CUDA 9.0即可。
打开VS Installer,修改安装,在安装选项里面的单个组件
选择VC++ 2017 版本 15.4 v14.11 工具集
安装。
在VS2017安装路径中查找vcvars64.bat
所在目录,参考路径为C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\
。拷贝路径下的vcvars64.bat
并重命名为vcvars64_VC1411.bat
,编辑修改vcvars64_VC1411.bat
内容为
@call "%~dp0vcvarsall.bat" x64 -vcvars_ver=14.11 %*
编辑文件%CAFFE_DIR%\caffe\scripts\build_win.cmd
,做如下修改后保存。
根据自己的需要修改70~99行的内容对编译进行配置。个人配置如下,供参考。
:: Change the settings here to match your setup
:: Change MSVC_VERSION to 12 to use VS 2013
:: 【使用VS2017,MSVC_VERSION=15】
if NOT DEFINED MSVC_VERSION set MSVC_VERSION=15
:: Change to 1 to use Ninja generator (builds much faster)
:: 【不使用Ninja】
if NOT DEFINED WITH_NINJA set WITH_NINJA=0
:: Change to 1 to build caffe without CUDA support
:: 【使用GPU加速】
if NOT DEFINED CPU_ONLY set CPU_ONLY=0
:: Change to generate CUDA code for one of the following GPU architectures
:: [Fermi Kepler Maxwell Pascal All]
if NOT DEFINED CUDA_ARCH_NAME set CUDA_ARCH_NAME=Auto
:: Change to Debug to build Debug. This is only relevant for the Ninja generator the Visual Studio generator will generate both Debug and Release configs
if NOT DEFINED CMAKE_CONFIG set CMAKE_CONFIG=Release
:: Set to 1 to use NCCL
if NOT DEFINED USE_NCCL set USE_NCCL=0
:: Change to 1 to build a caffe.dll
if NOT DEFINED CMAKE_BUILD_SHARED_LIBS set CMAKE_BUILD_SHARED_LIBS=0
:: Change to 3 if using python 3.5 (only 2.7 and 3.5 are supported)
:: 【在Anaconda配多版本python,这里用python2.7编译,PYTHON_VERSION=2】
if NOT DEFINED PYTHON_VERSION set PYTHON_VERSION=2
:: Change these options for your needs.
:: 【使用python接口】
if NOT DEFINED BUILD_PYTHON set BUILD_PYTHON=1
if NOT DEFINED BUILD_PYTHON_LAYER set BUILD_PYTHON_LAYER=1
:: 【使用matlab接口】
if NOT DEFINED BUILD_MATLAB set BUILD_MATLAB=1
:: If python is on your path leave this alone
if NOT DEFINED PYTHON_EXE set PYTHON_EXE=python
:: Run the tests
if NOT DEFINED RUN_TESTS set RUN_TESTS=0
:: Run lint
if NOT DEFINED RUN_LINT set RUN_LINT=0
:: Build the install target
if NOT DEFINED RUN_INSTALL set RUN_INSTALL=0
在112行插入如下代码。
if "%MSVC_VERSION%"=="15" (
set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
)
将158行的batch_file
值修改在6.1.2
节中修改的文件vcvars64_VC1411.bat
的绝对路径,如将原来的
set batch_file=!VS%MSVC_VERSION%0COMNTOOLS!..\..\VC\vcvarsall.bat
修改为
set batch_file=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64_VC1411.bat
编辑%CAFFE_DIR%\caffe\cmake\WindowsDownloadPrebuiltDependencies.cmake
文件,在第14行插入
set(DEPENDENCIES_URL_1911_27 "${DEPENDENCIES_URL_BASE}/v${DEPENDENCIES_VERSION}/${DEPENDENCIES_NAME_1900_27}${DEPENDENCIES_FILE_EXT}")
set(DEPENDENCIES_SHA_1911_27 "17eecb095bd3b0774a87a38624a77ce35e497cd2")
在这里,1911是MSVC编译器版本号,对应VC++ 2017 版本 15.4 v14.11工具集,27是python版本号。
编辑%CAFFE_DIR%\caffe\cmake\Cuda.cmake
文件,做如下修改并保存。
将90~104行注释,如下
# if(${CUDA_ARCH_NAME} STREQUAL "Fermi")
# set(__cuda_arch_bin "20 21(20)")
# elseif(${CUDA_ARCH_NAME} STREQUAL "Kepler")
# set(__cuda_arch_bin "30 35")
# elseif(${CUDA_ARCH_NAME} STREQUAL "Maxwell")
# set(__cuda_arch_bin "50")
# elseif(${CUDA_ARCH_NAME} STREQUAL "Pascal")
# set(__cuda_arch_bin "60 61")
# elseif(${CUDA_ARCH_NAME} STREQUAL "All")
# set(__cuda_arch_bin ${Caffe_known_gpu_archs})
# elseif(${CUDA_ARCH_NAME} STREQUAL "Auto")
# caffe_detect_installed_gpus(__cuda_arch_bin)
# else() # (${CUDA_ARCH_NAME} STREQUAL "Manual")
# set(__cuda_arch_bin ${CUDA_ARCH_BIN})
# endif()
在第104行插入
set(__cuda_arch_bin "61")
在这里,61代表的是我的GTX1080Ti显卡的GPU计算能力。可以在https://developer.nvidia.com/cuda-gpus
中查找Nvidia所有显卡的计算能力,修改为你的显卡对应计算能力数值即可。
由于项目需要,本文所安装的caffe在python 2.7环境下编译,但本机安装的Anaconda3默认安装的是python 3.6,因此需要利用创建一个python 2.7的虚拟环境。这里仅介绍用到的Anaconda的命令,其他用于管理虚拟环境的命令请读者自行搜索。
命令行运行
conda create -n py27 python=2.7
命令行运行
activate py27
激活成功后命令行前会显示(py27)
字样。注意,下文提到的命令行命令均在这个虚拟环境中运行。
本文需要安装caffe的python接口,因此需要先安装pycaffe所需的包。命令行运行
conda install --yes numpy scipy protobuf six scikit-image pyyaml pydotplus graphviz
等待安装结束。
命令行运行文件%CAFFE_DIR%\caffe\scripts\build_win.cmd
,则自动下载caffe依赖库,但会报openCV配置错误,错误打印片段如下
...
Call Stack (most recent call first):
cmake/Dependencies.cmake:114 (find_package)
CMakeLists.txt:80 (include)
CMake Error at cmake/Dependencies.cmake:114 (find_package):
Found package configuration file:
C:/Users/%USER_NAME%/.caffe/dependencies/libraries/OpenCVConfig.cmake
but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be
NOT FOUND.
Call Stack (most recent call first):
CMakeLists.txt:80 (include)
-- Configuring incomplete, errors occurred!
See also "%CAFFE_DIR%/caffe/build/CMakeFiles/CMakeOutput.log".
See also "%CAFFE_DIR%/caffe/build/CMakeFiles/CMakeError.log".
ERROR: Configure failed
...
这是因为自动生成的opencv及其他库的配置文件与VS2017不合导致的,我们需要手动修改。
编辑C:\Users\%USER_NAME%\.caffe\dependencies\libraries\OpenCVConfig.cmake
,在86行插入保存
elseif(MSVC_VERSION EQUAL 1911)
set(OpenCV_RUNTIME vc14)
编辑C:\Users\%USER_NAME%\.caffe\dependencies\libraries\include\boost-1_61\boost\config\compiler\nvcc.hpp
,将整个文件替换为如下内容并保存
// (C) Copyright Eric Jourdanneau, Joel Falcou 2010
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org for most recent version.
// NVIDIA CUDA C++ compiler setup
#ifndef BOOST_COMPILER
# define BOOST_COMPILER "NVIDIA CUDA C++ Compiler"
#endif
#if defined(__CUDACC_VER_MAJOR__) && defined(__CUDACC_VER_MINOR__) && defined(__CUDACC_VER_BUILD__)
# define BOOST_CUDA_VERSION __CUDACC_VER_MAJOR__ * 1000000 + __CUDACC_VER_MINOR__ * 10000 + __CUDACC_VER_BUILD__
#else
// We don't really know what the CUDA version is, but it's definitely before 7.5:
# define BOOST_CUDA_VERSION 7000000
#endif
// NVIDIA Specific support
// BOOST_GPU_ENABLED : Flag a function or a method as being enabled on the host and device
#define BOOST_GPU_ENABLED __host__ __device__
// A bug in version 7.0 of CUDA prevents use of variadic templates in some occasions
// https://svn.boost.org/trac/boost/ticket/11897
// This is fixed in 7.5. As the following version macro was introduced in 7.5 an existance
// check is enough to detect versions < 7.5
#if BOOST_CUDA_VERSION < 7050000
# define BOOST_NO_CXX11_VARIADIC_TEMPLATES
#endif
// The same bug is back again in 8.0:
#if (BOOST_CUDA_VERSION > 8000000) && (BOOST_CUDA_VERSION < 8010000)
# define BOOST_NO_CXX11_VARIADIC_TEMPLATES
#endif
// Most recent CUDA (8.0) has no constexpr support in msvc mode:
#if defined(_MSC_VER)
# define BOOST_NO_CXX11_CONSTEXPR
#endif
#ifdef __CUDACC__
//
// When compiing .cu files, there's a bunch of stuff that doesn't work with msvc:
//
#if defined(_MSC_VER)
# define BOOST_NO_CXX14_DIGIT_SEPARATORS
# define BOOST_NO_CXX11_UNICODE_LITERALS
#endif
//
// And this one effects the NVCC front end,
// See https://svn.boost.org/trac/boost/ticket/13049
//
#if (BOOST_CUDA_VERSION >= 8000000) && (BOOST_CUDA_VERSION < 8010000)
# define BOOST_NO_CXX11_NOEXCEPT
#endif
#endif
在命令行中运行%CAFFE_DIR%\caffe\scripts\build_win.cmd
,顺利进行编译。编译过程中会输出大量的警告,其中包含很多乱码,类似下面
...
d:\nvida gpu computing toolkit\cuda\v9.0\include\crt\math_functions.h(4417): warning C4819: 锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟节碉拷前锟
斤拷锟斤拷页(936)锟叫憋拷示锟斤拷锟街凤拷锟斤拷锟诫将锟斤拷锟侥硷拷锟斤拷锟斤拷为 Unicode 锟斤拷式锟皆凤拷止锟斤拷锟捷讹拷失 [%CAFFE_DIR%\caffe\scripts\build\src\caffe\caffe.vcx
proj]
d:\nvida gpu computing toolkit\cuda\v9.0\include\crt\math_functions.h(5319): warning C4819: 锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟节碉拷前锟
斤拷锟斤拷页(936)锟叫憋拷示锟斤拷锟街凤拷锟斤拷锟诫将锟斤拷锟侥硷拷锟斤拷锟斤拷为 Unicode 锟斤拷式锟皆凤拷止锟斤拷锟捷讹拷失 [%CAFFE_DIR%\caffe\scripts\build\src\caffe\caffe.vcx
proj]
...
这是由于编码字符问题造成的,和其他非乱码警告一样,并不影响编译过程,无需处理。大约经过10分钟,编译结束,如果错误数为0则编译成功。
将%CAFFE_DIR%\caffe\python
路径下的caffe
文件夹复制到Anaconda中虚拟环境py27
的site-packages
路径下,参考路径D:\Anaconda3_64\envs\py27\Lib\site-packages
。
在py27
虚拟环境下,运行命令进入python命令编辑模式。
python
运行命令
import caffe
如果没有报错则说明成功安装。
将%CAFFE_DIR%\caffe\matlab\+caffe\private\Release
路径下的caffe_.mexw64
拷贝到%CAFFE_DIR%\caffe\matlab\+caffe\private\
目录下。
在系统环境变量PATH
中添加%CAFFE_DIR%\caffe\scripts\build\install\bin
在MatLab中,选择主页
选项卡,点击设置路径
按钮添加以下MATLAB搜索路径
%CAFFE_DIR%\caffe\scripts\build\install\bin
%CAFFE_DIR%\caffe\scripts\build\install
接下来测试接口是否成功,从Link: bvlc_reference_caffenet.caffemodel下载bvlc_reference_caffenet.caffemodel
到%CAFFE_DIR%\caffe\models\bvlc_reference_caffenet
文件夹下,在MatLab中运行脚本%CAFFE_DIR%\caffe\matlab\demo\classification_demo.m
。若成功,会打印一个1000类的概率向量,其中第282类(tabby, tabby cat)的概率为0.2985。