Using LiteRT Metadata, developers can generate wrapper code to enable integration on Android. For most developers, the graphical interface of Android Studio ML Model Binding is the easiest to use. If you require more customisation or are using command line tooling, the LiteRT Codegen is also available.
Use Android Studio ML Model Binding
For LiteRT models enhanced with
metadata, developers can use Android
Studio ML Model Binding to automatically configure settings for the project and
generate wrapper classes based on the model metadata. The wrapper code removes
the need to interact directly with ByteBuffer. Instead, developers can
interact with the LiteRT model with typed objects such as Bitmap and Rect.
Import a LiteRT model in Android Studio
Right-click on the module you would like to use the TFLite model or click on
File, thenNew>Other>LiteRT ModelSelect the location of your TFLite file. Note that the tooling will configure the module's dependency on your behalf with ML Model binding and all dependencies automatically inserted into your Android module's
build.gradlefile.Optional: Select the second checkbox for importing TensorFlow GPU if you want to use GPU acceleration.
Click
Finish.The following screen will appear after the import is successful. To start using the model, select Kotlin or Java, copy and paste the code under the
Sample Codesection. You can get back to this screen by double clicking the TFLite model under themldirectory in Android Studio.
Accelerating model inference
ML Model Binding provides a way for developers to accelerate their code through the use of delegates and the number of threads.
Step 1. Check the module build.gradle file that it contains the following
dependency:
dependencies {
...
// For the LiteRT GPU delegate, we need
// 'com.google.ai.edge.litert:litert-gpu' version 1.*.
implementation 'com.google.ai.edge.litert:litert-gpu:1.4.1'
}
Step 2. Detect if GPU running on the device is compatible with TensorFlow GPU delegate, if not run the model using multiple CPU threads:
Kotlin
import org.tensorflow.lite.gpu.CompatibilityList import org.tensorflow.lite.gpu.GpuDelegate val compatList = CompatibilityList() val options = if(compatList.isDelegateSupportedOnThisDevice) { // if the device has a supported GPU, add the GPU delegate Model.Options.Builder().setDevice(Model.Device.GPU).build() } else { // if the GPU is not supported, run on 4 threads