Train an image object detection model

This page shows you how to train an AutoML object detection model from an imagedataset using either the Google Cloud console or the Vertex AI API.

Train an AutoML model

Google Cloud console

  1. In the Google Cloud console, in the Vertex AI section, go totheDatasets page.

    Go to the Datasets page

  2. Click the name of the dataset you want to use to train your model to openits details page.

  3. ClickTrain new model.

  4. For the training method, selectAutoML.

  5. In theChoose where to use the model section, choose the model hostlocation:Cloud,Edge, orVertex AI Vision.

  6. ClickContinue.

  7. Enter a name for the model.

  8. If you want manually set how your training data is split, expandAdvanced options and select a data split option.Learn more.

  9. ClickStart Training.

    Model training can take many hours, depending on the size and complexity of yourdata and your training budget, if you specified one. You can close this tab andreturn to it later. You will receive an email when your model has completedtraining.

API

Select the tab below for your language or environment:

REST

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

  • LOCATION: Region where dataset is located and Model is created. For example,us-central1.
  • PROJECT: Yourproject ID.
  • TRAININGPIPELINE_DISPLAYNAME: Required. A display name for the trainingPipeline.
  • DATASET_ID: The ID number for the dataset to use for training.
  • fractionSplit: Optional. One of several possible ML usesplit options for your data. ForfractionSplit, values must sum to 1. For example:
    • {"trainingFraction": "0.7","validationFraction": "0.15","testFraction": "0.15"}
  • MODEL_DISPLAYNAME*: A display name for the model uploaded (created) by the TrainingPipeline.
  • MODEL_DESCRIPTION*: A description for the model.
  • modelToUpload.labels*: Any set of key-value pairs to organize your models. For example:
    • "env": "prod"
    • "tier": "backend"
  • MODELTYPE: The type of Cloud-hosted model to train. The options are:
    • CLOUD_1 - A model best tailored to be used within Google Cloud, and whichcannot be exported. Compared to the CLOUD_HIGH_ACCURACY_1 and CLOUD_LOW_LATENCY_1 models above, it is expected to have higher prediction quality and lower latency.
    • CLOUD_HIGH_ACCURACY_1 - A model best tailored to be used within Google Cloud, and whichcannot be exported. This model is expected to have a higher latency, but should also have a higher prediction quality than other cloud models.
    • CLOUD_LOW_LATENCY_1 - A model best tailored to be used within Google Cloud, and which cannot be exported. This model is expected to have a low latency, but may have lower prediction quality than other cloud models.
    Other model type options can be found in thereference documentation.
  • NODE_HOUR_BUDGET: The actual training cost will be equal or less than this value. For Cloud models the budget must be: 20,000 - 900,000 milli node hours (inclusive). The default value is 216,000 which represents one day in wall time, assuming 9 nodes are used.
  • PROJECT_NUMBER: Your project's automatically generatedproject number
*Schema file's description you specify intrainingTaskDefinition describes the use of this field.
Schema file you specify intrainingTaskDefinition declares and describes this field.

HTTP method and URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/trainingPipelines

Request JSON body:

{  "displayName": "TRAININGPIPELINE_DISPLAYNAME",  "inputDataConfig": {    "datasetId": "DATASET_ID",    "fractionSplit": {      "trainingFraction": "DECIMAL",      "validationFraction": "DECIMAL",      "testFraction": "DECIMAL"    }  },  "modelToUpload": {    "displayName": "MODEL_DISPLAYNAME",    "description": "MODEL_DESCRIPTION",    "labels": {      "KEY": "VALUE"    }  },  "trainingTaskDefinition": "gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_image_object_detection_1.0.0.yaml",  "trainingTaskInputs": {    "modelType": ["MODELTYPE"],    "budgetMilliNodeHours":NODE_HOUR_BUDGET  }}

To send your request, choose one of these options:

curl

Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.

Save the request body in a file namedrequest.json, and execute the following command:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/trainingPipelines"

PowerShell

Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.

Save the request body in a file namedrequest.json, and execute the following command:

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

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/trainingPipelines" | Select-Object -Expand Content

The response contains information about specifications as well as theTRAININGPIPELINE_ID.

Response

{  "name": "projects/PROJECT_NUMBER/locations/LOCATION/trainingPipelines/TRAININGPIPELINE_ID",  "displayName": "TRAININGPIPELINE_DISPLAYNAME",  "inputDataConfig": {    "datasetId": "DATASET_ID",    "fractionSplit": {      "trainingFraction": 0.7,      "validationFraction": 0.15,      "testFraction": 0.15    }  },  "trainingTaskDefinition": "gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_image_object_detection_1.0.0.yaml",  "trainingTaskInputs": {    "modelType": "MODELTYPE",    "budgetMilliNodeHours": "NODE_HOUR_BUDGET"  },  "modelToUpload": {    "displayName": "MODEL_DISPLAYNAME",    "labels": {      "KEY1": "VALUE1",      "KEY2": "VALUE2"    }  },  "state": "PIPELINE_STATE_PENDING",  "createTime": "2020-07-14T17:16:55.098953Z",  "updateTime": "2020-07-14T17:16:55.098953Z"}

Java

Before trying this sample, follow theJava setup instructions in theVertex AI quickstart using client libraries. For more information, see theVertex AIJava API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, seeSet up authentication for a local development environment.

importcom.google.cloud.aiplatform.util.ValueConverter;importcom.google.cloud.aiplatform.v1.DeployedModelRef;importcom.google.cloud.aiplatform.v1.EnvVar;importcom.google.cloud.aiplatform.v1.FilterSplit;importcom.google.cloud.aiplatform.v1.FractionSplit;importcom.google.cloud.aiplatform.v1.InputDataConfig;importcom.google.cloud.aiplatform.v1.LocationName;importcom.google.cloud.aiplatform.v1.Model;importcom.google.cloud.aiplatform.v1.Model.ExportFormat;importcom.google.cloud.aiplatform.v1.ModelContainerSpec;importcom.google.cloud.aiplatform.v1.PipelineServiceClient;importcom.google.cloud.aiplatform.v1.PipelineServiceSettings;importcom.google.cloud.aiplatform.v1.Port;importcom.google.cloud.aiplatform.v1.PredefinedSplit;importcom.google.cloud.aiplatform.v1.PredictSchemata;importcom.google.cloud.aiplatform.v1.TimestampSplit;importcom.google.cloud.aiplatform.v1.TrainingPipeline;importcom.google.cloud.aiplatform.v1.schema.trainingjob.definition.AutoMlImageObjectDetectionInputs;importcom.google.cloud.aiplatform.v1.schema.trainingjob.definition.AutoMlImageObjectDetectionInputs.ModelType;importcom.google.rpc.Status;importjava.io.IOException;publicclassCreateTrainingPipelineImageObjectDetectionSample{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sample.StringtrainingPipelineDisplayName="YOUR_TRAINING_PIPELINE_DISPLAY_NAME";Stringproject="YOUR_PROJECT_ID";StringdatasetId="YOUR_DATASET_ID";StringmodelDisplayName="YOUR_MODEL_DISPLAY_NAME";createTrainingPipelineImageObjectDetectionSample(project,trainingPipelineDisplayName,datasetId,modelDisplayName);}staticvoidcreateTrainingPipelineImageObjectDetectionSample(Stringproject,StringtrainingPipelineDisplayName,StringdatasetId,StringmodelDisplayName)throwsIOException{PipelineServiceSettingspipelineServiceSettings=PipelineServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();// 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(PipelineServiceClientpipelineServiceClient=PipelineServiceClient.create(pipelineServiceSettings)){Stringlocation="us-central1";StringtrainingTaskDefinition="gs://google-cloud-aiplatform/schema/trainingjob/definition/"+"automl_image_object_detection_1.0.0.yaml";LocationNamelocationName=LocationName.of(project,location);AutoMlImageObjectDetectionInputsautoMlImageObjectDetectionInputs=AutoMlImageObjectDetectionInputs.newBuilder().setModelType(ModelType.CLOUD_HIGH_ACCURACY_1).setBudgetMilliNodeHours(20000).setDisableEarlyStopping(false).build();InputDataConfigtrainingInputDataConfig=InputDataConfig.newBuilder().setDatasetId(datasetId).build();Modelmodel=Model.newBuilder().setDisplayName(modelDisplayName).build();TrainingPipelinetrainingPipeline=TrainingPipeline.newBuilder().setDisplayName(trainingPipelineDisplayName).setTrainingTaskDefinition(trainingTaskDefinition).setTrainingTaskInputs(ValueConverter.toValue(autoMlImageObjectDetectionInputs)).setInputDataConfig(trainingInputDataConfig).setModelToUpload(model).build();TrainingPipelinetrainingPipelineResponse=pipelineServiceClient.createTrainingPipeline(locationName,trainingPipeline);System.out.println("Create Training Pipeline Image Object Detection Response");System.out.format("Name: %s\n",trainingPipelineResponse.getName());System.out.format("Display Name: %s\n",trainingPipelineResponse.getDisplayName());System.out.format("Training Task Definition %s\n",trainingPipelineResponse.getTrainingTaskDefinition());System.out.format("Training Task Inputs: %s\n",trainingPipelineResponse.getTrainingTaskInputs());System.out.format("Training Task Metadata: %s\n",trainingPipelineResponse.getTrainingTaskMetadata());System.out.format("State: %s\n",trainingPipelineResponse.getState());System.out.format("Create Time: %s\n",trainingPipelineResponse.getCreateTime());System.out.format("StartTime %s\n",trainingPipelineResponse.getStartTime());System.out.format("End Time: %s\n",trainingPipelineResponse.getEndTime());System.out.format("Update Time: %s\n",trainingPipelineResponse.getUpdateTime());System.out.format("Labels: %s\n",trainingPipelineResponse.getLabelsMap());InputDataConfiginputDataConfig=trainingPipelineResponse.getInputDataConfig();System.out.println("Input Data Config");System.out.format("Dataset Id: %s",inputDataConfig.getDatasetId());System.out.format("Annotations Filter: %s\n",inputDataConfig.getAnnotationsFilter());FractionSplitfractionSplit=inputDataConfig.getFractionSplit();System.out.println("Fraction Split");System.out.format("Training Fraction: %s\n",fractionSplit.getTrainingFraction());System.out.format("Validation Fraction: %s\n",fractionSplit.getValidationFraction());System.out.format("Test Fraction: %s\n",fractionSplit.getTestFraction());FilterSplitfilterSplit=inputDataConfig.getFilterSplit();System.out.println("Filter Split");System.out.format("Training Filter: %s\n",filterSplit.getTrainingFilter());System.out.format("Validation Filter: %s\n",filterSplit.getValidationFilter());System.out.format("Test Filter: %s\n",filterSplit.getTestFilter());PredefinedSplitpredefinedSplit=inputDataConfig.getPredefinedSplit();System.out.println("Predefined Split");System.out.format("Key: %s\n",predefinedSplit.getKey());TimestampSplittimestampSplit=inputDataConfig.getTimestampSplit();System.out.println("Timestamp Split");System.out.format("Training Fraction: %s\n",timestampSplit.getTrainingFraction());System.out.format("Validation Fraction: %s\n",timestampSplit.getValidationFraction());System.out.format("Test Fraction: %s\n",timestampSplit.getTestFraction());System.out.format("Key: %s\n",timestampSplit.getKey());ModelmodelResponse=trainingPipelineResponse.getModelToUpload();System.out.println("Model To Upload");System.out.format("Name: %s\n",modelResponse.getName());System.out.format("Display Name: %s\n",modelResponse.getDisplayName());System.out.format("Description: %s\n",modelResponse.getDescription());System.out.format("Metadata Schema Uri: %s\n",modelResponse.getMetadataSchemaUri());System.out.format("Metadata: %s\n",modelResponse.getMetadata());System.out.format("Training Pipeline: %s\n",modelResponse.getTrainingPipeline());System.out.format("Artifact Uri: %s\n",modelResponse.getArtifactUri());System.out.format("Supported Deployment Resources Types: %s\n",modelResponse.getSupportedDeploymentResourcesTypesList());System.out.format("Supported Input Storage Formats: %s\n",modelResponse.getSupportedInputStorageFormatsList());System.out.format("Supported Output Storage Formats: %s\n",modelResponse.getSupportedOutputStorageFormatsList());System.out.format("Create Time: %s\n",modelResponse.getCreateTime());System.out.format("Update Time: %s\n",modelResponse.getUpdateTime());System.out.format("Labels: %sn\n",modelResponse.getLabelsMap());PredictSchematapredictSchemata=modelResponse.getPredictSchemata();System.out.println("Predict Schemata");System.out.format("Instance Schema Uri: %s\n",predictSchemata.getInstanceSchemaUri());System.out.format("Parameters Schema Uri: %s\n",predictSchemata.getParametersSchemaUri());System.out.format("Prediction Schema Uri: %s\n",predictSchemata.getPredictionSchemaUri());for(ExportFormatexportFormat:modelResponse.getSupportedExportFormatsList()){System.out.println("Supported Export Format");System.out.format("Id: %s\n",exportFormat.getId());}ModelContainerSpecmodelContainerSpec=modelResponse.getContainerSpec();System.out.println("Container Spec");System.out.format("Image Uri: %s\n",modelContainerSpec.getImageUri());System.out.format("Command: %s\n",modelContainerSpec.getCommandList());System.out.format("Args: %s\n",modelContainerSpec.getArgsList());System.out.format("Predict Route: %s\n",modelContainerSpec.getPredictRoute());System.out.format("Health Route: %s\n",modelContainerSpec.getHealthRoute());for(EnvVarenvVar:modelContainerSpec.getEnvList()){System.out.println("Env");System.out.format("Name: %s\n",envVar.getName());System.out.format("Value: %s\n",envVar.getValue());}for(Portport:modelContainerSpec.getPortsList()){System.out.println("Port");System.out.format("Container Port: %s\n",port.getContainerPort());}for(DeployedModelRefdeployedModelRef:modelResponse.getDeployedModelsList()){System.out.println("Deployed Model");System.out.format("Endpoint: %s\n",deployedModelRef.getEndpoint());System.out.format("Deployed Model Id: %s\n",deployedModelRef.getDeployedModelId());}Statusstatus=trainingPipelineResponse.getError();System.out.println("Error");System.out.format("Code: %s\n",status.getCode());System.out.format("Message: %s\n",status.getMessage());}}}

Node.js

Before trying this sample, follow theNode.js setup instructions in theVertex AI quickstart using client libraries. For more information, see theVertex AINode.js API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, seeSet up authentication for a local development environment.

/** * TODO(developer): Uncomment these variables before running the sample.\ * (Not necessary if passing values as arguments) */// const datasetId = 'YOUR_DATASET_ID';// const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME';// const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME';// const project = 'YOUR_PROJECT_ID';// const location = 'YOUR_PROJECT_LOCATION';constaiplatform=require('@google-cloud/aiplatform');const{definition}=aiplatform.protos.google.cloud.aiplatform.v1.schema.trainingjob;constModelType=definition.AutoMlImageObjectDetectionInputs.ModelType;// Imports the Google Cloud Pipeline Service Client libraryconst{PipelineServiceClient}=aiplatform.v1;// Specifies the location of the api endpointconstclientOptions={apiEndpoint:'us-central1-aiplatform.googleapis.com',};// Instantiates a clientconstpipelineServiceClient=newPipelineServiceClient(clientOptions);asyncfunctioncreateTrainingPipelineImageObjectDetection(){// Configure the parent resourceconstparent=`projects/${project}/locations/${location}`;consttrainingTaskInputsObj=newdefinition.AutoMlImageObjectDetectionInputs({disableEarlyStopping:false,modelType:ModelType.CLOUD_1,budgetMilliNodeHours:20000,});consttrainingTaskInputs=trainingTaskInputsObj.toValue();constmodelToUpload={displayName:modelDisplayName};constinputDataConfig={datasetId:datasetId};consttrainingPipeline={displayName:trainingPipelineDisplayName,trainingTaskDefinition:'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_image_object_detection_1.0.0.yaml',trainingTaskInputs,inputDataConfig,modelToUpload,};constrequest={parent,trainingPipeline,};// Create training pipeline requestconst[response]=awaitpipelineServiceClient.createTrainingPipeline(request);console.log('Create training pipeline image object detection response');console.log(`Name :${response.name}`);console.log('Raw response:');console.log(JSON.stringify(response,null,2));}createTrainingPipelineImageObjectDetection();

Python

To learn how to install or update the Vertex AI SDK for Python, seeInstall the Vertex AI SDK for Python. For more information, see thePython API reference documentation.

fromgoogle.cloudimportaiplatformfromgoogle.cloud.aiplatform.gapic.schemaimporttrainingjobdefcreate_training_pipeline_image_object_detection_sample(project:str,display_name:str,dataset_id:str,model_display_name:str,location:str="us-central1",api_endpoint:str="us-central1-aiplatform.googleapis.com",):# The AI Platform services require regional API endpoints.client_options={"api_endpoint":api_endpoint}# Initialize client that will be used to create and send requests.# This client only needs to be created once, and can be reused for multiple requests.client=aiplatform.gapic.PipelineServiceClient(client_options=client_options)training_task_inputs=trainingjob.definition.AutoMlImageObjectDetectionInputs(model_type="CLOUD_HIGH_ACCURACY_1",budget_milli_node_hours=20000,disable_early_stopping=False,).to_value()training_pipeline={"display_name":display_name,"training_task_definition":"gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_image_object_detection_1.0.0.yaml","training_task_inputs":training_task_inputs,"input_data_config":{"dataset_id":dataset_id},"model_to_upload":{"display_name":model_display_name},}parent=f"projects/{project}/locations/{location}"response=client.create_training_pipeline(parent=parent,training_pipeline=training_pipeline)print("response:",response)

Control the data split using REST

You can control how your training data is split between the training,validation, and test sets. When using the Vertex AI API, use theSplit object to determineyour data split. TheSplit object can be included in theInputConfig objectas one of several object types, each of which provides a different way tosplit the training data. You can select one method only.

  • FractionSplit:
    • TRAINING_FRACTION: The fraction of the training data to be used for the training set.
    • VALIDATION_FRACTION: The fraction of the training data to be used for the validation set. Not used for video data.
    • TEST_FRACTION: The fraction of the training data to be used for the test set.

    If any of the fractions are specified, all must be specified. The fractions must add up to 1.0. Thedefault values for the fractions differ depending on your data type.Learn more.

    "fractionSplit": {  "trainingFraction":TRAINING_FRACTION,  "validationFraction":VALIDATION_FRACTION,  "testFraction":TEST_FRACTION},
  • FilterSplit:
    • TRAINING_FILTER: Data items that match this filter are used for the training set.
    • VALIDATION_FILTER: Data items that match this filter are used for the validation set. Must be "-" for video data.
    • TEST_FILTER: Data items that match this filter are used for the test set.

    These filters can be used with theml_use label,or with any labels you apply to your data. Learn more about usingthe ml-use labelandother labelsto filter your data.

    The following example shows how to use thefilterSplitobject with theml_use label, with the validationset included:

    "filterSplit": {"trainingFilter": "labels.aiplatform.googleapis.com/ml_use=training","validationFilter": "labels.aiplatform.googleapis.com/ml_use=validation","testFilter": "labels.aiplatform.googleapis.com/ml_use=test"}

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.