Files
model_garden/notebooks/official/automl/automl_image_classification_batch_prediction.ipynb
T

28 KiB

In [ ]:
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

AutoML training image classification model for batch prediction

Google Colaboratory logo
Open in Colab
Google Cloud Colab Enterprise logo
Open in Colab Enterprise
Vertex AI logo
Open in Workbench
GitHub logo
View on GitHub

Overview

This tutorial demonstrates how to use the Vertex AI SDK to create image classification models and do batch prediction using a Google Cloud AutoML model.

Learn more about Get predictions from an image classification model.

Objective

In this tutorial, you create an AutoML image classification model from a Python script, and then do a batch prediction using the Vertex SDK. You can alternatively create and deploy models using the gcloud command-line tool or online using the Cloud Console.

The steps performed include:

  • Create a Vertex dataset resource.
  • Train the model.
  • View the model evaluation.
  • Make a batch prediction.

There's one key difference between using batch prediction and using online prediction:

Online Prediction Service: Does an on-demand prediction for the entire set of instances (i.e., one or more data items) and returns the results in real-time.

Batch Prediction Service: Does a queued (batch) prediction for the entire set of instances in the background and stores the results in a Cloud Storage bucket when ready.

Dataset

The dataset used for this tutorial is the Flowers dataset from TensorFlow Datasets. The version of the dataset you use in this tutorial is stored in a public Cloud Storage bucket. The trained model predicts the type of flower an image is from a class of five flowers: daisy, dandelion, rose, sunflower, or tulip.

Costs

This tutorial uses billable components of Google Cloud:

  • Vertex AI
  • Cloud Storage

Learn about Vertex AI pricing and Cloud Storage pricing, and use the Pricing Calculator to generate a cost estimate based on your projected usage.

Get started

Install Vertex AI SDK for Python and other required packages

In [ ]:
! pip3 install --upgrade --quiet google-cloud-aiplatform \
                                 tensorflow==2.15.1

Restart runtime (Colab only)

To use the newly installed packages, you must restart the runtime on Google Colab.

In [ ]:
import sys

if "google.colab" in sys.modules:

    import IPython

    app = IPython.Application.instance()
    app.kernel.do_shutdown(True)
⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️

Authenticate your notebook environment (Colab only)

Authenticate your environment on Google Colab.

In [ ]:
import sys

if "google.colab" in sys.modules:

    from google.colab import auth

    auth.authenticate_user()

Set Google Cloud project information

To get started using Vertex AI, you must have an existing Google Cloud project. Learn more about setting up a project and a development environment.

In [ ]:
PROJECT_ID = "[your-project-id]"  # @param {type:"string"}
LOCATION = "us-central1"  # @param {type:"string"}

# Set the project id
! gcloud config set project {PROJECT_ID}

Create a Cloud Storage bucket

Create a storage bucket to store intermediate artifacts such as datasets.

In [ ]:
BUCKET_URI = f"gs://your-bucket-name-{PROJECT_ID}-unique"  # @param {type:"string"}

If your bucket doesn't already exist: Run the following cell to create your Cloud Storage bucket.

In [ ]:
! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}

Set up variables

Next, set up some variables used throughout the tutorial.

Import libraries and define constants

In [ ]:
from google.cloud import aiplatform

Initialize Vertex AI SDK for Python

Initialize the Vertex AI SDK for Python for your project and corresponding bucket.

In [ ]:
aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)

Tutorial

Now you're ready to start creating your own AutoML image classification model.

Location of Cloud Storage training data.

Now set the variable IMPORT_FILE to the location of the CSV index file in Cloud Storage.

In [ ]:
IMPORT_FILE = "gs://cloud-samples-data/ai-platform/flowers/flowers.csv"

Quick peek at your data

This tutorial uses a version of the Flowers dataset that is stored in a public Cloud Storage bucket, using a CSV index file.

Begin by taking a quick look at the data. First, count the number of examples by determining the number of rows in the CSV index file using the (wc -l) command. Then, preview the first few rows of the file.

In [ ]:
if "IMPORT_FILES" in globals():
    FILE = IMPORT_FILES[0]
else:
    FILE = IMPORT_FILE

count = ! gsutil cat $FILE | wc -l
print("Number of Examples", int(count[0]))

print("First 10 rows")
! gsutil cat $FILE | head

Create the Dataset

Next, create the dataset resource using the create method for the ImageDataset class, which takes the following parameters:

  • display_name: The human readable name for the dataset resource.
  • gcs_source: A list of one or more dataset index files to import the data items into the dataset resource.
  • import_schema_uri: The data labeling schema for the data items.

This operation may take several minutes.

In [ ]:
dataset = aiplatform.ImageDataset.create(
    display_name="Flowers",
    gcs_source=[IMPORT_FILE],
    import_schema_uri=aiplatform.schema.dataset.ioformat.image.single_label_classification,
)

print(dataset.resource_name)

Create and run training pipeline

To train an AutoML model, you perform two steps: 1) create a training pipeline, and 2) run the pipeline.

Create training pipeline

An AutoML training pipeline is created with the AutoMLImageTrainingJob class, with the following parameters:

  • display_name: The human readable name for the training job resource.
  • prediction_type: The type of task for which the model is trained.
    • classification: An image classification model.
    • object_detection: An image object detection model.
  • multi_label: For a classification task, specify whether it's multi-labeled (True) or single-labeled (False).
  • model_type: The type of model for deployment.
    • CLOUD: Deployment on Google Cloud
    • CLOUD_HIGH_ACCURACY_1: Optimized for accuracy over latency for deployment on Google Cloud.
    • CLOUD_LOW_LATENCY_: Optimized for latency over accuracy for deployment on Google Cloud.
    • MOBILE_TF_VERSATILE_1: Deployment on an edge device.
    • MOBILE_TF_HIGH_ACCURACY_1:Optimized for accuracy over latency for deployment on an edge device.
    • MOBILE_TF_LOW_LATENCY_1: Optimized for latency over accuracy for deployment on an edge device.
  • base_model: (optional) Transfer learning from existing model resource -- supported for image classification only.

The instantiated object is the DAG (directed acyclic graph) for the training job.

In [ ]:
dag = aiplatform.AutoMLImageTrainingJob(
    display_name="flowers",
    prediction_type="classification",
    multi_label=False,
    model_type="CLOUD",
    base_model=None,
)

print(dag)

Run the training pipeline

Next, run the DAG to start the training job by invoking the run method, with the following parameters:

  • dataset: The dataset resource to train the model.
  • model_display_name: The human readable name for the trained model.
  • training_fraction_split: The percentage of the dataset to use for training.
  • test_fraction_split: The percentage of the dataset to use for test (holdout data).
  • validation_fraction_split: The percentage of the dataset to use for validation.
  • budget_milli_node_hours: (optional) Maximum training time specified in unit of millihours (1000 = hour).
  • disable_early_stopping: By default, the model training stops early if the model performance doesn't improve. Setting disable_early_stopping = True overrides this behavior, allowing the model to train for the entire specified duration.

The run method, upon completion, returns the model resource

In [ ]:
model = dag.run(
    dataset=dataset,
    model_display_name="flowers",
    training_fraction_split=0.8,
    validation_fraction_split=0.1,
    test_fraction_split=0.1,
    budget_milli_node_hours=8000,
    disable_early_stopping=False,
)

Review model evaluation scores

After your model training is complete, you can review the evaluation scores for it using the list_model_evaluations() method. This method returns an iterator for each evaluation slice.

Send a batch prediction request

Send a batch prediction to your deployed model.

In [ ]:
model_evaluations = model.list_model_evaluations()

for model_evaluation in model_evaluations:
    print(model_evaluation.to_dict())

Get test item(s)

Now generate a batch prediction for your Vertex AI model. Use arbitrary examples from the dataset as test items. Don't be concerned that the example was likely used in training the model -- the point is to demonstrate how to make a prediction.

In [ ]:
test_items = !gsutil cat $IMPORT_FILE | head -n2
if len(str(test_items[0]).split(",")) == 3:
    _, test_item_1, test_label_1 = str(test_items[0]).split(",")
    _, test_item_2, test_label_2 = str(test_items[1]).split(",")
else:
    test_item_1, test_label_1 = str(test_items[0]).split(",")
    test_item_2, test_label_2 = str(test_items[1]).split(",")

print(test_item_1, test_label_1)
print(test_item_2, test_label_2)

Copy test item(s)

For the batch prediction, copy the test items over to your Cloud Storage bucket.

In [ ]:
file_1 = test_item_1.split("/")[-1]
file_2 = test_item_2.split("/")[-1]

! gsutil cp $test_item_1 $BUCKET_URI/$file_1
! gsutil cp $test_item_2 $BUCKET_URI/$file_2

test_item_1 = BUCKET_URI + "/" + file_1
test_item_2 = BUCKET_URI + "/" + file_2

Create the batch input file

Now make a batch input file, which is stored in your local Cloud Storage bucket. The batch input file can be either CSV or JSONL. Use JSONL in this tutorial. For JSONL file, make one dictionary entry per line for each data item (instance). The dictionary contains the key/value pairs:

  • content: The Cloud Storage path to the image.
  • mime_type: The content type. In our example, it's a jpeg file.

For example:

                    {'content': '[your-bucket]/file1.jpg', 'mime_type': 'jpeg'}
In [ ]:
import json

import tensorflow as tf

gcs_input_uri = BUCKET_URI + "/test.jsonl"
with tf.io.gfile.GFile(gcs_input_uri, "w") as f:
    data = {"content": test_item_1, "mime_type": "image/jpeg"}
    f.write(json.dumps(data) + "\n")
    data = {"content": test_item_2, "mime_type": "image/jpeg"}
    f.write(json.dumps(data) + "\n")

print(gcs_input_uri)
! gsutil cat $gcs_input_uri

Make the batch prediction request

Now that your model resource is trained, make a batch prediction by invoking the batch_predict() method, with the following parameters:

  • job_display_name: The human readable name for the batch prediction job.
  • gcs_source: A list of one or more batch request input files.
  • gcs_destination_prefix: The Cloud Storage location for storing the batch prediction results.
  • sync: Set True to wait until the completion of the job.
In [ ]:
batch_predict_job = model.batch_predict(
    job_display_name="flowers",
    gcs_source=gcs_input_uri,
    gcs_destination_prefix=BUCKET_URI,
    sync=False,
)

print(batch_predict_job)

Wait for completion of batch prediction job

Next, wait for the batch job to complete. Alternatively, you can set the parameter sync to True in the batch_predict() method to block until the batch prediction job is completed.

In [ ]:
batch_predict_job.wait()

Get the predictions

Next, get the results from the completed batch prediction job.

The results are written to the Cloud Storage output bucket specified in the batch prediction request. Call the iter_outputs() method to get a list of each Cloud Storage file generated with the results. Each file contains one or more prediction requests in a JSON format:

  • content: The prediction request.
  • prediction: The prediction response.
  • ids: The internally assigned unique identifiers for each prediction request.
  • displayNames: The class names for each class label.
  • confidences: The predicted confidence, between 0 and 1, per class label.
In [ ]:
import json

import tensorflow as tf

bp_iter_outputs = batch_predict_job.iter_outputs()

prediction_results = list()
for blob in bp_iter_outputs:
    if blob.name.split("/")[-1].startswith("prediction"):
        prediction_results.append(blob.name)

tags = list()
for prediction_result in prediction_results:
    gfile_name = f"gs://{bp_iter_outputs.bucket.name}/{prediction_result}"
    with tf.io.gfile.GFile(name=gfile_name, mode="r") as gfile:
        for line in gfile.readlines():
            line = json.loads(line)
            print(line)
            break

Cleaning up

To clean up all Google Cloud resources used in this project, you can delete the Google Cloud project you used for the tutorial.

Otherwise, you can delete the individual resources you created in this tutorial.

In [ ]:
# Delete the dataset using the Vertex dataset object
dataset.delete()

# Delete the model using the Vertex model object
model.delete()

# Delete the AutoML trainig job
dag.delete()

# Delete the batch prediction job
batch_predict_job.delete()

# Delete the cloud storage bucket
delete_bucket = False  # set True for deletion
if delete_bucket:
    ! gsutil rm -r $BUCKET_URI