Pycaffe GPU compilation process for Windows (Continued)

Posted by Kyori on Wed, 09 Feb 2022 22:30:21 +0100

0. Declaration

Current version C1 0 (for reference only).

Contact information of the author: E-mail: WindForest@yeah.net

First, the tool versions used in this article are listed as follows:

termvalue
Compile / target operating systemWindows 10 20H2 x64
VS version used in this articleVirtual Studio 2015 (VC14)
CUDA version supported by the operation steps in this articleCUDA8.0
cuDNN version used in conjunction with this articlecuDNN7.1.3
Python versionAnaconda Python3.5.4
Caffe sourcehttps://github.com/BVLC/caffe/tree/windows
Native CPU InformationIntel® Core™ i7-5500U CPU @ 2.40GHz
Local GPU informationNVIDIA GeForce 940M(Calculation force 5.0)

[recommendation]

It is better for all programs or environments to use their default directory to avoid unnecessary waste of time on environment configuration. If your computer C disk doesn't have enough space, it's better to replace it with a high-capacity SSD.

1. Version selection of VS and CUDA

Caffe framework is provided with source code, so it needs to be installed first Virtual Studio 2015(VC14) , according to some online materials, VS2013(VC12) can also compile caffe1 x. However, considering that the sample program provided by the target project is created based on VC14, VS2015 version is selected here.

If you need to compile the GPU version of Caffe, CUDA and cuDNN also need to be installed. There are some API changes in the higher version CUDA/cuDNN, so considering the suggestions given in the source code description file, CUDA8.0 is used Version 0.

The installation of VS should be before CUDA, so as to add support for VS during CUDA installation. During installation, English language pack support, Python Tools support and Windows SDK support are required. Then unzip cuDNN, copy the files to the folder with the same name in CUDA installation directory, and delete the files in CUDA installation directory \Add the lib\x64 directory to the system environment variable.

2. Python environment configuration

Install Anaconda3, find relevant information and replace the image source to speed up the download. In anaconda Create a new environment in navigator or execute the following statement in Anaconda Powershell Prompt to create a new environment:

conda create --name caffe-python3 python=3.5.4

[note]

If you create Python 3.0 in the graphical interface 5. The default installed version is Python 3 5.6.

Considering the project adaptation, I use Python 3 5.4, other small versions may be OK, but I haven't tried.

At anaconda Enter the command prompt interface of the environment in navigator or execute the following statement in Anaconda Powershell Prompt to activate the environment:

conda activate caffe-python3

Install dependent packages using the following command:

pip install numpy scipy protobuf scikit-image matplotlib pandas

[note]

These environments must be installed using pip instead of conda, otherwise python may exit without error after importing cafe.

If the installation speed is too slow, you can refer to relevant materials to replace the domestic image source for pip.

3. Adaptation and compilation of caffe

download Caffe windows bvlc project , modified/ scripts/build_win.cmd file:

  1. Modify compile switch

    ...
        :: 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                  --use VS2015
        :: Change to 1 to use Ninja generator (builds much faster)
        if NOT DEFINED WITH_NINJA set WITH_NINJA=0                       --Not used Ninja compiler
        :: Change to 1 to build caffe without CUDA support
        if NOT DEFINED CPU_ONLY set CPU_ONLY=0                           --close CPU Version compilation
        :: 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            --Automatic matching CUDA
        :: 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             --compile Release edition
        :: Set to 1 to use NCCL
        if NOT DEFINED USE_NCCL set USE_NCCL=1
        :: 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               --use Python3
        :: Change these options for your needs.
        if NOT DEFINED BUILD_PYTHON set BUILD_PYTHON=1                   --compile PyCaffe
        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
    ...
    

    [note]

    If NCCL is not enabled, it may lead to successful compilation and package import, but GPU training cannot be carried out.

  2. Global search cmake -G, add CUDNN path:

    ...
    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\.."
    ...
    
  3. Global search CONDA_ROOT, add the python installation path at the corresponding Python version:

    ...
        :: Set python 3.5 with conda as the default python
        if !PYTHON_VERSION! EQU 3 (
            set CONDA_ROOT=C:\Users\Hello\anaconda3\envs\caffe-python3
        )
    ...
    

Before you start compiling, you need to install CMake , CMake has no version restrictions. You can directly install the latest version. Then execute the script to generate VS project. If libraries appear during the generation process_ v140_ x64_ py35_ 1.1.0. tar. If bz2 download is too slow, you can manually download the file and copy it to the target directory to run the script again.

[note]

The execution of the compilation script should be carried out in the corresponding environment in Anaconda terminal, otherwise the Python environment other than Anaconda may be recognized.

Open with VS2015/ scripts/build/Caffe.sln project and generate the solution in Release x64 mode. At this time/ The content in the python / Cafe directory is the final pycafe.

4. PyCaffe installation and testing

Will/ Copy the python / Cafe directory to C:\Users \ username \ anaconda3 \ envs \ cafe-python3 \ lib \ site packages to complete the installation. Execute import cafe in the CONDA Python environment of cafe-python3 without error.

For the verification process using the example of mnist handwritten numeral recognition provided by PyCaffe, see reference 2: running mnist handwritten numeral recognition of Caffe.

reference resources

  1. Installation of cafe + vs2015 + python3 (based on windows)
  2. Caffe's running mnist handwritten numeral recognition

————2021-4-8 @ Yan Weibo————

Topics: Python Windows caffe