Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

codegen を使用した ARM Compute による深層学習の予測

この例では、ARM® プロセッサで深層学習を使用するロゴ分類アプリケーションのコードを生成するための codegen の使用方法を示します。ロゴ分類アプリケーションは、LogoNet 系列ネットワークを使用してイメージからのロゴ認識を実行します。生成されたコードは、コンピューター ビジョンおよび機械学習のための ARM Compute Library を利用します。

前提条件

  • NEON 拡張をサポートする ARM プロセッサ

  • Open Source Computer Vision Library (OpenCV) v3.1

  • ARM Compute Library および OpenCV ライブラリの環境変数

  • MATLAB® Coder™ (C++ コード生成のため)

  • MATLAB Coder Interface for Deep Learning サポート パッケージ

  • SeriesNetwork オブジェクトの使用のための Deep Learning Toolbox™

この例で使用する ARM Compute Library のバージョンは、コード生成でサポートされている最新バージョンではない可能性があります。サポートされているライブラリのバージョンと、環境変数の設定に関する詳細については、深層学習に MATLAB Coder を使用するための前提条件を参照してください。

この例は、Linux® および Windows® プラットフォームでサポートされており、MATLAB Online ではサポートされていません。

事前学習済みの系列ネットワークの取得

事前学習済みの LogoNet ネットワークをダウンロードし、これが存在していない場合は logonet.mat として保存します。ネットワークは MATLAB® で開発されました。そのアーキテクチャは AlexNet のアーキテクチャと同様です。このネットワークは、さまざまなライティング条件およびカメラの角度で、32 個のロゴを識別できます。

net = getLogonet();

ネットワークには、畳み込み、全結合および分類出力層を含む 22 の層が含まれます。

net.Layers
ans = 

  22×1 Layer array with layers:

     1   'imageinput'    Image Input             227×227×3 images with 'zerocenter' normalization and 'randfliplr' augmentations
     2   'conv_1'        2-D Convolution         96 5×5×3 convolutions with stride [1  1] and padding [0  0  0  0]
     3   'relu_1'        ReLU                    ReLU
     4   'maxpool_1'     2-D Max Pooling         3×3 max pooling with stride [2  2] and padding [0  0  0  0]
     5   'conv_2'        2-D Convolution         128 3×3×96 convolutions with stride [1  1] and padding [0  0  0  0]
     6   'relu_2'        ReLU                    ReLU
     7   'maxpool_2'     2-D Max Pooling         3×3 max pooling with stride [2  2] and padding [0  0  0  0]
     8   'conv_3'        2-D Convolution         384 3×3×128 convolutions with stride [1  1] and padding [0  0  0  0]
     9   'relu_3'        ReLU                    ReLU
    10   'maxpool_3'     2-D Max Pooling         3×3 max pooling with stride [2  2] and padding [0  0  0  0]
    11   'conv_4'        2-D Convolution         128 3×3×384 convolutions with stride [2  2] and padding [0  0  0  0]
    12   'relu_4'        ReLU                    ReLU
    13   'maxpool_4'     2-D Max Pooling         3×3 max pooling with stride [2  2] and padding [0  0  0  0]
    14   'fc_1'          Fully Connected         2048 fully connected layer
    15   'relu_5'        ReLU                    ReLU
    16   'dropout_1'     Dropout                 50% dropout
    17   'fc_2'          Fully Connected         2048 fully connected layer
    18   'relu_6'        ReLU                    ReLU
    19   'dropout_2'     Dropout                 50% dropout
    20   'fc_3'          Fully Connected         32 fully connected layer
    21   'softmax'       Softmax                 softmax
    22   'classoutput'   Classification Output   crossentropyex with 'adidas' and 31 other classes

環境変数の設定

ARM ターゲット ハードウェアで、ARM_COMPUTELIB が設定されていることと、LD_LIBRARY_PATH に ARM Compute Library フォルダーへのパスが含まれていることを確認します。

深層学習に MATLAB Coder を使用するための前提条件を参照してください。

関数 logonet_predict

エントリポイント関数 logonet_predict.m は、イメージ入力を受け取り、LogoNet MAT ファイルに保存されている深層学習ネットワークを使用して、イメージについて予測を実行します。この関数は LogoNet.mat から永続ネットワーク変数 logonet にネットワーク オブジェクトを読み込みます。後続の関数の呼び出しでは、永続的なオブジェクトが再利用されます。

type logonet_predict
function out = logonet_predict(in)
%#codegen

% Copyright 2017-2022 The MathWorks, Inc.

% A persistent object logonet is used to load the network object. At the
% first call to this function, the persistent object is constructed and
% setup. When the function is called subsequent times, the same object is
% reused to call predict on inputs, thus avoiding reconstructing and
% reloading the network object.
persistent logonet;

if isempty(logonet)
    
    logonet = coder.loadDeepLearningNetwork('LogoNet.mat','logonet');

end

out = logonet.predict(in);

end

スタティック ライブラリ用のコード生成構成オブジェクトの設定

ARM ベースのデバイスをターゲットにしたコードを生成するときにハードウェア サポート パッケージを使用しない場合は、ライブラリ用の構成オブジェクトを作成します。実行可能プログラム用の構成オブジェクトは作成しないでください。

C++ コードの生成およびコードの生成用の構成オブジェクトのみを設定します。

cfg = coder.config('lib');
cfg.TargetLang = 'C++';
cfg.GenCodeOnly = true;

深層学習コード生成用の構成オブジェクトの設定

coder.ARMNEONConfig オブジェクトを作成します。ターゲット ARM プロセッサのライブラリ バージョンとアーキテクチャを指定します。たとえば、ターゲット ボードが ARMv8 アーキテクチャと ARM Compute Library Version 20.02.1 を備えた HiKey/Rock960 ボードであるとします。

dlcfg = coder.DeepLearningConfig('arm-compute');
dlcfg.ArmComputeVersion = '20.02.1';
dlcfg.ArmArchitecture = 'armv8';

深層学習構成オブジェクトのコード生成構成オブジェクトへの添付

コード生成構成オブジェクトの DeepLearningConfig プロパティを深層学習構成オブジェクトに設定します。

cfg.DeepLearningConfig = dlcfg;

codegen を使用したソース C++ コードの生成

codegen -config cfg logonet_predict -args {ones(227, 227, 3, 'single')} -d arm_compute

コードは、ホスト コンピューターにある現在の作業フォルダー内の arm_compute フォルダーに生成されます。

関数 packNGo を使用した zip ファイルの生成

関数 packNGo は、すべての関連ファイルを zip 圧縮ファイルにパッケージ化します。

zipFileName = 'arm_compute.zip';
bInfo = load(fullfile('arm_compute','buildInfo.mat'));
packNGo(bInfo.buildInfo, {'fileName', zipFileName,'minimalHeaders', false, 'ignoreFileMissing',true});

ターゲット ハードウェアへの生成された zip ファイルのコピー

zip ファイルをコピーしてフォルダーに解凍します。ターゲット ハードウェアから zip ファイルを削除します。

次のコマンドで、置換を行います。

  • password をパスワードで置き換える

  • username をユーザー名で置き換える

  • targetname をデバイスの名前で置き換える

  • targetloc をファイルのコピー先フォルダーで置き換える

Linux から zip ファイルをコピーして解凍するには、次のコマンドを実行します。

if isunix, system(['sshpass -p password scp -r '  fullfile(pwd,zipFileName) ' username@targetname:targetloc/']), end
if isunix, system('sshpass -p password ssh username@targetname "if [ -d targetloc/arm_compute ]; then rm -rf targetloc/arm_compute; fi"'), end
if isunix, system(['sshpass -p password ssh username@targetname "unzip targetloc/' zipFileName ' -d targetloc/arm_compute"']), end
if isunix, system(['sshpass -p password ssh username@targetname "rm -rf  targetloc' zipFileName '"']), end

Windows から zip ファイルをコピーして解凍するには、次のコマンドを実行します。

if ispc, system(['pscp.exe -pw password -r '  fullfile(pwd,zipFileName) ' username@targetname:targetloc/']), end
if ispc, system('plink.exe -l username -pw password targetname "if [ -d targetloc/arm_compute ]; then rm -rf targetloc/arm_compute; fi"'), end
if ispc, system(['plink.exe -l username -pw password targetname "unzip targetloc/' zipFileName ' -d targetloc/arm_compute"']), end
if ispc, system(['plink.exe -l username -pw password targetname "rm -rf  targetloc' zipFileName '"']), end

ターゲット ハードウェアへのサンプル ファイルのコピー

次のサポート ファイルをホスト コンピューターからターゲット ハードウェアにコピーします。

  • 入力イメージ coderdemo_google.png

  • ライブラリを生成するための makefile logonet_predict_rtw.mk

  • 実行可能プログラムをビルドするための makefile makefile_arm_logo.mk

  • synset ディクショナリ synsetWordsLogoDet.txt

次のコマンドで、置換を行います。

  • password をパスワードで置き換える

  • username をユーザー名で置き換える

  • targetname をデバイスの名前で置き換える

  • targetloc をファイルのコピー先フォルダーで置き換える

Linux から実行する場合に必要なすべてのファイルをコピーするには、以下のステップを実行します。

if isunix, system('sshpass -p password scp logonet_predict_rtw.mk username@targetname:targetloc/arm_compute/'), end
if isunix, system('sshpass -p password scp coderdemo_google.png username@targetname:targetloc/arm_compute/'), end
if isunix, system('sshpass -p password scp makefile_arm_logo.mk username@targetname:targetloc/arm_compute/'), end
if isunix, system('sshpass -p password scp synsetWordsLogoDet.txt username@targetname:targetloc/arm_compute/'), end

Windows から実行する場合に必要なすべてのファイルをコピーするには、以下のステップを実行します。

if ispc, system('pscp.exe -pw password logonet_predict_rtw.mk username@targetname:targetloc/arm_compute/'), end
if ispc, system('pscp.exe -pw password coderdemo_google.png username@targetname:targetloc/arm_compute/'), end
if ispc, system('pscp.exe -pw password makefile_arm_logo.mk username@targetname:targetloc/arm_compute/'), end
if ispc, system('pscp.exe -pw password synsetWordsLogoDet.txt username@targetname:targetloc/arm_compute/'), end

ターゲット ハードウェアでのライブラリのビルド

ターゲット ハードウェアでライブラリをビルドするには、生成された makefile を ARM ハードウェアで実行します。

環境変数 ARM_COMPUTELIB と LD_LIBRARY_PATH をターゲット ハードウェアで設定していることを確認してください。深層学習に MATLAB Coder を使用するための前提条件を参照してください。変数 ARM_ARCH は、ARM アーキテクチャに基づいてコンパイラ フラグを渡すために makefile で使用されます。変数 ARM_VER は、ARM Compute のバージョンに基づいてコードをコンパイルするために makefile で使用されます。ハードウェアの資格情報とパスを、前の節と同様のコマンドで置き換えます。

Linux からライブラリをビルドするには、以下のステップを実行します。

if isunix, system('sshpass -p password scp main_arm_logo.cpp username@targetname:targetloc/arm_compute/'), end
if isunix, system(['sshpass -p password ssh username@targetname "make -C targetloc/arm_compute/ -f logonet_predict_rtw.mk ARM_ARCH=' dlcfg.ArmArchitecture ' ARM_VER=' dlcfg.ArmComputeVersion ' "']), end

Windows からライブラリをビルドするには、以下のステップを実行します。

if ispc, system('pscp.exe -pw password main_arm_logo.cpp username@targetname:targetloc/arm_compute/'), end
if ispc, system(['plink.exe -l username -pw password targetname "make -C targetloc/arm_compute/ -f logonet_predict_rtw.mk ARM_ARCH=' dlcfg.ArmArchitecture ' ARM_VER=' dlcfg.ArmComputeVersion ' "']), end

ターゲット ハードウェアでのライブラリからの実行可能ファイルの作成

ソースのメイン ラッパー ファイルを使用してライブラリをビルドし、実行可能ファイルを作成します。main_arm_logo.cpp は、関数 logonet_predict を呼び出す C++ メイン ラッパー ファイルです。

Linux から実行可能ファイルを作成するには、以下のコマンドを実行します。

if isunix, system('sshpass -p password ssh username@targetname "make -C targetloc/arm_compute/ -f makefile_arm_logo.mk targetDirName=targetloc/arm_compute"'), end

Windows から実行可能ファイルを作成するには、以下のコマンドを実行します。

if ispc, system('plink.exe -l username -pw password targetname "make -C targetloc/arm_compute/ -f makefile_arm_logo.mk targetDirName=targetloc/arm_compute"'), end

ターゲット ハードウェアでの実行可能ファイルの実行

Run the executable from Linux using below command.
if isunix, system('sshpass -p password ssh username@targetname "cd targetloc/arm_compute/; ./logonet coderdemo_google.png"'), end
Run the executable from Windows using below command.
if ispc, system('plink.exe -l username -pw password targetname "cd targetloc/arm_compute/; ./logonet coderdemo_google.png"'), end
Top 5 Predictions:
-----------------------------
99.992% google
0.003% corona
0.003% singha
0.001% esso
0.000% fedex

参考

| |

関連するトピック