机器学习套件 AutoML Vision Edge 迁移指南

您可以将 AutoML 训练的图片分类模型传递给自定义模型 API。您可以继续将模型捆绑到应用中,也可以将其作为自定义模型托管在 Firebase 控制台上。AutoML 图片标签 API 已从机器学习套件中移除,因为它已完全被自定义模型图片标签 API 取代。

API有何变化?
AutoML Vision Edge 图片标签 API 它已完全被自定义模型图片标签 API 取代。现有的 AutoML Vision Edge 图片标签 API 已被移除。

如果您使用 AutoML Vision Edge API,请按照 Android 和 iOS 的迁移说明进行操作。

常见问题解答

为何进行此项更改?

此项更改有助于简化机器学习套件 API,并让您更轻松地将机器学习套件集成到应用中。通过此项更改,您可以使用 AutoML 训练的模型,就像使用自定义模型一样。此外,除了我们支持的图片标签之外,您还可以使用 AutoML 训练的模型进行对象检测和跟踪。此外,自定义模型 API 同时支持元数据中嵌入了标签映射的模型,以及具有单独清单和标签文件的模型。

迁移到新 SDK 有哪些好处?

  • 新功能:能够使用 AutoML 训练的模型进行图片标签和对象检测与跟踪,并且能够使用元数据中嵌入了标签映射的模型。

Android 迁移指南

第 1 步:更新 Gradle 导入

根据下表,更新模块(应用级)Gradle 文件(通常为 app/build.gradle.kts)中 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 方法和新 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 方法和新 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 中 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 方法和新 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 方法和新 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];