您也可以透過 Java API,在 除了原生 API 以外尤其是 Google Play 的 LiteRT 這些服務可透過 LiteRT 轉譯器使用 API。
使用解譯器 API
LiteRT Translateer API 是由 TensorFlow 執行階段提供 提供的是一般用途的介面,可用來建構及執行機器學習模型。使用 請按照下列步驟,使用 TensorFlow 將 解譯 er API 執行推論 Google Play 服務執行階段的 Lite。
1. 新增專案依附元件
在應用程式專案程式碼中加入下列依附元件,即可存取 Google Play Services API for LiteRT:
dependencies {
...
// LiteRT dependencies for Google Play services
implementation 'com.google.android.gms:play-services-tflite-java:16.0.1'
// Optional: include LiteRT Support Library
implementation 'com.google.android.gms:play-services-tflite-support:16.0.1'
...
}
2. 新增 LiteRT 初始化
初始化 Google Play 服務 API 的 LiteRT 元件 ,再使用 LiteRT API:
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 服務提供 會以動態方式載入,就像 Play 服務版本的 Play 服務 解譯器 API。
正在檢查裝置相容性
並非所有裝置都支援 TFLite 的 GPU 硬體加速。為了
減少錯誤和潛在當機情形
透過 TfLiteGpu.isGpuDelegateAvailable
方法檢查裝置
與 GPU 委派功能相容
使用這個方法確認裝置是否與 GPU 相容,且會使用 CPU 做為不支援 GPU 時的備用方案
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; });
GPU 搭配解譯器 API
如何搭配使用 GPU 委派與解譯器 API:
更新專案依附元件,以使用來自 Play 服務的 GPU 委派:
implementation 'com.google.android.gms:play-services-tflite-gpu:16.1.0'
在 TFlite 初始化中啟用 GPU 委派選項:
Kotlin
TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(true) .build())
Java
TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(true) .build());
在翻譯選項中啟用 GPU 委派功能:將委派工廠設為 透過呼叫
addDelegateFactory() within
解譯 erApi.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 Services API,請參閱下列附加指引,瞭解如何更新 應用程式專案程式碼:
- 請參閱本頁的「限制」一節, 支援哪些用途
- 更新程式碼之前,請進行效能和正確性檢查 特別是使用舊版 LiteRT 因此,您還有基準可以用於與新推出的版本比較 。
- 如果您已將所有程式碼改為使用 Play 服務 API
LiteRT,您應移除現有的 LiteRT 執行階段
程式庫依附元件 (項目包括
org.tensorflow:tensorflow-lite:*
)。 檔案,這樣就能縮減應用程式大小。 - 找出程式碼中所有出現的
new Interpreter
物件建立作業。 並修改每個元素,使其使用 TranslateerApi.create() 呼叫。 新的 TfLite.initialize 會以非同步的方式執行,因此在多數情況下 插電式替換:必須在呼叫時, 完成。請參閱步驟 3 程式碼中的程式碼片段。 - 將
import org.tensorflow.lite.InterpreterApi;
和import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime;
新增至任何來源 透過org.tensorflow.lite.Interpreter
或org.tensorflow.lite.InterpreterApi
類別。 - 如果對
InterpreterApi.create()
的任何結果呼叫 單一引數,將new InterpreterApi.Options()
附加至引數清單。 - 將
.setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
附加至最後一個引數 任何對InterpreterApi.create()
的呼叫。 - 取代所有其他的
org.tensorflow.lite.Interpreter
類別org.tensorflow.lite.InterpreterApi
。
如要使用獨立的 LiteRT 和 Play 服務 API 並排顯示時,您必須使用 LiteRT 2.9 以上版本。LiteRT 2.8 和較舊版本與 Play 服務 API 版本不相容。