Problem

OpenCVには顔検出用の関数があるが、どうにも精度が良くないときがある。
そこで最近名前を聞くようになったdlibを使って見ることにする。

ただ、C#のラッパーなどは出回っておらず、C++から呼び出す泥臭いことが必要な模様。
ただ、ソースのサンプルが豊富なので、試してみるのは簡単そう….

と思っていましたがそうでは無かったです。

Preparation

ソース

まずはソースをダウンロード。

ページ左下の青いボタンがダウンロードになります。2017/07/05時点の最新版は19.4です。


ダウンロード後、任意の場所に展開します。
ここでは、D:\Works\Lib\DLib\19.4とします。
配下には下記のファイル、フォルダが展開されます。

  • dlib
  • docs
  • examples
  • python_examples
  • tools
  • CMakeLists.txt
  • documentation.html
  • MANIFEST.in
  • README.md
  • setup.py

CUDA

dlibはCUDAを利用することで性能を大幅に向上させることが出来ます。
ただし、利用できるCUDAは7.5以降になります。今回は8.0を選択。

ダウンロードは下記になります。

今回はWindows 10用のインストーラを入手します。

パッチもダウンロードできるようなので、そちらもダウンロードします。
バージョンは異なりますが、インストールは下記を参考にできます。

cuDNN

CUDAを利用するにはcuDNNが必要です。
インストールしたCUDAに合わせたバージョンを使います。6.0はdlibで利用できないので、5.1を利用します。

ダウンロードは下記になります。
NVIDIAの開発者登録が必要ですので、登録を済ませておいてください。

入手や展開は下記を参考にできます。

今回は、D:\Works\Lib\NVIDIA\cuDNN に展開します。

OpenCV

下記からインストーラをダウンロードしてインストールしておきます。2017/07/05の時点で最新は3.2.0になります。

今回は、D:\Works\Lib\OpenCV\opencv-3.2.0 に展開します。

CMake

Visual Studioのソリューションファイルを生成するために必要です。
下記でダウンロードしインストールします。2017/07/05時点の最新の安定版は3.8.2です。

インストール時、CMakeを環境変数PATHに追加するか選択できますが、そこは好みで。

Visual Studio

ビルドに使います。当たり前かもしれませんが、これがくせ者。
まず、dlibが対応しているCUDAは7.5以降になります。今回は8.0にします。
しかし、8.0に対応しているのはVisual Studio 2015以前です。
2017は 対応していません

Build with CUDA

まずコマンドプロンプトで、dlibの展開フォルダを開きます。
なお、buildフォルダの名前は自由です。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
D:\Works\Lib\DLib\19.4>cd examples
D:\Works\Lib\DLib\19.4\examples>mkdir build
D:\Works\Lib\DLib\19.4\examples>cd build
D:\Works\Lib\DLib\19.4\examples\build>"C:\Program Files\CMake\bin\cmake.exe" -G "Visual Studio 14 2015 Win64" -DCOMPILER_CAN_DO_CPP_11=ON -DOpenCV_DIR=D:\Works\Lib\OpenCV\opencv-3.2.0\build ..
-- The C compiler identification is MSVC 19.0.24215.1
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of void*
-- Check size of void* - done
-- Enabling SSE2 instructions
-- Looking for png_create_read_struct
-- Looking for png_create_read_struct - found
-- Looking for jpeg_read_header
-- Looking for jpeg_read_header - not found
-- Searching for BLAS and LAPACK
-- Found CUDA: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0 (found suitable version "8.0", minimum required is "7.5")
-- Looking for cuDNN install...
-- *** cuDNN V5.0 OR GREATER NOT FOUND. DLIB WILL NOT USE CUDA. ***
-- *** If you have cuDNN then set CMAKE_PREFIX_PATH to include cuDNN's folder.
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Works/Lib/DLib/19.4/examples/build

しかし、Cmakeが失敗します。
ネットを見ると、**-DCMAKE_PREFIX_PATH** に cuDNN のルートフォルダを指定する、みたいなことが書いてありますが、これでは私の環境ではダメでした。
解決策は、cuDNN内の

  • cuda\include\cudnn.h
  • cuda\lib\x64\cudnn.lib

を、それぞれ

  • C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include
  • C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64

にコピーします。
その後、buildフォルダ内を削除し、先のコマンドを実行します。

1
2
3
4
5
6
7
8
9
10
D:\Works\Lib\DLib\19.4\examples\build>"C:\Program Files\CMake\bin\cmake.exe" -G "Visual Studio 14 2015 Win64" -DCOMPILER_CAN_DO_CPP_11=ON -DOpenCV_DIR=D:\Works\Lib\OpenCV\opencv-3.2.0\build ..

-- Found CUDA: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0 (found suitable version "8.0", minimum required is "7.5")
-- Looking for cuDNN install...
-- Building a CUDA test project to see if your compiler is compatible with CUDA...
-- Checking if you have the right version of cuDNN installed.
-- Found cuDNN: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64/cudnn.lib
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Works/Lib/DLib/19.4/examples/build

CMakeでVisual Studioのソリューションファイルの生成に成功しましたので、下記のようにビルドします。

1
2
3
4
5
6
7
8
D:\Works\Lib\DLib\19.4>"C:\Program Files\CMake\bin\cmake.exe" --build . --config Release

..大量のログ..

188 個の警告
0 エラー

経過時間 00:21:08.86

Build without CUDA

もし、CUDAを無効にしたいなら

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
D:\Works\Lib\DLib\19.4>cd examples
D:\Works\Lib\DLib\19.4\examples>mkdir build_cuda_off
D:\Works\Lib\DLib\19.4\examples>cd build_cuda_off
D:\Works\Lib\DLib\19.4\examples\build_cuda_off>"C:\Program Files\CMake\bin\cmake.exe" -G "Visual Studio 14 2015 Win64" -DOpenCV_DIR=D:\Works\Lib\OpenCV\opencv-3.2.0\build -DDLIB_USE_CUDA=OFF ..
-- The C compiler identification is MSVC 19.0.24215.1
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- C++11 activated.
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of void*
-- Check size of void* - done
-- Enabling SSE2 instructions
-- Looking for png_create_read_struct
-- Looking for png_create_read_struct - found
-- Looking for jpeg_read_header
-- Looking for jpeg_read_header - not found
-- Searching for BLAS and LAPACK
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Works/Lib/DLib/19.4/examples/build_cuda_off

とします。
この際、cmakeを実行するフォルダを build フォルダと別名にしておかないと、CUDAを有効にしたバイナリが上書きされますので注意です。
ビルドは同様に、

1
2
3
4
5
6
7
8
D:\Works\Lib\DLib\19.4\examples\build_cuda_off>"C:\Program Files\CMake\bin\cmake.exe" --build . --config Release

..大量のログ..

188 個の警告
0 エラー

経過時間 00:21:28.63

とします。