After you have created (trained) a model you can make
an asynchronous
prediction request for a batch of images using the
batchPredict
method. The batchPredict
method applies annotations to your image based
on the objects identified by your model.
The maximum lifespan for a custom model is 18 months. You must create and train a new model to continue annotating content after that time.
Batch prediction
You can request annotations (predictions) for images by using the
batchPredict
command. The batchPredict
command takes, as input, a CSV file
stored in your Google Cloud Storage location that contains the paths to the
images to annotate. Each line specifies a separate path to an image in Google
Cloud Storage. For example:
batch_prediction.csv
:
gs://my-cloud-storage-bucket/prediction_files/image1.jpg gs://my-cloud-storage-bucket/prediction_files/image2.jpg gs://my-cloud-storage-bucket/prediction_files/image3.jpg gs://my-cloud-storage-bucket/prediction_files/image4.jpg gs://my-cloud-storage-bucket/prediction_files/image5.jpg gs://my-cloud-storage-bucket/prediction_files/image6.png
Depending on the number of images that you specified in your CSV file, the batch predict task can take some time to complete. Even on a small number of images batch prediction will take at minimum 30 minutes to complete.
REST
Before using any of the request data, make the following replacements:
- project-id: your GCP project ID.
- location-id: A valid location identifier. Currently you
must use the following value:
us-central1
- 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/location-id/models/IOD4412217016962778756
- model id:
IOD4412217016962778756
- model name:
- input-storage-path: the path to a CSV file stored on Google Cloud Storage. The requesting user must have at least read permission to the bucket.
- output-storage-bucket: a Google Cloud Storage
bucket/directory to save output files to, expressed in the following form:
gs://bucket/directory/
. The requesting user must have write permission to the bucket.
Field-specific considerations:
params.score_threshold
- A value between 0.0 and 1.0. Only results with scores greater or equal to this value will be returned.
HTTP method and URL:
POST https://automl.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/models/MODEL_ID:batchPredict
Request JSON body:
{ "inputConfig": { "gcsSource": { "inputUris": [ "INPUT_STORAGE_PATH" ] } }, "outputConfig": { "gcsDestination": { "outputUriPrefix": "OUTPUT_STORAGE_BUCKET" } }, "params": { "score_threshold": "0.0" } }
To send your request, choose one of these options:
curl
Save the request body in a file called request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-id" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://automl.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/models/MODEL_ID:batchPredict"
PowerShell
Save the request body in a file called request.json
,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-id" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://automl.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/models/MODEL_ID:batchPredict" | Select-Object -Expand Content
You should see output similar to the following:
{ "name": "projects/PROJECT_ID/locations/LOCATION_ID/operations/IOD926615623331479552", "metadata": { "@type": "type.googleapis.com/google.cloud.automl.v1.OperationMetadata", "createTime": "2019-06-19T21:28:35.302067Z", "updateTime": "2019-06-19T21:28:35.302067Z", "batchPredictDetails": { "inputConfig": { "gcsSource": { "inputUris": [ "INPUT_STORAGE_PATH" ] } } } } }
You can use the operation ID
(IOD926615623331479552
, in this case) to get the status of the task. For an
example, see Working with long-running operations.
Once the operation has completed, the state
shows as DONE
and your
results are written to the Google Cloud Storage file you specified:
{ "name": "projects/PROJECT_ID/locations/LOCATION_ID/operations/IOD926615623331479552", "metadata": { "@type": "type.googleapis.com/google.cloud.automl.v1.OperationMetadata", "createTime": "2019-06-19T21:28:35.302067Z", "updateTime": "2019-06-19T21:57:18.310033Z", "batchPredictDetails": { "inputConfig": { "gcsSource": { "inputUris": [ "INPUT_STORAGE_PATH" ] } }, "outputInfo": { "gcsOutputDirectory": "gs://STORAGE_BUCKET_VCM/SUBDIRECTORY/prediction-8370559933346329705-YYYY-MM-DDThh:mm:ss.sssZ" } } }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.automl.v1.BatchPredictResult" } }
See the Output JSONL files section below for a sample output file.
Java
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Node.js
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Python
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Additional languages
C#: Please follow the C# setup instructions on the client libraries page and then visit the AutoML Vision Object Detection reference documentation for .NET.
PHP: Please follow the PHP setup instructions on the client libraries page and then visit the AutoML Vision Object Detection reference documentation for PHP.
Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the AutoML Vision Object Detection reference documentation for Ruby.
Output JSONL files
When the batch predict task is complete, the output of the prediction is stored in the Google Cloud Storage location that you specified in your command.
In your output storage location (with your chosen object prefix) the
files image_object_detection_1.jsonl
, image_object_detection_2.jsonl
,...,
image_object_detection_N.jsonl
will be
created, where N may be 1, and depends on the total number of the successfully
predicted images and annotations.
A single image will be listed only once with all its annotations, and its annotations will never be split across files.
Each JSONL file will contain,
per line, a JSON representation of a proto that wraps image's "ID" :
"<id_value>" followed by a list of zero or more AnnotationPayload
protos (called annotations), which have imageObjectDetection
detail populated.
Example JSONL file (a single .jsonl file with 2 lines/file annotations):
image_object_detection_0.jsonl
Working with long-running operations
REST
Before using any of the request data, 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/location-id/operations/IOD5281059901324392598
- operation id:
IOD5281059901324392598
- operation name:
HTTP method and URL:
GET https://automl.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/operations/OPERATION_ID
To send your request, choose one of these options:
curl
Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-id" \
"https://automl.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/operations/OPERATION_ID"
PowerShell
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-id" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://automl.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/operations/OPERATION_ID" | Select-Object -Expand Content
{ "name": "projects/PROJECT_ID/locations/us-central1/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.automl.v1.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.v1.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.v1.Model", "name": "projects/PROJECT_ID/locations/us-central1/models/MODEL_ID" } }
Go
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Java
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Node.js
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Python
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Additional languages
C#: Please follow the C# setup instructions on the client libraries page and then visit the AutoML Vision Object Detection reference documentation for .NET.
PHP: Please follow the PHP setup instructions on the client libraries page and then visit the AutoML Vision Object Detection reference documentation for PHP.
Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the AutoML Vision Object Detection reference documentation for Ruby.