ML Kit AutoML Vision Edge 遷移指南

您可以將以 AutoML 訓練的圖片分類模型傳遞至自訂模型 API。您可以繼續將模型與應用程式一併發布,或在 Firebase 控制台上以自訂模型的形式代管。AutoML 圖片標籤 API 已從 ML Kit 移除,因為自訂模型圖片標籤 API 已完全取代該 API。

API異動內容
AutoML Vision Edge 圖片標籤 API 並完全由 Custom Model 圖像標籤 API 取代。移除現有的 AutoML Vision Edge 圖片標籤 API。

如果您使用 AutoML Vision Edge API,請按照 Android 和 iOS 的遷移作業說明操作。

常見問題

異動原因

這項變更可簡化 ML Kit API,方便您將 ML Kit 整合至應用程式。變更後,您可以使用 AutoML 訓練的模型,方式與自訂模型完全相同。此外,除了我們支援的圖片標記功能外,您還可以使用 AutoML 訓練的模型進行物件偵測和追蹤。此外,自訂模型 API 支援中繼資料內嵌標籤地圖的模型,以及具有獨立資訊清單和標籤檔案的模型。

遷移至新版 SDK 有哪些好處?

  • 新功能:可將 AutoML 訓練的模型用於圖片標籤和物件偵測與追蹤,並使用中繼資料內嵌標籤對應的模型。

Android 遷移指南

步驟 1:更新 Gradle 匯入項目

根據下表,更新模組 (應用程式層級) Gradle 檔案 (通常為 app/build.gradle.kts) 中 ML Kit Android 程式庫的依附元件:

功能舊構件新構件
圖片標籤 AutoML,不需下載遠端模型 com.google.mlkit:image-labeling-automl:16.2.1 com.google.mlkit:image-labeling-custom:16.0.0-beta5
使用 AutoML 進行圖片標籤作業,並從遠端下載模型 com.google.mlkit:image-labeling-automl:16.2.1
com.google.mlkit:linkfirebase:16.0.1
com.google.mlkit:image-labeling-custom:16.0.0-beta5
如要代管及下載自訂模型,請將模型移至 Cloud Storage,並在應用程式中新增下載邏輯,使用 LocalModel 載入模型。詳情請參閱 [Firebase ML 遷移至 Cloud Storage 的指南][migrate-storage]。

步驟 2:更新類別名稱

如果這個表格中顯示您的類別,請進行下列變更:

舊課程新課程
com.google.mlkit.vision.label.automl.AutoMLImageLabelerLocalModel com.google.mlkit.common.model.LocalModel
com.google.mlkit.vision.label.automl.AutoMLImageLabelerRemoteModel com.google.mlkit.common.model.LocalModel
應用程式必須包含下載遠端代管模型的邏輯,並使用 LocalModel 初始化模型。
com.google.mlkit.vision.label.automl.AutoMLImageLabelerOptions com.google.mlkit.vision.label.custom.CustomImageLabelerOptions

步驟 3:更新方法名稱

程式碼變更幅度很小:

  • 現在可以使用模型路徑 (如果模型含有包含標籤對應的中繼資料),或模型資訊清單路徑 (如果資訊清單、模型和標籤位於不同檔案中) 初始化 LocalModel
  • 在應用程式中新增邏輯,將遠端代管的模型下載至本機儲存空間,並使用 LocalModel 載入模型。

以下列舉幾個新舊 Kotlin 方法的範例:

val localModel = AutoMLImageLabelerLocalModel.Builder()
    .setAssetFilePath("automl/manifest.json")
    // or .setAbsoluteFilePath(absolute path to manifest file)
    .build()

val optionsWithLocalModel = AutoMLImageLabelerOptions.Builder(localModel)
    .setConfidenceThreshold(0.5f)
    .build()

val remoteModel = AutoMLImageLabelerRemoteModel.Builder("automl_remote_model")
    .build()

val optionsWithRemoteModel = AutoMLImageLabelerOptions.Builder(remoteModel)
    .build()

新增

// Similar process for both local and remotely-hosted models (that have been downloaded)
val localModel = LocalModel.Builder()
    .setAssetManifestFilePath("automl/manifest.json")
    // or .setAbsoluteManifestFilePath(absolute path to manifest file)
    .build()

val optionsWithLocalModel = CustomImageLabelerOptions.Builder(localModel)
    .setConfidenceThreshold(0.5f)
    .build()

以下列舉幾個新舊 Java 方法的範例:

AutoMLImageLabelerLocalModel localModel =
    new AutoMLImageLabelerLocalModel.Builder()
        .setAssetFilePath("automl/manifest.json")
        // or .setAbsoluteFilePath(absolute path to manifest file)
        .build();
AutoMLImageLabelerOptions optionsWithLocalModel =
    new AutoMLImageLabelerOptions.Builder(localModel)
        .setConfidenceThreshold(0.5f)
        .build();
AutoMLImageLabelerRemoteModel remoteModel =
    new AutoMLImageLabelerRemoteModel.Builder("automl_remote_model").build();
AutoMLImageLabelerOptions optionsWithRemoteModel =
    new AutoMLImageLabelerOptions.Builder(remoteModel)
        .build();

新增

// Similar process for local models and remotely-hosted models (that have been downloaded)
LocalModel localModel =
    new LocalModel.Builder()
        .setAssetManifestFilePath("automl/manifest.json")
        // or .setAbsoluteManifestFilePath(absolute path to manifest file)
        .build();
CustomImageLabelerOptions optionsWithLocalModel =
    new CustomImageLabelerOptions.Builder(localModel)
        .setConfidenceThreshold(0.5f)
        .build();

iOS 遷移指南

必要條件

  • 需要 Xcode 13.2.1 以上版本。

步驟 1:更新 Cocoapods

在應用程式的 Podfile 中,更新 ML Kit iOS cocoapods 的依附元件:

功能舊 Pod 名稱新 Pod 名稱
圖片標籤 AutoML,不需下載遠端模型 GoogleMLKit/ImageLabelingAutoML GoogleMLKit/ImageLabelingCustom
使用 AutoML 進行圖片標籤作業,並從遠端下載模型 GoogleMLKit/ImageLabelingAutoML
GoogleMLKit/LinkFirebase
GoogleMLKit/ImageLabelingCustom
如要代管及下載自訂模型,請將模型移至 Cloud Storage,並在應用程式中新增下載邏輯,將模型載入為本機模型。詳情請參閱 [Firebase ML 至 Cloud Storage 遷移指南][migrate-storage]。

步驟 2:更新類別名稱

如果這個表格中顯示您的類別,請進行下列變更:

Swift

舊課程新課程
AutoMLImageLabelerLocalModel LocalModel
AutoMLImageLabelerRemoteModel LocalModel
如要代管及下載自訂模型,請將模型移至 Cloud Storage,並在應用程式中新增下載邏輯,將模型載入為本機模型。詳情請參閱 [Firebase ML 至 Cloud Storage 遷移指南][migrate-storage]。
AutoMLImageLabelerOptions CustomImageLabelerOptions

Objective-C

舊課程新課程
MLKAutoMLImageLabelerLocalModel MLKLocalModel
MLKAutoMLImageLabelerRemoteModel MLKLocalModel
如要代管及下載自訂模型,請將模型移至 Cloud Storage,並在應用程式中新增下載邏輯,將模型載入為本機模型。詳情請參閱 [Firebase ML 至 Cloud Storage 遷移指南][migrate-storage]。
MLKAutoMLImageLabelerOptions MLKCustomImageLabelerOptions

步驟 3:更新方法名稱

程式碼變更幅度很小:

  • 現在可以使用模型路徑 (如果模型含有包含標籤對應的中繼資料),或模型資訊清單路徑 (如果資訊清單、模型和標籤位於不同檔案中) 初始化 LocalModel
  • 在應用程式中新增邏輯,將遠端代管的模型下載至本機儲存空間,並使用 LocalModel 載入模型。

以下列舉幾個新舊 Swift 方法的範例:

let localModel =
    AutoMLImageLabelerLocalModel(manifestPath: "automl/manifest.json")
let optionsWithLocalModel = AutoMLImageLabelerOptions(localModel: localModel)
let remoteModel = AutoMLImageLabelerRemoteModel(name: "automl_remote_model")
let optionsWithRemoteModel = AutoMLImageLabelerOptions(remoteModel: remoteModel)

新增

// Similar process for local models and remotely-hosted models (that have been downloaded)
guard let localModel = LocalModel(manifestPath: "automl/manifest.json") else { return }
let optionsWithLocalModel = CustomImageLabelerOptions(localModel: localModel)

以下列舉一些新舊 Objective-C 方法的範例:

MLKAutoMLImageLabelerLocalModel *localModel =
    [[MLKAutoMLImageLabelerLocalModel alloc]
        initWithManifestPath:"automl/manifest.json"];
MLKAutoMLImageLabelerOptions *optionsWithLocalModel =
    [[MLKAutoMLImageLabelerOptions alloc] initWithLocalModel:localModel];
MLKAutoMLImageLabelerRemoteModel *remoteModel =
    [[MLKAutoMLImageLabelerRemoteModel alloc]
        initWithManifestPath:"automl/manifest.json"];
MLKAutoMLImageLabelerOptions *optionsWithRemoteModel =
    [[MLKAutoMLImageLabelerOptions alloc] initWithRemoteModel:remoteModel];

新增

// Similar process for local models and remotely-hosted models (that have been downloaded)
MLKLocalModel *localModel =
    [[MLKLocalModel alloc] initWithManifestPath:"automl/manifest.json"];
MLKCustomImageLabelerOptions *optionsWithLocalModel =
    [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel];