Win10使用VS2017安装Caffe详细总结

本文链接

https://blog.csdn.net/fengtaoO08/article/details/83023428

1 前言

  本文将在Win10系统上安装和配置caffe。与网络上大多数文章不同,本文将在VS2017环境下编译CUDA9.0版本的caffe。事实上,本文配置流程对于更高版本的CUDA其安装流程是类似的,读者可进行参考。

2 符号约定

  本文中用%CAFFE_DIR%代表准备安装caffe的目录路径,请读者自行替换为实际路径,如D:\Caffe\
  本文中用%USER_NAME%代表Windows用户名,因此Windows用户文件夹路径为C:\Users\%USER_NAME%,请读者自行替换为实际路径。

3 系统环境

  • OS:Windows 10 x64
  • CPU:Intel Core i7-8700K 3.70GHz
  • GPU:Nvidia GEFORCE GTX 1080 Ti
  • Memory RAM:8GB x2

4 预安装

  • Microsoft Visual Studio Community 2017 version 15.8
  • Anaconda3 version 4.3.30 with Python 3.6.5
  • Nvidia CUDA 9.0
  • cuDNN v7.3.1 for CUDA 9.0
  • Git v2.19.1
  • OpenCV 3.4.1
  • MatLab 2018a

5 caffe的获取

  在准备安装caffe的目录下运行以下命令

git clone https://github.com/BVLC/caffe.git
cd caffe
git checkout windows

6 caffe的编译

  本节是最为繁琐的一步。

6.1 VC++ 2017 版本 15.4 v14.11工具集

  新的VS2017 15.8自带的VC++工具集版本与CUDA 9.0有严重的兼容性问题,直接使用该版本的工具集编译CUDA 9.0会出现许多莫名其妙的错误,且在网上很难找到解决方法。但我们并不需要因此重新安装旧版本的VS2017,而只需安装并使用旧的VC++ 2017 版本 15.4 v14.11工具集编译CUDA 9.0即可。

6.1.1 安装VC++工具集

  打开VS Installer,修改安装,在安装选项里面的单个组件选择VC++ 2017 版本 15.4 v14.11 工具集安装。

6.1.2 配置VC++工具集

  在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 %*

6.2 配置build_win.cmd

  编辑文件%CAFFE_DIR%\caffe\scripts\build_win.cmd,做如下修改后保存。

6.2.1 修改70~99行

  根据自己的需要修改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

6.2.2 插入第112行

  在112行插入如下代码。

if "%MSVC_VERSION%"=="15" (
	set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
)

6.2.3 修改第158行

  将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 

6.3 修改WindowsDownloadPrebuiltDependencies.cmake

  编辑%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版本号。

6.4 修改Cuda.cmake

  编辑%CAFFE_DIR%\caffe\cmake\Cuda.cmake文件,做如下修改并保存。

6.4.1 注释90~104行

  将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()

6.4.2 插入第104行

  在第104行插入

set(__cuda_arch_bin "61")

  在这里,61代表的是我的GTX1080Ti显卡的GPU计算能力。可以在https://developer.nvidia.com/cuda-gpus中查找Nvidia所有显卡的计算能力,修改为你的显卡对应计算能力数值即可。

6.5 Anaconda配置python环境

  由于项目需要,本文所安装的caffe在python 2.7环境下编译,但本机安装的Anaconda3默认安装的是python 3.6,因此需要利用创建一个python 2.7的虚拟环境。这里仅介绍用到的Anaconda的命令,其他用于管理虚拟环境的命令请读者自行搜索。

6.5.1 创建python 2.7虚拟环境

  命令行运行

conda create -n py27 python=2.7

6.5.2 激活python 2.7虚拟环境

  命令行运行

activate py27

  激活成功后命令行前会显示(py27)字样。注意,下文提到的命令行命令均在这个虚拟环境中运行。

6.5.3 安装pycaffe所需包

  本文需要安装caffe的python接口,因此需要先安装pycaffe所需的包。命令行运行

conda install --yes numpy scipy protobuf six scikit-image pyyaml pydotplus graphviz

  等待安装结束。

6.6 运行build_win.cmd

  命令行运行文件%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不合导致的,我们需要手动修改。

6.7 修改OpenCVConfig.cmake

  编辑C:\Users\%USER_NAME%\.caffe\dependencies\libraries\OpenCVConfig.cmake,在86行插入保存

elseif(MSVC_VERSION EQUAL 1911)
    set(OpenCV_RUNTIME vc14)

6.8 修改nvcc.hpp

  编辑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

6.9 再次运行build_win.cmd

  在命令行中运行%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则编译成功。

7 caffe的python接口

  将%CAFFE_DIR%\caffe\python路径下的caffe文件夹复制到Anaconda中虚拟环境py27site-packages路径下,参考路径D:\Anaconda3_64\envs\py27\Lib\site-packages
  在py27虚拟环境下,运行命令进入python命令编辑模式。

python

  运行命令

import caffe

如果没有报错则说明成功安装。

8 caffe的Matlab接口

  将%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。

你可能感兴趣的:(caffe)