淘先锋技术网

首页 1 2 3 4 5 6 7

最近因为要做识别方面用到了caffe深度学习相关的东西,本人花费了两天终于把caffemodel跑出来了,由于其中走了很多弯路,所以就想写篇文章来记录一下。

首先,环境配置如下:

windows10

vs2015

Anaconda3_4.2(要用python35,就是这个版本,会出各种问题,各位要是已经安装了36的话,建议还是换一下吧,本人试了几次,好像都是会报各种错。。。)

cmake-3.11.0

cudnn-8.0-windows10-x64-v5.1

cuda_8.0.44_win10

首先确保python和cmake各种环境变量配置完成,网上有很多教程,不再多说。

一、准备

1.安装cuda,官网下载cuda8.0 这里 选择


然后双击解压缩,随便一个文件夹,因为安装完成后会自动删除。

然后安装,选择自定义安装,选择第一个就好,然后自定义文件夹。。安装完成


然后把cudnn-8.0-windows10-x64-v5.1 点击这里下载 解压出三个文件,复制到cuda的安装目录,合并(注册,填写一些东西)。

全部文件下载安装完成后,进入下一步。。

2.从git检出caffe-windows

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

二、开始配置caffe文件

1.打开scripts的build-win.cmd文件,修改python为3,

if DEFINED APPVEYOR (
    echo Setting Appveyor defaults
    if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14 :: 选择vs版本 vs2015为14
    if NOT DEFINED WITH_NINJA set WITH_NINJA=0 :: 不使用ninja
    if NOT DEFINED CPU_ONLY set CPU_ONLY=1 
    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 :: python版本 3.5
    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 NOT DEFINED PYTHON_EXE set PYTHON_EXE=python
    if NOT DEFINED RUN_TESTS set RUN_TESTS=1
    if NOT DEFINED RUN_LINT set RUN_LINT=1
    if NOT DEFINED RUN_INSTALL set RUN_INSTALL=1

    :: Set python 2.7 with conda as the default python
    if !PYTHON_VERSION! EQU 2 (
        set CONDA_ROOT=XXX\Anaconda3_4.2 :: Anaconda 的安装目录,下同
    )
    :: Set python 3.5 with conda as the default python
    if !PYTHON_VERSION! EQU 3 (
        set CONDA_ROOT=XXX\Anaconda3_4.2
    )
    set PATH=!CONDA_ROOT!;!CONDA_ROOT!\Scripts;!CONDA_ROOT!\Library\bin;!PATH!
...

) else (
    :: 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 :: 若是只是用CPU 这里修改成1
    :: 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 :: 修改为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 :: python 版本
    :: 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
)

若是使用GPU则添加一行 如上。

2.复制Makefile.config.example,重命名为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 # 使用cudnn

# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1 # 若是只使用CPU

# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
USE_LMDB := 0 # 数据方面使用leveldb
# 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. 由于我们使用的是cuda8.0 所以把compute_60 和 61有关的都注释掉,不然在vs2015中会报错
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 := open
# 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 目录
PYTHON_INCLUDE := XXX/Anaconda3_4.2/include \
		XXX/Anaconda3_4.2/Lib/site-packages/numpy/core/include/numpy
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
# ANACONDA_HOME := $(HOME)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		# $(ANACONDA_HOME)/include/python2.7 \
		# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include


# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := XXX/Anaconda3_4.2 \ # ....
# We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := XXX/Anaconda3_4.2/Lib
# PYTHON_LIB := $(ANACONDA_HOME)/lib
# Whatever else you find you need goes here. caffe的配置输出目录 
INCLUDE_DIRS := $(PYTHON_INCLUDE) XXX/caffe/build/include
LIBRARY_DIRS := $(PYTHON_LIB) XXX/caffe/build/lib XXX/caffe/build/lib
至此配置完成

执行build_win.cmd 等一会...

在scripts目录下有一个build文件,用VS2015打开其中的Caffe.sln 文件,然后用Release 生成,估计要等一会,要是没有报错,caffe至此就配置完成

。。明天更新训练自己的数据部分。有问题留言大家一起交流,小白一只,大神轻喷...