1. 程式人生 > >windows10+cuda8.0+cudnn6.0+caffe+vs2015

windows10+cuda8.0+cudnn6.0+caffe+vs2015

我們之前介紹了使用vs2013來安裝caffe,但是這樣使用的python版本是2.7,然後在windows的python2.7中,並沒有支援的tensorflow,這就很尷尬了。對於既要使用tensorflow,又要使用caffe的python介面的我來說,不想來回切換python版本,因此下面將嘗試安裝caffe+python3介面

1. cuda+cudnn安裝
    關於cuda和cudnn的安裝,在此就不在贅述了
2. vs2015安裝
    關於vs2015的安裝,這裡也不在贅述了
3. cmake的安裝
    這裡需要注意的是cmake一定要比vs晚安裝,不然呼叫cmake編譯程式時,會找不到編譯器版本。
    只需將cmake下載解壓,並將其中的bin目錄放入環境變數的path中即可。
3. caffe下載
    caffe下載地址:[caffe][1]
4.安裝caffe
    解壓檔案後,進入scripts資料夾,使用Notep++開啟build_win.cmd,修改裡面的配置,
if DEFINED APPVEYOR (
    echo Setting Appveyor defaults
    if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14 ::vs的版本
    if NOT DEFINED WITH_NINJA set WITH_NINJA=0
    if NOT DEFINED CPU_ONLY set CPU_ONLY=0
    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的版本 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=0 if NOT DEFINED RUN_LINT set RUN_LINT=0 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 ) ::設定python3的路徑 :: Set python 3.5 with conda as the default python if !PYTHON_VERSION! EQU 3 ( set CONDA_ROOT=D:\Anaconda\envs\tensorflow ) 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 :: 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=1 ) :: 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 ) ...... :: Setup the environement for VS x64 set batch_file=!VS%MSVC_VERSION%0COMNTOOLS!..\..\VC\vcvarsall.bat :: echo !VS%MSVC_VERSION%0COMNTOOLS!..\.. :: set batch_file=D:\Program Files (x86)\Microsoft Visual Studio 14.0\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% ^ -DCUDNN_ROOT=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0 ^ "%~dp0\.."
    該檔案可以按照上面的方式進行修改。
5. 編譯caffe
    編譯caffe是最為複雜和困難的一步,裡面有很多很多的坑。
    開啟cmd,執行build_win.cmd
    problem 1:找不到可以使用的編譯器版本
    solution :把C:\Program Files(x86)\Windows Kits\8.1\bin\x86新增到環境變數path中,然後把C:\Program Files (x86)\Windows Kits\8.1\bin\x86裡的rc.exe&rcdll.dll複製到C:\Program Files(x86)\Micrsoft Visual Studio 14.0\VC\bin ,再次執行build_win.cmd即可。
    以上內容參考[部落格][2]:https://blog.csdn.net/Uranus1024/article/details/78430308
    如果還是找不到編譯器版本,那說明vs2015安裝的程式包無法滿足我們的需求,開啟vs2015 installers,選擇修改,我們需要安裝的包有Visual C++&windows 10 SDK(10.0.14393)這幾個程式包是必須要安裝的,可以檢查一下是否安裝齊全,vs2015的自動安裝可能不包括這些程式包。


    problem 2:無法下載依賴庫
    solution:依賴庫有很大的可能行不能下載,在這裡提供一個可用的[下載地址][3],根據我們的需要,下載libraries_v140_x64_py35_1.1.0.tar.bz2,然後將該壓縮包放置在C:\Users\XXX\.caffe\dependencies\download資料夾下,同時修改caffe\cmake\WindowsDownloadPrebuiltDependencies.cmake檔案70-80行,將他們都註釋掉。
    #if(_download_file)
    #    message(STATUS "Downloading prebuilt dependencies to ${_download_path}")
    #    file(DOWNLOAD "${DEPENDENCIES_URL}"
    #                  "${_download_path}"
    #                  EXPECTED_HASH SHA1=${DEPENDENCIES_SHA}
    #                  SHOW_PROGRESS
    #                  )
    #    if(EXISTS ${CAFFE_DEPENDENCIES_DIR}/libraries)
    #        file(REMOVE_RECURSE ${CAFFE_DEPENDENCIES_DIR}/libraries)
    #    endif()
    #endif()
    problem 3:找不到pthread.h標頭檔案
    solution: [pthread下載地址][4],下載後解壓,發現有3個資料夾,我們主要使用的資料夾是Pre-built.2,首先將裡面的include和lib資料夾裡的所有檔案複製到vs安裝目錄的include和lib資料夾下,然後將dll裡的x64資料夾裡的兩個檔案複製到C:\Windows\System32下,將x86資料夾裡的5個檔案複製到C:\Windows\SysWOW64,最後在vs的安裝目錄的include裡找到pthread.h檔案,33-35行新增
#if !defined( PTHREAD_H )
#define PTHREAD_H
#define HAVE_STRUCT_TIMESPEC
    以上內容參考[部落格][5]:https://blog.csdn.net/hanmx_x/article/details/69541911
    和[部落格][6]:https://blog.csdn.net/shujun19941226/article/details/51526506
    這樣就可以成功編譯了
6. vs2015編譯
    在build_win.cmd編譯完成後,在scripts的build資料夾下有Caffe.sln,使用vs2015開啟,並進行Release操作,等待完成,這是一個漫長的過程,然後回到caffe\python資料夾下,將caffe放入python3的路徑中,在此是D:\Anaconda\envs\tensorflow,在使用pip安裝scikit-image和protobuf這兩個包,就完成caffe的安裝啦~