LoginSignup
3
6

More than 5 years have passed since last update.

NNablaの学習済モデルをC++だけで実行する

Posted at

はじめに

ソニーのGUIディープラーニングツール Neural Network Console や NNablaで学習したネットワークが、環境に応じてC++だけで推論を実行できるようにアップデートされました。
Ubuntuでのチュートリアルが書かれていますが、Macでも動かしてみます。

できること

  • NNablaで学習したモデルファイル(.h5)をC++で読み込み可能なファイル(.nnp)に変換する。
  • mnistの学習済みモデルを使ってC++で推論を実行する。

環境

  • MacBook Pro (2.7 GHz Intel Core i5,16 GB 1867 MHz DDR3)
  • macOS Sierra

手順

C++モジュールのインストール

インストール方法はこちらに書いてある通りに進めます。すでにPythonパッケージの最新版がインストールされている状態でC++モジュールをインストールしてください。

hdf5, libarchiveなどはMacではbrewで入れます。

% brew install hdf5
% brew install libarchive
==> Installing dependencies for libarchive: xz
==> Installing libarchive dependency: xz
==> Downloading https://homebrew.bintray.com/bottles/xz-5.2.3.sierra.bottle.tar.
######################################################################## 100.0%
==> Pouring xz-5.2.3.sierra.bottle.tar.gz
  /usr/local/Cellar/xz/5.2.3: 92 files, 1.4MB
==> Installing libarchive
==> Downloading https://homebrew.bintray.com/bottles/libarchive-3.3.2.sierra.bot
######################################################################## 100.0%
==> Pouring libarchive-3.3.2.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/libarchive/bin:$PATH"' >> ~/.zshrc

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/libarchive/lib
    CPPFLAGS: -I/usr/local/opt/libarchive/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/libarchive/lib/pkgconfig

==> Summary

ここで出力されるlibarchiveのインストール先はNNablaのビルド時に必要になります。

% git clone https://github.com/sony/nnabla
% mkdir -p nnabla/build && cd nnabla/build
% cmake .. -DBUILD_CPP_UTILS=ON -DBUILD_PYTHON_API=OFF
-- HDF5: Using hdf5 compiler wrapper to determine C configuration
CMake Error at /usr/local/Cellar/cmake/3.6.3/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
  Could NOT find LibArchive (missing: LibArchive_INCLUDE_DIR)
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.6.3/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
  /usr/local/Cellar/cmake/3.6.3/share/cmake/Modules/FindLibArchive.cmake:65 (find_package_handle_standard_args)
  src/nbla_utils/CMakeLists.txt:28 (find_package)


-- Configuring incomplete, errors occurred!
See also "/Users/sapugc/programming/nnabla/nnabla/build/CMakeFiles/CMakeOutput.log".

私の環境では、インストールしたはずのLibArchiveが見つからないと出てしまいます。LibAarchiveは/usr/local/opt/libarchive/にインストールされているので、以下のようにCMakeCache.txtを書き換えます。

CMakeCache.txt
//Path to a file.
//LibArchive_INCLUDE_DIR:PATH=LibArchive_INCLUDE_DIR-NOTFOUND
LibArchive_INCLUDE_DIR:PATH=/usr/local/opt/libarchive/include

//Path to a library.
//LibArchive_LIBRARY:FILEPATH=/usr/lib/libarchive.dylib
LibArchive_LIBRARY:FILEPATH=/usr/local/opt/libarchive/lib/libarchive.dylib

これで問題なくインストールできました。

% cmake .. -DBUILD_CPP_UTILS=ON -DBUILD_PYTHON_API=OFF
% make
% sudo make install

実行

インストールが完了すれば、サンプルはこちらの通りにすれば実行できます。

# at examples/vision/mnist
% python classification.py
% python save_nnp_classification.py
% make
% ./mnist_runtime lenet_010000.nnp ./1.pgm
Prediction scores: -4.77821 13.8166 -3.646 -16.9732 -2.66022 -3.2229 -3.20213 -2.40796 0.523485 -1.83818
Prediction: 1
3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6