windows安装caffe

比较靠谱的博客:
https://blog.csdn.net/qq_42722197/article/details/122749759

2022.3.29安装完毕,如果有朋友有问题可在评论区提问(不要私信,csdn私信广告太多,楼主不看私信)

1、环境要求:
Anaconda,并创建python3.5版本的环境,并将该环境加入到环境变量(只能创建python3.5或者2.7的,其他的都不行,楼主用的3.5)
在这里插入图片描述

ninja:安装地址https://github.com/ninja-build/ninja/releases
注意:ninja-win.zip和Source code都要下载,并且将解压ninja-win后得到的ninja.exe放入Source code解压后的文件夹里,并将该文件夹添加到环境变量
在这里插入图片描述
在这里插入图片描述
Visual Studio 2015:自行安装,并添加到环境变量
cmake:自行安装最新版本(若安装了Anaconda,则可能自带的有,可以查找看看),并添加到环境变量

2、环境准备完毕后,按照官方地址https://github.com/BVLC/caffe/tree/windows所给安装方法,
依次执行

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

在执行 scripts\build_win.cmd前,修改scripts文件夹下的build_win.cmd文件(用记事本打开即可)。修改其中配置如下(注意是用ninja安装的,所以和网上大部分配置有所不同,且需要修改的地方很多,楼主就不一一指出了):

@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=0
    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 2.7 with conda as the default python
    if !PYTHON_VERSION! EQU 2 (
        set CONDA_ROOT=F:\Anaconda\file\envs\python35
    )
    :: Set python 3.5 with conda as the default python
    if !PYTHON_VERSION! EQU 3 (
        set CONDA_ROOT=F:\Anaconda\file\envs\python35
    )
    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=1
    :: 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=0
    :: 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

其中
在这里插入图片描述
改成你的python环境路径(理论上说不改也没有影响)

3、保存 build_win.cmd文件,并执行

 scripts\build_win.cmd

这部分bug是最多的,一些楼主遇到的bug详见文末
4、完成编译之后,手动把caffe\python下的caffe文件夹整个拷贝到你的python环境的Lib文件夹下的site-packages里面,如F:\Anaconda\file\envs\python35\Lib\site-packages

5、运行python3.5,import caffe,大功告成
在这里插入图片描述

一些坑:
1、新建环境变量后,cmd需重启才有效
2、报错: 无法打开包括文件: “caffe/include_symbols.hpp”
解决方法:找到并打开export.hpp,将里面include caffe/include_symbols.hpp改成include include_symbols.hpp
(根据楼主观察,caffe在编译前几个文件时又会改成include caffe/include_symbols.hpp, 需要在编译几个文件后做修改并保存)
3、要提前在python3.5的环境里安装好numpy scipy protobuf==3.1.0 six scikit-image pyyaml pydotplus graphviz
如果有pip可以运行

pip install numpy scipy protobuf==3.1.0 six scikit-image pyyaml pydotplus graphviz

4、一定要确保在cmd里敲击python、ninja、cl不会报错‘xxx’不是内部或外部命令,也不是可运行的程序,即这些文件都在环境变量里,且python版本一定要正确(可在cmd里敲击python后查看python版本)

5、报错ninja: build stopped: subcommand failed.
注意查看上面输出信息里的fatal error(要仔细找),看看里面写的什么,那个才是真正的报错原因

6、报错No CMAKE_CXX_COMPILER could be found. 注意ninja是否在环境变量里,且 if NOT DEFINED WITH_NINJA set WITH_NINJA=1(不是0)

7、‘cmake’ 不是内部或外部命令,也不是可运行的程序
确保cmake在环境变量里,若不在,请先自行百度解决方法

8、Could not find url for MSVC version = 1900 and Python version = 3.7.
python版本不是3.5,打开cmd输入python --version,查看版本。若版本不对,用Anaconda新建环境,并将新建环境添加到环境变量,具体可参考文章开头的第一步

9、The dependency target "pycaffe" of target "pytest" does

没有安装numpy

10、Unsupported gpu architecture ‘compute_20’
没有设置CPU_ONLY为1,以及cuda相关配置为0,参考文章中所给的配置选项

  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要在Windows上配置Caffe,首先需要确保已经安装了所需的软件和工具。以下是详细的步骤: 1. 安装CUDA和CUDNN:Caffe在GPU上进行加速需要CUDA和CUDNN的支持。首先,下载并安装适合你的显卡型号的CUDA驱动程序。然后,从NVIDIA的官方网站下载并安装CUDNN库。 2. 安装依赖库:在Windows上运行Caffe还需要一些依赖库。你可以使用包管理工具(如Chocolatey)快速安装这些库。运行命令`choco install git python2`来安装Git和Python。 3. 克隆Caffe:打开命令提示符,导航到你想要存储Caffe的目录,并使用以下命令克隆Caffe的代码库:`git clone https://github.com/BVLC/caffe.git` 4. 配置环境:进入Caffe目录,在根目录下创建一个新的文件夹"build",用于存储编译后的文件。然后,将模板文件夹"windows"复制到"build"文件夹内。重命名此文件夹为"CMake"。 5. 使用CMake配置:打开CMake GUI,选择"Caffe"文件夹作为源代码目录,选择"build"文件夹作为目标目录。点击"Configure"按钮选择你安装的编译器和生成器(如Visual Studio)。然后点击"Generate"生成解决方案。 6. 编译Caffe:打开生成的解决方案,选择合适的编译选项(如Release、Debug等),然后开始编译。这可能需要一些时间,取决于你的电脑配置和编译选项。 7. 配置环境变量:将Caffe的"build\bin"路径添加到系统的环境变量中,这样才能在命令提示符或其他地方运行Caffe命令。 现在,你已经成功在Windows上配置了Caffe,可以使用它进行深度学习任务了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值