notebooks/official/automl/sdk_automl_image_object_detection_batch.ipynb (939 lines of code) (raw):

{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "copyright" }, "outputs": [], "source": [ "# Copyright 2022 Google LLC\n", "#\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the Lice`nse is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "title" }, "source": [ "# Vertex AI SDK : AutoML training image object detection model for batch prediction\n", "\n", "<table align=\"left\">\n", " \n", " <td style=\"text-align: center\">\n", " <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_image_object_detection_batch.ipynb\">\n", " <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"><br> Open in Colab\n", " </a>\n", " </td>\n", " <td style=\"text-align: center\">\n", " <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fautoml%2Fsdk_automl_image_object_detection_batch.ipynb\">\n", " <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n", " </a>\n", " </td> \n", " <td style=\"text-align: center\">\n", " <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_image_object_detection_batch.ipynb\">\n", " <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br>\n", " View on GitHub\n", " </a>\n", " </td>\n", " <td style=\"text-align: center\">\n", "<a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/official/automl/sdk_automl_image_object_detection_batch.ipynb\" target='_blank'> \n", " <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br>\n", " Open in Vertex AI Workbench\n", " </a>\n", " </td> \n", "</table>\n", "<br/><br/><br/>" ] }, { "cell_type": "markdown", "metadata": { "id": "overview:automl" }, "source": [ "## Overview\n", "\n", "\n", "This tutorial demonstrates how to use the Vertex AI SDK to create image object detection models and do batch prediction using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model.\n", "\n", "Learn more about [Object detection for image data](https://cloud.google.com/vertex-ai/docs/training-overview#object_detection_for_images)." ] }, { "cell_type": "markdown", "metadata": { "id": "objective:automl,training,batch_prediction" }, "source": [ "### Objective\n", "\n", "In this tutorial, you create an AutoML image object detection model from a Python script, and then do a batch prediction using the Vertex AI SDK for Python. Alternatively, you can create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n", "\n", "This tutorial uses the following Google Cloud Vertex AI services:\n", "\n", "- AutoML Training\n", "- Vertex AI datasets\n", "\n", "The steps performed include:\n", "\n", "- Create a Vertex dataset resource.\n", "- Train the model.\n", "- View the model evaluation.\n", "- Make a batch prediction.\n", "\n", "There is one key difference between using batch prediction and using online prediction:\n", "\n", "* 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.\n", "\n", "* 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." ] }, { "cell_type": "markdown", "metadata": { "id": "dataset:salads,iod" }, "source": [ "### Dataset\n", "\n", "The dataset used for this tutorial is the Salads category of the [OpenImages dataset](https://www.tensorflow.org/datasets/catalog/open_images_v4) from [TensorFlow Datasets](https://www.tensorflow.org/datasets/catalog/overview). This dataset does not require any feature engineering. The version of the dataset you will use in this tutorial is stored in a public Cloud Storage bucket. The trained model predicts the bounding box locations and the corresponding type of salad items in an image from a class of five items: salad, seafood, tomato, baked goods, or cheese." ] }, { "cell_type": "markdown", "metadata": { "id": "costs" }, "source": [ "### Costs\n", "\n", "This tutorial uses billable components of Google Cloud:\n", "\n", "* Vertex AI\n", "* Cloud Storage\n", "\n", "Learn about [Vertex AI\n", "pricing](https://cloud.google.com/vertex-ai/pricing) and [Cloud Storage\n", "pricing](https://cloud.google.com/storage/pricing), and use the [Pricing\n", "Calculator](https://cloud.google.com/products/calculator/)\n", "to generate a cost estimate based on your projected usage." ] }, { "cell_type": "markdown", "metadata": { "id": "f0316df526f8" }, "source": [ "## Get started" ] }, { "cell_type": "markdown", "metadata": { "id": "a2c2cb2109a0" }, "source": [ "### Install Vertex AI SDK for Python and other required packages\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "0ddf41aaf363" }, "outputs": [], "source": [ "! pip3 install --upgrade --quiet google-cloud-aiplatform \\\n", " google-cloud-storage \\\n", " tensorflow \\\n", " gcsfs" ] }, { "cell_type": "markdown", "metadata": { "id": "ff555b32bab8" }, "source": [ "### Restart runtime (Colab only)\n", "\n", "To use the newly installed packages, you must restart the runtime on Google Colab." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "f09b4dff629a" }, "outputs": [], "source": [ "import sys\n", "\n", "if \"google.colab\" in sys.modules:\n", "\n", " import IPython\n", "\n", " app = IPython.Application.instance()\n", " app.kernel.do_shutdown(True)" ] }, { "cell_type": "markdown", "metadata": { "id": "ee775571c2b5" }, "source": [ "<div class=\"alert alert-block alert-warning\">\n", "<b>⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️</b>\n", "</div>\n" ] }, { "cell_type": "markdown", "metadata": { "id": "92e68cfc3a90" }, "source": [ "### Authenticate your notebook environment (Colab only)\n", "\n", "Authenticate your environment on Google Colab.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "46604f70e831" }, "outputs": [], "source": [ "import sys\n", "\n", "if \"google.colab\" in sys.modules:\n", "\n", " from google.colab import auth\n", "\n", " auth.authenticate_user()" ] }, { "cell_type": "markdown", "metadata": { "id": "4f872cd812d0" }, "source": [ "### Set Google Cloud project information and initialize Vertex AI SDK for Python\n", "\n", "To get started using Vertex AI, you must have an existing Google Cloud project and [enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com). Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "294fe4e5a671" }, "outputs": [], "source": [ "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n", "LOCATION = \"us-central1\" # @param {type:\"string\"}" ] }, { "cell_type": "markdown", "metadata": { "id": "9aac652df561" }, "source": [ "### Create a Cloud Storage bucket\n", "\n", "Create a storage bucket to store intermediate artifacts such as datasets." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "e0e9bca8f832" }, "outputs": [], "source": [ "BUCKET_URI = f\"gs://your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}" ] }, { "cell_type": "markdown", "metadata": { "id": "c73989a0c3dd" }, "source": [ "**If your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "d10aa833fbbe" }, "outputs": [], "source": [ "! gsutil mb -l $LOCATION -p $PROJECT_ID $BUCKET_URI" ] }, { "cell_type": "markdown", "metadata": { "id": "a56633b047ee" }, "source": [ "### Initialize Vertex AI SDK for Python" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "4dc6b3ba241c" }, "outputs": [], "source": [ "from google.cloud import aiplatform\n", "\n", "aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI, location=LOCATION)" ] }, { "cell_type": "markdown", "metadata": { "id": "tutorial_start:automl" }, "source": [ "# Tutorial\n", "\n", "Now you are ready to start creating your own AutoML image object detection model." ] }, { "cell_type": "markdown", "metadata": { "id": "import_file:u_dataset,csv" }, "source": [ "#### Location of Cloud Storage training data.\n", "\n", "Now set the variable `IMPORT_FILE` to the location of the CSV index file in Cloud Storage." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "import_file:salads,csv,iod" }, "outputs": [], "source": [ "IMPORT_FILE = \"gs://cloud-samples-data/vision/salads.csv\"" ] }, { "cell_type": "markdown", "metadata": { "id": "f41a55981d90" }, "source": [ "### Copying data between Google Cloud Storage Buckets \n", "\n", "In this step, you prevent access issues for the images in your original dataset. The code below extracts folder paths from image paths, constructs destination paths for Google Cloud Storage (GCS), copies images using gsutil commands, updates image paths in the DataFrame, and finally saves the modified DataFrame back to GCS as a CSV file." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "df98442ace03" }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "# Read the CSV file\n", "df = pd.read_csv(IMPORT_FILE, header=None)\n", "\n", "# Extract folder paths from image paths\n", "df[\"folder_path\"] = df.iloc[:, 0].apply(lambda x: \"/\".join(x.split(\"/\")[:-1]))\n", "\n", "# Construct destination paths in your bucket (adding a trailing slash for directories)\n", "df[\"destination_path\"] = (\n", " BUCKET_URI\n", " + \"/img/openimage/\"\n", " + df[\"folder_path\"].apply(lambda x: x.split(\"/\")[-1])\n", " + \"/\"\n", ")\n", "\n", "# Copy images using gsutil commands directly\n", "for src, dest in zip(df.iloc[:, 0], df[\"destination_path\"]):\n", " ! gsutil -m cp {src} {dest}\n", "\n", "print(f\"Files copied to {BUCKET_URI}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "7ca1626de99f" }, "outputs": [], "source": [ "# Combine the destination folder paths with the original image filenames\n", "df[\"new_image_path\"] = df[\"destination_path\"] + df.iloc[:, 0].apply(\n", " lambda x: x.split(\"/\")[-1]\n", ")\n", "\n", "# Replace the original image path column with the new full paths\n", "df.iloc[:, 0] = df[\"new_image_path\"]\n", "\n", "# Drop the temporary columns\n", "df = df.drop(columns=[\"new_image_path\", \"destination_path\", \"folder_path\"])\n", "\n", "# Specify the destination file path in your bucket for the updated CSV\n", "CSV_DESTINATION_PATH = f\"{BUCKET_URI}/vision/salads.csv\"\n", "\n", "# Save the updated DataFrame directly to GCS\n", "df.to_csv(CSV_DESTINATION_PATH, index=False, header=None)" ] }, { "cell_type": "markdown", "metadata": { "id": "ecc97105a2d7" }, "source": [ "#### Location of Cloud Storage training data.\n", "\n", "Redefining the variable `IMPORT_FILE` to the location of the CSV index file in Cloud Storage." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "028fb6ec54e0" }, "outputs": [], "source": [ "IMPORT_FILE = CSV_DESTINATION_PATH\n", "\n", "print(IMPORT_FILE)" ] }, { "cell_type": "markdown", "metadata": { "id": "f4fd562be838" }, "source": [ "#### Quick peek at your data\n", "\n", "This tutorial uses a version of salads dataset which is copied to the project's Cloud Storage Bucket.\n", "\n", "Start by doing a quick peek at the data. You count the number of examples by counting the number of rows in the CSV index file (`wc -l`) and then peek at the first few rows." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "1db4b3d511a6" }, "outputs": [], "source": [ "if \"IMPORT_FILES\" in globals():\n", " FILE = IMPORT_FILES[0]\n", "else:\n", " FILE = IMPORT_FILE\n", "\n", "count = ! gsutil cat $FILE | wc -l\n", "print(\"Number of Examples\", int(count[0]))\n", "\n", "print(\"First 10 rows\")\n", "! gsutil cat $FILE | head" ] }, { "cell_type": "markdown", "metadata": { "id": "create_dataset:image,iod" }, "source": [ "### Create the dataset\n", "\n", "Next, create the dataset resource using the `create` method for the `ImageDataset` class, which takes the following parameters:\n", "\n", "- `display_name`: The human readable name for the dataset resource.\n", "- `gcs_source`: A list of one or more dataset index files to import the data items into the dataset resource.\n", "- `import_schema_uri`: The data labeling schema for the data items.\n", "\n", "This operation may take several minutes." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "FYyaPzfRv5mc" }, "outputs": [], "source": [ "DISPLAY_NAME = \"salads_unique\"\n", "dataset = aiplatform.ImageDataset.create(\n", " display_name=DISPLAY_NAME,\n", " gcs_source=[IMPORT_FILE],\n", " import_schema_uri=aiplatform.schema.dataset.ioformat.image.bounding_box,\n", ")\n", "\n", "print(dataset.resource_name)" ] }, { "cell_type": "markdown", "metadata": { "id": "create_automl_pipeline:image,iod" }, "source": [ "### Create and run training pipeline\n", "\n", "To train an AutoML model, you perform two steps: 1) create a training pipeline, and 2) run the pipeline.\n", "\n", "#### Create training pipeline\n", "\n", "An AutoML training pipeline is created with the `AutoMLImageTrainingJob` class, with the following parameters:\n", "\n", "- `display_name`: The human readable name for the TrainingJob resource.\n", "- `prediction_type`: The type task to train the model for.\n", " - `classification`: An image classification model.\n", " - `object_detection`: An image object detection model.\n", "- `multi_label`: If a classification task, whether single (`False`) or multi-labeled (`True`).\n", "- `model_type`: The type of model for deployment.\n", " - `CLOUD`: Deployment on Google Cloud\n", " - `CLOUD_HIGH_ACCURACY_1`: Optimized for accuracy over latency for deployment on Google Cloud.\n", " - `CLOUD_LOW_LATENCY_`: Optimized for latency over accuracy for deployment on Google Cloud.\n", " - `MOBILE_TF_VERSATILE_1`: Deployment on an edge device.\n", " - `MOBILE_TF_HIGH_ACCURACY_1`:Optimized for accuracy over latency for deployment on an edge device.\n", " - `MOBILE_TF_LOW_LATENCY_1`: Optimized for latency over accuracy for deployment on an edge device.\n", "- `base_model`: (optional) Transfer learning from existing model resource -- supported for image classification only.\n", "\n", "The instantiated object is the job for the training job." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "vtAgY1Nmv5md" }, "outputs": [], "source": [ "job = aiplatform.AutoMLImageTrainingJob(\n", " display_name=DISPLAY_NAME,\n", " prediction_type=\"object_detection\",\n", " multi_label=False,\n", " model_type=\"CLOUD\",\n", " base_model=None,\n", ")\n", "\n", "print(job)" ] }, { "cell_type": "markdown", "metadata": { "id": "run_automl_pipeline:image" }, "source": [ "#### Run the training pipeline\n", "\n", "Next, you run the job to start the training job by invoking the method `run`, with the following parameters:\n", "\n", "- `dataset`: The dataset resource to train the model.\n", "- `model_display_name`: The human readable name for the trained model.\n", "- `training_fraction_split`: The percentage of the dataset to use for training.\n", "- `test_fraction_split`: The percentage of the dataset to use for test (holdout data).\n", "- `validation_fraction_split`: The percentage of the dataset to use for validation.\n", "- `budget_milli_node_hours`: (optional) Maximum training time specified in unit of millihours (1000 = hour).\n", "- `disable_early_stopping`: If `True`, the entire budget is used. Else, training maybe completed before using the entire budget if the service believes it cannot further improve on the model objective measurements.\n", "\n", "The `run` method when completed returns the model resource.\n", "\n", "The execution of the training pipeline will take upto 1 hour 30 minutes." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "SvV1nFDTv5md" }, "outputs": [], "source": [ "model = job.run(\n", " dataset=dataset,\n", " model_display_name=DISPLAY_NAME,\n", " training_fraction_split=0.8,\n", " validation_fraction_split=0.1,\n", " test_fraction_split=0.1,\n", " budget_milli_node_hours=20000,\n", " disable_early_stopping=False,\n", ")" ] }, { "cell_type": "markdown", "metadata": { "id": "evaluate_the_model:mbsdk" }, "source": [ "## Review model evaluation scores\n", "After your model has finished training, you can review the evaluation scores for it.\n", "\n", "First, you need to get a reference to the new model. As with datasets, you can either use the reference to the model variable you created when you deployed the model or you can list all of the models in your project." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "RcpDJMgev5me" }, "outputs": [], "source": [ "# Get model resource ID\n", "filter_name = f\"display_name={DISPLAY_NAME}\"\n", "models = aiplatform.Model.list(filter=filter_name)\n", "\n", "# Get a reference to the Model Service client\n", "client_options = {\"api_endpoint\": f\"{LOCATION}-aiplatform.googleapis.com\"}\n", "model_service_client = aiplatform.gapic.ModelServiceClient(\n", " client_options=client_options\n", ")\n", "\n", "model_evaluations = model_service_client.list_model_evaluations(\n", " parent=models[0].resource_name\n", ")\n", "model_evaluation = list(model_evaluations)[0]\n", "print(model_evaluation)" ] }, { "cell_type": "markdown", "metadata": { "id": "make_prediction" }, "source": [ "## Send a batch prediction request\n", "\n", "Send a batch prediction to your deployed model." ] }, { "cell_type": "markdown", "metadata": { "id": "get_test_items:batch_prediction" }, "source": [ "### Get test item(s)\n", "\n", "Now do a batch prediction to your Vertex model. You will use arbitrary examples out of the dataset as a test items. Don't be concerned that the examples were likely used in training the model -- we just want to demonstrate how to make a prediction." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "get_test_items:automl,iod,csv" }, "outputs": [], "source": [ "test_items = !gsutil cat $IMPORT_FILE | head -n2\n", "cols_1 = str(test_items[0]).split(\",\")\n", "cols_2 = str(test_items[1]).split(\",\")\n", "if len(cols_1) == 11:\n", " test_item_1 = str(cols_1[1])\n", " test_label_1 = str(cols_1[2])\n", " test_item_2 = str(cols_2[1])\n", " test_label_2 = str(cols_2[2])\n", "else:\n", " test_item_1 = str(cols_1[0])\n", " test_label_1 = str(cols_1[1])\n", " test_item_2 = str(cols_2[0])\n", " test_label_2 = str(cols_2[1])\n", "\n", "print(test_item_1, test_label_1)\n", "print(test_item_2, test_label_2)" ] }, { "cell_type": "markdown", "metadata": { "id": "copy_test_items:batch_prediction" }, "source": [ "### Copy test item(s)\n", "\n", "For the batch prediction, copy the test items over to your Cloud Storage bucket." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "JXpg67zjv5mf" }, "outputs": [], "source": [ "file_1 = test_item_1.split(\"/\")[-1]\n", "file_2 = test_item_2.split(\"/\")[-1]\n", "\n", "! gsutil cp $test_item_1 $BUCKET_URI/$file_1\n", "! gsutil cp $test_item_2 $BUCKET_URI/$file_2\n", "\n", "test_item_1 = BUCKET_URI + \"/\" + file_1\n", "test_item_2 = BUCKET_URI + \"/\" + file_2" ] }, { "cell_type": "markdown", "metadata": { "id": "make_batch_file:automl,image" }, "source": [ "### Make the batch input file\n", "\n", "Now make a batch input file, which you will store in your local Cloud Storage bucket. The batch input file can be either CSV or JSONL. You will use JSONL in this tutorial. For JSONL file, you make one dictionary entry per line for each data item (instance). The dictionary contains the key/value pairs:\n", "\n", "- `content`: The Cloud Storage path to the image.\n", "- `mime_type`: The content type. In our example, it is a `jpeg` file.\n", "\n", "For example:\n", "\n", " {'content': '[your-bucket]/file1.jpg', 'mime_type': 'jpeg'}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "hFjX62hvv5mg" }, "outputs": [], "source": [ "import json\n", "\n", "import tensorflow as tf\n", "\n", "gcs_input_uri = BUCKET_URI + \"/test.jsonl\"\n", "with tf.io.gfile.GFile(gcs_input_uri, \"w\") as f:\n", " data = {\"content\": test_item_1, \"mime_type\": \"image/jpeg\"}\n", " f.write(json.dumps(data) + \"\\n\")\n", " data = {\"content\": test_item_2, \"mime_type\": \"image/jpeg\"}\n", " f.write(json.dumps(data) + \"\\n\")\n", "\n", "print(gcs_input_uri)\n", "! gsutil cat $gcs_input_uri" ] }, { "cell_type": "markdown", "metadata": { "id": "batch_request:mbsdk" }, "source": [ "### Make the batch prediction request\n", "\n", "Now that your Model resource is trained, you can make a batch prediction by invoking the batch_predict() method, with the following parameters:\n", "\n", "- `job_display_name`: The human readable name for the batch prediction job.\n", "- `gcs_source`: A list of one or more batch request input files.\n", "- `gcs_destination_prefix`: The Cloud Storage location for storing the batch prediction resuls.\n", "- `machine_type`: The type of machine for running batch prediction on dedicated resources. Not specifying machine type will result in batch prediction job being run with automatic resources.\n", "- `starting_replica_count`: The number of machine replicas used at the start of the batch operation. If not set, Vertex AI decides starting number, not greater than `max_replica_count`. Only used if `machine_type` is set.\n", "- `max_replica_count`: The maximum number of machine replicas the batch operation may be scaled to. Only used if `machine_type` is set. Default is 10.\n", "- `sync`: If set to True, the call will block while waiting for the asynchronous batch job to complete." ] }, { "cell_type": "markdown", "metadata": { "id": "axn-CW7xv5mg" }, "source": [ "For AutoML models, only manual scaling is supported. In manual scaling both starting_replica_count and max_replica_count have the same value.\n", "For this batch job we are using manual scaling. Here we are setting both starting_replica_count and max_replica_count to the same value that is 1. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "5VMMaJhbv5mh" }, "outputs": [], "source": [ "batch_predict_job = model.batch_predict(\n", " job_display_name=DISPLAY_NAME,\n", " gcs_source=gcs_input_uri,\n", " gcs_destination_prefix=BUCKET_URI,\n", " machine_type=\"n1-standard-4\",\n", " starting_replica_count=1,\n", " max_replica_count=1,\n", " sync=False,\n", ")\n", "\n", "print(batch_predict_job)" ] }, { "cell_type": "markdown", "metadata": { "id": "batch_request_wait:mbsdk" }, "source": [ "### Wait for completion of batch prediction job\n", "\n", "Next, wait for the batch job to complete. Alternatively, one can set the parameter `sync` to `True` in the `batch_predict()` method to block until the batch prediction job is completed." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Yc3YaEqGv5mh" }, "outputs": [], "source": [ "batch_predict_job.wait()" ] }, { "cell_type": "markdown", "metadata": { "id": "get_batch_prediction:mbsdk,iod" }, "source": [ "### Get the predictions\n", "\n", "Next, get the results from the completed batch prediction job.\n", "\n", "The results are written to the Cloud Storage output bucket you specified in the batch prediction request. You call the method iter_outputs() 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:\n", "\n", "- `content`: The prediction request.\n", "- `prediction`: The prediction response.\n", " - `ids`: The internal assigned unique identifiers for each prediction request.\n", " - `displayNames`: The class names for each class label.\n", " - `bboxes`: The bounding box of each detected object." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "UGGJxFjEv5mh" }, "outputs": [], "source": [ "import json\n", "\n", "bp_iter_outputs = batch_predict_job.iter_outputs()\n", "\n", "prediction_results = list()\n", "for blob in bp_iter_outputs:\n", " if blob.name.split(\"/\")[-1].startswith(\"prediction\"):\n", " prediction_results.append(blob.name)\n", "\n", "tags = list()\n", "for prediction_result in prediction_results:\n", " gfile_name = f\"gs://{bp_iter_outputs.bucket.name}/{prediction_result}\"\n", " with tf.io.gfile.GFile(name=gfile_name, mode=\"r\") as gfile:\n", " for line in gfile.readlines():\n", " line = json.loads(line)\n", " print(line)" ] }, { "cell_type": "markdown", "metadata": { "id": "cleanup:mbsdk" }, "source": [ "# Cleaning up\n", "\n", "To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n", "project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n", "\n", "Otherwise, you can delete the individual resources you created in this tutorial:\n", "\n", "- Dataset\n", "- Model\n", "- AutoML Training Job\n", "- Batch Job\n", "- Cloud Storage Bucket" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "_Olkhs6xv5mi" }, "outputs": [], "source": [ "delete_bucket = False\n", "\n", "# Delete the dataset using the Vertex dataset object\n", "dataset.delete()\n", "\n", "# Delete the model using the Vertex model object\n", "model.delete()\n", "\n", "# Delete the AutoML or Pipeline trainig job\n", "job.delete()\n", "\n", "# Delete the batch prediction job using the Vertex batch prediction object\n", "batch_predict_job.delete()\n", "\n", "if delete_bucket:\n", " ! gsutil rm -r $BUCKET_URI" ] } ], "metadata": { "colab": { "name": "sdk_automl_image_object_detection_batch.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }