训练 Edge 可导出模型

创建自定义模型的方法,是使用准备好的数据集对其进行训练。AutoML API 使用数据集中的条目来训练、测试模型并评估其性能。您可以查看结果、根据需要调整训练数据集,并使用改进的数据集训练新模型。

训练模型可能需要几个小时才能完成。借助 AutoML API,您可以查看训练的状态

每次开始训练时,AutoML Vision 都会创建新模型,因此您的项目可能包含大量模型。您可以获取项目中模型的列表删除不再需要的模型。另外,您也可以使用 Cloud AutoML Vision 界面来列出和删除通过 AutoML API 创建且不再需要的模型。

注意

  • 自正式版发布之日起,自定义模型的最长使用期限为 18 个月。该时间期限过后,您必须创建并训练新模型,才能继续对内容进行分类。
  • Edge 模型针对 Edge 设备上的推断进行了优化。因此,Edge 模型准确率与 Cloud 模型准确率 不同

模型基于 Google 最先进的研究。您的模型将采用 TF Lite 软件包的形式。如需详细了解如何使用 TensorFlow Lite SDK 集成 TensorFlow Lite 模型,请点击此处针对 iOSAndroid 的相应链接。

训练 Edge 模型

如果您有一个包含一组带标签的固定训练项的数据集,就可以创建和训练自定义 Edge 模型了。

TensorFlow Serving 和 TF Lite 模型

训练时,您可以根据具体使用场景选择所需的 Edge 模型类型:

  • 低延时 (mobile-low-latency-1)
  • 通用目的 (mobile-versatile-1)
  • 更高预测质量 (mobile-high-accuracy-1)

网页界面

  1. 打开 Vision Dashboard

    数据集页面显示当前项目的可用数据集。

    列出数据集页面
  2. 选择要用于训练自定义模型的数据集。
  3. 准备好数据集后,选择训练标签页和训练新模型按钮。

    此操作会打开包含训练选项的训练新模型侧边窗口。

  4. 定义模型训练部分中,更改模型名称(或使用默认值)并选择 Edge 作为模型类型。选择训练 Edge 模型后,选择继续训练 Edge 模型单选按钮图片
  5. 在接下来的模型优化选项部分中,选择所需的优化条件:较高准确率 (Higher accuracy)、最佳权衡 (Best tradeoff) 或较快预测速度 (Faster prediction)。选择优化规范后,再选择继续

    最佳权衡单选按钮图片
  6. 在接下来的设置节点时预算部分中,使用建议的节点时预算或指定其他值。

    默认情况下,对于大多数数据集来说,24 节点时足以训练模型。此建议值是使模型完全收敛的估算值。但是,您也可以选择其他数值。图像分类的最小节点时为 8。对于 Object Detection,此最小值为 20。

    设置节点预算部分
  7. 选择开始训练以开始训练模型。

训练模型可能需要几个小时才能完成。模型训练成功后,您用于 Google Cloud Platform 项目的电子邮件地址会收到一封邮件。

REST 和命令行

在训练 Edge 模型时,您可以在 modelType 字段中指定三个不同的值,具体取决于您的模型需求:

  • mobile-low-latency-1 表示低延时
  • mobile-versatile-1 表示通用目的
  • mobile-high-accuracy-1 表示较高预测质量

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

  • project-id:您的 GCP 项目 ID。
  • display-name:您选择的字符串显示名。
  • dataset-id:您的数据集的 ID。此 ID 是数据集名称的最后一个元素。例如:
    • 数据集名称:projects/project-id/locations/location-id/datasets/3104518874390609379
    • 数据集 ID:3104518874390609379

HTTP 方法和网址:

POST https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models

请求 JSON 正文:

{
  "displayName": "display-name",
  "datasetId": "dataset-id",
  "imageClassificationModelMetadata": {
    "trainBudget": "1",
    "modelType": "mobile-low-latency-1"
  }
}

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

curl

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

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models

PowerShell

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

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

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models" | Select-Object -Expand Content

您应该会看到类似如下所示的输出。可以使用操作 ID(本例中为 ICN2106290444865378475)来获取任务的状态。如需查看示例,请参阅处理长时间运行的操作

{
  "name": "projects/project-id/locations/us-central1/operations/ICN2106290444865378475",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1.OperationMetadata",
    "createTime": "2019-10-30T20:06:08.253243Z",
    "updateTime": "2019-10-30T20:06:08.253243Z",
    "createModelDetails": {}
  }
}

Go

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。

import (
	"context"
	"fmt"
	"io"

	automl "cloud.google.com/go/automl/apiv1"
	automlpb "google.golang.org/genproto/googleapis/cloud/automl/v1"
)

// visionClassificationCreateModel creates a model for image classification.
func visionClassificationCreateModel(w io.Writer, projectID string, location string, datasetID string, modelName string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	// datasetID := "ICN123456789..."
	// modelName := "model_display_name"

	ctx := context.Background()
	client, err := automl.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %v", err)
	}
	defer client.Close()

	req := &automlpb.CreateModelRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
		Model: &automlpb.Model{
			DisplayName: modelName,
			DatasetId:   datasetID,
			ModelMetadata: &automlpb.Model_ImageClassificationModelMetadata{
				ImageClassificationModelMetadata: &automlpb.ImageClassificationModelMetadata{
					TrainBudgetMilliNodeHours: 1000, // 1000 milli-node hours are 1 hour
				},
			},
		},
	}

	op, err := client.CreateModel(ctx, req)
	if err != nil {
		return fmt.Errorf("CreateModel: %v", err)
	}
	fmt.Fprintf(w, "Processing operation name: %q\n", op.Name())
	fmt.Fprintf(w, "Training started...\n")

	return nil
}

Java

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.ImageClassificationModelMetadata;
import com.google.cloud.automl.v1.LocationName;
import com.google.cloud.automl.v1.Model;
import com.google.cloud.automl.v1.OperationMetadata;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class VisionClassificationCreateModel {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR_PROJECT_ID";
    String datasetId = "YOUR_DATASET_ID";
    String displayName = "YOUR_DATASET_NAME";
    createModel(projectId, datasetId, displayName);
  }

  // Create a model
  static void createModel(String projectId, String datasetId, String displayName)
      throws IOException, ExecutionException, InterruptedException {
    // 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 (AutoMlClient client = AutoMlClient.create()) {
      // A resource that represents Google Cloud Platform location.
      LocationName projectLocation = LocationName.of(projectId, "us-central1");
      // Set model metadata.
      ImageClassificationModelMetadata metadata =
          ImageClassificationModelMetadata.newBuilder().setTrainBudgetMilliNodeHours(24000).build();
      Model model =
          Model.newBuilder()
              .setDisplayName(displayName)
              .setDatasetId(datasetId)
              .setImageClassificationModelMetadata(metadata)
              .build();

      // Create a model with the model metadata in the region.
      OperationFuture<Model, OperationMetadata> future =
          client.createModelAsync(projectLocation, model);
      // OperationFuture.get() will block until the model is created, which may take several hours.
      // 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("Training operation name: %s%n", future.getInitialFuture().get().getName());
      System.out.println("Training started...");
    }
  }
}

Node.js

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const dataset_id = 'YOUR_DATASET_ID';
// const displayName = 'YOUR_DISPLAY_NAME';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require('@google-cloud/automl').v1;

// Instantiates a client
const client = new AutoMlClient();

async function createModel() {
  // Construct request
  const request = {
    parent: client.locationPath(projectId, location),
    model: {
      displayName: displayName,
      datasetId: datasetId,
      imageClassificationModelMetadata: {
        trainBudgetMilliNodeHours: 24000,
      },
    },
  };

  // Don't wait for the LRO
  const [operation] = await client.createModel(request);
  console.log(`Training started... ${operation}`);
  console.log(`Training operation name: ${operation.name}`);
}

createModel();

PHP

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。

use Google\Cloud\AutoMl\V1\AutoMlClient;
use Google\Cloud\AutoMl\V1\Model;
use Google\Cloud\AutoMl\V1\ImageClassificationModelMetadata;

/** Uncomment and populate these variables in your code */
// $projectId = '[Google Cloud Project ID]';
// $location = 'us-central1';
// $datasetId = 'my_dataset_id_123';
// $displayName = 'your_dataset_name';

$client = new AutoMlClient();

try {
    // resource that represents Google Cloud Platform location
    $formattedParent = $client->locationName(
        $projectId,
        $location
    );

    // leave model unset to use the default base model provided by Google
    $metadata = new ImageClassificationModelMetadata();
    $model = (new Model())
        ->setDisplayName($displayName)
        ->setDatasetId($datasetId)
        ->setImageClassificationModelMetadata($metadata);

    // create model with above location and metadata
    $operationResponse = $client->createModel($formattedParent, $model);
    $operation = $operationResponse->getOperation();
    printf('Training operation name: %s' . PHP_EOL, $operation->getName());
    print('Training started...' . PHP_EOL);
} finally {
    $client->close();
}

Python

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。

from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"
# dataset_id = "YOUR_DATASET_ID"
# display_name = "your_models_display_name"

client = automl.AutoMlClient()

# A resource that represents Google Cloud Platform location.
project_location = client.location_path(project_id, "us-central1")
# Leave model unset to use the default base model provided by Google
# train_budget_milli_node_hours: The actual train_cost will be equal or
# less than this value.
# https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#imageclassificationmodelmetadata
metadata = automl.types.ImageClassificationModelMetadata(
    train_budget_milli_node_hours=24000
)
model = automl.types.Model(
    display_name=display_name,
    dataset_id=dataset_id,
    image_classification_model_metadata=metadata,
)

# Create a model with the model metadata in the region.
response = client.create_model(project_location, model)

print("Training operation name: {}".format(response.operation.name))
print("Training started...")

Ruby

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。

require "google/cloud/automl"

project_id = "YOUR_PROJECT_ID"
dataset_id = "YOUR_DATASET_ID"
display_name = "YOUR_MODEL_NAME"

client = Google::Cloud::AutoML.auto_ml

# A resource that represents Google Cloud Platform location.
project_location = client.location_path project: project_id,
                                        location: "us-central1"
# Leave model unset to use the default base model provided by Google
model = {
  display_name: display_name,
  dataset_id: dataset_id,
  image_classification_model_metadata: {}
)

# Create a model with the model metadata in the region.
operation = client.create_model parent: project_location,
                                model: model

puts "Training started..."

# Wait until the long running operation is done
operation.wait_until_done!

puts "Training complete."

列出操作状态

使用以下代码示例列出项目的操作并过滤结果。

REST 和命令行

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

  • project-id:您的 GCP 项目 ID。

HTTP 方法和网址:

GET https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/operations

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

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/operations

PowerShell

执行以下命令:

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

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/operations" | Select-Object -Expand Content

您看到的输出会因您请求的操作而异。

您还可以使用选择查询参数(operationIddoneworksOn)过滤返回的操作。例如,如需返回已完成运行的操作列表,请修改网址:

GET https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/operations?filter="done=true"

Go

在试用此示例之前,请按照 API 与参考文档 > 客户端库页面上与此编程语言对应的设置说明进行操作。

import (
	"context"
	"fmt"
	"io"

	automl "cloud.google.com/go/automl/apiv1"
	"google.golang.org/api/iterator"
	"google.golang.org/genproto/googleapis/longrunning"
)

// listOperationStatus lists existing operations' status.
func listOperationStatus(w io.Writer, projectID string, location string) error {
	// projectID := "my-project-id"
	// location := "us-central1"

	ctx := context.Background()
	client, err := automl.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %v", err)
	}
	defer client.Close()

	req := &longrunning.ListOperationsRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
	}

	it := client.LROClient.ListOperations(ctx, req)

	// Iterate over all results
	for {
		op, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListOperations.Next: %v", err)
		}

		fmt.Fprintf(w, "Name: %v\n", op.GetName())
		fmt.Fprintf(w, "Operation details:\n")
		fmt.Fprintf(w, "%v", op)
	}

	return nil
}

Java

在试用此示例之前,请按照 API 与参考文档 > 客户端库页面上与此编程语言对应的设置说明进行操作。

import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.LocationName;
import com.google.longrunning.ListOperationsRequest;
import com.google.longrunning.Operation;
import java.io.IOException;

class ListOperationStatus {

  static void listOperationStatus() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR_PROJECT_ID";
    listOperationStatus(projectId);
  }

  // Get the status of an operation
  static void listOperationStatus(String projectId) throws IOException {
    // 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 (AutoMlClient client = AutoMlClient.create()) {
      // A resource that represents Google Cloud Platform location.
      LocationName projectLocation = LocationName.of(projectId, "us-central1");

      // Create list operations request.
      ListOperationsRequest listrequest =
          ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build();

      // List all the operations names available in the region by applying filter.
      for (Operation operation :
          client.getOperationsClient().listOperations(listrequest).iterateAll()) {
        System.out.println("Operation details:");
        System.out.format("\tName: %s\n", operation.getName());
        System.out.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl());
        System.out.format("\tDone: %s\n", operation.getDone());
        if (operation.hasResponse()) {
          System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl());
        }
        if (operation.hasError()) {
          System.out.println("\tResponse:");
          System.out.format("\t\tError code: %s\n", operation.getError().getCode());
          System.out.format("\t\tError message: %s\n\n", operation.getError().getMessage());
        }
      }
    }
  }
}

Node.js

在试用此示例之前,请按照 API 与参考文档 > 客户端库页面上与此编程语言对应的设置说明进行操作。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require('@google-cloud/automl').v1;

// Instantiates a client
const client = new AutoMlClient();

async function listOperationStatus() {
  // Construct request
  const request = {
    name: client.locationPath(projectId, location),
    filter: '',
  };

  const [response] = await client.operationsClient.listOperations(request);

  console.log('List of operation status:');
  for (const operation of response) {
    console.log(`Name: ${operation.name}`);
    console.log('Operation details:');
    console.log(`${operation}`);
  }
}

listOperationStatus();

PHP

在试用此示例之前,请按照 API 与参考文档 > 客户端库页面上与此编程语言对应的设置说明进行操作。

use Google\ApiCore\LongRunning\OperationsClient;

/** Uncomment and populate these variables in your code */
// $projectId = '[Google Cloud Project ID]';
// $location = 'us-central1';
// $operationId = 'my_operation_id_123';

$client = new OperationsClient();

try {
    // resource that represents Google Cloud Platform location
    $formattedName = $client->locationName(
        $projectId,
        $location
    );

    // list all operations
    $filter = '';
    $pagedResponse = $client->listOperations($formattedName, '');

    print('List of models' . PHP_EOL);
    foreach ($pagedResponse->iteratePages() as $page) {
        foreach ($page as $operation) {
            // display operation information
            printf('Operation name: %s' . PHP_EOL, $operation->getName());
            print('Operation details: ');
            print($operation);
        }
    }
} finally {
    if (isset($client)) {
        $client->close();
    }
}

Python

在试用此示例之前,请按照 API 与参考文档 > 客户端库页面上与此编程语言对应的设置说明进行操作。

from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"

client = automl.AutoMlClient()
# A resource that represents Google Cloud Platform location.
project_location = f"projects/{project_id}/locations/us-central1"
# List all the operations names available in the region.
response = client._transport.operations_client.list_operations(
    name=project_location, filter_="", timeout=5
)

print("List of operations:")
for operation in response:
    print("Name: {}".format(operation.name))
    print("Operation details:")
    print(operation)

处理长时间运行的操作

REST 和命令行

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

  • project-id:您的 GCP 项目 ID。
  • operation-id:您的操作的 ID。此 ID 是操作名称的最后一个元素。例如:
    • 操作名称:projects/project-id/locations/location-id/operations/IOD5281059901324392598
    • 操作 ID:IOD5281059901324392598

HTTP 方法和网址:

GET https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/operations/operation-id

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

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/operations/operation-id

PowerShell

执行以下命令:

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

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/operations/operation-id" | Select-Object -Expand Content
完成导入操作后,您应该会看到类似如下所示的输出:
{
  "name": "projects/project-id/locations/us-central1/operations/operation-id",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1.OperationMetadata",
    "createTime": "2018-10-29T15:56:29.176485Z",
    "updateTime": "2018-10-29T16:10:41.326614Z",
    "importDataDetails": {}
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.protobuf.Empty"
  }
}

完成创建模型操作后,您应会看到如下输出:

{
  "name": "projects/project-id/locations/us-central1/operations/operation-id",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1.OperationMetadata",
    "createTime": "2019-07-22T18:35:06.881193Z",
    "updateTime": "2019-07-22T19:58:44.972235Z",
    "createModelDetails": {}
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.cloud.automl.v1.Model",
    "name": "projects/project-id/locations/us-central1/models/model-id"
  }
}

Go

在试用此示例之前,请按照 API 与参考文档 > 客户端库页面上与此编程语言对应的设置说明进行操作。

import (
	"context"
	"fmt"
	"io"

	automl "cloud.google.com/go/automl/apiv1"
	"google.golang.org/genproto/googleapis/longrunning"
)

// getOperationStatus gets an operation's status.
func getOperationStatus(w io.Writer, projectID string, location string, operationID string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	// operationID := "TRL123456789..."

	ctx := context.Background()
	client, err := automl.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %v", err)
	}
	defer client.Close()

	req := &longrunning.GetOperationRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/operations/%s", projectID, location, operationID),
	}

	op, err := client.LROClient.GetOperation(ctx, req)
	if err != nil {
		return fmt.Errorf("GetOperation: %v", err)
	}

	fmt.Fprintf(w, "Name: %v\n", op.GetName())
	fmt.Fprintf(w, "Operation details:\n")
	fmt.Fprintf(w, "%v", op)

	return nil
}

Java

在试用此示例之前,请按照 API 与参考文档 > 客户端库页面上与此编程语言对应的设置说明进行操作。

import com.google.cloud.automl.v1.AutoMlClient;
import com.google.longrunning.Operation;
import java.io.IOException;

class GetOperationStatus {

  static void getOperationStatus() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String operationFullId = "projects/[projectId]/locations/us-central1/operations/[operationId]";
    getOperationStatus(operationFullId);
  }

  // Get the status of an operation
  static void getOperationStatus(String operationFullId) throws IOException {
    // 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 (AutoMlClient client = AutoMlClient.create()) {
      // Get the latest state of a long-running operation.
      Operation operation = client.getOperationsClient().getOperation(operationFullId);

      // Display operation details.
      System.out.println("Operation details:");
      System.out.format("\tName: %s\n", operation.getName());
      System.out.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl());
      System.out.format("\tDone: %s\n", operation.getDone());
      if (operation.hasResponse()) {
        System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl());
      }
      if (operation.hasError()) {
        System.out.println("\tResponse:");
        System.out.format("\t\tError code: %s\n", operation.getError().getCode());
        System.out.format("\t\tError message: %s\n", operation.getError().getMessage());
      }
    }
  }
}

Node.js

在试用此示例之前,请按照 API 与参考文档 > 客户端库页面上与此编程语言对应的设置说明进行操作。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const operationId = 'YOUR_OPERATION_ID';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require('@google-cloud/automl').v1;

// Instantiates a client
const client = new AutoMlClient();

async function getOperationStatus() {
  // Construct request
  const request = {
    name: `projects/${projectId}/locations/${location}/operations/${operationId}`,
  };

  const [response] = await client.operationsClient.getOperation(request);

  console.log(`Name: ${response.name}`);
  console.log('Operation details:');
  console.log(`${response}`);
}

getOperationStatus();

PHP

在试用此示例之前,请按照 API 与参考文档 > 客户端库页面上与此编程语言对应的设置说明进行操作。

use Google\ApiCore\LongRunning\OperationsClient;

/** Uncomment and populate these variables in your code */
// $projectId = '[Google Cloud Project ID]';
// $location = 'us-central1';
// $operationId = 'my_operation_id_123';

$client = new OperationsClient();

try {
    // full name of operation
    $formattedName = 'projects/' . $projectId . '/locations/us-central1/operations/' . $operationId;

    // get latest state of long running operation
    $operation = $client->getOperation($name);
    printf('Operation name: %s' . PHP_EOL, $operation->getName());
    print('Operation details: ');
    print($operation);
} finally {
    if (isset($client)) {
        $client->close();
    }
}

Python

在试用此示例之前,请按照 API 与参考文档 > 客户端库页面上与此编程语言对应的设置说明进行操作。

from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# operation_full_id = \
#     "projects/[projectId]/locations/us-central1/operations/[operationId]"

client = automl.AutoMlClient()
# Get the latest state of a long-running operation.
response = client._transport.operations_client.get_operation(
    operation_full_id
)

print("Name: {}".format(response.name))
print("Operation details:")
print(response)

取消操作

您可以使用操作 ID 取消导入任务或训练任务。

REST 和命令行

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

  • project-id:您的 GCP 项目 ID。
  • operation-id:您的操作的 ID。此 ID 是操作名称的最后一个元素。例如:
    • 操作名称:projects/project-id/locations/location-id/operations/IOD5281059901324392598
    • 操作 ID:IOD5281059901324392598

HTTP 方法和网址:

POST https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/operations/operation-id:cancel

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

curl

执行以下命令:

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d "" \
https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/operations/operation-id:cancel

PowerShell

执行以下命令:

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

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-Uri "https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/operations/operation-id:cancel" | Select-Object -Expand Content
如果请求成功,则将返回空的 JSON 对象:
{}

获取模型的相关信息

训练完成后,您可以获取有关新创建的模型的信息。

REST 和命令行

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

  • project-id:您的 GCP 项目 ID。
  • model-id:您的模型的 ID(从创建模型时返回的响应中获取)。此 ID 是模型名称的最后一个元素。 例如:
    • 模型名称:projects/project-id/locations/location-id/models/IOD4412217016962778756
    • 模型 ID:IOD4412217016962778756

HTTP 方法和网址:

GET https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models/model-id

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

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models/model-id

PowerShell

执行以下命令:

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

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models/model-id" | Select-Object -Expand Content

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



    {
  "name": "projects/project-id/locations/us-central1/models/model-id",
  "displayName": "display-name",
  "datasetId": "dataset-id",
  "createTime": "2019-10-30T20:06:08.253243Z",
  "deploymentState": "UNDEPLOYED",
  "updateTime": "2019-10-30T20:54:50.472328Z",
  "imageClassificationModelMetadata": {
    "trainBudget": "1",
    "modelType": "mobile-low-latency-1",
    "nodeQps": 3.2
  }
}

Go

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。

import (
	"context"
	"fmt"
	"io"

	automl "cloud.google.com/go/automl/apiv1"
	automlpb "google.golang.org/genproto/googleapis/cloud/automl/v1"
)

// getModel gets a model.
func getModel(w io.Writer, projectID string, location string, modelID string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	// modelID := "TRL123456789..."

	ctx := context.Background()
	client, err := automl.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %v", err)
	}
	defer client.Close()

	req := &automlpb.GetModelRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/models/%s", projectID, location, modelID),
	}

	model, err := client.GetModel(ctx, req)
	if err != nil {
		return fmt.Errorf("GetModel: %v", err)
	}

	// Retrieve deployment state.
	deploymentState := "undeployed"
	if model.GetDeploymentState() == automlpb.Model_DEPLOYED {
		deploymentState = "deployed"
	}

	// Display the model information.
	fmt.Fprintf(w, "Model name: %v\n", model.GetName())
	fmt.Fprintf(w, "Model display name: %v\n", model.GetDisplayName())
	fmt.Fprintf(w, "Model create time:\n")
	fmt.Fprintf(w, "\tseconds: %v\n", model.GetCreateTime().GetSeconds())
	fmt.Fprintf(w, "\tnanos: %v\n", model.GetCreateTime().GetNanos())
	fmt.Fprintf(w, "Model deployment state: %v\n", deploymentState)

	return nil
}

Java

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。

import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.Model;
import com.google.cloud.automl.v1.ModelName;
import com.google.protobuf.Timestamp;
import java.io.IOException;

class GetModel {

  static void getModel() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR_PROJECT_ID";
    String modelId = "YOUR_MODEL_ID";
    getModel(projectId, modelId);
  }

  // Get a model
  static void getModel(String projectId, String modelId) throws IOException {
    // 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 (AutoMlClient client = AutoMlClient.create()) {
      // Get the full path of the model.
      ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
      Model model = client.getModel(modelFullId);

      // Display the model information.
      System.out.format("Model name: %s%n", model.getName());
      // To get the model id, you have to parse it out of the `name` field. As models Ids are
      // required for other methods.
      // Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}`
      String[] names = model.getName().split("/");
      String retrievedModelId = names[names.length - 1];
      System.out.format("Model id: %s%n", retrievedModelId);
      System.out.format("Model display name: %s%n", model.getDisplayName());
      System.out.println("Model create time:");
      Timestamp createdTime = model.getCreateTime();
      System.out.format("\tseconds: %s%n", createdTime.getSeconds());
      System.out.format("\tnanos: %s%n", createdTime.getNanos());
      System.out.format("Model deployment state: %s%n", model.getDeploymentState());
    }
  }
}

Node.js

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require('@google-cloud/automl').v1;

// Instantiates a client
const client = new AutoMlClient();

async function getModel() {
  // Construct request
  const request = {
    name: client.modelPath(projectId, location, modelId),
  };

  const [response] = await client.getModel(request);

  console.log(`Model name: ${response.name}`);
  console.log(
    `Model id: ${
      response.name.split('/')[response.name.split('/').length - 1]
    }`
  );
  console.log(`Model display name: ${response.displayName}`);
  console.log('Model create time');
  console.log(`\tseconds ${response.createTime.seconds}`);
  console.log(`\tnanos ${response.createTime.nanos / 1e9}`);
  console.log(`Model deployment state: ${response.deploymentState}`);
}

getModel();

PHP

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。

use Google\Cloud\AutoMl\V1\AutoMlClient;
use Google\Cloud\AutoMl\V1\Model\DeploymentState;

/** Uncomment and populate these variables in your code */
// $projectId = '[Google Cloud Project ID]';
// $location = 'us-central1';
// $modelId = 'my_model_id_123';

$client = new AutoMlClient();

try {
    // get full path of model
    $formattedName = $client->modelName(
        $projectId,
        $location,
        $modelId
    );

    $model = $client->getModel($formattedName);

    // retrieve deployment state
    if ($model->getDeploymentState() == DeploymentState::DEPLOYED) {
        $deployment_state = 'deployed';
    } else {
        $deployment_state = 'undeployed';
    }

    // display model information
    $splitName = explode('/', $model->getName());
    printf('Model name: %s' . PHP_EOL, $model->getName());
    printf('Model id: %s' . PHP_EOL, end($splitName));
    printf('Model display name: %s' . PHP_EOL, $model->getDisplayName());
    printf('Model create time' . PHP_EOL);
    printf('seconds: %d' . PHP_EOL, $model->getCreateTime()->getSeconds());
    printf('nanos : %d' . PHP_EOL, $model->getCreateTime()->getNanos());
    printf('Model deployment state: %s' . PHP_EOL, $deployment_state);
} finally {
    $client->close();
}

Python

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。

from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"
# model_id = "YOUR_MODEL_ID"

client = automl.AutoMlClient()
# Get the full path of the model.
model_full_id = client.model_path(project_id, "us-central1", model_id)
model = client.get_model(model_full_id)

# Retrieve deployment state.
if model.deployment_state == automl.enums.Model.DeploymentState.DEPLOYED:
    deployment_state = "deployed"
else:
    deployment_state = "undeployed"

# Display the model information.
print("Model name: {}".format(model.name))
print("Model id: {}".format(model.name.split("/")[-1]))
print("Model display name: {}".format(model.display_name))
print("Model create time:")
print("\tseconds: {}".format(model.create_time.seconds))
print("\tnanos: {}".format(model.create_time.nanos))
print("Model deployment state: {}".format(deployment_state))

Ruby

在试用此示例之前,请按照客户端库页面中与此编程语言对应的设置说明执行操作。

require "google/cloud/automl"

project_id = "YOUR_PROJECT_ID"
model_id = "YOUR_MODEL_ID"

client = Google::Cloud::AutoML.auto_ml

# Get the full path of the model.
model_full_id = client.model_path project: project_id,
                                  location: "us-central1",
                                  model: model_id

model = client.get_model name: model_full_id

# Retrieve deployment state.
deployment_state = if model.deployment_state == :DEPLOYED
                     "deployed"
                   else
                     "undeployed"
                   end

# Display the model information.
puts "Model name: #{model.name}"
puts "Model id: #{model.name.split('/').last}"
puts "Model display name: #{model.display_name}"
puts "Model create time: #{model.create_time.to_time}"
puts "Model deployment state: #{deployment_state}"

可恢复训练

您现在可以暂停并在后续恢复大型数据集(包含一千张以上的图片)的自定义模型训练。 后续训练具有以下限制:

  • 基本模型时间限制 - 您只能对最近 14 天内训练过的模型继续训练;在请求发出 14 天之前创建的基本模型不符合后续训练的条件。
  • 不可修改标签 - 如果您更改基本模型数据集中的标签,则后续训练将失败。
  • 无法保证更好的性能 - 对模型进行后续训练并不能保证获得更好的模型性能。

网页界面

  1. 打开 AutoML Vision UI

    数据集页面显示当前项目的可用数据集。

    数据集列表页面
  2. 选择要用于训练自定义模型的数据集。

    所选数据集的显示名会显示在标题栏中,该页面还会列出数据集中的各个训练项及其标签。

    图片项页面
  3. 查看完数据集后,选择标题栏正下方的训练标签页。

    如果您以前训练过模型,则它们将显示在此标签页的列表中。

    经过训练的模型列表
  4. 如果可以对某个模型进行可恢复的训练,则该训练将在查看完整评估选项旁显示为一个选项。

    选择继续训练即可继续训练适用的模型。

    “继续训练”选项
  5. 与训练原始模型类似,选择继续训练将打开“训练新模型”侧边窗口。在定义模型部分中,您可以更改新模型的名称。您还将看到用于训练此模型的基本模型的名称。

    “定义模型”侧边窗口

    定义好模型后,选择继续以转到下一部分。

  6. 在下面的设置节点时预算部分中,指定节点时预算。与初始模型训练类似,此预算受价格结构和配额限制的约束。

    指定训练预算后,选择开始训练。完成可恢复的训练后,您会收到一封电子邮件。

REST 和命令行

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

  • base-model-id:基本(现有)模型的 ID。新模型基于这个基本模型创建。以下新模型规范必须与基本模型匹配:

    * 即使新模型请求中省略了这个字段,系统也会检查基本和新的 modelType

  • project-id:您的 GCP 项目 ID。
  • display-name:您选择的字符串显示名。
  • dataset-id:您的数据集的 ID。此 ID 是数据集名称的最后一个元素。例如:
    • 数据集名称:projects/project-id/locations/location-id/datasets/3104518874390609379
    • 数据集 ID:3104518874390609379

HTTP 方法和网址:

POST https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models

请求 JSON 正文:

{
  "baseModelId": base-model-id,
  "displayName": "display-name",
  "datasetId": "dataset-id",
  "imageClassificationModelMetadata": {
    "trainBudget": "1",
    "modelType": "mobile-low-latency-1"
  }
}

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

curl

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

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models

PowerShell

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

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

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models" | Select-Object -Expand Content

您应该会看到类似如下所示的输出。可以使用操作 ID(本例中为 ICN2106290444865378475)来获取任务的状态。如需查看示例,请参阅处理长时间运行的操作

{
  "name": "projects/project-id/locations/us-central1/operations/ICN2106290444865378475",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1.OperationMetadata",
    "createTime": "2019-10-30T20:06:08.253243Z",
    "updateTime": "2019-10-30T20:06:08.253243Z",
    "createModelDetails": {}
  }
}