このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
ARM ターゲットでの深層学習用のコード生成
この例では、ハードウェア サポート パッケージを使用せずに、ARM® ベースのデバイスで予測のためのコードを生成して展開する方法を説明します。
ARM Compute Library とハードウェア サポート パッケージを使用して予測のためのコードを生成する場合、codegen
はホスト コンピューターでコードを生成し、生成されたファイルをターゲット ハードウェアにコピーして、ターゲット ハードウェアで実行可能ファイルをビルドします。ハードウェア サポート パッケージがないと、codegen
はホスト コンピューターでコードを生成します。コマンドを実行してファイルをコピーし、ターゲット ハードウェアで実行可能プログラムをビルドしなければなりません。
この例では、関数 packNGo
を使用してすべての関連ファイルを zip 圧縮ファイルにパッケージ化します。この例を使用して、ハードウェア サポート パッケージのない ARM Neon ターゲットに生成されたコードを packNGo
を使用して展開する方法を学習します。
前提条件
NEON 拡張をサポートする ARM プロセッサ
ARM Compute Library (ターゲット ARM ハードウェア上)
Open Source Computer Vision Library (Open CV)
コンパイラおよびライブラリの環境変数
MATLAB® Coder™
MATLAB Coder Interface for Deep Learning サポート パッケージ
Deep Learning Toolbox™
この例で使用する ARM Compute Library のバージョンは、コード生成でサポートされている最新バージョンではない可能性があります。サポートされているライブラリのバージョンと、環境変数の設定に関する詳細については、深層学習に MATLAB Coder を使用するための前提条件を参照してください。
この例は MATLAB Online ではサポートされていません。
関数 squeezenet_predict
この例では、DAG ネットワーク SqueezeNet を使用して、ARM Compute Library を使用したイメージ分類を示します。MATLAB 用の事前学習済みの SqueezeNet は、Deep Learning Toolbox で利用可能です。関数 squeezenet_predict
は、SqueezeNet ネットワークを永続的なネットワーク オブジェクトに読み込みます。後続の関数の呼び出しでは、永続的なオブジェクトが再利用されます。
type squeezenet_predict
% Copyright 2018 The MathWorks, Inc. function out = squeezenet_predict(in) %#codegen % A persistent object mynet is used to load the DAG network object. % At the first call to this function, the persistent object is constructed and % set up. When the function is called subsequent times, the same object is reused % to call predict on inputs, avoiding reconstructing and reloading the % network object. persistent mynet; if isempty(mynet) mynet = coder.loadDeepLearningNetwork('squeezenet','squeezenet'); end out = mynet.predict(in);
スタティック ライブラリ用のコード生成構成オブジェクトの設定
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 squeezenet_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 ファイルをコピーしてフォルダーに解凍し、ハードウェアの 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
ターゲット ハードウェアへのサンプル ファイルのコピー
次のサポート ファイルをホスト コンピューターからターゲット ハードウェアにコピーします。
入力イメージ
coffeemug.png
ライブラリを生成するための makefile
squeezenet_predict_rtw.mk
実行可能プログラムをビルドするための makefile
makefile_squeezenet_arm_generic.mk
synset ディクショナリ
synsetWords.txt
次のコマンドで、置換を行います。
password
をパスワードで置き換えるusername
をユーザー名で置き換えるtargetname
をデバイスの名前で置き換えるtargetloc
をファイルのコピー先フォルダーで置き換える
Linux から実行する場合に必要なすべてのファイルをコピーするには、以下のステップを実行します。
if isunix, system('sshpass -p password scp squeezenet_predict_rtw.mk username@targetname:targetloc/arm_compute/'), end if isunix, system('sshpass -p password scp coffeemug.png username@targetname:targetloc/arm_compute/'), end if isunix, system('sshpass -p password scp makefile_squeezenet_arm_generic.mk username@targetname:targetloc/arm_compute/'), end if isunix, system('sshpass -p password scp synsetWords.txt username@targetname:targetloc/arm_compute/'), end
Windows から実行する場合に必要なすべてのファイルをコピーするには、以下のステップを実行します。
if ispc, system('pscp.exe -pw password squeezenet_predict_rtw.mk username@targetname:targetloc/arm_compute/'), end if ispc, system('pscp.exe -pw password coffeemug.png username@targetname:targetloc/arm_compute/'), end if ispc, system('pscp.exe -pw password makefile_squeezenet_arm_generic.mk username@targetname:targetloc/arm_compute/'), end if ispc, system('pscp.exe -pw password synsetWords.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_squeezenet_arm_generic.cpp username@targetname:targetloc/arm_compute/'), end if isunix, system(['sshpass -p password ssh username@targetname "make -C targetloc/arm_compute/ -f squeezenet_predict_rtw.mk ARM_ARCH=' dlcfg.ArmArchitecture ' ARM_VER=' dlcfg.ArmComputeVersion ' "']), end
Windows からライブラリをビルドするには、以下のステップを実行します。
if ispc, system('pscp.exe -pw password main_squeezenet_arm_generic.cpp username@targetname:targetloc/arm_compute/'), end if ispc, system(['plink.exe -l username -pw password targetname "make -C targetloc/arm_compute/ -f squeezenet_predict_rtw.mk ARM_ARCH=' dlcfg.ArmArchitecture ' ARM_VER=' dlcfg.ArmComputeVersion ' "']), end
ターゲット ハードウェアでのライブラリからの実行可能ファイルの作成
ソースのメイン ラッパー ファイルを使用してライブラリをビルドし、実行可能ファイルを作成します。main_squeezenet_arm_generic.cpp
は、関数 squeezenet_predict を呼び出して実行可能ファイルを作成する C++ メイン ラッパー ファイルです。
Linux から実行可能ファイルを作成するには、以下のコマンドを実行します。
if isunix, system('sshpass -p password ssh username@targetname "make -C targetloc/arm_compute/ -f makefile_squeezenet_arm_generic.mk targetDirName=targetloc/arm_compute"'), end
Windows から実行可能ファイルを作成するには、以下のコマンドを実行します。
if ispc, system('plink.exe -l username -pw password targetname "make -C targetloc/arm_compute/ -f makefile_squeezenet_arm_generic.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/; ./squeezenet coffeemug.png"'), end
Run the executable from Windows using below command.
if ispc, system('plink.exe -l username -pw password targetname "cd targetloc/arm_compute/; ./squeezenet coffeemug.png"'), end
Top 5 Predictions: ----------------------------- 88.299% coffee mug 7.309% cup 1.098% candle 0.634% paper towel 0.591% water jug
参考
coder.ARMNEONConfig
| coder.HardwareImplementation
| coder.DeepLearningConfig
| packNGo