TensorFlow Lite (TFLite) モデルのコード生成と Raspberry Pi への展開
R2026bこの例では、Tensorflow™ Lite モデルを使用して推論を実行するコードを生成し、そのコードを Raspberry Pi® ハードウェアに展開する方法を説明します。この例では、イメージ分類ネットワーク Mobilenet-V1 用の事前学習済み TensorFlow Lite モデルを使用します。この例では、codegen (MATLAB Coder)コマンドを使用して、生成された実行可能ファイルをターゲット ハードウェア上で実行する PIL MEX 関数を生成します。このワークフローは、int8 と float のどちらの TensorFlow Lite モデルにも使用できます。
この例は、Windows® および Linux® のホスト プラットフォームでサポートされています。
サードパーティの前提条件
Raspberry Pi ハードウェア
TensorFlow Lite ライブラリ (ターゲット ARM® ハードウェア上)
事前学習済み TensorFlow Lite モデル
モデルのダウンロード
次のスクリプトを実行し、以下に示す URL からイメージ分類ネットワーク Mobilenet-V1 をダウンロードします。
if ~exist("model.tgz","file") disp('Downloading 5 MB Mobilenet-V1 model file...'); url = "https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_2018_02_22/mobilenet_v1_0.5_224.tgz"; websave("model.tgz",url); untar("model.tgz"); end
Downloading 5 MB Mobilenet-V1 model file...
tflite_predict エントリポイント関数
loadTFLiteModel 関数は、Mobilenet-V1 モデルを TFLiteModel オブジェクトに読み込みます。このオブジェクトのプロパティには、モデルの入力と出力の数やサイズといった、モデルに関する情報が含まれています。
net = loadTFLiteModel('mobilenet_v1_0.5_224.tflite'); disp(net); TFLiteModel with properties: ModelName: 'mobilenet_v1_0.5_224.tflite' NumInputs: 1 NumOutputs: 1 InputSize: {[224 224 3]} OutputSize: {[1001 1]} NumThreads: 8 Mean: 127.5000 StandardDeviation: 127.5000
この例では、エントリポイント関数 tflite_predict.m のコードを生成します。この関数は、loadTFLiteModel 関数を使用して、Mobilenet-V1 モデルを永続的なネットワーク オブジェクトに読み込みます。パフォーマンスを最適化するには、ネットワーク オブジェクトを作成した後、ハードウェア ボードで使用できるスレッドの数に基づいて NumThreads プロパティを設定します。
tflite_predict 関数は、ネットワーク オブジェクトを predict 関数に渡すことで予測を実行します。この関数のそれ以降の呼び出しでは、この永続オブジェクトが再利用されます。
type tflite_predict.mfunction out = tflite_predict(in)
persistent net;
if isempty(net)
net = loadTFLiteModel('mobilenet_v1_0.5_224.tflite');
% To optimize performance, set NumThreads property based on the number
% of threads available on the hardware board
net.NumThreads = 4;
end
out = net.predict(in);
end
Raspberry Pi ハードウェア上で、環境変数 TFLITE_PATH を TensorFlow Lite ライブラリの場所に設定します。TensorFlow Lite ライブラリのビルド方法と環境変数の設定方法の詳細については、Prerequisites for Deep Learning with TensorFlow Lite Modelsを参照してください。
PIL MEX 関数の生成
コード構成オブジェクトの作成
指定されたエントリポイント関数に対して PIL MEX 関数を生成するには、スタティック ライブラリのコード構成オブジェクトを作成し、検証モードを 'PIL' に設定します。ターゲット言語を C++ に設定します。
cfg = coder.config('lib', 'ecoder', true); cfg.TargetLang = 'C++'; cfg.VerificationMode = 'PIL';
Raspberry Pi との接続の設定
Raspberry Pi® Blockset の関数 raspi を使用して、Raspberry Pi への接続を作成します。
以下のコードで、次を置き換えます。
raspiname: 自分の Raspberry Pi ボードの名前
username: 自分のユーザー名
password: 自分のパスワード
r = raspi('raspiname','username','password');
Raspberry Pi 用のコード生成ハードウェア パラメーターの構成
Raspberry Pi 用のcoder.hardware (MATLAB Coder)オブジェクトを作成してコード生成構成オブジェクトに追加します。
hw = coder.hardware('Raspberry Pi');
cfg.Hardware = hw;
ターゲット ハードウェアへの TensorFlow Lite モデルのコピー
TensorFlow Lite モデルを Raspberry Pi ボードにコピーします。ハードウェア ボード上で、環境変数 TFLITE_MODEL_PATH を TensorFlow Lite モデルの場所に設定します。環境変数の設定に関する詳細については、Prerequisites for Deep Learning with TensorFlow Lite Modelsを参照してください。
次のコマンドにおいて、targetDir を Raspberry Pi ボード上の TensorFlow Lite モデルのインストール先フォルダーに置き換えてください。
r.putFile('mobilenet_v1_0.5_224.tflite',targetDir)
PIL MEX の生成
ホスト プラットフォーム上で、codegen コマンドを実行して PIL MEX 関数 tflite_predict_pil を生成します。
codegen -config cfg tflite_predict -args ones(224,224,3,'single')
生成された PIL MEX の実行
imread を使用して、入力イメージを読み取ります。ネットワークで必要とされる入力サイズに合わせて入力のサイズを変更します。
I = imread('peppers.png');
I1 = single(imresize(I,[224,224]));
サイズ変更後の入力を渡し、生成された PIL MEX を実行します。
predictionScores = tflite_predict_pil(I1);
予測スコアをイメージ上にマッピングします。
DisplayPredsonImage(predictionScores, I);

参考
loadTFLiteModel | predict | TFLiteModel | codegen (MATLAB Coder)