VS2015 + 新caffe + CUDA8.0 + CUDNN5.1在windows10下配置

时间:2021-02-22 22:51:20

    在linux下折腾了一段时间,打算在笔记本上面折腾一下的时候发现新版本的caffe删除了./windows文件夹,纠结了半天重新解决。只是自已折腾能用,并不代表所有步骤都是最优,仅供参考。

    (由于内容为配置成功后根据自已记忆编写,可能存在一些忘记的地方。)

    在进行下列步骤前,应该已经装好了CUDA8,CUDNN5.1,VS2015,cmake 3.4.2, Anaconda3-4.2.0。

    CUDA8、VS2015、cmake3.4.2、Anaconda3-4.2.0直接下载安装在某个位置即可,然后将CUDNN5.1中的三个文件夹复制到CUDA8的安装路径中就可以。


    1、下载caffe后解压,进入到caffe-windows主目录。下载地址:https://github.com/BVLC/caffe/tree/windows,里面有官方说明。

    2、跟以前步骤一样,复制主目录下的Makefile.config.example文件并重命名为Makefile.config。

    3、修改Makefile.config文件中内容,以下为根据自已需要修改。

        

## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!

# cuDNN acceleration switch (uncomment to build with cuDNN).
 USE_CUDNN := 1

# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1

# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0

# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
#	You should not set this flag if you will be reading LMDBs with any
#	possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1

# Uncomment if you're using OpenCV 3
 OPENCV_VERSION := 3

# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++

# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := D:/CUDA8
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
		-gencode arch=compute_20,code=sm_21 \
		-gencode arch=compute_30,code=sm_30 \
		-gencode arch=compute_35,code=sm_35 \
		-gencode arch=compute_50,code=sm_50 \
		-gencode arch=compute_52,code=sm_52 \
		-gencode arch=compute_60,code=sm_60 \
		-gencode arch=compute_61,code=sm_61 \
		-gencode arch=compute_61,code=compute_61

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
 BLAS_INCLUDE := /path/to/your/blas
 BLAS_LIB := /path/to/your/blas

# Homebrew puts openblas in a directory that is not on the standard search path
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib

# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
#PYTHON_INCLUDE := /usr/include/python2.7 \
#		/usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
 ANACONDA_HOME := D:/Anaconda3
 PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		 $(ANACONDA_HOME)/include/python3.5 \
		 $(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
#                 /usr/lib/python3.5/dist-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.
#PYTHON_LIB := /usr/lib
 PYTHON_LIB := $(ANACONDA_HOME)/lib

# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib

# Uncomment to support layers written in Python (will link against Python libs)
 WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib

# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1

# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1

# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute

# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1

# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0

# enable pretty build (comment to see full commands)
Q ?= @
 

     4、进入scripts文件夹中,在scripts中打开cmd命令窗口(按住shift在文件夹中点击鼠标右键,或者使用cd e:\caffe-windows\scripts和e:两句命令切换进去)。

        用记事本等文件打开build_win.cmd文件,找到:: Change the settings here to match your setup(我目前的文件约在70行)这句话,然后进行修改,如下:

        

    :: Change the settings here to match your setup
    :: Change MSVC_VERSION to 12 to use VS 2013
    if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14
    :: Change to 1 to use Ninja generator (builds much faster)
    if NOT DEFINED WITH_NINJA set WITH_NINJA=0
    :: Change to 1 to build caffe without CUDA support
    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)
    if NOT DEFINED PYTHON_VERSION set PYTHON_VERSION=3
    :: Change these options for your needs.
    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=0
    :: 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

        在命令行中输入./build_win.cmd,如果配置没问题会编绎一段时间,最后会有几千个警告, 0 errors的提示,则认为成功了。这个时候可以打开caffe-windows\scripts\build\caffe.sln工程,跟以前就差不多了,不过好像是少了一东西,例如libcaffe。

     若是在编绎过程中出现libraries_v140_x64_py35_1.1.0相关的错误,那么去网上下载libraries_v140_x64_py35_1.1.0.tar.bz2,放在报错提示的位置,例如我的是C:\Users\Think\.caffe\dependencies\download\然后删除掉caffe-windows\scripts下的build文件夹并重新编绎。(这个地方我在网上看到有说修改配置文件中的一个地方,然后可以把该文件放在一个地方就不会联 网下载出现问题,没细研究)

    5、如果需要在python中调用caffe,那么简单的办法是将E:\caffe-windows\python\caffe文件夹复制到D:\Anaconda3\Lib\site-packages\内,然后在命令行中输入python进入到python界面中,输入import caffe成功即可在python中调用caffe的相关功能。(关于这个地方我觉得有其他简单的办法,但是我一直没找到,如果有哪位知道还请指点一下,感谢!)

        6、如果需要在python中调用opencv,那么可以看看https://pypi.org/project/opencv-python/3.1.0.1/里面的介绍,打开Anaconda3的命令行,输入

pip install opencv-python

        这个时候会应该提示你pip的版本太低,那么根据提示升级一下pip,然后再次执行。这样还有一个可能是会特别慢,如果你像我一样不愿意等,就在上面的网址里面下载opencv_python-3.1.0.1-cp35-cp35m-win_amd64.whl,然后使用

pip install d:\opencv_python-3.1.0.1-cp35-cp35m-win_amd64.whl

    注意上面的路径。安装完成后进入python的命令行,输入python和import cv2,没有错误就算成功了,当然可以通过cv2.__version__查看版本。

    7、可以在cmd命令行中先切换路径到C:\Program Files\NVIDIA Corporation\NVSMI\, 然后输入./nvidia-smi查看显卡情况,使用./nvidia-smi -l 3定时每3秒刷新一次。在linux下则是watch -n 3 nvidia-smi。

    8、若要在caffe中添加新层,删除caffe-windows\scripts\build文件夹,按照添加新层的顺序添加完然后重新进行编绎即可。

    其他配置使用过程中也不知道还有没有忘记的,若是有小伙伴参考这个成功了请告知一下,若有错误的地方我及时修改。