Friday, March 6, 2020

Build Caffe for VS 2017 (2)


In the previous posting, we built prerequisite libraries for Caffe deep learning framework in Windows 10 with VS 2017.

Now it is time to build Caffe itself.

Because we are going to use Caffe in Windows, we need to check and clone Windows Caffe repo.

Like the case of Caffe-Builder in the previous posting, we cannot build Caffe by following the walk-through in README file.

We need few modifications in the following file:
%caffe-root%/scripts/build_win.exe


69     ) else (
70         :: Change the settings here to match your setup
71         :: Change MSVC_VERSION to 12 to use VS 2013
72    -    if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14
72    +    if NOT DEFINED MSVC_VERSION set MSVC_VERSION=15
73         :: Change to 1 to use Ninja generator (builds much faster)
74         if NOT DEFINED WITH_NINJA set WITH_NINJA=1
75         :: Change to 1 to build caffe without CUDA support
76         if NOT DEFINED CPU_ONLY set CPU_ONLY=0
77         :: Change to generate CUDA code for one of the following GPU architectures
78         :: [Fermi  Kepler  Maxwell  Pascal  All]
79    -    if NOT DEFINED CUDA_ARCH_NAME set CUDA_ARCH_NAME=Auto
79    +    if NOT DEFINED CUDA_ARCH_NAME set CUDA_ARCH_NAME=Pascal
80         :: 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
81         if NOT DEFINED CMAKE_CONFIG set CMAKE_CONFIG=Release
82         :: Set to 1 to use NCCL
83         if NOT DEFINED USE_NCCL set USE_NCCL=0
84         :: Change to 1 to build a caffe.dll
85    -    if NOT DEFINED CMAKE_BUILD_SHARED_LIBS set CMAKE_BUILD_SHARED_LIBS=0
85    +    if NOT DEFINED CMAKE_BUILD_SHARED_LIBS set CMAKE_BUILD_SHARED_LIBS=1
86         :: Change to 3 if using python 3.5 (only 2.7 and 3.5 are supported)
87         if NOT DEFINED PYTHON_VERSION set PYTHON_VERSION=2
88         :: Change these options for your needs.
89    -    if NOT DEFINED BUILD_PYTHON set BUILD_PYTHON=1
90    -    if NOT DEFINED BUILD_PYTHON_LAYER set BUILD_PYTHON_LAYER=1
89    +    if NOT DEFINED BUILD_PYTHON set BUILD_PYTHON=0
90    +    if NOT DEFINED BUILD_PYTHON_LAYER set BUILD_PYTHON_LAYER=0
91         if NOT DEFINED BUILD_MATLAB set BUILD_MATLAB=0
92         :: If python is on your path leave this alone
93         if NOT DEFINED PYTHON_EXE set PYTHON_EXE=python
94         :: Run the tests
95    -    if NOT DEFINED RUN_TESTS set RUN_TESTS=0
95    +    if NOT DEFINED RUN_TESTS set RUN_TESTS=1
96         :: Run lint
97         if NOT DEFINED RUN_LINT set RUN_LINT=0
98         :: Build the install target
99    -    if NOT DEFINED RUN_INSTALL set RUN_INSTALL=0
99    +    if NOT DEFINED RUN_INSTALL set RUN_INSTALL=1
100    )
101
102    :: Set the appropriate CMake generator
103    :: Use the exclamation mark ! below to delay the
104    :: expansion of CMAKE_GENERATOR
105    if %WITH_NINJA% EQU 0 (
106   +     if "%MSVC_VERSION%"=="15" (
107   +         set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
109   +     )
100         if "%MSVC_VERSION%"=="14" (
110             set CMAKE_GENERATOR=Visual Studio 14 2015 Win64
111         )

...

156    :: Setup the environement for VS x64
157   -set batch_file=!VS%MSVC_VERSION%0COMNTOOLS!..\..\VC\vcvarsall.bat
157   +set batch_file=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat
158    call "%batch_file%" amd64
159
160    :: Configure using cmake and using the caffe-builder dependencies
161    :: Add -DCUDNN_ROOT=C:/Projects/caffe/cudnn-8.0-windows10-x64-v5.1/cuda ^
162    :: below to use cuDNN
163    cmake -G"!CMAKE_GENERATOR!" ^
164   +      -C"D:\GitHub\caffe-builder\build\libraries\caffe-builder-config.cmake" ^
165           -DBLAS=Open ^
166           -DCMAKE_BUILD_TYPE:STRING=%CMAKE_CONFIG% ^
167   +       -DCMAKE_CXX_FLAGS="/DNO_STRICT /EHsc" ^
168           -DBUILD_SHARED_LIBS:BOOL=%CMAKE_BUILD_SHARED_LIBS% ^
169           -DBUILD_python:BOOL=%BUILD_PYTHON% ^
170           -DBUILD_python_layer:BOOL=%BUILD_PYTHON_LAYER% ^
171           -DBUILD_matlab:BOOL=%BUILD_MATLAB% ^
172           -DCPU_ONLY:BOOL=%CPU_ONLY% ^
173   -       -DCOPY_PREREQUISITES:BOOL=1 ^
174   -       -DINSTALL_PREREQUISITES:BOOL=1 ^
173   +       -DCOPY_PREREQUISITES:BOOL=0 ^
174   +       -DINSTALL_PREREQUISITES:BOOL=0 ^
175   +       -DUSE_PREBUILT_DEPENDENCIES:BOOL=0 ^
176   +       -DUSE_OPENCV:BOOL=0 ^
177           -DUSE_NCCL:BOOL=!USE_NCCL! ^
178           -DCUDA_ARCH_NAME:STRING=%CUDA_ARCH_NAME% ^
179           "%~dp0\.."
180    if ERRORLEVEL 1 (


Modifying build_win.cmd file is not straightforward.

Here are the detail explanations.

  • if NOT DEFINED MSVC_VERSION set MSVC_VERSION=15
    • MSVC_VERSION for VS 2017 is 15
  • if NOT DEFINED CUDA_ARCH_NAME set CUDA_ARCH_NAME=Pascal
    • I am using NVIDIA GeForce GTX 1070 as a graphic card which uses Pascal CUDA architecture
    • Without this modification, this build script may not correctly find your GPU
    • If you want to know CUDA architecture of your graphic card, please visit this article in Wikipedia or NVIDIA official website
  • if NOT DEFINED CMAKE_BUILD_SHARED_LIBS set CMAKE_BUILD_SHARED_LIBS=1
    • We are going to build Caffe as a shared library
    • This is optional
  • if NOT DEFINED BUILD_PYTHON set BUILD_PYTHON=0
    if NOT DEFINED BUILD_PYTHON_LAYER set BUILD_PYTHON_LAYER=0
    • We are not going to build a python interface of Caffe
    • This is optional
  • if NOT DEFINED RUN_TESTS set RUN_TESTS=1
    if NOT DEFINED RUN_INSTALL set RUN_INSTALL=1
    • To automatically run tests and install
    • These are optional
  • if "%MSVC_VERSION%"=="15" (
       set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
    )
    • To make this script correctly find VS 2017
  • set batch_file=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat
    • IMPORTANT
    • The role of vcvarall.bat file is to select a proper variant of VS 2017 compilers
    • This path is my case and yours may be located in different place
    • Just be careful about this
  • -C"D:\GitHub\caffe-builder\build\libraries\caffe-builder-config.cmake" ^
    • IMPORTANT
    • This option makes CMake aware prerequisite libraries we built through Caffe-Builder
    • This path is also my case and please change it to your location
  • -DCMAKE_CXX_FLAGS="/DNO_STRICT /EHsc" ^
    • /DNO_STRICT
      • To disable strict type checking for VS compiler
      • Please refer this article
    • /EHsc
      • Without this option, errors related to the exception handling occur during the build
      • Please refer this article
  • -DCOPY_PREREQUISITES:BOOL=0 ^
    -DINSTALL_PREREQUISITES:BOOL=0 ^
    -DUSE_PREBUILT_DEPENDENCIES:BOOL=0 ^
    • We built prerequisite libraries (dependencies) through Caffe-Builder
    • These options are unnecessary for us
  • -DUSE_OPENCV:BOOL=0 ^
    • We are not going to use OpenCV for Caffe
    • This is optional

After modification, we need the Ninja to run build_win.cmd script

You can find and download the Ninja build system at this site, and please add the folder containing a Ninja executable in the PATH environment variable

Now, you can run build_win.bat script and successfully build Caffe!

A long journey to build Caffe framework in Windows 10 with VS 2017 ends here