windows下配置cpu版caffe+python

想在自己电脑上配置一下caffe环境,搜了一些教程发现很多都是过时的,官网的版本现在已经更新了,很多方法并不具有参考价值。因此自己参考官网的指导进行了安装。配置时间为2019/09/16,官网地址:https://github.com/BVLC/caffe/tree/windows

This is an experimental, community based branch led by Guillaume Dumont (@willyd). It is a work-in-progress.

也就是说本人安装的时候该windows版本可能并不完善,后面估计会改进。

1 Requirements

官网提供了这几个版本的配置,但是我看的时候这些链接都是无效的,点进去并没有任何东西。只能自己搞,我这里选的是VS2015+Python3.5+CPU。
config

  1. Visual Studio 2015
    首先如果自己电脑上有其他版本的Visual Studio,请卸载,保证卸载干净。然后去官网下载对应版本进行安装,这是链接https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/
  2. Python3.5
    我这里使用的是Anaconda,首先这篇文章给出了Anaconda版本和python版本的对应关系,python3.5系列的都可以下载,我这里下载的是Anaconda3-4.2.0(之前用的4.0.0发现spyder内核存在一些问题)。如果官网下载太慢可以去清华大学开源镜像网站下载https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/。注意在安装过程中确保将其添加在环境变量中。
  3. CMAKE3.4或更高版本
    自行搜索下载,我这里下载的最新版本,同样确保添加在环境变量中。
  4. Git
    这里主要是为了在windows下使用git命令,在https://gitforwindows.org/下载安装即可。如果有其他方法使用git命令也可。

2 安装

上面的配置好之后,安装其实很简单,官网给的代码如下:

C:\Projects> git clone https://github.com/BVLC/caffe.git
C:\Projects> cd caffe
C:\Projects\caffe> git checkout windows
:: Edit any of the options inside build_win.cmd to suit your needs
C:\Projects\caffe> scripts\build_win.cmd

前三句应该都没什么问题,下载工程然后切换为windows。但是不要急着运行最后一句,还需要对scripts文件夹里的build_win.cmd进行修改。

build_win.cmd修改

这里发现里面有一个条件语句,涉及两个部分的setting,为了以防万一将两部分都做修改。首先是刚开始第一部分的setting(第5行开始),主要修改WITH_NINJA=0(使用VS编译而不使用NINJA),CPU_ONLY=1,PYTHON_VERSION=3。

if DEFINED APPVEYOR (
    echo Setting Appveyor defaults
    if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14
    if NOT DEFINED WITH_NINJA set WITH_NINJA=0
    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
    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

另一部分的setting同样进行修改(第69行开始),个人感觉这一部分应该是我们要用的,因为我刚开始只修改了第一部分,结果build失败了:

) 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=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
    :: 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
)

除此之外对python路径进行修改,改到自己的Anaconda目录下(第24行开始):

    if !PYTHON_VERSION! EQU 2 (
        set CONDA_ROOT=D:\Anaconda2
    )
    :: Set python 3.5 with conda as the default python
    if !PYTHON_VERSION! EQU 3 (
        set CONDA_ROOT=D:\Anaconda3
    )

这里就已经对build_win.cmd文件修改完毕了。

python 配置

因为要编译pycaffe,所以我在这里按照官网的要求添加channel并下载了几个包。(这一步骤可能不是必须的,build_win.cmd好像会自行下载包)

conda config --add channels conda-forge
conda config --add channels willyd
conda install --yes cmake ninja numpy scipy protobuf==3.1.0 six scikit-image pyyaml pydotplus graphviz

build

之后直接进行build即可,在caffe目录下运行:
C:\Projects\caffe> scripts\build_win.cmd 编译成功。

如果要使用pycaffe,有两种方法:

In order to use the python interface you need to either add the C:\Projects\caffe\python folder to your python path or copy the C:\Projects\caffe\python\caffe folder to your site_packages folder.

我这里用的第二种方法,将caffe\python\caffe文件夹拷贝到Anaconda3\Lib\site-packages下,然后在python中import caffe,成功即可。

至此caffe安装成功,之后使用mnist数据集进行测试,测试成功。测试可参考这一博文:https://www.cnblogs.com/denny402/p/5041122.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值