Managing models

You create a custom model by training it using a prepared dataset. AutoML Vision uses the items 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.

Since AutoML Vision creates a new model each time you start training, your project may include numerous models. You can get a list of the models in your project and can delete models that you no longer need.

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

The maximum lifespan for a custom model is two years. You must create and train a new model to continue classifying content after that amount of time.

Using curl

To make it more convenient to run the curl samples in this topic, set the following environment variable. Replace project-id with the name of your GCP project.

export PROJECT_ID="project-id"

Getting information about a model

After training your custom model is complete, you can get information about the newly created model.

The examples in this section return the basic metadata about a model. To get details about a model's accuracy and readiness, see Evaluating models.

Command-line

* Replace model-name with the full name of your model, from the response when you created the model. The full name has the format: projects/{project-id}/locations/us-central1/models/{model-id}
curl \
  -X GET \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)"
  -H "Content-Type: application/json" \
  https://automl.googleapis.com/v1beta1/model-name

You should see output similar to the following:

{
  "model": [
    {
      "name": "projects/434039606874/locations/us-central1/models/3745331181667467569",
      "createTime": "2018-04-27T02:00:22.329970Z",
      "imageClassificationModelMetadata": {
        "trainBudget": "1",
        "trainCost": "1",
        "stopReason": "BUDGET_REACHED"
      },
      "displayName": "a_98487760535e48319dd204e6394670"
    },
}

Python

Before you can run this code example, you must install the Python Client Libraries.
# TODO(developer): Uncomment and set the following variables
# project_id = 'PROJECT_ID_HERE'
# compute_region = 'COMPUTE_REGION_HERE'
# model_id = 'MODEL_ID_HERE'

from google.cloud import automl_v1beta1 as automl
from google.cloud.automl_v1beta1 import enums

client = automl.AutoMlClient()

# Get the full path of the model.
model_full_id = client.model_path(project_id, compute_region, model_id)

# Get complete detail of the model.
model = client.get_model(model_full_id)

# Retrieve deployment state.
if model.deployment_state == 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("Image classification model metadata:")
print(
    "Training budget: {}".format(
        model.image_classification_model_metadata.train_budget
    )
)
print(
    "Training cost: {}".format(
        model.image_classification_model_metadata.train_cost
    )
)
print(
    "Stop reason: {}".format(
        model.image_classification_model_metadata.stop_reason
    )
)
print(
    "Base model id: {}".format(
        model.image_classification_model_metadata.base_model_id
    )
)
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))

Java

/**
 * Demonstrates using the AutoML client to get model details.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param modelId the Id of the model.
 * @throws IOException on Input/Output errors.
 */
public static void getModel(String projectId, String computeRegion, String modelId)
    throws IOException {
  AutoMlClient client = AutoMlClient.create();

  // Get the full path of the model.
  ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId);

  // Get complete detail of the model.
  Model model = client.getModel(modelFullId);

  // Display the model information.
  System.out.println(String.format("Model name: %s", model.getName()));
  System.out.println(
      String.format(
          "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1]));
  System.out.println(String.format("Model display name: %s", model.getDisplayName()));
  System.out.println("Image classification model metadata:");
  System.out.println(
      "Tranning budget: " + model.getImageClassificationModelMetadata().getTrainBudget());
  System.out.println(
      "Tranning cost:" + model.getImageClassificationModelMetadata().getTrainCost());
  System.out.println(
      String.format(
          "Stop reason: %s", model.getImageClassificationModelMetadata().getStopReason()));
  System.out.println(
      String.format(
          "Base model id: %s", model.getImageClassificationModelMetadata().getBaseModelId()));
  System.out.println("Model create time:");
  System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds()));
  System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos()));
  System.out.println(String.format("Model deployment state: %s", model.getDeploymentState()));
}

Node.js

  const automl = require(`@google-cloud/automl`).v1beta1;

  const client = new automl.AutoMlClient();

  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`;
  // const computeRegion = `region-name, e.g. "us-central1"`;
  // const modelId = `id of the model, e.g. “ICN12345”`;

  // Get the full path of the model.
  const modelFullId = client.modelPath(projectId, computeRegion, modelId);

  // Get complete detail of the model.
  const [model] = await client.getModel({name: modelFullId});

  // Display the model information.
  console.log(`Model name: ${model.name}`);
  console.log(`Model id: ${model.name.split(`/`).pop(-1)}`);
  console.log(`Model display name: ${model.displayName}`);
  console.log(`Model dataset id: ${model.datasetId}`);
  if (model.modelMetadata === `translationModelMetadata`) {
    console.log(`Translation model metadata:`);
    console.log(`\tBase model: ${model.translationModelMetadata.baseModel}`);
    console.log(
      `\tSource language code: ${
        model.translationModelMetadata.sourceLanguageCode
      }`
    );
    console.log(
      `\tTarget language code: ${
        model.translationModelMetadata.targetLanguageCode
      }`
    );
  } else if (model.modelMetadata === `textClassificationModelMetadata`) {
    console.log(
      `Text classification model metadata: ${
        model.textClassificationModelMetadata
      }`
    );
  } else if (model.modelMetadata === `imageClassificationModelMetadata`) {
    console.log(`Image classification model metadata:`);
    console.log(
      `\tBase model id: ${model.imageClassificationModelMetadata.baseModelId}`
    );
    console.log(
      `\tTrain budget: ${model.imageClassificationModelMetadata.trainBudget}`
    );
    console.log(
      `\tTrain cost: ${model.imageClassificationModelMetadata.trainCost}`
    );
    console.log(
      `\tStop reason: ${model.imageClassificationModelMetadata.stopReason}`
    );
  }
  console.log(`Model create time:`);
  console.log(`\tseconds: ${model.createTime.seconds}`);
  console.log(`\tnanos: ${model.createTime.nanos}`);
  console.log(`Model update time:`);
  console.log(`\tseconds: ${model.updateTime.seconds}`);
  console.log(`\tnanos: ${model.updateTime.nanos}`);
  console.log(`Model deployment state: ${model.deploymentState}`);

Listing models

A project can include numerous models. This section describes how to retrieve a list of the available models for a project.

Web UI

To see a list of the available models using the AutoML Vision UI, click the lightbulb icon in the left navigation bar.

A list of models

To see the models for a different project, select the project from the drop-down list in the upper right of the title bar.

Command-line

curl \
  -X GET \
  -H "Authorization: Bearer $(gcloud auth application-default  \
print-access-token)" \
  -H "Content-Type: application/json" \
https://automl.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/models

You should see output similar to the following:

{
  "model": [
    {
      "name": "projects/434039606874/locations/us-central1/models/7537307368641647584",
      "displayName": "c982e11ffbd5455e8d9bee2734f01f81",
      "imageClassificationModelMetadata": {
        "trainBudget": "1",
        "trainCost": "1",
        "stopReason": "BUDGET_REACHED"
      },
      "createTime": "2018-04-30T23:06:19.223230Z"
    },
    {
      "name": "projects/434039606874/locations/us-central1/models/6877109870585533885",
      "displayName": "test_201801111318",
      "imageClassificationModelMetadata": {
        "trainBudget": "3",
        "trainCost": "2",
        "stopReason": "MODEL_CONVERGED"
      },
      "createTime": "2018-01-11T21:25:05.893590Z"
    }
  ]
}

Python

Before you can run this code example, you must install the Python Client Libraries.
# TODO(developer): Uncomment and set the following variables
# project_id = 'PROJECT_ID_HERE'
# compute_region = 'COMPUTE_REGION_HERE'
# filter_ = 'DATASET_ID_HERE'

from google.cloud import automl_v1beta1 as automl
from google.cloud.automl_v1beta1 import enums

client = automl.AutoMlClient()

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

# List all the models available in the region by applying filter.
response = client.list_models(project_location, filter_)

print("List of models:")
for model in response:
    # Retrieve deployment state.
    if model.deployment_state == 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("Image classification model metadata:")
    print(
        "Training budget: {}".format(
            model.image_classification_model_metadata.train_budget
        )
    )
    print(
        "Training cost: {}".format(
            model.image_classification_model_metadata.train_cost
        )
    )
    print(
        "Stop reason: {}".format(
            model.image_classification_model_metadata.stop_reason
        )
    )
    print(
        "Base model id: {}".format(
            model.image_classification_model_metadata.base_model_id
        )
    )
    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))

Java

/**
 * Demonstrates using the AutoML client to list all models.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param filter - Filter expression.
 * @throws IOException on Input/Output errors.
 */
public static void listModels(String projectId, String computeRegion, String filter)
    throws IOException {
  AutoMlClient client = AutoMlClient.create();

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

  // Create list models request
  ListModelsRequest listModelsRequest =
      ListModelsRequest.newBuilder()
          .setParent(projectLocation.toString())
          .setFilter(filter)
          .build();

  System.out.println("List of models:");
  for (Model model : client.listModels(listModelsRequest).iterateAll()) {
    // Display the model information.
    System.out.println(String.format("Model name: %s", model.getName()));
    System.out.println(
        String.format(
            "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1]));
    System.out.println(String.format("Model display name: %s", model.getDisplayName()));
    System.out.println("Image classification model metadata:");
    System.out.println(
        "Tranning budget: " + model.getImageClassificationModelMetadata().getTrainBudget());
    System.out.println(
        "Tranning cost: " + model.getImageClassificationModelMetadata().getTrainCost());
    System.out.println(
        String.format(
            "Stop reason: %s", model.getImageClassificationModelMetadata().getStopReason()));
    System.out.println(
        String.format(
            "Base model id: %s", model.getImageClassificationModelMetadata().getBaseModelId()));
    System.out.println("Model create time:");
    System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds()));
    System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos()));
    System.out.println(String.format("Model deployment state: %s", model.getDeploymentState()));
  }
}

Node.js

  const automl = require(`@google-cloud/automl`);

  const client = new automl.v1beta1.AutoMlClient();

  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`;
  // const computeRegion = `region-name, e.g. "us-central1"`;
  // const filter = `filter expressions, must specify field, e.g. “imageClassificationModelMetadata:*”`;

  // A resource that represents Google Cloud Platform location.
  const projectLocation = client.locationPath(projectId, computeRegion);

  // List all the models available in the region by applying filter.
  const [models] = await client.listModels({
    parent: projectLocation,
    filter: filter,
  });

  // Display the model information.
  console.log(`List of models:`);
  models.forEach(model => {
    console.log(`Model name: ${model.name}`);
    console.log(`Model id: ${model.name.split(`/`).pop(-1)}`);
    console.log(`Model display name: ${model.displayName}`);
    console.log(`Model dataset id: ${model.datasetId}`);
    if (model.modelMetadata === `translationModelMetadata`) {
      console.log(`Translation model metadata:`);
      console.log(`\tBase model: ${model.translationModelMetadata.baseModel}`);
      console.log(
        `\tSource language code: ${
          model.translationModelMetadata.sourceLanguageCode
        }`
      );
      console.log(
        `\tTarget language code: ${
          model.translationModelMetadata.targetLanguageCode
        }`
      );
    } else if (model.modelMetadata === `textClassificationModelMetadata`) {
      console.log(
        `Text classification model metadata: ${
          model.textClassificationModelMetadata
        }`
      );
    } else if (model.modelMetadata === `imageClassificationModelMetadata`) {
      console.log(`Image classification model metadata:`);
      console.log(
        `\tBase model id: ${model.imageClassificationModelMetadata.baseModelId}`
      );
      console.log(
        `\tTrain budget: ${model.imageClassificationModelMetadata.trainBudget}`
      );
      console.log(
        `\tTrain cost: ${model.imageClassificationModelMetadata.trainCost}`
      );
      console.log(
        `\tStop reason: ${model.imageClassificationModelMetadata.stopReason}`
      );
    }
    console.log(`Model create time:`);
    console.log(`\tseconds: ${model.createTime.seconds}`);
    console.log(`\tnanos: ${model.createTime.nanos}`);
    console.log(`Model update time:`);
    console.log(`\tseconds: ${model.updateTime.seconds}`);
    console.log(`\tnanos: ${model.updateTime.nanos}`);
    console.log(`Model deployment state: ${model.deploymentState}`);
    console.log(`\n`);
  });

Deleting a model

The following example deletes a model.

Web UI

  1. In the AutoML Vision UI, click the lightbulb icon in the left navigation menu to display the list of available models.

  2. Click the three-dot menu at the far right of the row you want to delete and select Delete model.

  3. Click Delete in the confirmation dialog box.

    Delete a model

Command-line

  • Replace model-name with the full name of your model, from the response when you created the model. The full name has the format: projects/{project-id}/locations/us-central1/models/{model-id}
curl \
  -X DELETE \
  -H "Authorization: Bearer $(gcloud auth application-default  \
print-access-token)" \
  -H "Content-Type: application/json" \
https://automl.googleapis.com/v1beta1/model-name

You should see output similar to the following. You can use the operation ID to get the status of the delete operation. For an example, see Getting the Status of an Operation.

{
  "name": "projects/434039606874/locations/us-central1/operations/4422270194425422927",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "progressPercentage": 100,
    "createTime": "2018-04-27T02:33:02.479200Z",
    "updateTime": "2018-04-27T02:35:17.309060Z"
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.protobuf.Empty"
  }
}

Python

Before you can run this code example, you must install the Python Client Libraries.

  • The model_id variable is the ID of your model. The ID is the last element of the name of your model. For example, if the name of your model is projects/434039606874/locations/us-central1/models/3745331181667467569, then the ID of your model is 3745331181667467569.
# TODO(developer): Uncomment and set the following variables
# project_id = 'PROJECT_ID_HERE'
# compute_region = 'COMPUTE_REGION_HERE'
# model_id = 'MODEL_ID_HERE'

from google.cloud import automl_v1beta1 as automl

client = automl.AutoMlClient()

# Get the full path of the model.
model_full_id = client.model_path(project_id, compute_region, model_id)

# Delete a model.
response = client.delete_model(model_full_id)

# synchronous check of operation status.
print("Model deleted. {}".format(response.result()))

Java

/**
 * Demonstrates using the AutoML client to delete a model.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param modelId the Id of the model.
 * @throws Exception on AutoML Client errors
 */
public static void deleteModel(String projectId, String computeRegion, String modelId)
    throws Exception {
  AutoMlClient client = AutoMlClient.create();

  // Get the full path of the model.
  ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId);

  // Delete a model.
  Empty response = client.deleteModelAsync(modelFullId).get();

  System.out.println("Model deletion started...");
}

Node.js

  const automl = require(`@google-cloud/automl`).v1beta1;

  const client = new automl.AutoMlClient();

  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`;
  // const computeRegion = `region-name, e.g. "us-central1"`;
  // const modelId = `id of the model, e.g. “ICN12345”`;

  // Get the full path of the model.
  const modelFullId = client.modelPath(projectId, computeRegion, modelId);

  // Delete a model.
  const [operation] = await client.deleteModel({name: modelFullId});
  const [, , response] = await operation.promise();
  // The final result of the operation.
  if (response.done) {
    console.log(`Model deleted.`);
  }

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud AutoML Vision
Need help? Visit our support page.