欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Windows10下安装VS2015和Caffe

程序员文章站 2022-05-21 15:02:23
...

##一,准备条件

1:下载Caffe-Windows(BLVC),地址:https://github.com/BVLC/caffe/tree/windows,并解压到指定的文件夹Caffe下。

2:安装CMake3.4以上版本,并将其安装包下的bin路径到系统环境Path变量中。

3,下载anaconda 3 4.2.0 (一定要python3.5或者2.5版本的,本文是3.5的)https://repo.continuum.io/archive/

4,VS2015

5,编译时下载的包文件:https://github.com/willyd/caffe-builder/releases/download/v1.0.1/libraries_v140_x64_py35_1.0.1.tar.bz2

##二,开始安装

1,安装VS2015,详细教程见以下链接 :VS2015安装教程

2,在GitHub中下载caffe源码:https://github.com/BVLC/caffe/tree/windows,解压到D:\Caffe_Prj\下。

3,依次分别安装anaconda3,Cmake3.4,安装完成后,将python.exe文件和cmake.exe所在的路径添加到path环境变量中。
Windows10下安装VS2015和Caffe

##二,开始安装

1,安装VS2015,详细教程见以下链接 :VS2015安装教程

2,在GitHub中下载caffe源码:https://github.com/BVLC/caffe/tree/windows,解压到D:\Caffe_Prj\下。

3,依次分别安装anaconda3,Cmake3.4,安装完成后,将python.exe文件和cmake.exe所在的路径添加到path环境变量中。

4,进入scripts文件夹,把build_win.cmd的后缀改成txt,然后用编辑器打开,参考以下修改:

@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=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





:: Set python 3.5 with conda as the default python

if !PYTHON_VERSION! EQU 3 (

set CONDA_ROOT=D:\MachineLearning\VS2015_CAFFE\anaconda3

)

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 (

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

)



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

其中需要注意的是:

                Line8:填写0
                
                Line9:填写1
                
                Line10:填写Auto
                
                Line14:填写3,表示编译器使用的是python3版本的
                
                Line24:if判断注释掉::,因为本文用python3编译的
                
                Line29:填写刚才安装python3安装包python.exe所在的路径、
                
                Line74:填0
                
                Line76:1
                
                Line79:Auto
                
                Line87:3
                
                Line150--Line159:注释::
                
                
                
 5,开始编译

第一步:在Caffe文件下shift+右键,点击“在此处打开命令窗口”,然后将Caffe\scripts下的build_win.bat文件拖到cmd窗口中,回车并编译。 这时候用python3.6的会报错:

cannot find url for MSVC_VERSION:1900 and Python version:3.7

大致是这样的错误,根据错误文档找到了文件:WindowsDownloadPre

builtDependencies,用文本编辑器,把文件里面的python3.5全部改为Python3.7(报错的版本,就是你刚才安装的的python版本),因为之前支持的Anaconda仅到3.5,所以文件一直没改动。

set(DEPENDENCIES_VERSION 1.1.0)
        
set(DEPENDENCIES_NAME_1800_27 libraries_v120_x64_
    
py27_${DEPENDENCIES_VERSION})
    
set(DEPENDENCIES_NAME_1900_27 libraries_v140_x64_py27_${DEP

ENDENCIES_VERSION})

set(DEPENDENCIES_NAME_1900_37 libraries_v140_x64_py35_${DEPENDENCIES_VERSION})



set(DEPENDENCIES_URL_BASE https://github.com/willyd/caffe-builder/releases/download)

set(DEPENDENCIES_FILE_EXT .tar.bz2)

set(DEPENDENCIES_URL_1800_27 "${DEPENDENCIES_URL_BASE}/v${DEPENDENCIES_VERSION}/${DEPENDENCIES_NAME_1800_27}${DEPENDENCIES_FILE_EXT}")

set(DEPENDENCIES_SHA_1800_27 "ba833d86d19b162a04d68b09b06df5e0dad947d4")

set(DEPENDENCIES_URL_1900_27 "${DEPENDENCIES_URL_BASE}/v${DEPENDENCIES_VERSION}/${DEPENDENCIES_NAME_1900_27}${DEPENDENCIES_FILE_EXT}")

set(DEPENDENCIES_SHA_1900_27 "17eecb095bd3b0774a87a38624a77ce35e497cd2")

set(DEPENDENCIES_URL_1900_37 "${DEPENDENCIES_URL_BASE}/v${DEPENDENCIES_VERSION}/${DEPENDENCIES_NAME_1900_35}${DEPENDENCIES_FILE_EXT}")

set(DEPENDENCIES_SHA_1900_37 "da39a3ee5e6b4b0d3255bfef95601890afd80709")



caffe_option(USE_PREBUILT_DEPENDENCIES "Download and use the prebuilt dependencies" ON IF MSVC)

if(MSVC)

  file(TO_CMAKE_PATH $ENV{USERPROFILE} USERPROFILE_DIR)

  if(NOT EXISTS ${USERPROFILE_DIR})

message(FATAL_ERROR "Could not find %USERPROFILE% directory. Please specify an alternate CAFFE_DEPENDENCIES_ROOT_DIR")

  endif()

  set(CAFFE_DEPENDENCIES_ROOT_DIR ${USERPROFILE_DIR}/.caffe/dependencies CACHE PATH "Prebuild depdendencies root directory")

  set(CAFFE_DEPENDENCIES_DOWNLOAD_DIR ${CAFFE_DEPENDENCIES_ROOT_DIR}/download CACHE PATH "Download directory for prebuilt dependencies")

endif()

if(USE_PREBUILT_DEPENDENCIES)

# Determine the python version

if(BUILD_python)

if(NOT PYTHONINTERP_FOUND)

if(NOT "${python_version}" VERSION_LESS "3.0.0")

find_package(PythonInterp 3.7)

else()

find_package(PythonInterp 2.7)

endif()

endif()

set(_pyver ${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR})

else()

message(STATUS "Building without python. Prebuilt dependencies will default to Python 2.7")

set(_pyver 27)

endif()

if(NOT DEFINED DEPENDENCIES_URL_${MSVC_VERSION}_${_pyver})

message(FATAL_ERROR "Could not find url for MSVC version = ${MSVC_VERSION} and Python version = ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}.")

endif()

# set the dependencies URL and SHA1

set(DEPENDENCIES_URL ${DEPENDENCIES_URL_${MSVC_VERSION}_${_pyver}})

set(DEPENDENCIES_SHA ${DEPENDENCIES_SHA_${MSVC_VERSION}_${_pyver}})

set(CAFFE_DEPENDENCIES_DIR ${CAFFE_DEPENDENCIES_ROOT_DIR}/${DEPENDENCIES_NAME_${MSVC_VERSION}_${_pyver}})



foreach(_dir ${CAFFE_DEPENDENCIES_ROOT_DIR}

 ${CAFFE_DEPENDENCIES_DOWNLOAD_DIR}

 ${CAFFE_DEPENDENCIES_DIR})

  # create the directory if it does not exist

  if(NOT EXISTS ${_dir})

file(MAKE_DIRECTORY ${_dir})

  endif()

endforeach()

# download and extract the file if it does not exist or if does not match the sha1

get_filename_component(_download_filename ${DEPENDENCIES_URL} NAME)

set(_download_path ${CAFFE_DEPENDENCIES_DOWNLOAD_DIR}/${_download_filename})

set(_download_file 1)

if(EXISTS ${_download_path})

file(SHA1 ${_download_path} _file_sha)

if("${_file_sha}" STREQUAL "${DEPENDENCIES_SHA}")

set(_download_file 0)

else()

set(_download_file 1)

message(STATUS "Removing file because sha1 does not match.")

file(REMOVE ${_download_path})

endif()

endif()

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()

if(EXISTS ${_download_path} AND NOT EXISTS ${CAFFE_DEPENDENCIES_DIR}/libraries)

message(STATUS "Extracting dependencies")

execute_process(COMMAND ${CMAKE_COMMAND} -E tar xjf ${_download_path}

WORKING_DIRECTORY ${CAFFE_DEPENDENCIES_DIR}

)

endif()

if(EXISTS ${CAFFE_DEPENDENCIES_DIR}/libraries/caffe-builder-config.cmake)

include(${CAFFE_DEPENDENCIES_DIR}/libraries/caffe-builder-config.cmake)

else()

message(FATAL_ERROR "Something went wrong while dowloading dependencies could not open caffe-builder-config.cmake")

endif()

endif()

第二步:第一步会出错,因为下载libraries_v140_x64_py35_1.0.1.tar失败,这需要手动的下载。这个会放在我的百度网盘里面,一会儿同意给出链接。

第三步:下载后将其拷贝到用户目录下:.caffe\download目录下,我的目录是C:\Users\Administrator.caffe\dependencies\download

,这里可以先不着急拷贝进去,因为当前文件夹可能还没生成,我也是在报错的时候才找到这个文件夹的。同时要,重新命名为.tar。

Line13:下载的那个库文件包的解压sha码,系统报错时候会给出前后这两个码区别,你把我们自己下载的压缩包的sha码赋值,覆盖这一行的sha码。

Tip:关于这个Libraries下载存放位置,可以参考下make文件中路径,也可以参考下,Dos界面报错的路径,报错提示:…caffe\dependencies\libraries_v140_x64_py35_1.0.1\libraries\bin下缺少***库文件,此时,libraries_v140_x64_py35_1.0.1.tar解压出来的libraries文件夹应该放在libraries_v140_x64_py35_1.0.1\下一级,并且将子目录下的bin/lib/x64/vc14/bin文件放在path下。

其他问题:(我没有遇到,网上其他技术友反映的)

问题:error MSB6006: “cmd.exe” 退出,错误代码 -1073741515

编译报错,找不到包含文件pthread.h,error MSB6006: “cmd.exe” 退出,错误代码 -1073741515。这个神奇的问题困扰了我好几天,翻遍了网上的犄角旮旯也都没找到答案。我开始以为真的是因为缺少pthread.h文件,就下载了thread压缩包添加到caffe文件夹中,但是当然没有用啦!

后来我灰心之下尝试了Google论坛上一个小小的不起眼的建议,重新下载protoc库文件并替换依赖包中相应的文件,居然成了!

6,删除build文件夹下(自己建立的build)除了cmd编译文件以外的所有文件,重新打开DOS执行一遍,此时你会发现,在这个文件夹下生成一个:caffe.sln的VS2015项目文件。

7,打开这个caffe.sln,点击解决方案,选择Release,鼠标右键–重新生成,之后便找到Release文件夹下的caffe.exe,将此路径加入到path中。

8,之后便是测试数据集,参考这个链接:https://www.e-learn.cn/content/qita/1193355