Training models

You create a custom model by training it using a prepared dataset. AutoML API uses the images from the dataset to train the model, test it, and evaluate its performance. You review the results, adjust the training dataset as needed, and train a new model using the improved dataset.

Training a model can take several hours to complete. The AutoML API enables you to check the status of training.

Note: Unless otherwise specified in applicable terms of service or documentation, custom models created in Cloud AutoML products cannot be exported.

Training models

When you have a dataset with a solid set of annotated training images with bounding boxes and labels, you are ready to create and train the custom model.

Web UI

  1. Open the Cloud AutoML Vision Object Detection UI.

    The Datasets page shows the available datasets for the current project.

    Listing a dataset page

  2. Select the dataset you want to use to train the custom model.

  3. When the dataset is ready, select the Train tab and Train new model button.

    This will open a "Train new model" side window with training options.

  4. From the Define your model section, specify a Model name (or accept the default name). Select radio_button_checked Cloud hosted as the model type if not already specified. After selecting to train an Edge model select Continue.

    Train Cloud model radio button image

  5. In the following Optimize model for section, select your desired optimization criterion: radio_button_checked Higher accuracy or Faster prediction. After selecting the optimization specification select Continue.

    Best trade off radio button image

  6. In the following Set a node hour budget section specify your desired node budget.

    In this section you can also opt in to auto-deployment of your model after training by selecting check_box Deploy model to 1 node after training. Otherwise you will have to manually deploy your model after training is finished.

    setting the node budget section

  7. Select Start training to begin model training.

Training a model can take several hours to complete. After the model is successfully trained, you will receive a message at the e-mail address that you used for your Google Cloud Platform project.

REST & CMD LINE

Before using any of the request data below, make the following replacements:

  • project-id: your GCP project ID.
  • dataset-id: the ID of your dataset. The ID is the last element of the name of your dataset. For example:
    • dataset name: projects/${project-id}/locations/us-central1/datasets/3104518874390609379
    • dataset id: 3104518874390609379
  • display-name: a string display name of your choosing.

Specific field considerations:

  • imageObjectDetectionModelMetadata.modelType- Indicates one of two available options for how to optimize the model:
    • cloud-low-latency-1 - Optimizes training for latency.
    • cloud-high-accuracy-1 - Optimizes training for accuracy.
  • imageObjectDetectionModelMetadata.trainBudgetMilliNodeHours - The train budget of creating this model, expressed in milli node hours (1,000 value in this field means 1 node hour). The actual trainCostMilliNodeHours will be equal or less than this value. If further model training ceases to provide any improvements, it will stop without using full budget and the stopReason will be MODEL_CONVERGED.

    Note: node_hour = actual_hour * number_of_nodes_involved.

    For model type cloud-high-accuracy-1(default) and cloud-low-latency-1, the train budget must be between 20,000 and 2,000,000 milli node hours, inclusive. The default value is 216,000, which represents one day in wall-clock time.

HTTP method and URL:

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

Request JSON body:

{
  "displayName": "display-name",
  "datasetId": "dataset-id",
  "imageObjectDetectionModelMetadata": {
    "modelType": "cloud-low-latency-1",
    "trainBudgetMilliNodeHours": "216000"
  }
}

To send your request, expand one of these options:

You should see output similar to the following. You can use the operation ID (IOD3074819451447675546, in this case) to get the status of the task. For an example, see Getting the status of an operation.

{
  "name": "projects/project-id/locations/us-central1/operations/IOD3074819451447675546",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "createTime": "2019-07-26T21:10:18.338846Z",
    "updateTime": "2019-07-26T21:10:18.338846Z",
    "createModelDetails": {}
  }
}

Java

/**
 * Demonstrates using the AutoML client to create a model.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name. (e.g., "us-central1")
 * @param datasetId the Id of the dataset to which model is created.
 * @param modelName the name of the model.
 * @throws IOException
 * @throws InterruptedException
 * @throws ExecutionException
 */
public static void createModel(
    String projectId, String computeRegion, String datasetId, String modelName)
    throws IOException, InterruptedException, ExecutionException {
  // Instantiates a client
  AutoMlClient client = AutoMlClient.create();

  // A resource that represents Google Cloud Platform location.
  LocationName projectLocation = LocationName.of(projectId, computeRegion);

  // Set model metadata.
  ImageObjectDetectionModelMetadata imageObjectDetectionModelMetadata =
      ImageObjectDetectionModelMetadata.newBuilder().build();

  // Set model name and model metadata for the image dataset.
  Model myModel =
      Model.newBuilder()
          .setDisplayName(modelName)
          .setDatasetId(datasetId)
          .setImageObjectDetectionModelMetadata(imageObjectDetectionModelMetadata)
          .build();

  // Create a model with the model metadata in the region.
  OperationFuture<Model, OperationMetadata> response =
      client.createModelAsync(projectLocation, myModel);

  System.out.println(
      String.format("Training operation name: %s", response.getInitialFuture().get().getName()));
  System.out.println("Training started...");
}

Node.js

/**
 * Demonstrates using the AutoML client to create a model.
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project";
// const computeRegion = '[REGION_NAME]' e.g., "us-central1";
// const datasetId = '[DATASET_ID]' e.g., "IOD34216801856389120";
// const modelName = '[MODEL_NAME]' e.g., "myModel";
//Imports the Google Cloud Automl library
const {AutoMlClient} = require('@google-cloud/automl').v1beta1;

// Instantiates a client
const automlClient = new AutoMlClient();
async function createModel() {
  // A resource that represents Google Cloud Platform location.
  const projectLocation = automlClient.locationPath(projectId, computeRegion);

  // Set datasetId, model name and model metadata for the dataset.
  const myModel = {
    displayName: modelName,
    datasetId: datasetId,
    imageObjectDetectionModelMetadata: {},
  };

  // Create a model with the model metadata in the region.
  const [response] = await automlClient.createModel({
    parent: projectLocation,
    model: myModel,
  });

  const initialApiResponse = response[1];
  console.log(`Training operation name: ${initialApiResponse.name}`);
  console.log(`Training started...`);
}
createModel();

Python

## To do: Uncomment and set the following variables
# project_id = '[PROJECT_ID]'
# compute_region = '[COMPUTE_REGION]'
# dataset_id = '[DATASET_ID]' # From the previous step. N.B. id != display_name
# model_name = '[MODEL_NAME]'

from google.cloud import automl_v1beta1 as automl

client = automl.AutoMlClient()

# A resource that represents Google Cloud Platform location.
project_location = client.location_path(project_id, compute_region)

# Set model name and model metadata for the image dataset.
my_model = {
    'display_name': model_name,
    'dataset_id': dataset_id,
    'image_object_detection_model_metadata': {}
}

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

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

List operations status

You can list your project's operations, and filter results.

REST & CMD LINE

Before using any of the request data below, make the following replacements:

  • project-id: your GCP project ID.

HTTP method and URL:

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

To send your request, expand one of these options:

The output you see will vary depending on the operations you have requested.

You can also filter the operations returned by using select query parameters (operationId, done, and worksOn). For example, to return a list of operations that have finished running modify the URL:

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

Java

/**
 * Demonstrates using the AutoML client to list the operations status.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name. (e.g., "us-central1")
 * @param filter the filter expression.
 * @throws IOException
 */
public static void listOperationsStatus(String projectId, String computeRegion, String filter)
    throws IOException {
  // Instantiates a client.
  AutoMlClient client = AutoMlClient.create();

  // A resource that represents Google Cloud Platform location.
  LocationName projectLocation = LocationName.of(projectId, computeRegion);

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

  // List all the operations names available in the region by applying filter.
  for (Operation operation :
      client.getOperationsClient().listOperations(listrequest).iterateAll()) {
    // Display operation details.
    System.out.println(String.format("Operation details:"));
    System.out.println(String.format("\tName: %s", operation.getName()));
    System.out.println(String.format("\tMetadata:"));
    System.out.println(String.format("\t\tType Url: %s", operation.getMetadata().getTypeUrl()));
    System.out.println(
        String.format(
            "\t\tValue: %s",
            operation.getMetadata().getValue().toStringUtf8().replace("\n", "")));

    System.out.println(String.format("\tDone: %s", operation.getDone()));
    if (operation.hasResponse()) {
      System.out.println("\tResponse:");
      System.out.println(String.format("\t\tType Url: %s", operation.getResponse().getTypeUrl()));
      System.out.println(
          String.format(
              "\t\tValue: %s",
              operation.getResponse().getValue().toStringUtf8().replace("\n", "")));
    }
    if (operation.hasError()) {
      System.out.println("\tResponse:");
      System.out.println(String.format("\t\tError code: %s", operation.getError().getCode()));
      System.out.println(
          String.format("\t\tError message: %s", operation.getError().getMessage()));
    }
  }
}

Node.js


/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project";
// const computeRegion = '[REGION_NAME]' e.g., "us-central1";
// const filter = '[FILTER_EXPRESSIONS]';

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

// Instantiates a client
const automlClient = new AutoMlClient();
async function listOperationStatus() {
  // A resource that represents Google Cloud Platform location.
  const projectLocation = automlClient.locationPath(projectId, computeRegion);

  // List all the operations available in the region by applying filter.
  const [operations] = await automlClient.operationsClient.listOperations({
    name: projectLocation,
    filter: filter,
  });
  for (const element of operations) {
    console.log(`\nOperation details:`);
    console.log(`Name: ${element.name}`);
    console.log(`Metadata:`);
    console.log(`Type Url: ${element.metadata.typeUrl}`);
    console.log(`Done: ${element.done}`);
  }
}
listOperationStatus();

Getting the status of an operation

REST & CMD LINE

Before using any of the request data below, make the following replacements:

  • project-id: your GCP project ID.
  • operation-id: the ID of your operation. The ID is the last element of the name of your operation. For example:
    • operation name: projects/${project-id}/locations/us-central1/operations/IOD5281059901324392598
    • operation id: IOD5281059901324392598

HTTP method and URL:

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

To send your request, expand one of these options:

You should see output similar to the following for a completed import operation:
{
  "name": "projects/project-id/locations/us-central1/operations/operation-id",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.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"
  }
}

You should see output similar to the following for a completed create model operation:

{
  "name": "projects/project-id/locations/us-central1/operations/operation-id",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.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.v1beta1.Model",
    "name": "projects/project-id/locations/us-central1/models/model-id"
  }
}

Java

/**
 * Demonstrates using the AutoML client to get operation status.
 *
 * @param operationFullId the complete name of a operation. For example, the name of your
 *     operation is projects/[projectId]/locations/us-central1/operations/[operationId].
 * @throws IOException
 */
public static void getOperationStatus(String operationFullId) throws IOException {
  // Instantiates a client
  AutoMlClient client = AutoMlClient.create();

  // Get the latest state of a long-running operation.
  Operation response = client.getOperationsClient().getOperation(operationFullId);

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

Node.js

/**
 * Demonstrates using the AutoML client to get operation status.
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const operationFullId = '[OPERATION_FULL_ID]'
// eg., "projects/<projectId>/locations/us-central1/operations/<operationId>",
// `Full name of an operation`;

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

// Instantiates a client
const automlClient = new AutoMlClient();
async function getOperationStatus() {
  // Get the latest state of a long-running operation.
  const [response] = await automlClient.operationsClient.getOperation({
    name: operationFullId,
  });
  console.log(`Operation details:`);
  console.log(`\tName: ${response.name}`);
  console.log(`\tMetadata:`);
  console.log(`\t\tType Url: ${response.metadata.typeUrl}`);
  console.log(`\tDone: ${response.done}`);

  if (response.response) {
    console.log(`\tResponse:`);
    console.log(`\t\tType Url: ${response.response.typeUrl}`);
  }

  if (response.error) {
    console.log(`\tResponse:`);
    console.log(`\t\tError code: ${response.error.code}`);
    console.log(`\t\tError message: ${response.error.message}`);
  }
}
getOperationStatus();

Cancelling an Operation

You can cancel an import or training task using the operation ID.

REST & CMD LINE

Before using any of the request data below, make the following replacements:

  • project-id: your GCP project ID.
  • operation-id: the ID of your operation. The ID is the last element of the name of your operation. For example:
    • operation name: projects/${project-id}/locations/us-central1/operations/IOD5281059901324392598
    • operation id: IOD5281059901324392598

HTTP method and URL:

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

To send your request, expand one of these options:

You will see an empty JSON object returned from a successful request:
{}

Getting information about a model

When training is complete, you can get information about the newly created model.

REST & CMD LINE

Before using any of the request data below, make the following replacements:

  • project-id: your GCP project ID.
  • model-id: the ID of your model, from the response when you created the model. The ID is the last element of the name of your model. For example:
    • model name: projects/${project-id}/locations/us-central1/models/IOD4412217016962778756
    • model id: IOD4412217016962778756

HTTP method and URL:

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

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "name": "projects/${project-id}/locations/us-central1/models/${model-id}",
  "displayName": "my_display_name",
  "datasetId": "${dataset-id}",
  "createTime": "2019-07-26T21:10:18.338846Z",
  "deploymentState": "UNDEPLOYED",
  "updateTime": "2019-07-26T22:28:57.464076Z",
  "imageObjectDetectionModelMetadata": {
    "modelType": "cloud-low-latency-1",
    "nodeQps": 1.2987012987012987,
    "stopReason": "MODEL_CONVERGED",
    "trainBudgetMilliNodeHours": "216000",
    "trainCostMilliNodeHours": "8230"
  }
}
Was this page helpful? Let us know how we did:

Send feedback about...

Cloud AutoML Vision Object Detection