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.
def automl_create_model(dataset_id, model_name):
  """ start training """
  #dataset_id = dataset_full_id.split('/')[-1]
  my_model = {
    "display_name" : model_name,
    "dataset_id" : dataset_id,
    "image_classification_model_metadata": { "train_budget": "1" }
  }
  print(my_model)
  operation = client.create_model(parent, my_model)
  print('\nTraining started')
  #synchronous check of operation status
  print('\nTraining started')
  result = operation.result()
  model_full_id = result.name
  print("Model id: {}", model_id)

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.
def automl_get_model(model_id):
  """ Get model evaluation """
  name = client.model_path(project_id, 'us-central1', model_id)
  print("\nModel:")
  response = client.get_model(name)
  print(response)

You should see output similar to the following:

Model:
name: "projects/434039606874/locations/us-central1/models/7537307368641647584"
display_name: "c982e11ffbd5455e8d9bee2734f01f81"
image_classification_model_metadata {
  train_budget: "1"
}
create_time {
  seconds: 1525129579
  nanos: 223230000
}

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.
def automl_list_models():
  """ # list all models """
  filter_ = ''
  response = client.list_models(parent, filter_)
  print("\nList of models:")
  for element in response:
    print(element)

List of models:
name: "projects/434039606874/locations/us-central1/models/5043148733665046848"
display_name: "b32ef44908384f6c92539f5c65b7ae37"
image_classification_model_metadata {
  train_budget: "1",
  train_cost: "1",
  stop_reason: "BUDGET_REACHED"
}
create_time {
  seconds: 1525380150
  nanos: 705450000
}

name: "projects/434039606874/locations/us-central1/models/6453919078412494996"
display_name: "a_66f84595e8ea4a368293bb085f35df"
image_classification_model_metadata {
  train_budget: "3",
  train_cost: "2",
  stop_reason: "MODEL_CONVERGED"
}

Deleting a model

The following example deletes a model.

Web UI

  1. In the AutoML Vision UI, click the lighbuld 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.
def automl_delete_model(model_id):
  """ delete a model """
  name = client.model_path(project_id, 'us-central1', model_id)
  operation = client.delete_model(name)
  #synchronous check of operation status
  result = operation.result()
  print( 'Model deletion started \n{}', result)

You should see output similar to the following:

Model deletion started
create_time {
  seconds: 1525399139
  nanos: 50740000
}
update_time {
  seconds: 1525399139
  nanos: 50760000
}

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

Send feedback about...

Cloud AutoML Vision