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 can delete models you no longer need.

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"

Training models

When you have a dataset with a solid set of labeled training items, you are ready to create and train the custom model.

Web UI

  1. Open the AutoML Vision UI.

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

    Dataset list page

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

    The display name of the selected dataset appears in the title bar, and the page lists the individual items in the dataset along with their labels.

    Image items page

  3. When you are done reviewing the dataset, click the Train tab just below the title bar.

    The training page provides a basic analysis of your dataset and advises you about whether it is adequate for training. If AutoML Vision suggests changes, consider returning to the Images page and adding items or labels.

  4. When the dataset is ready, click Start Training.

Training a model can take several hours to complete. After the model is successfully trained, you will receive a message at the email address that you used to sign up for the program.

Command-line

  • Replace dataset-id with the ID of your dataset. The ID is the last element of the name of your dataset. For example, if the name of your dataset is projects/434039606874/locations/us-central1/datasets/3104518874390609379, then the ID of your dataset is 3104518874390609379.
curl \
  -X POST \
  -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 \
  -d '{
    "displayName": "test_model",
    "dataset_id": "dataset-id",
    "imageClassificationModelMetadata": {
      "trainBudget": "1"
    },
  }'

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

{
  "name": "projects/434039606874/locations/us-central1/operations/1979469554520652445",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "createTime": "2018-04-27T01:28:41.338120Z",
    "updateTime": "2018-04-27T01:28:41.338120Z",
    "cancellable": true
  }
}

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'
# dataset_id = 'DATASET_ID_HERE'
# model_name = 'MODEL_NAME_HERE'
# train_budget = integer amount for maximum cost of model

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_classification_model_metadata": {"train_budget": train_budget}
    if train_budget
    else {},
}

# 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...")

Java

/**
 * Demonstrates using the AutoML client to create a model.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param dataSetId the Id of the dataset to which model is created.
 * @param modelName the Name of the model.
 * @param trainBudget the Budget for training the model.
 * @throws Exception on AutoML Client errors
 */
public static void createModel(
    String projectId,
    String computeRegion,
    String dataSetId,
    String modelName,
    String trainBudget)
    throws Exception {
  // Instantiates a client
  AutoMlClient client = AutoMlClient.create();

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

  // Set model metadata.
  ImageClassificationModelMetadata imageClassificationModelMetadata =
      Long.valueOf(trainBudget) == 0
          ? ImageClassificationModelMetadata.newBuilder().build()
          : ImageClassificationModelMetadata.newBuilder()
              .setTrainBudget(Long.valueOf(trainBudget))
              .build();

  // Set model name and model metadata for the image dataset.
  Model myModel =
      Model.newBuilder()
          .setDisplayName(modelName)
          .setDatasetId(dataSetId)
          .setImageClassificationModelMetadata(imageClassificationModelMetadata)
          .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

  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 datasetId = `Id of the dataset`;
  // const modelName = `Name of the model, e.g. "myModel"`;
  // const trainBudget = `Budget for training model, e.g. 50`;

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

  // Check train budget condition.
  if (trainBudget === 0) {
    trainBudget = {};
  } else {
    trainBudget = {trainBudget: trainBudget};
  }

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

  // Create a model with the model metadata in the region.
  client
    .createModel({parent: projectLocation, model: myModel})
    .then(responses => {
      const operation = responses[0];
      const initialApiResponse = responses[1];

      console.log(`Training operation name: `, initialApiResponse.name);
      console.log(`Training started...`);
      return operation.promise();
    })
    .then(responses => {
      // The final result of the operation.
      const model = responses[0];

      // Retrieve deployment state.
      let deploymentState = ``;
      if (model.deploymentState === 1) {
        deploymentState = `deployed`;
      } else if (model.deploymentState === 2) {
        deploymentState = `undeployed`;
      }

      // 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 create time:`);
      console.log(`\tseconds: ${model.createTime.seconds}`);
      console.log(`\tnanos: ${model.createTime.nanos}`);
      console.log(`Model deployment state: ${deploymentState}`);
    })
    .catch(err => {
      console.error(err);
    });

Getting the status of an operation

You can check the status of a long-running task (importing items into a dataset or training a model) using the operation ID from the response when you started the task.

You can only check the status of operations using the AutoML API.

In the command below, replace operation-name with the full name of your operation. The full name has the format projects/{project-id}/locations/us-central1/operations/{operation-id}.

curl \
  -X GET \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json" \
  https://automl.googleapis.com/v1beta1/operation-id

You should see output similar to the following for an import operation:

{
  "name": "projects/434039606874/locations/us-central1/operations/2116326435840390257",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "progressPercentage": 100,
    "partialFailures": [
      {
        "code": 7,
        "message": "Duplicated files detected gs://my-project-lcm/training-data/astros.txt
        and gs://my-project-lcm/training-data/cubs.txt have the same content"
      },
    ],
    "createTime": "2018-04-27T01:39:59.821460Z",
    "updateTime": "2018-04-27T01:43:09.564770Z"
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.protobuf.Empty"
  }
}

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

{
  "name": "projects/434039606874/locations/us-central1/operations/2126599795587061786",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "progressPercentage": 100,
    "createTime": "2018-04-27T01:56:28.395640Z",
    "updateTime": "2018-04-27T02:04:12.336070Z"
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.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"
  }
}

Cancelling an 0peration

You can cancel an import or training task using the operation ID. In the command below, replace operation-name with the full name of your operation. The full name has the format projects/{project-id}/locations/us-central1/operations/{operation-id}.

curl \
  -X POST "Content-Type: application/json" \
  -H "Authorization: Bearer `gcloud auth application-default print-access-token`" \
  https://automl.googleapis.com/v1beta1/operation-name:cancel"

Getting information about a model

When training 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.
  client
    .getModel({name: modelFullId})
    .then(responses => {
      const model = responses[0];

      // 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}`);
    })
    .catch(err => {
      console.error(err);
    });

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.

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.
  client
    .listModels({parent: projectLocation, filter: filter})
    .then(responses => {
      const models = responses[0];

      // 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`);
      });
    })
    .catch(err => {
      console.error(err);
    });

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.

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.
  client
    .deleteModel({name: modelFullId})
    .then(responses => {
      const operation = responses[0];
      return operation.promise();
    })
    .then(responses => {
      // The final result of the operation.
      if (responses[2].done) {
        console.log(`Model deleted.`);
      }
    })
    .catch(err => {
      console.error(err);
    });

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

Send feedback about...

Cloud AutoML Vision
Need help? Visit our support page.