导出 AutoML Edge 模型

本页面介绍如何使用 Vertex AI 将 AutoML Edge 模型图片和视频导出到 Cloud Storage。

如需了解如何导出表格模型,请参阅导出 AutoML 表格模型

简介

训练 AutoML Edge 模型后,在某些情况下,您可以根据所需的使用方式以不同格式导出模型。导出的模型文件保存在 Cloud Storage 存储桶中,并可用于在您选择的环境中进行预测。

您无法在 Vertex AI 中使用 Edge 模型来执行预测;您必须将 Edge 模型部署到外部设备才能进行预测。

导出模型

使用以下代码示例确定 AutoML Edge 模型,指定输出文件存储位置,然后发送导出模型请求。

图片

在下面选择您的目标对应的标签页:

分类

经过训练的 AutoML Edge 图片分类模型可以按以下格式导出:

  • TF Lite - 将模型导出为 TF Lite 软件包,以在边缘或移动设备上运行模型。
  • Edge TPU TF Lite - 将模型导出为 TF Lite 软件包,以在 Edge TPU 设备上运行模型。
  • 容器 - 将模型导出为 TF 保存的模型以在 Docker 容器上运行。
  • Core ML - 导出 .mlmodel 文件,以在 iOS 和 macOS 设备上运行模型。
  • Tensorflow.js - 将模型导出为 TensorFlow.js 软件包,以在浏览器和 Node.js 中运行模型。

在下面选择您的语言或环境对应的标签页:

控制台

  1. 在 Google Cloud 控制台的 Vertex AI 部分中,前往模型页面。

    打开“模型”页面

  2. 点击要导出的 AutoML Edge 模型的版本号,以打开其详情页面。
  3. 点击导出
  4. 导出模型侧边窗口中,指定 Cloud Storage 中存储 Edge 模型导出输出的位置。
  5. 点击导出
  6. 点击完成关闭导出模型侧边窗口。

REST

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION:您的项目的位置。
  • PROJECT:您的项目 ID
  • MODEL_ID:要导出的经过训练的 AutoML Edge 模型的 ID 编号。
  • EXPORT_FORMAT:要导出的 Edge 模型的类型。对于此目标,选项包括:
    • tflite (TF Lite) - 将模型导出为 TF Lite 软件包,以在边缘或移动设备上运行模型。
    • edgetpu-tflite (Edge TPU TF Lite) - 模型导出为 TF Lite 软件包,以在 Edge TPU 设备上运行模型。
    • tf-saved-model(容器)- 将模型导出为 TF 保存的模型以在 Docker 容器上运行。
    • core-ml (Core ML) - 导出 .mlmodel 文件,以在 iOS 和 macOS 设备上运行模型。
    • tf-js (Tensorflow.js) - 将模型导出为 TensorFlow.js 软件包,以在浏览器和 Node.js 中运行模型。
  • OUTPUT_BUCKET:要存储 Edge 模型文件的 Cloud Storage 存储桶目录的路径。
  • HTTP 方法和网址:

    POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID:export

    请求 JSON 正文:

    {
      "outputConfig": {
        "exportFormatId": "EXPORT_FORMAT",
        "artifactDestination": {
          "outputUriPrefix": "gs://OUTPUT_BUCKET/"
        }
      }
    }
    

    如需发送请求,请选择以下方式之一:

    curl

    将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID:export"

    PowerShell

    将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID:export" | Select-Object -Expand Content

    响应包含有关规范的信息以及 OPERATION_ID

    您可以获取导出操作状态,以了解导出操作完成的时间。

Java

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Java 设置说明执行操作。如需了解详情,请参阅 Vertex AI Java API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1.ExportModelOperationMetadata;
import com.google.cloud.aiplatform.v1.ExportModelRequest;
import com.google.cloud.aiplatform.v1.ExportModelResponse;
import com.google.cloud.aiplatform.v1.GcsDestination;
import com.google.cloud.aiplatform.v1.ModelName;
import com.google.cloud.aiplatform.v1.ModelServiceClient;
import com.google.cloud.aiplatform.v1.ModelServiceSettings;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ExportModelSample {

  public static void main(String[] args)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    String project = "YOUR_PROJECT_ID";
    String modelId = "YOUR_MODEL_ID";
    String gcsDestinationOutputUriPrefix = "gs://YOUR_GCS_SOURCE_BUCKET/path_to_your_destination/";
    String exportFormat = "YOUR_EXPORT_FORMAT";
    exportModelSample(project, modelId, gcsDestinationOutputUriPrefix, exportFormat);
  }

  static void exportModelSample(
      String project, String modelId, String gcsDestinationOutputUriPrefix, String exportFormat)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    ModelServiceSettings modelServiceSettings =
        ModelServiceSettings.newBuilder()
            .setEndpoint("us-central1-aiplatform.googleapis.com:443")
            .build();

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ModelServiceClient modelServiceClient = ModelServiceClient.create(modelServiceSettings)) {
      String location = "us-central1";
      GcsDestination.Builder gcsDestination = GcsDestination.newBuilder();
      gcsDestination.setOutputUriPrefix(gcsDestinationOutputUriPrefix);

      ModelName modelName = ModelName.of(project, location, modelId);
      ExportModelRequest.OutputConfig outputConfig =
          ExportModelRequest.OutputConfig.newBuilder()
              .setExportFormatId(exportFormat)
              .setArtifactDestination(gcsDestination)
              .build();

      OperationFuture<ExportModelResponse, ExportModelOperationMetadata> exportModelResponseFuture =
          modelServiceClient.exportModelAsync(modelName, outputConfig);
      System.out.format(
          "Operation name: %s\n", exportModelResponseFuture.getInitialFuture().get().getName());
      System.out.println("Waiting for operation to finish...");
      ExportModelResponse exportModelResponse =
          exportModelResponseFuture.get(300, TimeUnit.SECONDS);

      System.out.format("Export Model Response: %s\n", exportModelResponse);
    }
  }
}

Node.js

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Node.js 设置说明执行操作。如需了解详情,请参阅 Vertex AI Node.js API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

/**
 * TODO(developer): Uncomment these variables before running the sample.\
   (Not necessary if passing values as arguments)
 */

// const modelId = 'YOUR_MODEL_ID';
// const gcsDestinationOutputUriPrefix ='YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
//    eg. "gs://<your-gcs-bucket>/destination_path"
// const exportFormat = 'YOUR_EXPORT_FORMAT';
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';

// Imports the Google Cloud Model Service Client library
const {ModelServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
  apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const modelServiceClient = new ModelServiceClient(clientOptions);

async function exportModel() {
  // Configure the name resources
  const name = `projects/${project}/locations/${location}/models/${modelId}`;
  // Configure the outputConfig resources
  const outputConfig = {
    exportFormatId: exportFormat,
    gcsDestination: {
      outputUriPrefix: gcsDestinationOutputUriPrefix,
    },
  };
  const request = {
    name,
    outputConfig,
  };

  // Export Model request
  const [response] = await modelServiceClient.exportModel(request);
  console.log(`Long running operation : ${response.name}`);

  // Wait for operation to complete
  await response.promise();
  const result = response.result;

  console.log(`Export model response : ${JSON.stringify(result)}`);
}
exportModel();

Python

如需了解如何安装或更新 Python,请参阅安装 Python 版 Vertex AI SDK。 如需了解详情,请参阅 Python API 参考文档

from google.cloud import aiplatform

def export_model_sample(
    project: str,
    model_id: str,
    gcs_destination_output_uri_prefix: str,
    location: str = "us-central1",
    api_endpoint: str = "us-central1-aiplatform.googleapis.com",
    timeout: int = 300,
):
    # The AI Platform services require regional API endpoints.
    client_options = {"api_endpoint": api_endpoint}
    # Initialize client that will be used to create and send requests.
    # This client only needs to be created once, and can be reused for multiple requests.
    client = aiplatform.gapic.ModelServiceClient(client_options=client_options)
    output_config = {
        "artifact_destination": {
            "output_uri_prefix": gcs_destination_output_uri_prefix
        },
        # For information about export formats: https://cloud.google.com/ai-platform-unified/docs/export/export-edge-model#aiplatform_export_model_sample-drest
        "export_format_id": "tf-saved-model",
    }
    name = client.model_path(project=project, location=location, model=model_id)
    response = client.export_model(name=name, output_config=output_config)
    print("Long running operation:", response.operation.name)
    print("output_info:", response.metadata.output_info)
    export_model_response = response.result(timeout=timeout)
    print("export_model_response:", export_model_response)

分类

经过训练的 AutoML Edge 图片分类模型可以按以下格式导出:

  • TF Lite - 将模型导出为 TF Lite 软件包,以在边缘或移动设备上运行模型。
  • Edge TPU TF Lite - 将模型导出为 TF Lite 软件包,以在 Edge TPU 设备上运行模型。
  • 容器 - 将模型导出为 TF 保存的模型以在 Docker 容器上运行。
  • Core ML - 导出 .mlmodel 文件,以在 iOS 和 macOS 设备上运行模型。
  • Tensorflow.js - 将模型导出为 TensorFlow.js 软件包,以在浏览器和 Node.js 中运行模型。

在下面选择您的语言或环境对应的标签页:

控制台

  1. 在 Google Cloud 控制台的 Vertex AI 部分中,前往模型页面。

    打开“模型”页面

  2. 点击要导出的 AutoML Edge 模型的版本号,以打开其详情页面。
  3. 点击导出
  4. 导出模型侧边窗口中,指定 Cloud Storage 中存储 Edge 模型导出输出的位置。
  5. 点击导出
  6. 点击完成关闭导出模型侧边窗口。

REST

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION:您的项目的位置。
  • PROJECT:您的项目 ID
  • MODEL_ID:要导出的经过训练的 AutoML Edge 模型的 ID 编号。
  • EXPORT_FORMAT:要导出的 Edge 模型的类型。对于此目标,选项包括:
    • tflite (TF Lite) - 将模型导出为 TF Lite 软件包,以在边缘或移动设备上运行模型。
    • edgetpu-tflite (Edge TPU TF Lite) - 模型导出为 TF Lite 软件包,以在 Edge TPU 设备上运行模型。
    • tf-saved-model(容器)- 将模型导出为 TF 保存的模型以在 Docker 容器上运行。
    • core-ml (Core ML) - 导出 .mlmodel 文件,以在 iOS 和 macOS 设备上运行模型。
    • tf-js (Tensorflow.js) - 将模型导出为 TensorFlow.js 软件包,以在浏览器和 Node.js 中运行模型。
  • OUTPUT_BUCKET:要存储 Edge 模型文件的 Cloud Storage 存储桶目录的路径。
  • HTTP 方法和网址:

    POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID:export

    请求 JSON 正文:

    {
      "outputConfig": {
        "exportFormatId": "EXPORT_FORMAT",
        "artifactDestination": {
          "outputUriPrefix": "gs://OUTPUT_BUCKET/"
        }
      }
    }
    

    如需发送请求,请选择以下方式之一:

    curl

    将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID:export"

    PowerShell

    将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID:export" | Select-Object -Expand Content

    响应包含有关规范的信息以及 OPERATION_ID

    您可以获取导出操作状态,以了解导出操作完成的时间。

Java

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Java 设置说明执行操作。如需了解详情,请参阅 Vertex AI Java API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1.ExportModelOperationMetadata;
import com.google.cloud.aiplatform.v1.ExportModelRequest;
import com.google.cloud.aiplatform.v1.ExportModelResponse;
import com.google.cloud.aiplatform.v1.GcsDestination;
import com.google.cloud.aiplatform.v1.ModelName;
import com.google.cloud.aiplatform.v1.ModelServiceClient;
import com.google.cloud.aiplatform.v1.ModelServiceSettings;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ExportModelSample {

  public static void main(String[] args)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    String project = "YOUR_PROJECT_ID";
    String modelId = "YOUR_MODEL_ID";
    String gcsDestinationOutputUriPrefix = "gs://YOUR_GCS_SOURCE_BUCKET/path_to_your_destination/";
    String exportFormat = "YOUR_EXPORT_FORMAT";
    exportModelSample(project, modelId, gcsDestinationOutputUriPrefix, exportFormat);
  }

  static void exportModelSample(
      String project, String modelId, String gcsDestinationOutputUriPrefix, String exportFormat)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    ModelServiceSettings modelServiceSettings =
        ModelServiceSettings.newBuilder()
            .setEndpoint("us-central1-aiplatform.googleapis.com:443")
            .build();

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ModelServiceClient modelServiceClient = ModelServiceClient.create(modelServiceSettings)) {
      String location = "us-central1";
      GcsDestination.Builder gcsDestination = GcsDestination.newBuilder();
      gcsDestination.setOutputUriPrefix(gcsDestinationOutputUriPrefix);

      ModelName modelName = ModelName.of(project, location, modelId);
      ExportModelRequest.OutputConfig outputConfig =
          ExportModelRequest.OutputConfig.newBuilder()
              .setExportFormatId(exportFormat)
              .setArtifactDestination(gcsDestination)
              .build();

      OperationFuture<ExportModelResponse, ExportModelOperationMetadata> exportModelResponseFuture =
          modelServiceClient.exportModelAsync(modelName, outputConfig);
      System.out.format(
          "Operation name: %s\n", exportModelResponseFuture.getInitialFuture().get().getName());
      System.out.println("Waiting for operation to finish...");
      ExportModelResponse exportModelResponse =
          exportModelResponseFuture.get(300, TimeUnit.SECONDS);

      System.out.format("Export Model Response: %s\n", exportModelResponse);
    }
  }
}

Node.js

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Node.js 设置说明执行操作。如需了解详情,请参阅 Vertex AI Node.js API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

/**
 * TODO(developer): Uncomment these variables before running the sample.\
   (Not necessary if passing values as arguments)
 */

// const modelId = 'YOUR_MODEL_ID';
// const gcsDestinationOutputUriPrefix ='YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
//    eg. "gs://<your-gcs-bucket>/destination_path"
// const exportFormat = 'YOUR_EXPORT_FORMAT';
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';

// Imports the Google Cloud Model Service Client library
const {ModelServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
  apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const modelServiceClient = new ModelServiceClient(clientOptions);

async function exportModel() {
  // Configure the name resources
  const name = `projects/${project}/locations/${location}/models/${modelId}`;
  // Configure the outputConfig resources
  const outputConfig = {
    exportFormatId: exportFormat,
    gcsDestination: {
      outputUriPrefix: gcsDestinationOutputUriPrefix,
    },
  };
  const request = {
    name,
    outputConfig,
  };

  // Export Model request
  const [response] = await modelServiceClient.exportModel(request);
  console.log(`Long running operation : ${response.name}`);

  // Wait for operation to complete
  await response.promise();
  const result = response.result;

  console.log(`Export model response : ${JSON.stringify(result)}`);
}
exportModel();

Python

如需了解如何安装或更新 Python,请参阅安装 Python 版 Vertex AI SDK。 如需了解详情,请参阅 Python API 参考文档

from google.cloud import aiplatform

def export_model_sample(
    project: str,
    model_id: str,
    gcs_destination_output_uri_prefix: str,
    location: str = "us-central1",
    api_endpoint: str = "us-central1-aiplatform.googleapis.com",
    timeout: int = 300,
):
    # The AI Platform services require regional API endpoints.
    client_options = {"api_endpoint": api_endpoint}
    # Initialize client that will be used to create and send requests.
    # This client only needs to be created once, and can be reused for multiple requests.
    client = aiplatform.gapic.ModelServiceClient(client_options=client_options)
    output_config = {
        "artifact_destination": {
            "output_uri_prefix": gcs_destination_output_uri_prefix
        },
        # For information about export formats: https://cloud.google.com/ai-platform-unified/docs/export/export-edge-model#aiplatform_export_model_sample-drest
        "export_format_id": "tf-saved-model",
    }
    name = client.model_path(project=project, location=location, model=model_id)
    response = client.export_model(name=name, output_config=output_config)
    print("Long running operation:", response.operation.name)
    print("output_info:", response.metadata.output_info)
    export_model_response = response.result(timeout=timeout)
    print("export_model_response:", export_model_response)

对象检测

经过训练的 AutoML Edge 对象检测模型可以按以下格式导出:

  • TF Lite - 将模型导出为 TF Lite 软件包,以在边缘或移动设备上运行模型。
  • 容器 - 将模型导出为 TF 保存的模型以在 Docker 容器上运行。
  • Tensorflow.js - 将模型导出为 TensorFlow.js 软件包,以在浏览器和 Node.js 中运行模型。

在下面选择您的语言或环境对应的标签页:

控制台

  1. 在 Google Cloud 控制台的 Vertex AI 部分中,前往模型页面。

    打开“模型”页面

  2. 点击要导出的 AutoML Edge 模型的版本号,以打开其详情页面。
  3. 选择部署和测试标签页查看可用的导出格式。
  4. Use your edge-optimized model 部分选择所需的导出模型格式。
  5. 导出模型侧边窗口中,指定 Cloud Storage 中存储 Edge 模型导出输出的位置。
  6. 点击导出
  7. 点击完成关闭导出模型侧边窗口。

REST

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION:您的项目的位置。
  • PROJECT:您的项目 ID
  • MODEL_ID:要导出的经过训练的 AutoML Edge 模型的 ID 编号。
  • EXPORT_FORMAT:要导出的 Edge 模型的类型。对于此目标,选项包括:
    • tflite (TF Lite) - 将模型导出为 TF Lite 软件包,以在边缘或移动设备上运行模型。
    • tf-saved-model(容器)- 将模型导出为 TF 保存的模型以在 Docker 容器上运行。
    • tf-js (Tensorflow.js) - 将模型导出为 TensorFlow.js 软件包,以在浏览器和 Node.js 中运行模型。
  • OUTPUT_BUCKET:要存储 Edge 模型文件的 Cloud Storage 存储桶目录的路径。
  • HTTP 方法和网址:

    POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID:export

    请求 JSON 正文:

    {
      "outputConfig": {
        "exportFormatId": "EXPORT_FORMAT",
        "artifactDestination": {
          "outputUriPrefix": "gs://OUTPUT_BUCKET/"
        }
      }
    }
    

    如需发送请求,请选择以下方式之一:

    curl

    将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID:export"

    PowerShell

    将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID:export" | Select-Object -Expand Content

    响应包含有关规范的信息以及 OPERATION_ID

    您可以获取导出操作状态,以了解导出操作完成的时间。

Java

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Java 设置说明执行操作。如需了解详情,请参阅 Vertex AI Java API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1.ExportModelOperationMetadata;
import com.google.cloud.aiplatform.v1.ExportModelRequest;
import com.google.cloud.aiplatform.v1.ExportModelResponse;
import com.google.cloud.aiplatform.v1.GcsDestination;
import com.google.cloud.aiplatform.v1.ModelName;
import com.google.cloud.aiplatform.v1.ModelServiceClient;
import com.google.cloud.aiplatform.v1.ModelServiceSettings;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ExportModelSample {

  public static void main(String[] args)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    String project = "YOUR_PROJECT_ID";
    String modelId = "YOUR_MODEL_ID";
    String gcsDestinationOutputUriPrefix = "gs://YOUR_GCS_SOURCE_BUCKET/path_to_your_destination/";
    String exportFormat = "YOUR_EXPORT_FORMAT";
    exportModelSample(project, modelId, gcsDestinationOutputUriPrefix, exportFormat);
  }

  static void exportModelSample(
      String project, String modelId, String gcsDestinationOutputUriPrefix, String exportFormat)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    ModelServiceSettings modelServiceSettings =
        ModelServiceSettings.newBuilder()
            .setEndpoint("us-central1-aiplatform.googleapis.com:443")
            .build();

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ModelServiceClient modelServiceClient = ModelServiceClient.create(modelServiceSettings)) {
      String location = "us-central1";
      GcsDestination.Builder gcsDestination = GcsDestination.newBuilder();
      gcsDestination.setOutputUriPrefix(gcsDestinationOutputUriPrefix);

      ModelName modelName = ModelName.of(project, location, modelId);
      ExportModelRequest.OutputConfig outputConfig =
          ExportModelRequest.OutputConfig.newBuilder()
              .setExportFormatId(exportFormat)
              .setArtifactDestination(gcsDestination)
              .build();

      OperationFuture<ExportModelResponse, ExportModelOperationMetadata> exportModelResponseFuture =
          modelServiceClient.exportModelAsync(modelName, outputConfig);
      System.out.format(
          "Operation name: %s\n", exportModelResponseFuture.getInitialFuture().get().getName());
      System.out.println("Waiting for operation to finish...");
      ExportModelResponse exportModelResponse =
          exportModelResponseFuture.get(300, TimeUnit.SECONDS);

      System.out.format("Export Model Response: %s\n", exportModelResponse);
    }
  }
}

Node.js

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Node.js 设置说明执行操作。如需了解详情,请参阅 Vertex AI Node.js API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

/**
 * TODO(developer): Uncomment these variables before running the sample.\
   (Not necessary if passing values as arguments)
 */

// const modelId = 'YOUR_MODEL_ID';
// const gcsDestinationOutputUriPrefix ='YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
//    eg. "gs://<your-gcs-bucket>/destination_path"
// const exportFormat = 'YOUR_EXPORT_FORMAT';
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';

// Imports the Google Cloud Model Service Client library
const {ModelServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
  apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const modelServiceClient = new ModelServiceClient(clientOptions);

async function exportModel() {
  // Configure the name resources
  const name = `projects/${project}/locations/${location}/models/${modelId}`;
  // Configure the outputConfig resources
  const outputConfig = {
    exportFormatId: exportFormat,
    gcsDestination: {
      outputUriPrefix: gcsDestinationOutputUriPrefix,
    },
  };
  const request = {
    name,
    outputConfig,
  };

  // Export Model request
  const [response] = await modelServiceClient.exportModel(request);
  console.log(`Long running operation : ${response.name}`);

  // Wait for operation to complete
  await response.promise();
  const result = response.result;

  console.log(`Export model response : ${JSON.stringify(result)}`);
}
exportModel();

Python

如需了解如何安装或更新 Python,请参阅安装 Python 版 Vertex AI SDK。 如需了解详情,请参阅 Python API 参考文档

from google.cloud import aiplatform

def export_model_sample(
    project: str,
    model_id: str,
    gcs_destination_output_uri_prefix: str,
    location: str = "us-central1",
    api_endpoint: str = "us-central1-aiplatform.googleapis.com",
    timeout: int = 300,
):
    # The AI Platform services require regional API endpoints.
    client_options = {"api_endpoint": api_endpoint}
    # Initialize client that will be used to create and send requests.
    # This client only needs to be created once, and can be reused for multiple requests.
    client = aiplatform.gapic.ModelServiceClient(client_options=client_options)
    output_config = {
        "artifact_destination": {
            "output_uri_prefix": gcs_destination_output_uri_prefix
        },
        # For information about export formats: https://cloud.google.com/ai-platform-unified/docs/export/export-edge-model#aiplatform_export_model_sample-drest
        "export_format_id": "tf-saved-model",
    }
    name = client.model_path(project=project, location=location, model=model_id)
    response = client.export_model(name=name, output_config=output_config)
    print("Long running operation:", response.operation.name)
    print("output_info:", response.metadata.output_info)
    export_model_response = response.result(timeout=timeout)
    print("export_model_response:", export_model_response)

视频

在下面选择您的目标对应的标签页:

动作识别

经过训练的 AutoML Edge 动作识别模型可以按保存模型格式导出:

在下面选择您的语言或环境对应的标签页:

控制台

  1. 在 Google Cloud 控制台的 Vertex AI 部分中,前往模型页面。

    打开“模型”页面

  2. 点击要导出的 AutoML Edge 模型的版本号,以打开其详情页面。
  3. 点击导出
  4. 导出模型侧边窗口中,指定 Cloud Storage 中存储 Edge 模型导出输出的位置。
  5. 点击导出
  6. 点击完成关闭导出模型侧边窗口。

REST

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION:存储模型的区域。例如 us-central1
  • MODEL_ID:要导出的经过训练的 AutoML Edge 模型的 ID 编号。
  • EXPORT_FORMAT:要导出的 Edge 模型的类型。对于视频动作识别,模型选项为:
    • tf-saved-model(容器)- 将模型导出为 TF 保存的模型以在 Docker 容器上运行。
  • OUTPUT_BUCKET:要存储 Edge 模型文件的 Cloud Storage 存储桶目录的路径。
  • PROJECT_NUMBER:自动生成的项目编号

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/models/MODEL_ID:export

请求 JSON 正文:

{
  "outputConfig": {
    "exportFormatId": "EXPORT_FORMAT",
    "artifactDestination": {
    "outputUriPrefix": "gs://OUTPUT_BUCKET/"
    }
  }
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/models/MODEL_ID:export"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/models/MODEL_ID:export" | Select-Object -Expand Content

响应包含有关规范的信息以及 OPERATION_ID

您可以获取导出操作状态,以了解导出操作完成的时间。

Java

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Java 设置说明执行操作。如需了解详情,请参阅 Vertex AI Java API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1.ExportModelOperationMetadata;
import com.google.cloud.aiplatform.v1.ExportModelRequest;
import com.google.cloud.aiplatform.v1.ExportModelResponse;
import com.google.cloud.aiplatform.v1.GcsDestination;
import com.google.cloud.aiplatform.v1.ModelName;
import com.google.cloud.aiplatform.v1.ModelServiceClient;
import com.google.cloud.aiplatform.v1.ModelServiceSettings;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class ExportModelVideoActionRecognitionSample {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    String project = "PROJECT";
    String modelId = "MODEL_ID";
    String gcsDestinationOutputUriPrefix = "GCS_DESTINATION_OUTPUT_URI_PREFIX";
    String exportFormat = "EXPORT_FORMAT";
    exportModelVideoActionRecognitionSample(
        project, modelId, gcsDestinationOutputUriPrefix, exportFormat);
  }

  static void exportModelVideoActionRecognitionSample(
      String project, String modelId, String gcsDestinationOutputUriPrefix, String exportFormat)
      throws IOException, ExecutionException, InterruptedException {
    ModelServiceSettings settings =
        ModelServiceSettings.newBuilder()
            .setEndpoint("us-central1-aiplatform.googleapis.com:443")
            .build();
    String location = "us-central1";

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ModelServiceClient client = ModelServiceClient.create(settings)) {
      GcsDestination gcsDestination =
          GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build();
      ExportModelRequest.OutputConfig outputConfig =
          ExportModelRequest.OutputConfig.newBuilder()
              .setArtifactDestination(gcsDestination)
              .setExportFormatId(exportFormat)
              .build();
      ModelName name = ModelName.of(project, location, modelId);
      OperationFuture<ExportModelResponse, ExportModelOperationMetadata> response =
          client.exportModelAsync(name, outputConfig);

      // You can use OperationFuture.getInitialFuture to get a future representing the initial
      // response to the request, which contains information while the operation is in progress.
      System.out.format("Operation name: %s\n", response.getInitialFuture().get().getName());

      // OperationFuture.get() will block until the operation is finished.
      ExportModelResponse exportModelResponse = response.get();
      System.out.format("exportModelResponse: %s\n", exportModelResponse);
    }
  }
}

Python

如需了解如何安装或更新 Python,请参阅安装 Python 版 Vertex AI SDK。 如需了解详情,请参阅 Python API 参考文档

from google.cloud import aiplatform

def export_model_video_action_recognition_sample(
    project: str,
    model_id: str,
    gcs_destination_output_uri_prefix: str,
    export_format: str,
    location: str = "us-central1",
    api_endpoint: str = "us-central1-aiplatform.googleapis.com",
    timeout: int = 300,
):
    # The AI Platform services require regional API endpoints.
    client_options = {"api_endpoint": api_endpoint}
    # Initialize client that will be used to create and send requests.
    # This client only needs to be created once, and can be reused for multiple requests.
    client = aiplatform.gapic.ModelServiceClient(client_options=client_options)
    gcs_destination = {"output_uri_prefix": gcs_destination_output_uri_prefix}
    output_config = {
        "artifact_destination": gcs_destination,
        "export_format_id": export_format,
    }
    name = client.model_path(project=project, location=location, model=model_id)
    response = client.export_model(name=name, output_config=output_config)
    print("Long running operation:", response.operation.name)
    print("output_info:", response.metadata.output_info)
    export_model_response = response.result(timeout=timeout)
    print("export_model_response:", export_model_response)

分类

经过训练的 AutoML Edge 视频分类模型只能以保存模型格式导出。

在下面选择您的语言或环境对应的标签页:

控制台

  1. 在 Google Cloud 控制台的 Vertex AI 部分中,前往模型页面。

    打开“模型”页面

  2. 点击要导出的 AutoML Edge 模型的版本号,以打开其详情页面。
  3. 点击导出
  4. 导出模型侧边窗口中,指定 Cloud Storage 中存储 Edge 模型导出输出的位置。
  5. 点击导出
  6. 点击完成关闭导出模型侧边窗口。

REST

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION:存储模型的区域。例如 us-central1
  • MODEL_ID:要导出的经过训练的 AutoML Edge 模型的 ID 编号。
  • EXPORT_FORMAT:要导出的 Edge 模型的类型。对于视频分类,模型选项为:
    • tf-saved-model(容器)- 将模型导出为 TF 保存的模型以在 Docker 容器上运行。
  • OUTPUT_BUCKET:要存储 Edge 模型文件的 Cloud Storage 存储桶目录的路径。
  • PROJECT_NUMBER:自动生成的项目编号

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/models/MODEL_ID:export

请求 JSON 正文:

{
  "outputConfig": {
    "exportFormatId": "EXPORT_FORMAT",
    "artifactDestination": {
    "outputUriPrefix": "gs://OUTPUT_BUCKET/"
    }
  }
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/models/MODEL_ID:export"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/models/MODEL_ID:export" | Select-Object -Expand Content

响应包含有关规范的信息以及 OPERATION_ID

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION/models/MODEL_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.aiplatform.v1.ExportModelOperationMetadata",
    "genericMetadata": {
      "createTime": "2020-10-12T20:53:40.130785Z",
      "updateTime": "2020-10-12T20:53:40.130785Z"
    },
    "outputInfo": {
      "artifactOutputUri": "gs://OUTPUT_BUCKET/model-MODEL_ID/EXPORT_FORMAT/YYYY-MM-DDThh:mm:ss.sssZ"
    }
  }
}

您可以获取导出操作状态,以了解导出操作完成的时间。

对象跟踪

经过训练的 AutoML Edge 视频对象跟踪模型可以按以下格式导出:

  • TF Lite - 将模型导出为 TensorFlow Lite 软件包,以在边缘或移动设备上运行模型。
  • 容器 - 将模型导出为 TensorFlow 保存模型以在 Docker 容器上运行。

在下面选择您的语言或环境对应的标签页:

控制台

  1. 在 Google Cloud 控制台的 Vertex AI 部分中,前往模型页面。

    打开“模型”页面

  2. 点击要导出的 AutoML Edge 模型的版本号,以打开其详情页面。
  3. 点击导出
  4. 导出模型侧边窗口中,指定 Cloud Storage 中存储 Edge 模型导出输出的位置。
  5. 点击导出
  6. 点击完成关闭导出模型侧边窗口。

REST

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION:存储模型的区域。例如 us-central1
  • MODEL_ID:要导出的经过训练的 AutoML Edge 模型的 ID 编号。
  • EXPORT_FORMAT:要导出的 Edge 模型的类型。对于视频对象跟踪模型,选项包括:
    • tflite (TF Lite) - 将模型导出为 TF Lite 软件包,以在边缘或移动设备上运行模型。
    • edgetpu-tflite (Edge TPU TF Lite) - 模型导出为 TF Lite 软件包,以在 Edge TPU 设备上运行模型。
    • tf-saved-model(容器)- 将模型导出为 TF 保存的模型以在 Docker 容器上运行。
  • OUTPUT_BUCKET:要存储 Edge 模型文件的 Cloud Storage 存储桶目录的路径。
  • PROJECT_NUMBER:自动生成的项目编号

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/models/MODEL_ID:export

请求 JSON 正文:

{
  "outputConfig": {
    "exportFormatId": "EXPORT_FORMAT",
    "artifactDestination": {
    "outputUriPrefix": "gs://OUTPUT_BUCKET/"
    }
  }
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/models/MODEL_ID:export"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/models/MODEL_ID:export" | Select-Object -Expand Content

响应包含有关规范的信息以及 OPERATION_ID

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION/models/MODEL_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.aiplatform.v1.ExportModelOperationMetadata",
    "genericMetadata": {
      "createTime": "2020-10-12T20:53:40.130785Z",
      "updateTime": "2020-10-12T20:53:40.130785Z"
    },
    "outputInfo": {
      "artifactOutputUri": "gs://OUTPUT_BUCKET/model-MODEL_ID/EXPORT_FORMAT/YYYY-MM-DDThh:mm:ss.sssZ"
    }
  }
}

您可以获取导出操作状态,以了解导出操作完成的时间。

获取操作状态

图片

使用以下代码获取导出操作的状态。此代码对于所有目标均相同:

REST

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION:您的项目的位置。
  • PROJECT:您的项目 ID
  • OPERATION_ID:目标操作的 ID。此 ID 通常包含在原始请求的响应中。

HTTP 方法和网址:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/operations/OPERATION_ID

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/operations/OPERATION_ID"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/operations/OPERATION_ID" | Select-Object -Expand Content
操作完成后,您应该会看到类似如下所示的输出:
{
  "name": "projects/PROJECT/locations/LOCATION/models/MODEL_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.aiplatform.v1.ExportModelOperationMetadata",
    "genericMetadata": {
      "createTime": "2020-10-12T20:53:40.130785Z",
      "updateTime": "2020-10-12T20:53:40.793983Z"
    },
    "outputInfo": {
      "artifactOutputUri": "gs://OUTPUT_BUCKET/model-MODEL_ID/EXPORT_FORMAT/YYYY-MM-DDThh:mm:ss.sssZ"
    }
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.cloud.aiplatform.v1.ExportModelResponse"
  }
}

视频

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_NUMBER:自动生成的项目编号
  • LOCATION:存储模型的区域。例如 us-central1
  • OPERATION_ID:您的操作的 ID。

HTTP 方法和网址:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/operations/OPERATION_ID

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/operations/OPERATION_ID"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/operations/OPERATION_ID" | Select-Object -Expand Content

您应该收到类似以下内容的 JSON 响应:

输出文件

图片

在下面选择您的模型格式对应的标签页:

TF Lite

您在请求中指定的 OUTPUT_BUCKET 决定了输出文件的存储位置。存储输出文件的目录采用以下格式:

  • gs://OUTPUT_BUCKET/model-MODEL_ID/tflite/YYYY-MM-DDThh:mm:ss.sssZ/

文件

  1. model.tflite:包含可立即用于 TensorFlow Lite 的模型版本的文件。

Edge TPU

您在请求中指定的 OUTPUT_BUCKET 决定了输出文件的存储位置。存储输出文件的目录采用以下格式:

  • gs://OUTPUT_BUCKET/model-MODEL_ID/edgetpu-tflite/YYYY-MM-DDThh:mm:ss.sssZ/

文件

  1. edgetpu_model.tflite:包含 TensorFlow Lite 模型版本的文件,通过 Edge TPU 编译器传递,以便与 Edge TPU 兼容。

容器

您在请求中指定的 OUTPUT_BUCKET 决定了输出文件的存储位置。存储输出文件的目录采用以下格式:

  • gs://OUTPUT_BUCKET/model-MODEL_ID/tf-saved-model/YYYY-MM-DDThh:mm:ss.sssZ/

文件

  1. saved_model.pb:包含图定义和模型权重的协议缓冲区文件。

Core ML

您在请求中指定的 OUTPUT_BUCKET 决定了输出文件的存储位置。存储输出文件的目录采用以下格式:

  • gs://OUTPUT_BUCKET/model-MODEL_ID/core-ml/YYYY-MM-DDThh:mm:ss.sssZ/

文件

  1. dict.txt:标签文件。标签文件 dict.txt 中的每一行表示模型返回的预测结果的一个标签,与请求的顺序相同。

    示例 dict.txt

    roses
    daisy
    tulips
    dandelion
    sunflowers
    
  2. model.mlmodel:指定 Core ML 模型的文件。

Tensorflow.js

您在请求中指定的 OUTPUT_BUCKET 决定了输出文件的存储位置。存储输出文件的目录采用以下格式:

  • gs://OUTPUT_BUCKET/model-MODEL_ID/tf-js/YYYY-MM-DDThh:mm:ss.sssZ/

文件

  1. dict.txt:标签文件。标签文件 dict.txt 中的每一行表示模型返回的预测结果的一个标签,与请求的顺序相同。

    示例 dict.txt

    roses
    daisy
    tulips
    dandelion
    sunflowers
    
  2. group1-shard1of3.bin:二进制文件。
  3. group1-shard2of3.bin:二进制文件。
  4. group1-shard3of3.bin:二进制文件。
  5. model.json:模型的 JSON 文件表示法。

    示例 model.json(为简洁起见,省略了部分内容)

    {
      "format": "graph-model",
      "generatedBy": "2.4.0",
      "convertedBy": "TensorFlow.js Converter v1.7.0",
      "userDefinedMetadata": {
        "signature": {
          "inputs": {
            "image:0": {
              "name": "image:0",
              "dtype": "DT_FLOAT",
              "tensorShape": {
                "dim": [
                  {
                    "size": "1"
                  },
                  {
                    "size": "224"
                  },
                  {
                    "size": "224"
                  },
                  {
                    "size": "3"
                  }
                ]
              }
            }
          },
          "outputs": {
            "scores:0": {
              "name": "scores:0",
              "dtype": "DT_FLOAT",
              "tensorShape": {
                "dim": [
                  {
                    "size": "1"
                  },
                  {
                    "size": "5"
                  }
                ]
              }
            }
          }
        }
      },
      "modelTopology": {
        "node": [
          {
            "name": "image",
            "op": "Placeholder",
            "attr": {
              "dtype": {
                "type": "DT_FLOAT"
              },
              "shape": {
                "shape": {
                  "dim": [
                    {
                      "size": "1"
                    },
                    {
                      "size": "224"
                    },
                    {
                      "size": "224"
                    },
                    {
                      "size": "3"
                    }
                  ]
                }
              }
            }
          },
          {
            "name": "mnas_v4_a_1/feature_network/feature_extractor/Mean/reduction_indices",
            "op": "Const",
            "attr": {
              "value": {
                "tensor": {
                  "dtype": "DT_INT32",
                  "tensorShape": {
                    "dim": [
                      {
                        "size": "2"
                      }
                    ]
                  }
                }
              },
              "dtype": {
                "type": "DT_INT32"
              }
            }
          },
          ...
          {
            "name": "scores",
            "op": "Identity",
            "input": [
              "Softmax"
            ],
            "attr": {
              "T": {
                "type": "DT_FLOAT"
              }
            }
          }
        ],
        "library": {},
        "versions": {}
      },
      "weightsManifest": [
        {
          "paths": [
            "group1-shard1of3.bin",
            "group1-shard2of3.bin",
            "group1-shard3of3.bin"
          ],
          "weights": [
            {
              "name": "mnas_v4_a_1/feature_network/feature_extractor/Mean/reduction_indices",
              "shape": [
                2
              ],
              "dtype": "int32"
            },
            {
              "name": "mnas_v4_a/output/fc/tf_layer/kernel",
              "shape": [
                1280,
                5
              ],
              "dtype": "float32"
            },
            ...
            {
              "name": "mnas_v4_a_1/feature_network/lead_cell_17/op_0/conv2d_0/Conv2D_weights",
              "shape": [
                1,
                1,
                320,
                1280
              ],
              "dtype": "float32"
            },
            {
              "name": "mnas_v4_a_1/feature_network/cell_14/op_0/expand_0/Conv2D_bn_offset",
              "shape": [
                1152
              ],
              "dtype": "float32"
            }
          ]
        }
      ]
    }

视频

在下面选择您的模型格式对应的标签页:

TF Lite

您在请求中指定的 OUTPUT_BUCKET 决定了输出文件的存储位置。存储输出文件的目录采用以下格式:

  • gs://OUTPUT_BUCKET/model-MODEL_ID/tflite/YYYY-MM-DDThh:mm:ss.sssZ/

文件

  1. model.tflite:包含可立即用于 TensorFlow Lite 的模型版本的文件。
  2. frozen_inference_graph.pb:包含图定义和模型权重的序列化协议缓冲区文件。
  3. label_map.pbtxt:标签映射文件,将每个使用的标签映射到一个整数值。

Edge TPU

您在请求中指定的 OUTPUT_BUCKET 决定了输出文件的存储位置。存储输出文件的目录采用以下格式:

  • gs://OUTPUT_BUCKET/model-MODEL_ID/edgetpu-tflite/YYYY-MM-DDThh:mm:ss.sssZ/

文件

  1. edgetpu_model.tflite:包含 TensorFlow Lite 模型版本的文件,通过 Edge TPU 编译器传递,以便与 Edge TPU 兼容。
  2. label_map.pbtxt:标签映射文件,将每个使用的标签映射到一个整数值。

容器

您在请求中指定的 OUTPUT_BUCKET 决定了输出文件的存储位置。存储输出文件的目录采用以下格式:

  • gs://OUTPUT_BUCKET/model-MODEL_ID/tf-saved-model/YYYY-MM-DDThh:mm:ss.sssZ/

文件

  1. frozen_inference_graph.pb:包含图定义和模型权重的序列化协议缓冲区文件。
  2. label_map.pbtxt:标签映射文件,将每个使用的标签映射到一个整数值。
  3. saved_model/saved_model.pb:此文件存储实际的 TensorFlow 程序(或模型)以及一组已命名的签名,每个签名标识一个接受张量输入并生成张量输出的函数。
  4. saved_model/variables/:此变量目录包含标准训练检查点。