OpenVINO 2022.1 install & C,CPP sample実行 ubuntu編

ついにOpenVINO 2022.1がリリースされました。

OpenVINO 2022.1 からは、LinuxではDev ToolのインストールはPIP,Git,Dockerでのインストール、Runtimeはsh,PIP,APTなどでのインストールとなっています。

前回は、インストールからPythonでのサンプル実行までを説明しましたが、今回はC,CPP でのサンプル実行を説明します。

Dev Toolsのインストール

今回はUbuntu20.04LTSにOpenVINO 2022.1 Dev Tools PIP版をインストールしてみます。

https://www.intel.com/content/www/us/en/developer/tools/openvino-toolkit/download.html
にアクセスすると、パッケージを選ぶ画面になるので、必要なフレームワークを選んでください。
ここでは全選択しています。

インストールしたばかりのUbuntuの場合は、pipも入っていないと思いますので、下記のように実行してください。

sudo apt-get update

sudo apt-get install python3-pip

pip install openvino-dev[caffe,kaldi,mxnet,pytorch,onnx,tensorflow2]==2022.1.0

仮想環境を使う場合は、下記のコマンドでできると思います。

sudo apt-get install python3-pip

python3 -m venv openvino_env

source openvino_env/bin/activate

python -m pip install --upgrade pip

pip install openvino-dev[caffe,kaldi,mxnet,pytorch,onnx,tensorflow2]==2022.1.0

これで開発環境はインストールできました。次はサンプルプログラムを動作させてみます。

サンプルのインストール

OpenVINO 2022.1からはサンプルプログラムはRuntimeに含まれて配布されています。
ダウンロードしてみましょう

Languageの選択にC++をチェックしておきましょう。

ダウンロードしたスクリプトを実行します。
PythonのRuntimeの時はCLIで実行しましたが、今回はGUIで実行してみます。

インストール(1)

インストール(2)

インストール(3)

インストール(4)

インストールが完了すると、ルートディレクトリにintelというフォルダができます。

外部依存ファイルのインストール

cd install_dependencies/

sudo -E ./install_openvino_dependencies.sh

環境の設定

intel/openvion_2022ディレクトリにあるsetupvars.shを.bashrcに加えます。

echo "source ~/intel/openvino_2022/setupvars.sh >> ~/.bashrc

OpenCVのインストール

サンプルやOpenModelZooを使用するにはOpenCVが必要になるのでインストールします。

cd ~/intel/openvino_2022/extras/scripts

bash download_opencv.sh

GPU、NeuralComputeStick2などの設定

下記の記事を参考にしてください。

Cサンプルのビルド

cd samples/

cd c

bash build_samples.sh 

Setting environment variables for building samples...
[setupvars.sh] OpenVINO environment initialized
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/klf/inference_engine_c_samples_build
Scanning dependencies of target opencv_c_wrapper
Scanning dependencies of target hello_nv12_input_classification_c
[ 14%] Building C object hello_nv12_input_classification/CMakeFiles/hello_nv12_input_classification_c.dir/main.c.o
[ 28%] Building CXX object common/opencv_c_wrapper/CMakeFiles/opencv_c_wrapper.dir/opencv_c_wrapper.cpp.o
[ 42%] Building C object common/opencv_c_wrapper/CMakeFiles/opencv_c_wrapper.dir/bmp_reader.c.o
[ 57%] Linking C executable ../intel64/Release/hello_nv12_input_classification_c
[ 57%] Built target hello_nv12_input_classification_c
[ 71%] Linking CXX shared library ../../intel64/Release/lib/libopencv_c_wrapper.so
[ 71%] Built target opencv_c_wrapper
Scanning dependencies of target hello_classification_c
[ 85%] Building C object hello_classification/CMakeFiles/hello_classification_c.dir/main.c.o
[100%] Linking C executable ../intel64/Release/hello_classification_c
[100%] Built target hello_classification_c
Scanning dependencies of target ie_samples
[100%] Built target ie_samples

Build completed, you can find binaries for all samples in the /home/klf/inference_engine_c_samples_build/intel64/Release subfolder.

実行ファイルは、ホームディレクトリの inference_engine_c_samples_build/intel64/Release に格納されます。

hello_classificationの実行

別のターミナルを起動して、モデルをダウンロードします。

source openvino_env/bin/activate

omz_downloader --name alexnet

omz_converter --name alexnet

ここまでで、alexnetがカレントフォルダのpublicというフォルダに格納され、OpenVINO用のモデルに変換してくれます。

https://storage.openvinotoolkit.org/data/test_data/images/ からサンプルイメージがダウンロードできるので、適当にダウンロードします。

wget https://storage.openvinotoolkit.org/data/test_data/images/apple.jpg

ここまでで必要なものはそろったので、下記のコマンドで実行してみます。

./hello_classification_c ~/public/alexnet/FP16/alexnet.xml ~/images/apple.jpg CPU

Top 10 results:

Image /home/klf/images/apple.jpg

classid probability
------- -----------
957       0.995042
950       0.001760
989       0.001309
948       0.000941
951       0.000750
952       0.000139
949       0.000024
988       0.000010
940       0.000007
990       0.000005

This sample is an API example, for any performance measurements please use the dedicated benchmark_app tool

このサンプルはリンゴの写真を推論させています。

https://storage.openvinotoolkit.org/data/test_data/images/ から取得)

C++サンプルのビルド

Runtimeがインストールされたフォルダにsample/cppがあるので、移動してビルドを行います。

cd samples/cpp

bash build_samples.sh

bash build_samples.sh 

Setting environment variables for building samples...
[setupvars.sh] OpenVINO environment initialized
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include unistd.h
-- Looking for C++ include unistd.h - found
-- Looking for C++ include stdint.h
-- Looking for C++ include stdint.h - found
-- Looking for C++ include sys/types.h
-- Looking for C++ include sys/types.h - found
-- Looking for C++ include fnmatch.h
-- Looking for C++ include fnmatch.h - found
-- Looking for strtoll
-- Looking for strtoll - found
-- Using the single-header code from /home/klf/intel/openvino_2022/samples/cpp/thirdparty/json/nlohmann_json/single_include/
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Looking for CL_VERSION_2_2
-- Looking for CL_VERSION_2_2 - not found
-- Looking for CL_VERSION_2_1
-- Looking for CL_VERSION_2_1 - not found
-- Looking for CL_VERSION_2_0
-- Looking for CL_VERSION_2_0 - not found
-- Looking for CL_VERSION_1_2
-- Looking for CL_VERSION_1_2 - not found
-- Looking for CL_VERSION_1_1
-- Looking for CL_VERSION_1_1 - not found
-- Looking for CL_VERSION_1_0
-- Looking for CL_VERSION_1_0 - not found
-- Could NOT find OpenCL (missing: OpenCL_LIBRARY OpenCL_INCLUDE_DIR) 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/klf/inference_engine_cpp_samples_build
Scanning dependencies of target gflags_nothreads_static
Scanning dependencies of target zlib
[  1%] Building CXX object thirdparty/gflags/gflags/CMakeFiles/gflags_nothreads_static.dir/src/gflags_completions.cc.o
[  3%] Building CXX object thirdparty/gflags/gflags/CMakeFiles/gflags_nothreads_static.dir/src/gflags.cc.o
[  5%] Building CXX object thirdparty/gflags/gflags/CMakeFiles/gflags_nothreads_static.dir/src/gflags_reporting.cc.o
[  7%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/adler32.c.o
[  9%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/gzclose.c.o
[ 11%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/deflate.c.o
[ 13%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/compress.c.o
[ 15%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/crc32.c.o
[ 16%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/gzlib.c.o
[ 18%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/gzread.c.o
[ 20%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/gzwrite.c.o
[ 22%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/inflate.c.o
[ 24%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/infback.c.o
[ 26%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/inftrees.c.o
[ 28%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/inffast.c.o
[ 30%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/trees.c.o
[ 32%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/uncompr.c.o
[ 33%] Building C object thirdparty/zlib/CMakeFiles/zlib.dir/zlib/zutil.c.o
[ 35%] Linking C static library ../../intel64/Release/lib/libzlib.a
[ 35%] Built target zlib
Scanning dependencies of target cnpy
[ 37%] Building CXX object thirdparty/cnpy/CMakeFiles/cnpy.dir/cnpy.cpp.o
[ 39%] Linking CXX static library ../../../intel64/Release/lib/libgflags_nothreads.a
[ 39%] Built target gflags_nothreads_static
Scanning dependencies of target ie_samples_utils
[ 41%] Building CXX object common/utils/CMakeFiles/ie_samples_utils.dir/src/common.cpp.o
[ 43%] Building CXX object common/utils/CMakeFiles/ie_samples_utils.dir/src/args_helper.cpp.o
[ 45%] Building CXX object common/utils/CMakeFiles/ie_samples_utils.dir/src/slog.cpp.o
[ 47%] Linking CXX static library ../../intel64/Release/lib/libcnpy.a
[ 47%] Built target cnpy
[ 49%] Linking CXX static library ../../intel64/Release/lib/libie_samples_utils.a
[ 49%] Built target ie_samples_utils
Scanning dependencies of target format_reader
Scanning dependencies of target speech_sample
Scanning dependencies of target hello_query_device
[ 50%] Building CXX object hello_query_device/CMakeFiles/hello_query_device.dir/main.cpp.o
[ 52%] Building CXX object speech_sample/CMakeFiles/speech_sample.dir/fileutils.cpp.o
[ 54%] Building CXX object speech_sample/CMakeFiles/speech_sample.dir/main.cpp.o
[ 56%] Building CXX object common/format_reader/CMakeFiles/format_reader.dir/MnistUbyte.cpp.o
[ 58%] Building CXX object common/format_reader/CMakeFiles/format_reader.dir/format_reader.cpp.o
[ 60%] Building CXX object common/format_reader/CMakeFiles/format_reader.dir/bmp.cpp.o
[ 62%] Building CXX object common/format_reader/CMakeFiles/format_reader.dir/yuv_nv12.cpp.o
[ 64%] Building CXX object common/format_reader/CMakeFiles/format_reader.dir/opencv_wrapper.cpp.o
[ 66%] Linking CXX shared library ../../intel64/Release/lib/libformat_reader.so
[ 66%] Built target format_reader
Scanning dependencies of target hello_classification
Scanning dependencies of target benchmark_app
Scanning dependencies of target classification_sample_async
Scanning dependencies of target hello_reshape_ssd
Scanning dependencies of target hello_nv12_input_classification
[ 67%] Building CXX object hello_classification/CMakeFiles/hello_classification.dir/main.cpp.o
[ 69%] Building CXX object hello_nv12_input_classification/CMakeFiles/hello_nv12_input_classification.dir/main.cpp.o
[ 71%] Building CXX object classification_sample_async/CMakeFiles/classification_sample_async.dir/main.cpp.o
[ 73%] Building CXX object benchmark_app/CMakeFiles/benchmark_app.dir/inputs_filling.cpp.o
[ 75%] Building CXX object hello_reshape_ssd/CMakeFiles/hello_reshape_ssd.dir/main.cpp.o
[ 77%] Linking CXX executable ../intel64/Release/hello_query_device
[ 79%] Building CXX object benchmark_app/CMakeFiles/benchmark_app.dir/main.cpp.o
[ 79%] Built target hello_query_device
Scanning dependencies of target model_creation_sample
[ 81%] Building CXX object model_creation_sample/CMakeFiles/model_creation_sample.dir/main.cpp.o
[ 83%] Linking CXX executable ../intel64/Release/hello_classification
[ 83%] Built target hello_classification
[ 84%] Building CXX object benchmark_app/CMakeFiles/benchmark_app.dir/remote_tensors_filling.cpp.o
[ 86%] Linking CXX executable ../intel64/Release/hello_nv12_input_classification
[ 86%] Built target hello_nv12_input_classification
[ 88%] Building CXX object benchmark_app/CMakeFiles/benchmark_app.dir/statistics_report.cpp.o
[ 90%] Linking CXX executable ../intel64/Release/classification_sample_async
[ 92%] Linking CXX executable ../intel64/Release/hello_reshape_ssd
[ 92%] Built target classification_sample_async
[ 94%] Building CXX object benchmark_app/CMakeFiles/benchmark_app.dir/utils.cpp.o
[ 96%] Linking CXX executable ../intel64/Release/speech_sample
[ 96%] Built target hello_reshape_ssd
[ 96%] Built target speech_sample
[ 98%] Linking CXX executable ../intel64/Release/model_creation_sample
[ 98%] Built target model_creation_sample
[100%] Linking CXX executable ../intel64/Release/benchmark_app
[100%] Built target benchmark_app
Scanning dependencies of target ie_samples
[100%] Built target ie_samples

Build completed, you can find binaries for all samples in the /home/klf/inference_engine_cpp_samples_build/intel64/Release subfolder.

実行ファイルは、ホームディレクトリの inference_engine_cpp_samples_build /intel64/Release に格納されます。

classification_sample_async の実行

cd ~/inference_engine_cpp_samples_build/intel64/Release

./classification_sample_async 
[ INFO ] OpenVINO Runtime version ......... 2022.1.0
[ INFO ] Build ........... 2022.1.0-7019-cdb9bec7210-releases/2022/1
[ INFO ] 
[ INFO ] Parsing input parameters

classification_sample_async [OPTION]
Options:

    -h                      Print a usage message.
    -m "<path>"             Required. Path to an .xml file with a trained model.
    -i "<path>"             Required. Path to a folder with images or path to an image files: a .ubyte file for LeNet and a .bmp file for the other networks.
    -d "<device>"           Optional. Specify the target device to infer on (the list of available devices is shown below). Default value is CPU. Use "-d HETERO:<comma_separated_devices_list>" format to specify HETERO plugin. Sample will look for a suitable plugin for device specified.
[ ERROR ] Model is required but not set. Please set -m option.

このサンプルは先ほどのCのサンプルのモデルと画像が使用できるので同じように実行してみます。

./classification_sample_async -m ~/public/alexnet/FP16/alexnet.xml -i ~/images/apple.jpg 
[ INFO ] OpenVINO Runtime version ......... 2022.1.0
[ INFO ] Build ........... 2022.1.0-7019-cdb9bec7210-releases/2022/1
[ INFO ] 
[ INFO ] Parsing input parameters
[ INFO ] Files were added: 1
[ INFO ]     /home/klf/images/apple.jpg
[ INFO ] Loading model files:
[ INFO ] /home/klf/public/alexnet/FP16/alexnet.xml
[ INFO ] model name: AlexNet
[ INFO ]     inputs
[ INFO ]         input name: data
[ INFO ]         input type: f32
[ INFO ]         input shape: {1, 3, 227, 227}
[ INFO ]     outputs
[ INFO ]         output name: prob
[ INFO ]         output type: f32
[ INFO ]         output shape: {1, 1000}
[ INFO ] Read input images
[ WARNING ] Image is resized from (300, 300) to (227, 227)
[ INFO ] Set batch size 1
[ INFO ] model name: AlexNet
[ INFO ]     inputs
[ INFO ]         input name: data
[ INFO ]         input type: u8
[ INFO ]         input shape: {1, 227, 227, 3}
[ INFO ]     outputs
[ INFO ]         output name: prob
[ INFO ]         output type: f32
[ INFO ]         output shape: {1, 1000}
[ INFO ] Loading model to the device CPU
[ INFO ] Create infer request
[ INFO ] Start inference (asynchronous executions)
[ INFO ] Completed 1 async request execution
[ INFO ] Completed 2 async request execution
[ INFO ] Completed 3 async request execution
[ INFO ] Completed 4 async request execution
[ INFO ] Completed 5 async request execution
[ INFO ] Completed 6 async request execution
[ INFO ] Completed 7 async request execution
[ INFO ] Completed 8 async request execution
[ INFO ] Completed 9 async request execution
[ INFO ] Completed 10 async request execution
[ INFO ] Completed async requests execution

Top 10 results:

Image /home/klf/images/apple.jpg

classid probability
------- -----------
957     0.9949166  
950     0.0017935  
989     0.0013619  
948     0.0009567  
951     0.0007669  
952     0.0001433  
949     0.0000239  
988     0.0000107  
940     0.0000079  
990     0.0000050  

サンプルの実行はこのような形になります。

次回はOpen Model Zooを実行してみます。