您也可以使用 Java API 存取 Google Play 服務中的 LiteRT,除了 Native API 之外,還能透過 Java 或 Kotlin 程式碼使用。具體來說,您可以使用 LiteRT Interpreter API,存取 Google Play 服務中的 LiteRT。
使用 Interpreter API
TensorFlow 執行階段提供的 LiteRT 解譯器 API,可做為建構及執行機器學習模型的一般用途介面。請按照下列步驟,使用 Google Play 服務中的 TensorFlow Lite 執行階段,透過解譯器 API 執行推論。
1. 新增專案依附元件
將下列依附元件新增至應用程式專案程式碼,即可存取 LiteRT 的 Play 服務 API:
dependencies {
...
// LiteRT dependencies for Google Play services
implementation 'com.google.android.gms:play-services-tflite-java:16.1.0'
// Optional: include LiteRT Support Library
implementation 'com.google.android.gms:play-services-tflite-support:16.1.0'
...
}
2. 新增 LiteRT 的初始化作業
使用 LiteRT API 之前,請先初始化 Google Play 服務 API 的 LiteRT 元件:
Kotlin
val initializeTask: Task<Void> by lazy { TfLite.initialize(this) }
Java
Task<Void> initializeTask = TfLite.initialize(context);
3. 建立解譯器並設定執行階段選項
使用 InterpreterApi.create()
建立解譯器,並呼叫 InterpreterApi.Options.setRuntime()
,將解譯器設定為使用 Google Play 服務執行階段,如以下程式碼範例所示:
Kotlin
import org.tensorflow.lite.InterpreterApi import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime ... private lateinit var interpreter: InterpreterApi ... initializeTask.addOnSuccessListener { val interpreterOption = InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY) interpreter = InterpreterApi.create( modelBuffer, interpreterOption )} .addOnFailureListener { e -> Log.e("Interpreter", "Cannot initialize interpreter", e) }
Java
import org.tensorflow.lite.InterpreterApi import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime ... private InterpreterApi interpreter; ... initializeTask.addOnSuccessListener(a -> { interpreter = InterpreterApi.create(modelBuffer, new InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)); }) .addOnFailureListener(e -> { Log.e("Interpreter", String.format("Cannot initialize interpreter: %s", e.getMessage())); });
您應使用上述實作方式,因為這樣可避免封鎖 Android 使用者介面執行緒。如要更密切地管理執行緒執行作業,您可以在建立解譯器時新增 Tasks.await()
呼叫:
Kotlin
import androidx.lifecycle.lifecycleScope ... lifecycleScope.launchWhenStarted { // uses coroutine initializeTask.await() }
Java
@BackgroundThread InterpreterApi initializeInterpreter() { Tasks.await(initializeTask); return InterpreterApi.create(...); }
4. 執行推論
使用您建立的 interpreter
物件,呼叫 run()
方法來產生推論。
Kotlin
interpreter.run(inputBuffer, outputBuffer)
Java
interpreter.run(inputBuffer, outputBuffer);
硬體加速
LiteRT 可讓您使用圖形處理器 (GPU) 等專用硬體處理器,加快模型效能。您可以使用稱為「委派」的硬體驅動程式,充分運用這些專用處理器。
GPU 委派是透過 Google Play 服務提供,並會動態載入,就像 Interpreter API 的 Play 服務版本一樣。
檢查裝置相容性
並非所有裝置都支援使用 TFLite 的 GPU 硬體加速功能。為避免發生錯誤和潛在當機問題,請使用 TfLiteGpu.isGpuDelegateAvailable
方法檢查裝置是否與 GPU 委派項目相容。
使用這個方法確認裝置是否與 GPU 相容,並在不支援 GPU 時改用 CPU。
useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context)
有了 useGpuTask
這類變數後,您就可以用來判斷裝置是否使用 GPU 委派。
Kotlin
val interpreterTask = useGpuTask.continueWith { task -> val interpreterOptions = InterpreterApi.Options() .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY) if (task.result) { interpreterOptions.addDelegateFactory(GpuDelegateFactory()) } InterpreterApi.create(FileUtil.loadMappedFile(context, MODEL_PATH), interpreterOptions) }
Java
Task<InterpreterApi.Options> interpreterOptionsTask = useGpuTask.continueWith({ task -> InterpreterApi.Options options = new InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY); if (task.getResult()) { options.addDelegateFactory(new GpuDelegateFactory()); } return options; });
搭配 Interpreter API 使用 GPU
如要搭配 Interpreter API 使用 GPU 委派:
更新專案依附元件,以使用 Play 服務的 GPU 委派:
implementation 'com.google.android.gms:play-services-tflite-gpu:16.2.0'
在 TFlite 初始化中啟用 GPU 委派選項:
Kotlin
TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(true) .build())
Java
TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(true) .build());
在解譯器選項中啟用 GPU 委派:呼叫
addDelegateFactory() within
InterpreterApi.Options()`,將委派工廠設為 GpuDelegateFactory:Kotlin
val interpreterOption = InterpreterApi.Options() .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY) .addDelegateFactory(GpuDelegateFactory())
Java
Options interpreterOption = InterpreterApi.Options() .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY) .addDelegateFactory(new GpuDelegateFactory());
從獨立 LiteRT 遷移
如果您打算將應用程式從獨立的 LiteRT 遷移至 Play 服務 API,請參閱下列其他指引,瞭解如何更新應用程式專案程式碼:
- 請參閱「限制」一節,確認系統支援您的用途。
- 更新程式碼前,建議您先檢查模型的效能和準確度,特別是使用 2.1 之前的 LiteRT (TF Lite) 版本時,這樣才能與新實作項目比較。
- 如果您已將所有程式碼遷移至使用 LiteRT 的 Play 服務 API,請從 build.gradle 檔案中移除現有的 LiteRT 執行階段程式庫依附元件 (含有
org.tensorflow:tensorflow-lite:*
的項目),藉此縮減應用程式大小。 - 找出程式碼中所有
new Interpreter
物件的建立位置,並修改每個位置,使其使用InterpreterApi.create()
呼叫。新的TfLite.initialize
是非同步,因此在大多數情況下,這並非直接取代項目:您必須註冊事件監聽器,以便在呼叫完成時收到通知。請參閱步驟 3 中的程式碼片段。 - 使用
org.tensorflow.lite.Interpreter
或org.tensorflow.lite.InterpreterApi
類別,將import org.tensorflow.lite.InterpreterApi;
和import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime;
新增至任何來源檔案。 - 如果對
InterpreterApi.create()
的任何結果呼叫只有單一引數,請將new InterpreterApi.Options()
附加至引數清單。 - 將
.setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
附加至對InterpreterApi.create()
的任何呼叫的最後一個引數。 - 將所有其他出現的
org.tensorflow.lite.Interpreter
類別替換為org.tensorflow.lite.InterpreterApi
。
如要並行使用獨立的 LiteRT 和 Play 服務 API,必須使用 LiteRT (TF Lite) 2.9 以上版本。LiteRT (TF Lite) 2.8 版和更早版本與 Play 服務 API 版本不相容。