win10+官方caffe+vs2015+cuda8.0+cudnn5.1+python3.5

-------------------------------------------    时间截止20190319,亲测可以使用。   --------------------------------------------

注意,万一下面有环境变量不明白的,请参考我的环境变量:
我的个人用户的环境变量为

C:\Users\smj\AppData\Local\Programs\Python\Python35\;C:\Users\smj\AppData\Local\Programs\Python\Python35\Scripts\;C:\Users\smj\AppData\Local\Microsoft\WindowsApps;C:\Users\smj\AppData\Roaming\npm;C:\Users\smj\AppData\Local\GitHubDesktop\bin;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;

系统环境变量为

下面仅仅贴了path的路径: 

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\libnvvp;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVSMI;C:\Users\smj\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\openslide-win64-20170912-nightly;C:\openslide-win64-20170912-nightly\bin;C:\openslide-win64-20170912-nightly\lib;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\nodejs\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\TortoiseGit\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Aperio\Common;%SYSTEMROOT%\System32\OpenSSH\;C:\Program Files (x86)\IDM Computer Solutions\UltraCompare;C:\Program Files\IDM Computer Solutions\UltraEdit;C:\Program Files\MATLAB\R2017a\runtime\win64;C:\Program Files\MATLAB\R2017a\bin;C:\Program Files\CMake\bin

1.安装vs2015

下载链接

ed2k://|file|cn_visual_studio_community_2015_with_update_3_x86_x64_dvd_8923246.iso|7633565696|605D19028916D09E1BFA2DABBD266FE9|/

安装vs的时候千万记得勾选c++编译器,如果不知道怎么选择就全部安装

2.python3.5

去anaconda官网下载一个python3.5安装。(我的是原版python3.5,自己额外安装的一些库)。

安装完成后,自己查看python是否添加到了环境变量中。

3.安装cuda8.0和cudnn5.1

可以参考https://blog.csdn.net/u013066730/article/details/80980940

要确认cuda8.0在环境变量中有,其中我没有添加cudnn的路径是因为我将cudnn的文件拷贝到cuda所在的文件夹下了,我的cudnn路径为C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin\cudnn64_6.dll;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include\cudnn.h;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64\cudnn.lib;

cuda版本为:

cudnn版本为:

4.安装CMAKE

需要3.4版本以上,我用的是3.13.4,https://cmake.org/download/

5.下载官方的caffe

下载官方BVLC版本的caffe ,https://github.com/BVLC/caffe/tree/windows

6.生成可编译文件

用notepad修改caffe路径下scripts文件下build_win.cmd

@echo off
@setlocal EnableDelayedExpansion

:: Default values #这是默认值,可以修改也可不修改。这里我就没有修改
if DEFINED APPVEYOR (
    echo Setting Appveyor defaults
    if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14
    if NOT DEFINED WITH_NINJA set WITH_NINJA=1
    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=2
    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=C:\Miniconda-x64
    )
    :: Set python 3.5 with conda as the default python
    if !PYTHON_VERSION! EQU 3 (
        set CONDA_ROOT=C:\Miniconda35-x64
    )
    set PATH=!CONDA_ROOT!;!CONDA_ROOT!\Scripts;!CONDA_ROOT!\Library\bin;!PATH!

    :: Check that we have the right python version
    !PYTHON_EXE! --version
    :: Add the required channels
    conda config --add channels conda-forge
    conda config --add channels willyd
    :: Update conda
    conda update conda -y
    :: Download other required packages
    conda install --yes cmake ninja numpy scipy protobuf==3.1.0 six scikit-image pyyaml pydotplus graphviz

    if ERRORLEVEL 1  (
      echo ERROR: Conda update or install failed
      exit /b 1
    )

    :: Install cuda and disable tests if needed
    if !WITH_CUDA! == 1 (
        call %~dp0\appveyor\appveyor_install_cuda.cmd
        set CPU_ONLY=0
        set RUN_TESTS=0
        set USE_NCCL=1
    ) else (
        set CPU_ONLY=1
    )

    :: Disable the tests in debug config
    if "%CMAKE_CONFIG%" == "Debug" (
        echo Disabling tests on appveyor with config == %CMAKE_CONFIG%
        set RUN_TESTS=0
    )

    :: Disable linting with python 3 until we find why the script fails
    if !PYTHON_VERSION! EQU 3 (
        set RUN_LINT=0
    )

) else ( #######这里是需要修改的,根据自己的电脑环境和需要进行相应的修改,具体我是vs2015,python3.5,需要使用gpu,cuda8.0,cudnn5.1
    :: 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
)

:: Set the appropriate CMake generator
:: Use the exclamation mark ! below to delay the
:: expansion of CMAKE_GENERATOR
if %WITH_NINJA% EQU 0 (
    if "%MSVC_VERSION%"=="14" (
        set CMAKE_GENERATOR=Visual Studio 14 2015 Win64
    )
    if "%MSVC_VERSION%"=="12" (
        set CMAKE_GENERATOR=Visual Studio 12 2013 Win64
    )
    if "!CMAKE_GENERATOR!"=="" (
        echo ERROR: Unsupported MSVC version
        exit /B 1
    )
) else (
    set CMAKE_GENERATOR=Ninja
)

echo INFO: ============================================================
echo INFO: Summary:
echo INFO: ============================================================
echo INFO: MSVC_VERSION               = !MSVC_VERSION!
echo INFO: WITH_NINJA                 = !WITH_NINJA!
echo INFO: CMAKE_GENERATOR            = "!CMAKE_GENERATOR!"
echo INFO: CPU_ONLY                   = !CPU_ONLY!
echo INFO: CUDA_ARCH_NAME             = !CUDA_ARCH_NAME!
echo INFO: CMAKE_CONFIG               = !CMAKE_CONFIG!
echo INFO: USE_NCCL                   = !USE_NCCL!
echo INFO: CMAKE_BUILD_SHARED_LIBS    = !CMAKE_BUILD_SHARED_LIBS!
echo INFO: PYTHON_VERSION             = !PYTHON_VERSION!
echo INFO: BUILD_PYTHON               = !BUILD_PYTHON!
echo INFO: BUILD_PYTHON_LAYER         = !BUILD_PYTHON_LAYER!
echo INFO: BUILD_MATLAB               = !BUILD_MATLAB!
echo INFO: PYTHON_EXE                 = "!PYTHON_EXE!"
echo INFO: RUN_TESTS                  = !RUN_TESTS!
echo INFO: RUN_LINT                   = !RUN_LINT!
echo INFO: RUN_INSTALL                = !RUN_INSTALL!
echo INFO: ============================================================

:: Build and exectute the tests
:: Do not run the tests with shared library
if !RUN_TESTS! EQU 1 (
    if %CMAKE_BUILD_SHARED_LIBS% EQU 1 (
        echo WARNING: Disabling tests with shared library build
        set RUN_TESTS=0
    )
)

if NOT EXIST build mkdir build
pushd build

:: Setup the environement for VS x64
set batch_file=!VS%MSVC_VERSION%0COMNTOOLS!..\..\VC\vcvarsall.bat
call "%batch_file%" amd64

:: Configure using cmake and using the caffe-builder dependencies
:: Add -DCUDNN_ROOT=C:/Projects/caffe/cudnn-8.0-windows10-x64-v5.1/cuda ^
:: below to use cuDNN
cmake -G"!CMAKE_GENERATOR!" ^
      -DBLAS=Open ^
      -DCMAKE_BUILD_TYPE:STRING=%CMAKE_CONFIG% ^
      -DBUILD_SHARED_LIBS:BOOL=%CMAKE_BUILD_SHARED_LIBS% ^
      -DBUILD_python:BOOL=%BUILD_PYTHON% ^
      -DBUILD_python_layer:BOOL=%BUILD_PYTHON_LAYER% ^
      -DBUILD_matlab:BOOL=%BUILD_MATLAB% ^
      -DCPU_ONLY:BOOL=%CPU_ONLY% ^
      -DCOPY_PREREQUISITES:BOOL=1 ^
      -DINSTALL_PREREQUISITES:BOOL=1 ^
      -DUSE_NCCL:BOOL=!USE_NCCL! ^
      -DCUDA_ARCH_NAME:STRING=%CUDA_ARCH_NAME% ^
      "%~dp0\.."

if ERRORLEVEL 1 (
  echo ERROR: Configure failed
  exit /b 1
)

:: Lint
if %RUN_LINT% EQU 1 (
    cmake --build . --target lint  --config %CMAKE_CONFIG%
)

if ERRORLEVEL 1 (
  echo ERROR: Lint failed
  exit /b 1
)

:: Build the library and tools
cmake --build . --config %CMAKE_CONFIG%

if ERRORLEVEL 1 (
  echo ERROR: Build failed
  exit /b 1
)

:: Build and exectute the tests
if !RUN_TESTS! EQU 1 (
    cmake --build . --target runtest --config %CMAKE_CONFIG%

    if ERRORLEVEL 1 (
        echo ERROR: Tests failed
        exit /b 1
    )

    if %BUILD_PYTHON% EQU 1 (
        if %BUILD_PYTHON_LAYER% EQU 1 (
            :: Run python tests only in Release build since
            :: the _caffe module is _caffe-d is debug
            if "%CMAKE_CONFIG%"=="Release" (
                :: Run the python tests
                cmake --build . --target pytest

                if ERRORLEVEL 1 (
                    echo ERROR: Python tests failed
                    exit /b 1
                )
            )
        )
    )
)

if %RUN_INSTALL% EQU 1 (
    cmake --build . --target install --config %CMAKE_CONFIG%
)

popd
@endlocal

修改完毕后,返回上级目录,cmd下执行下面的命令。

在caffe文件下 cmd下执行scripts\build_win.cmd

最后

 在执行scripts\build_win.cmd命令后,会自动下载依赖并且编译,但是如果网比较差可能下载不成功,这样就需要进行下面的手动下载操作了。

如果下载依赖不成功,请自行下载:

下载地址https://github.com/willyd/caffe-builder/releases,由于我的是vs2015和python3,所以下载的是libraries_v140_x64_py35_1.1.0.tar.bz2

放到路径C:\Users\smj\.caffe\dependencies\download,没有这个路径自己在C:\Users\xxx\(xxx就是你的用户名)目录下创建一个,然后自己解压文件到C:\Users\smj\.caffe\dependencies。具体的目录结构如下图所示:

 

 

完毕后在caffe下可以找到Caffe.sln,使用VS2015打开,对整个工程进行编译即可。

完毕后就会生成需要的库文件。

7.测试caffe.exe

(1)mnist官网下载数据集http://yann.lecun.com/exdb/mnist/

 下载完后记得解压。

然后自己制作上图中的mnist_test_lmdb,mnist_train_lmdb。

创建create_train_minst.bat:

D:\smj\myproject\origin_caffe\caffe-windows\build\examples\mnist\Release\convert_mnist_data.exe D:\smj\myproject\origin_caffe\test_caffe_code\mnist\train-images.idx3-ubyte D:\smj\myproject\origin_caffe\test_caffe_code\mnist\train-labels.idx1-ubyte D:\smj\myproject\origin_caffe\test_caffe_code\mnist\mnist_train_lmdb
pause

双击运行。

创建 create_test_mnist.bat:

D:\smj\myproject\origin_caffe\caffe-windows\build\examples\mnist\Release\convert_mnist_data.exe D:\smj\myproject\origin_caffe\test_caffe_code\mnist\t10k-images.idx3-ubyte D:\smj\myproject\origin_caffe\test_caffe_code\mnist\t10k-labels.idx1-ubyte D:\smj\myproject\origin_caffe\test_caffe_code\mnist\mnist_test_lmdb
pause

双击运行。

(2)在路径D:\smj\myproject\origin_caffe\caffe-windows\examples\mnist下修改lenet_train_test.prototxt:

name: "LeNet"
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "D:/smj/myproject/origin_caffe/test_caffe_code/mnist/mnist_train_lmdb"#######修改
    batch_size: 64
    backend: LMDB
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "D:/smj/myproject/origin_caffe/test_caffe_code/mnist/mnist_test_lmdb"#######修改
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

注意:修改加#######号处,相信你看得懂,就是刚才拷贝的两个文件夹的绝对路径;以及两处格式

(3)修改D:\smj\myproject\origin_caffe\caffe-windows\examples\mnist下的lenet_solver.prototxt文件:

# The train/test net protocol buffer definition
net: "D:/smj/myproject/origin_caffe/caffe-windows/examples/mnist/lenet_train_test.prototxt" #####################################需要修改
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100
# Carry out testing every 500 training iterations.
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations
display: 100
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 5000
snapshot_prefix: "D:/smj/myproject/origin_caffe/test_caffe_code/mnist_model/"###########需要修改
# solver mode: CPU or GPU
solver_mode: GPU #######################需要修改

注意:修改加#处文字,相信你看得懂,就是修改文件或者文件夹的绝对路径;如果你的硬件不支持GPU编程,最后一句修改为CPU。

(4)在minist文件夹下编写批处理文件run.bat,内容如下:

D:\smj\myproject\origin_caffe\caffe-windows\build\tools\Release\caffe.exe train --solver=D:\smj\myproject\origin_caffe\caffe-windows\examples\mnist\lenet_solver.prototxt
Pause

说明:第一行,就是执行你编译得到的caffe.exe,所以需要换成你的caffe.exe的绝对路径。

注意:

·       斜杠必须使用backslash:\。使用Slash:/会出错。

·       train 和—solver之间有一空格,不能省略。无空格会出错。

(5)​双击run.bat程序能够运行,并且之后也不会报错,那么恭喜你,Caffe-Windows配置成功了。

8.在python中测试caffe

在我编译完成后,先贴一张编译前后D:\smj\myproject\origin_caffe\caffe-windows\python\caffe中有什么不同。

没有编译时,python文件夹中是这样

编译完成后,python文件夹是这样

不难看出添加了dll文件以及pyd文件。这时我们就可以在python中使用caffe了,具体使用代码如下,如果直接运行没有报错,说明没有问题。

import sys
caffe_root= r'D:\smj\myproject\origin_caffe\caffe-windows'
sys.path.insert(0, caffe_root + '\\python')

import caffe
from caffe import layers as L, params as P

除了强制将caffe的路径加到python的命名空间中。还可以将caffe文件夹(这个文件夹是caffe-windows文件夹下D:\smj\myproject\origin_caffe\caffe-windows\python\caffe,不是build文件夹中的python\caffe)直接拷贝到python的文件夹中,我的python文件夹为C:\Users\smj\AppData\Local\Programs\Python\Python35\Lib\site-packages。

这样,就可以直接import caffe了,但是我不清楚这样会不会导致某些功能的缺失。

参考:https://blog.csdn.net/chris_zhangrx/article/details/79096015 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值