使用中繼資料產生模型介面

開發人員可以使用 LiteRT 中繼資料產生包裝函式程式碼,以便在 Android 上整合模型。對大多數開發人員來說,Android Studio ML Model Binding 的圖形介面最容易使用。如需更多自訂選項或使用指令列工具,也可以使用 LiteRT Codegen

使用 Android Studio ML 模型繫結

對於以中繼資料強化的 LiteRT 模型,開發人員可以使用 Android Studio 機器學習模型繫結,根據模型中繼資料自動設定專案,並產生包裝函式類別。包裝函式程式碼可免除直接與 ByteBuffer 互動的需求。開發人員可以改用 BitmapRect 等輸入物件,與 LiteRT 模型互動。

在 Android Studio 中匯入 LiteRT 模型

  1. 在要使用 TFLite 模型的模組上按一下滑鼠右鍵,或依序點選 FileNewOtherLiteRT Model

  2. 選取 TFLite 檔案的位置。請注意,工具會代表您設定模組對 ML 模型繫結的依附元件,並自動將所有依附元件插入 Android 模組的 build.gradle 檔案。

    選用:如要使用 GPU 加速功能,請選取第二個核取方塊,匯入 TensorFlow GPU。

  3. 按一下「Finish」。

  4. 匯入成功後,系統會顯示以下畫面。如要開始使用模型,請選取 Kotlin 或 Java,然後複製並貼上「Sample Code」部分下方的程式碼。如要返回這個畫面,請在 Android Studio 的 ml 目錄下按兩下 TFLite 模型。

加速模型推論

開發人員可透過委派和執行緒數量,使用機器學習模型繫結功能加速程式碼。

步驟 1:檢查模組 build.gradle 檔案是否包含下列依附元件:

    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'
    }

步驟 2:偵測裝置上執行的 GPU 是否與 TensorFlow GPU 委派相容,如果不相容,請使用多個 CPU 執行緒執行模型:

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
        Model.Options.Builder().setNumThreads(4).build()
    }

    // Initialize the model as usual feeding in the options object
    val myModel = MyModel.newInstance(context, options)

    // Run inference per sample code
      

Java

    import org.tensorflow.lite.support.model.Model
    import org.tensorflow.lite.gpu.CompatibilityList;
    import org.tensorflow.lite.gpu.GpuDelegate;

    // Initialize interpreter with GPU delegate
    Model.Options options;
    CompatibilityList compatList = CompatibilityList();

    if(compatList.isDelegateSupportedOnThisDevice()){
        // if the device has a supported GPU, add the GPU delegate
        options = Model.Options.Builder().setDevice(Model.Device.GPU).build();
    } else {
        // if the GPU is not supported, run on 4 threads
        options = Model.Options.Builder().setNumThreads(4).build();
    }

    MyModel myModel = new MyModel.newInstance(context, options);

    // Run inference per sample code
      

使用 LiteRT 程式碼產生器產生模型介面

如果 LiteRT 模型已透過中繼資料強化,開發人員可以使用 LiteRT Android 包裝函式程式碼產生器,建立平台專屬的包裝函式程式碼。包裝函式程式碼可讓您不必直接與 ByteBuffer 互動。開發人員可以改用 BitmapRect 等型別物件,與 TensorFlow Lite 模型互動。

程式碼產生器的實用性取決於 LiteRT 模型的中繼資料項目是否完整。請參閱 metadata_schema.fbs 中相關欄位下的 <Codegen usage> 區段,瞭解程式碼產生工具如何剖析每個欄位。

生成包裝函式程式碼

您需要在終端機中安裝下列工具:

pip install tflite-support

完成後,即可使用下列語法使用程式碼產生器:

tflite_codegen --model=./model_with_metadata/mobilenet_v1_0.75_160_quantized.tflite \
    --package_name=org.tensorflow.lite.classify \
    --model_class_name=MyClassifierModel \
    --destination=./classify_wrapper

產生的程式碼會位於目標目錄中。如果您使用 Google Colab 或其他遠端環境,或許可以將結果壓縮成 ZIP 封存檔,然後下載到 Android Studio 專案:

# Zip up the generated code
!zip -r classify_wrapper.zip classify_wrapper/

# Download the archive
from google.colab import files
files.download('classify_wrapper.zip')

使用產生的程式碼

步驟 1:匯入產生的程式碼

如有需要,請將產生的程式碼解壓縮至目錄結構。產生的程式碼根目錄應為 SRC_ROOT

開啟要使用 LiteRT 模型的 Android Studio 專案,然後匯入產生的模組:依序選取「File」->「New」->「Import Module」-> 選取 SRC_ROOT

以上述範例來說,匯入的目錄和模組會稱為 classify_wrapper

步驟 2:更新應用程式的 build.gradle 檔案

在要使用所產生程式庫模組的應用程式模組中:

在 android 區段下方新增下列內容:

aaptOptions {
   noCompress "tflite"
}

在依附元件區段下方,新增下列項目:

implementation project(":classify_wrapper")

步驟 3:使用模型

// 1. Initialize the model
MyClassifierModel myImageClassifier = null;

try {
    myImageClassifier = new MyClassifierModel(this);
} catch (IOException io){
    // Error reading the model
}

if(null != myImageClassifier) {

    // 2. Set the input with a Bitmap called inputBitmap
    MyClassifierModel.Inputs inputs = myImageClassifier.createInputs();
    inputs.loadImage(inputBitmap));

    // 3. Run the model
    MyClassifierModel.Outputs outputs = myImageClassifier.run(inputs);

    // 4. Retrieve the result
    Map<String, Float> labeledProbability = outputs.getProbability();
}

加速模型推論

開發人員可透過生成的程式碼,使用委派和執行緒數量加快程式碼速度。初始化模型物件時可以設定這些參數,因為模型物件會採用三個參數:

  • Context:來自 Android Activity 或 Service 的內容
  • (選用) Device:TFLite 加速委派。例如: GPUDelegate
  • (選用) numThreads:用於執行模型的執行緒數量,預設為一。

舉例來說,如要使用 GPU 委派和最多三個執行緒,可以按照下列方式初始化模型:

try {
    myImageClassifier = new MyClassifierModel(this, Model.Device.GPU, 3);
} catch (IOException io){
    // Error reading the model
}

疑難排解

如果收到「java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed」錯誤,請在應用程式模組的 android 區段下方插入下列程式碼行,該模組會使用程式庫模組:

aaptOptions {
   noCompress "tflite"
}