diff --git a/notebooks/official/migration/sdk-automl-object-tracking-batch-prediction.ipynb b/notebooks/official/migration/sdk-automl-object-tracking-batch-prediction.ipynb
deleted file mode 100644
index 830877c84..000000000
--- a/notebooks/official/migration/sdk-automl-object-tracking-batch-prediction.ipynb
+++ /dev/null
@@ -1,1023 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "copyright"
- },
- "outputs": [],
- "source": [
- "# Copyright 2021 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 License 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:migration,new"
- },
- "source": [
- "# Vertex AI Migration: AutoML Video Object Tracking\n",
- "\n",
- "
\n",
- "\n",
- " \n",
- " \n",
- "  Open in Colab\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- "  Open in Colab Enterprise\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- "  \n",
- " View on GitHub\n",
- " \n",
- " | \n",
- " \n",
- "\n",
- "  \n",
- " Open in Vertex AI Workbench\n",
- " \n",
- " | \n",
- "
\n",
- "
"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "7a8a13b86a8b"
- },
- "source": [
- "## Overview\n",
- "\n",
- "This tutorial demonstrates how to use the Vertex AI SDK for Python to train an AutoML video object tracking model and do batch prediction.\n",
- "\n",
- "Learn more about [Migrate to Vertex AI](https://cloud.google.com/vertex-ai/docs/start/migrating-to-vertex-ai) and [Object tracking for video data](https://cloud.google.com/vertex-ai/docs/training-overview#object_tracking_for_videos)."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "618cfedf829a"
- },
- "source": [
- "### Objective\n",
- "\n",
- "In this tutorial, you learn to use AutoML to train a video model and use Vertex AI batch prediction to do batch predictions.\n",
- "\n",
- "\n",
- "This tutorial uses the following Google Cloud ML services:\n",
- "\n",
- "- AutoML\n",
- "- Vertex AI batch prediction\n",
- "- Vertex AI model resource\n",
- "- Vertex AI endpoint resource\n",
- "\n",
- "The steps performed include:\n",
- "\n",
- "- Train an AutoML video object tracking model.\n",
- "- Make a batch prediction."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "dataset:traffic,vot"
- },
- "source": [
- "### Dataset\n",
- "\n",
- "The dataset used for this tutorial is the Traffic. The version of the dataset you use in this tutorial is stored in a public Cloud Storage bucket."
- ]
- },
- {
- "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": "5143c7e7acab"
- },
- "outputs": [],
- "source": [
- "! pip3 install --upgrade --quiet google-cloud-aiplatform \\\n",
- " google-cloud-storage \\\n",
- " tensorflow"
- ]
- },
- {
- "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": [
- "\n",
- "⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️\n",
- "
\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": "b49704f4eeca"
- },
- "outputs": [],
- "source": [
- "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
- "LOCATION = \"us-central1\" # @param {type:\"string\"}\n",
- "\n",
- "\n",
- "from google.cloud import aiplatform\n",
- "\n",
- "aiplatform.init(project=PROJECT_ID, location=LOCATION)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "timestamp"
- },
- "source": [
- "#### UUID\n",
- "\n",
- "If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "timestamp"
- },
- "outputs": [],
- "source": [
- "import random\n",
- "import string\n",
- "\n",
- "\n",
- "# Generate a uuid of a specifed length(default=8)\n",
- "def generate_uuid(length: int = 8) -> str:\n",
- " return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
- "\n",
- "\n",
- "UUID = generate_uuid()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "bucket:mbsdk"
- },
- "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": "bucket"
- },
- "outputs": [],
- "source": [
- "BUCKET_URI = f\"gs://your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_bucket"
- },
- "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": "create_bucket"
- },
- "outputs": [],
- "source": [
- "! gsutil mb -l {LOCATION} {BUCKET_URI}"
- ]
- },
- {
- "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:traffic,csv,vot"
- },
- "outputs": [],
- "source": [
- "IMPORT_FILE = \"gs://automl-video-demo-data/traffic_videos/traffic_videos_labels.csv\""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "quick_peek:csv"
- },
- "source": [
- "#### Quick peek at your data\n",
- "\n",
- "This tutorial uses a version of the Traffic dataset that is stored in a public Cloud Storage bucket, using a CSV index file.\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": "quick_peek:csv"
- },
- "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_a_dataset:migration"
- },
- "source": [
- "## Create a dataset"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "datasets_create:migration,new,mbsdk"
- },
- "source": [
- "### [datasets.create-dataset-api](https://cloud.google.com/vertex-ai/docs/datasets/create-dataset-api)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_dataset:video,vot"
- },
- "source": [
- "### Create the dataset\n",
- "\n",
- "Next, create the dataset resource using the `create` method for the `VideoDataset` 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",
- "\n",
- "This operation may take several minutes."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "create_dataset:video,vot"
- },
- "outputs": [],
- "source": [
- "import os\n",
- "\n",
- "dataset = aiplatform.VideoDataset.create(\n",
- " display_name=\"Traffic\" + \"_\" + UUID,\n",
- " gcs_source=[IMPORT_FILE],\n",
- " import_schema_uri=aiplatform.schema.dataset.ioformat.video.object_tracking,\n",
- ")\n",
- "if os.getenv(\"IS_TESTING\"):\n",
- " import time\n",
- "\n",
- " time.sleep(30)\n",
- "\n",
- "print(dataset.resource_name)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_dataset:video,vot"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Creating VideoDataset\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Create VideoDataset backing LRO: projects/759209241365/locations/us-central1/datasets/5948525032035581952/operations/6913187331100901376\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:VideoDataset created. Resource name: projects/759209241365/locations/us-central1/datasets/5948525032035581952\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:To use this VideoDataset in another session:\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:ds = aiplatform.VideoDataset('projects/759209241365/locations/us-central1/datasets/5948525032035581952')\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Importing VideoDataset data: projects/759209241365/locations/us-central1/datasets/5948525032035581952\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Import VideoDataset data backing LRO: projects/759209241365/locations/us-central1/datasets/5948525032035581952/operations/6800597340416638976"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "train_a_model:migration"
- },
- "source": [
- "## Train a model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "trainingpipelines_create:migration,new,mbsdk"
- },
- "source": [
- "### [training.automl-api](https://cloud.google.com/vertex-ai/docs/training/automl-api)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_automl_pipeline:video,vot"
- },
- "source": [
- "### Create and run training pipeline\n",
- "\n",
- "To train an AutoML model, you perform two steps: \n",
- "1) create a training pipeline, and \n",
- "2) run the pipeline.\n",
- "\n",
- "#### Create training pipeline\n",
- "\n",
- "An AutoML training pipeline is created with the `AutoMLVideoTrainingJob` 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`: A video classification model.\n",
- " - `object_tracking`: A video object tracking model.\n",
- " - `action_recognition`: A video action recognition model.\n",
- "\n",
- "The instantiated object is the DAG (directed acyclic graph) for the training pipeline."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "create_automl_pipeline:video,vot"
- },
- "outputs": [],
- "source": [
- "dag = aiplatform.AutoMLVideoTrainingJob(\n",
- " display_name=\"traffic_\" + UUID,\n",
- " prediction_type=\"object_tracking\",\n",
- ")\n",
- "\n",
- "print(dag)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_automl_pipeline:video,vot"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "run_automl_pipeline:video"
- },
- "source": [
- "#### Run the training pipeline\n",
- "\n",
- "Next, you run the DAG 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",
- "\n",
- "The `run` method when completed returns the model resource.\n",
- "\n",
- "The execution of the training pipeline take upto 20 minutes."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "run_automl_pipeline:video"
- },
- "outputs": [],
- "source": [
- "model = dag.run(\n",
- " dataset=dataset,\n",
- " model_display_name=\"traffic_\" + UUID,\n",
- " training_fraction_split=0.8,\n",
- " test_fraction_split=0.2,\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "run_automl_pipeline:video"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.training_jobs:View Training:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/training/6090621516762841088?project=759209241365\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " ...\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob run completed. Resource name: projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088\n",
- " INFO:google.cloud.aiplatform.training_jobs:Model available at projects/759209241365/locations/us-central1/models/1899701006099283968"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "evaluate_the_model:migration"
- },
- "source": [
- "## Evaluate the model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "models_evaluations_list:migration,new"
- },
- "source": [
- "### [projects.locations.models.evaluations.list](https://cloud.devsite.corp.google.com/ai-platform-unified/docs/reference/rest/v1beta1/projects.locations.models.evaluations/list)"
- ]
- },
- {
- "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": "evaluate_the_model:mbsdk"
- },
- "outputs": [],
- "source": [
- "# Get model resource ID\n",
- "models = aiplatform.Model.list(filter=\"display_name=traffic_\" + UUID)\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": "evaluate_the_model:mbsdk"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " name: \"projects/759209241365/locations/us-central1/models/623915674158235648/evaluations/4280507618583117824\"\n",
- " metrics_schema_uri: \"gs://google-cloud-aiplatform/schema/modelevaluation/classification_metrics_1.0.0.yaml\"\n",
- " metrics {\n",
- " struct_value {\n",
- " fields {\n",
- " key: \"auPrc\"\n",
- " value {\n",
- " number_value: 0.9891107\n",
- " }\n",
- " }\n",
- " fields {\n",
- " key: \"confidenceMetrics\"\n",
- " value {\n",
- " list_value {\n",
- " values {\n",
- " struct_value {\n",
- " fields {\n",
- " key: \"precision\"\n",
- " value {\n",
- " number_value: 0.2\n",
- " }\n",
- " }\n",
- " fields {\n",
- " key: \"recall\"\n",
- " value {\n",
- " number_value: 1.0\n",
- " }\n",
- " }\n",
- " }\n",
- " }"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_batch_predictions:migration"
- },
- "source": [
- "## Make batch predictions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batchpredictionjobs_create:migration,new,mbsdk"
- },
- "source": [
- "### [predictions.batch-prediction](https://cloud.google.com/vertex-ai/docs/predictions/batch-predictions)"
- ]
- },
- {
- "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 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,vot,csv"
- },
- "outputs": [],
- "source": [
- "test_items = ! gsutil cat $IMPORT_FILE | head -n2\n",
- "\n",
- "cols_1 = test_items[0].split(\",\")\n",
- "cols_2 = test_items[1].split(\",\")\n",
- "if len(cols_1) > 12:\n",
- " test_item_1 = str(cols_1[1])\n",
- " test_item_2 = str(cols_2[1])\n",
- " test_label_1 = str(cols_1[2])\n",
- " test_label_2 = str(cols_2[2])\n",
- "else:\n",
- " test_item_1 = str(cols_1[0])\n",
- " test_item_2 = str(cols_2[0])\n",
- " test_label_1 = str(cols_1[1])\n",
- " test_label_2 = str(cols_2[1])\n",
- "\n",
- "\n",
- "print(test_item_1, test_label_1)\n",
- "print(test_item_2, test_label_2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_batch_file:automl,video"
- },
- "source": [
- "### Make a batch input file\n",
- "\n",
- "Now make a batch input file, which you store in your local Cloud Storage bucket. The batch input file can be either CSV or JSONL. You use JSONL in this tutorial. For JSONL file, you make one dictionary entry per line for each video. The dictionary contains the key/value pairs:\n",
- "\n",
- "- `content`: The Cloud Storage path to the video.\n",
- "- `mimeType`: The content type. In our example, it is a avi file.\n",
- "- `timeSegmentStart`: The start timestamp in the video to do prediction on. *Note*, the timestamp must be specified as a string and followed by s (second), m (minute) or h (hour).\n",
- "- `timeSegmentEnd`: The end timestamp in the video to do prediction on."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "make_batch_file:automl,video"
- },
- "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 = {\n",
- " \"content\": test_item_1,\n",
- " \"mimeType\": \"video/avi\",\n",
- " \"timeSegmentStart\": \"0.0s\",\n",
- " \"timeSegmentEnd\": \"5.0s\",\n",
- " }\n",
- " f.write(json.dumps(data) + \"\\n\")\n",
- " data = {\n",
- " \"content\": test_item_2,\n",
- " \"mimeType\": \"video/avi\",\n",
- " \"timeSegmentStart\": \"0.0s\",\n",
- " \"timeSegmentEnd\": \"5.0s\",\n",
- " }\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",
- "- `sync`: If set to True, the call block while waiting for the asynchronous batch job to complete."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "batch_request:mbsdk"
- },
- "outputs": [],
- "source": [
- "batch_predict_job = model.batch_predict(\n",
- " job_display_name=\"traffic_\" + UUID,\n",
- " gcs_source=gcs_input_uri,\n",
- " gcs_destination_prefix=BUCKET_URI,\n",
- " sync=False,\n",
- ")\n",
- "\n",
- "print(batch_predict_job)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batch_request:mbsdk"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.jobs:Creating BatchPredictionJob\n",
- " is waiting for upstream dependencies to complete.\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob created. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296\n",
- " INFO:google.cloud.aiplatform.jobs:To use this BatchPredictionJob in another session:\n",
- " INFO:google.cloud.aiplatform.jobs:bpj = aiplatform.BatchPredictionJob('projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296')\n",
- " INFO:google.cloud.aiplatform.jobs:View Batch Prediction Job:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/batch-predictions/5110965452507447296?project=759209241365\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296 current state:\n",
- " JobState.JOB_STATE_RUNNING"
- ]
- },
- {
- "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": "batch_request_wait:mbsdk"
- },
- "outputs": [],
- "source": [
- "batch_predict_job.wait()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batch_request_wait:mbsdk"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob created. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328\n",
- " INFO:google.cloud.aiplatform.jobs:To use this BatchPredictionJob in another session:\n",
- " INFO:google.cloud.aiplatform.jobs:bpj = aiplatform.BatchPredictionJob('projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328')\n",
- " INFO:google.cloud.aiplatform.jobs:View Batch Prediction Job:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/batch-predictions/181835033978339328?project=759209241365\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_SUCCEEDED\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob run completed. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_batch_prediction:mbsdk,vot"
- },
- "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",
- " - `id`: The internal assigned unique identifiers for each prediction request.\n",
- " - `displayName`: The class names for the predicted label.\n",
- " - `confidences`: The predicted confidence, between 0 and 1, per class label.\n",
- " - `timeSegmentStart`: The time offset in the video to the start of the video sequence.\n",
- " - `timeSegmentEnd`: The time offset in the video to the end of the video sequence.\n",
- " - `frames`: Location with frames of the tracked object."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "get_batch_prediction:mbsdk,vot"
- },
- "outputs": [],
- "source": [
- "import json\n",
- "\n",
- "import tensorflow as tf\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)\n",
- " break"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_batch_prediction:mbsdk,vot"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " {'instance': {'content': 'gs://automl-video-demo-data/traffic_videos/highway_005.mp4', 'mimeType': 'video/avi', 'timeSegmentStart': '0.0s', 'timeSegmentEnd': '5.0s'}, 'prediction': [{'id': '3767468895993069568', 'displayName': 'sedan', 'timeSegmentStart': '5s', 'timeSegmentEnd': '5s', 'confidence': 0.61968166, 'frames': [{'timeOffset': '5s', 'xMin': 0.62725407, 'xMax': 0.8197686, 'yMin': 0.5445565, 'yMax': 0.6752002}]}, {'id': '3767468895993069568', 'displayName': 'sedan', 'timeSegmentStart': '0.100s', 'timeSegmentEnd': '0.600s', 'confidence': 0.5820198, 'frames': [{'timeOffset': '0.100s', 'xMin': 0.82725096, 'xMax': 0.99906015, 'yMin': 0.6930067, 'yMax': 0.87328714}, {'timeOffset': '0.200s', 'xMin': 0.7275455, 'xMax': 0.96733177, 'yMin': 0.56486815, 'yMax': 0.7271576}, {'timeOffset': '0.300s', 'xMin': 0.6559273, 'xMax': 0.922594, 'yMin': 0.67381793, 'yMax': 0.87236154}, {'timeOffset': '0.400s', 'xMin': 0.52804255, 'xMax': 0.8188649, 'yMin': 0.7136257, 'yMax': 0.9117379}, {'timeOffset': '0.500s', 'xMin': 0.38841337, 'xMax': 0.68499833, 'yMin': 0.7241354, 'yMax': 0.9293221}, {'timeOffset': '0.600s', 'xMin': 0.22706872, 'xMax': 0.5414151, 'yMin': 0.71608, 'yMax': 0.93481314}]}, {'id': '3767468895993069568', 'displayName': 'sedan', 'timeSegmentStart': '3s', 'timeSegmentEnd': '4s', 'confidence': 0.49189684, 'frames': [{'timeOffset': '3s', 'xMin': 0.7998861, 'xMax': 0.9865881, 'yMin': 0.6979363, 'yMax': 0.8698516}, {'timeOffset': '3.100s', 'xMin': 0.7835889, 'xMax': 0.9862553, 'yMin': 0.7052906, 'yMax': 0.8742318}, {'timeOffset': '3.200s', 'xMin': 0.7530042, 'xMax': 0.9377618, 'yMin': 0.5567795, 'yMax': 0.6924586}, {'timeOffset': '3.300s', 'xMin': 0.6876858, 'xMax': 0.8742672, 'yMin': 0.5218122, 'yMax': 0.6595952}, {'timeOffset': '3.400s', 'xMin': 0.62348735, 'xMax': 0.8095002, 'yMin': 0.5097083, 'yMax': 0.6480995}, {'timeOffset': ..."
- ]
- },
- {
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "cleanup:mbsdk"
- },
- "outputs": [],
- "source": [
- "import os\n",
- "\n",
- "delete_bucket = False\n",
- "\n",
- "# Delete the dataset using the Vertex dataset object\n",
- "\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",
- "\n",
- "dag.delete()\n",
- "\n",
- "# Delete the batch prediction job using the Vertex batch prediction object\n",
- "batch_predict_job.delete()\n",
- "\n",
- "if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
- " ! gsutil rm -r $BUCKET_URI"
- ]
- }
- ],
- "metadata": {
- "colab": {
- "name": "sdk-automl-object-tracking-batch-prediction.ipynb",
- "toc_visible": true
- },
- "kernelspec": {
- "display_name": "Python 3",
- "name": "python3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
diff --git a/notebooks/official/migration/sdk-automl-text-classification-batch-prediction.ipynb b/notebooks/official/migration/sdk-automl-text-classification-batch-prediction.ipynb
deleted file mode 100644
index 8e340a761..000000000
--- a/notebooks/official/migration/sdk-automl-text-classification-batch-prediction.ipynb
+++ /dev/null
@@ -1,1212 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "copyright"
- },
- "outputs": [],
- "source": [
- "# Copyright 2021 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 License 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": "90a6064798e9"
- },
- "source": [
- "Starting on September 15, 2024, you can only customize classification, entity extraction, and sentiment analysis models by moving to Vertex AI Gemini prompts and tuning. Training or updating models for Vertex AI AutoML for Text classification, entity extraction, and sentiment analysis objectives will no longer be available. You can continue using existing Vertex AI AutoML Text objectives until June 15, 2025. For more information about how Gemini offers enhanced user experience through improved prompting capabilities, see \n",
- "[Introduction to tuning](https://cloud.google.com/vertex-ai/generative-ai/docs/models/tune-gemini-overview)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "title:migration,new"
- },
- "source": [
- "# Vertex AI Migration : AutoML Text Classification\n",
- "\n",
- "\n",
- " \n",
- "\n",
- " Run in Colab\n",
- " \n",
- " | \n",
- " \n",
- "\n",
- " \n",
- " View on GitHub\n",
- " \n",
- " | \n",
- " \n",
- "\n",
- " \n",
- " Open in Vertex AI Workbench\n",
- " \n",
- " | \n",
- "
\n",
- "
"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "cc58cf7bf111"
- },
- "source": [
- "## Overview\n",
- "\n",
- "This notebook demonstrates how to create an AutoML Text Classification Model, with a Vertex AI text dataset, and how to serve the model for batch prediction. It requires you provide a bucket where the dataset will be stored.\n",
- "\n",
- "Note: you may incur charges for training, prediction, storage or usage of other GCP products in connection with testing this SDK.\n",
- "\n",
- "Learn more about [Migrate to Vertex AI](https://cloud.google.com/vertex-ai/docs/start/migrating-to-vertex-ai) and [Classification for text data](https://cloud.google.com/vertex-ai/docs/training-overview#classification_for_text)."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "8ea675435b19"
- },
- "source": [
- "### Objective\n",
- "\n",
- "The objective of this notebook is to build a AutoML Text Classification Model. The following steps have been followed:\n",
- "This tutorial uses the following Google Cloud ML services :\n",
- "\n",
- "* Vertex AI Dataset resource\n",
- "* AutoML Training\n",
- "* Vertex AI Model resource\n",
- "* Vertex AI Batch Prediction\n",
- "\n",
- "The steps performed include the following:\n",
- "\n",
- "* Set your task name, and GCS prefix\n",
- "* Copy AutoML text demo train data for creating managed dataset\n",
- "* Create a dataset on Vertex AI.\n",
- "* Configure a training job\n",
- "* Launch a training job and create a model on Vertex AI\n",
- "* Copy AutoML Text Demo Prediction Data for creating batch prediction job\n",
- "* Perform batch prediction job on the model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "dd81fd5c3454"
- },
- "source": [
- "### Dataset\n",
- "\n",
- "The dataset used for this tutorial is the [Happy Moments dataset](https://www.kaggle.com/ritresearch/happydb) from [Kaggle Datasets](https://www.kaggle.com/ritresearch/happydb). The version of the dataset you will use in this tutorial is stored in a public Cloud Storage bucket."
- ]
- },
- {
- "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": "install_aip:mbsdk"
- },
- "source": [
- "## Installation\n",
- "\n",
- "Install the packages required for executing this notebook."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "install_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "! pip3 install --upgrade --quiet google-cloud-aiplatform \\\n",
- " google-cloud-storage \\\n",
- " tensorflow "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "restart"
- },
- "source": [
- "### Colab only: Uncomment the following cell to restart the kernel"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "D-ZBOjErv5mM"
- },
- "outputs": [],
- "source": [
- "# Automatically restart kernel after installs so that your environment can access the new packages\n",
- "# import IPython\n",
- "\n",
- "# app = IPython.Application.instance()\n",
- "# app.kernel.do_shutdown(True)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "before_you_begin:nogpu"
- },
- "source": [
- "## Before you begin"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "before_you_begin:nogpu"
- },
- "source": [
- "### Set your project ID\n",
- "\n",
- "**If you don't know your project ID**, try the following:\n",
- "* Run `gcloud config list`.\n",
- "* Run `gcloud projects list`.\n",
- "* See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "set_project_id"
- },
- "outputs": [],
- "source": [
- "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
- "\n",
- "# Set the project id\n",
- "! gcloud config set project {PROJECT_ID}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "region"
- },
- "source": [
- "#### Region\n",
- "\n",
- "You can also change the `REGION` variable used by Vertex AI. Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "region"
- },
- "outputs": [],
- "source": [
- "REGION = \"us-central1\" # @param {type: \"string\"}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "timestamp"
- },
- "source": [
- "#### UUID\n",
- "\n",
- "If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial.\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "timestamp"
- },
- "outputs": [],
- "source": [
- "import random\n",
- "import string\n",
- "\n",
- "\n",
- "# Generate a uuid of a specifed length(default=8)\n",
- "def generate_uuid(length: int = 8) -> str:\n",
- " return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
- "\n",
- "\n",
- "UUID = generate_uuid()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "gcp_authenticate"
- },
- "source": [
- "### Authenticate your Google Cloud account\n",
- "\n",
- "Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below.\n",
- "\n",
- "**1. Vertex AI Workbench**\n",
- "* Do nothing as you are already authenticated.\n",
- "\n",
- "**2. Local JupyterLab instance, uncomment and run:**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "ce6043da7b33"
- },
- "outputs": [],
- "source": [
- "# ! gcloud auth login"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "0367eac06a10"
- },
- "source": [
- "**3. Colab, uncomment and run:**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "21ad4dbb4a61"
- },
- "outputs": [],
- "source": [
- "# from google.colab import auth\n",
- "# auth.authenticate_user()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "c13224697bfb"
- },
- "source": [
- "**4. Service account or other**\n",
- "* See how to grant Cloud Storage permissions to your service account at https://cloud.google.com/storage/docs/gsutil/commands/iam#ch-examples."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "bucket:mbsdk"
- },
- "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": "bucket"
- },
- "outputs": [],
- "source": [
- "BUCKET_URI = f\"gs://your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_bucket"
- },
- "source": [
- "**Only 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": "create_bucket"
- },
- "outputs": [],
- "source": [
- "! gsutil mb -l {REGION} {BUCKET_URI}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "setup_vars"
- },
- "source": [
- "### Set up variables\n",
- "\n",
- "Next, set up some variables used throughout the tutorial.\n",
- "### Import libraries and define constants"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "import_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "import google.cloud.aiplatform as aip"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "init_aip:mbsdk"
- },
- "source": [
- "## Initialize Vertex AI SDK for Python\n",
- "\n",
- "Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "init_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "aip.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
- ]
- },
- {
- "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:happydb,csv,tcn"
- },
- "outputs": [],
- "source": [
- "IMPORT_FILE = \"gs://cloud-ml-data/NL-classification/happiness.csv\""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "quick_peek:csv"
- },
- "source": [
- "#### Quick peek at your data\n",
- "\n",
- "This tutorial uses a version of the Happy Moments dataset that is stored in a public Cloud Storage bucket, using a CSV index file.\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": "quick_peek:csv"
- },
- "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:text,tcn"
- },
- "source": [
- "### Create the Dataset\n",
- "\n",
- "### [datasets.create-dataset-api](https://cloud.google.com/vertex-ai/docs/datasets/create-dataset-api)\n",
- "\n",
- "Next, create the `Dataset` resource using the `create` method for the `TextDataset` 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": "create_dataset:text,tcn"
- },
- "outputs": [],
- "source": [
- "dataset = aip.TextDataset.create(\n",
- " display_name=\"Happy Moments\" + \"_\" + UUID,\n",
- " gcs_source=[IMPORT_FILE],\n",
- " import_schema_uri=aip.schema.dataset.ioformat.text.single_label_classification,\n",
- ")\n",
- "\n",
- "print(dataset.resource_name)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_dataset:text,tcn"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Creating TextDataset\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Create TextDataset backing LRO: projects/759209241365/locations/us-central1/datasets/3704325042721521664/operations/3193181053544038400\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:TextDataset created. Resource name: projects/759209241365/locations/us-central1/datasets/3704325042721521664\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:To use this TextDataset in another session:\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:ds = aiplatform.TextDataset('projects/759209241365/locations/us-central1/datasets/3704325042721521664')\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Importing TextDataset data: projects/759209241365/locations/us-central1/datasets/3704325042721521664\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Import TextDataset data backing LRO: projects/759209241365/locations/us-central1/datasets/3704325042721521664/operations/5152246891450204160\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:TextDataset data imported. Resource name: projects/759209241365/locations/us-central1/datasets/3704325042721521664\n",
- " projects/759209241365/locations/us-central1/datasets/3704325042721521664"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_automl_pipeline:text,tcn"
- },
- "source": [
- "## Train a model\n",
- "\n",
- "### [training.automl-api](https://cloud.google.com/vertex-ai/docs/training/automl-api)\n",
- "\n",
- "### 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 `AutoMLTextTrainingJob` 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`: A text classification model.\n",
- " - `sentiment`: A text sentiment analysis model.\n",
- " - `extraction`: A text entity extraction model.\n",
- "- `multi_label`: If a classification task, whether single (False) or multi-labeled (True).\n",
- "- `sentiment_max`: If a sentiment analysis task, the maximum sentiment value.\n",
- "\n",
- "The instantiated object is the DAG (directed acyclic graph) for the training pipeline."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "create_automl_pipeline:text,tcn"
- },
- "outputs": [],
- "source": [
- "dag = aip.AutoMLTextTrainingJob(\n",
- " display_name=\"happydb_\" + UUID,\n",
- " prediction_type=\"classification\",\n",
- " multi_label=False,\n",
- ")\n",
- "\n",
- "print(dag)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_automl_pipeline:text,tcn"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "run_automl_pipeline:text"
- },
- "source": [
- "#### Run the training pipeline\n",
- "\n",
- "Next, you run the DAG 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",
- "\n",
- "The `run` method when completed returns the `Model` resource.\n",
- "\n",
- "The execution of the training pipeline will take up a few hours."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "run_automl_pipeline:text"
- },
- "outputs": [],
- "source": [
- "model = dag.run(\n",
- " dataset=dataset,\n",
- " model_display_name=\"happydb_\" + UUID,\n",
- " training_fraction_split=0.8,\n",
- " validation_fraction_split=0.1,\n",
- " test_fraction_split=0.1,\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "run_automl_pipeline:text"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.training_jobs:View Training:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/training/8859754745456230400?project=759209241365\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " ...\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob run completed. Resource name: projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400\n",
- " INFO:google.cloud.aiplatform.training_jobs:Model available at projects/759209241365/locations/us-central1/models/6389525951797002240"
- ]
- },
- {
- "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.\n",
- "\n",
- "### [projects.locations.models.evaluations.list](https://cloud.devsite.corp.google.com/ai-platform-unified/docs/reference/rest/v1beta1/projects.locations.models.evaluations/list)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "evaluate_the_model:mbsdk"
- },
- "outputs": [],
- "source": [
- "# Get model resource ID\n",
- "models = aip.Model.list(filter=\"display_name=happydb_\" + UUID)\n",
- "\n",
- "# Get a reference to the Model Service client\n",
- "client_options = {\"api_endpoint\": f\"{REGION}-aiplatform.googleapis.com\"}\n",
- "model_service_client = aip.gapic.ModelServiceClient(client_options=client_options)\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": "evaluate_the_model:mbsdk"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " name: \"projects/759209241365/locations/us-central1/models/623915674158235648/evaluations/4280507618583117824\"\n",
- " metrics_schema_uri: \"gs://google-cloud-aiplatform/schema/modelevaluation/classification_metrics_1.0.0.yaml\"\n",
- " metrics {\n",
- " struct_value {\n",
- " fields {\n",
- " key: \"auPrc\"\n",
- " value {\n",
- " number_value: 0.9891107\n",
- " }\n",
- " }\n",
- " fields {\n",
- " key: \"confidenceMetrics\"\n",
- " value {\n",
- " list_value {\n",
- " values {\n",
- " struct_value {\n",
- " fields {\n",
- " key: \"precision\"\n",
- " value {\n",
- " number_value: 0.2\n",
- " }\n",
- " }\n",
- " fields {\n",
- " key: \"recall\"\n",
- " value {\n",
- " number_value: 1.0\n",
- " }\n",
- " }\n",
- " }\n",
- " }"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batchpredictionjobs_create:migration,new,mbsdk"
- },
- "source": [
- "## Make batch predictions\n",
- "\n",
- "### [predictions.batch-prediction](https://cloud.google.com/vertex-ai/docs/predictions/batch-predictions)"
- ]
- },
- {
- "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 -- you just want to demonstrate how to make a prediction."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "get_test_items:automl,tcn,csv"
- },
- "outputs": [],
- "source": [
- "test_items = ! gsutil cat $IMPORT_FILE | head -n2\n",
- "if len(test_items[0]) == 3:\n",
- " _, test_item_1, test_label_1 = str(test_items[0]).split(\",\")\n",
- " _, test_item_2, test_label_2 = str(test_items[1]).split(\",\")\n",
- "else:\n",
- " test_item_1, test_label_1 = str(test_items[0]).split(\",\")\n",
- " test_item_2, test_label_2 = str(test_items[1]).split(\",\")\n",
- "\n",
- "print(test_item_1, test_label_1)\n",
- "print(test_item_2, test_label_2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_batch_file:automl,text"
- },
- "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 only be in JSONL format. 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 file with the text item.\n",
- "- `mime_type`: The content type. In our example, it is a `text` file.\n",
- "\n",
- "For example:\n",
- "\n",
- " {'content': '[your-bucket]/file1.txt', 'mime_type': 'text'}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "make_batch_file:automl,text"
- },
- "outputs": [],
- "source": [
- "import json\n",
- "import os\n",
- "\n",
- "import tensorflow as tf\n",
- "\n",
- "gcs_test_item_1 = BUCKET_URI + \"/test1.txt\"\n",
- "with tf.io.gfile.GFile(gcs_test_item_1, \"w\") as f:\n",
- " f.write(test_item_1 + \"\\n\")\n",
- "gcs_test_item_2 = BUCKET_URI + \"/test2.txt\"\n",
- "with tf.io.gfile.GFile(gcs_test_item_2, \"w\") as f:\n",
- " f.write(test_item_2 + \"\\n\")\n",
- "\n",
- "gcs_input_uri = BUCKET_URI + \"/test.jsonl\"\n",
- "with tf.io.gfile.GFile(gcs_input_uri, \"w\") as f:\n",
- " data = {\"content\": gcs_test_item_1, \"mime_type\": \"text/plain\"}\n",
- " f.write(json.dumps(data) + \"\\n\")\n",
- " data = {\"content\": gcs_test_item_2, \"mime_type\": \"text/plain\"}\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",
- "- `sync`: If set to True, the call will block while waiting for the asynchronous batch job to complete."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "batch_request:mbsdk"
- },
- "outputs": [],
- "source": [
- "batch_predict_job = model.batch_predict(\n",
- " job_display_name=\"happydb_\" + UUID,\n",
- " gcs_source=gcs_input_uri,\n",
- " gcs_destination_prefix=BUCKET_URI,\n",
- " sync=False,\n",
- ")\n",
- "\n",
- "print(batch_predict_job)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batch_request:mbsdk"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.jobs:Creating BatchPredictionJob\n",
- " is waiting for upstream dependencies to complete.\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob created. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296\n",
- " INFO:google.cloud.aiplatform.jobs:To use this BatchPredictionJob in another session:\n",
- " INFO:google.cloud.aiplatform.jobs:bpj = aiplatform.BatchPredictionJob('projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296')\n",
- " INFO:google.cloud.aiplatform.jobs:View Batch Prediction Job:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/batch-predictions/5110965452507447296?project=759209241365\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296 current state:\n",
- " JobState.JOB_STATE_RUNNING"
- ]
- },
- {
- "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": "batch_request_wait:mbsdk"
- },
- "outputs": [],
- "source": [
- "batch_predict_job.wait()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batch_request_wait:mbsdk"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob created. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328\n",
- " INFO:google.cloud.aiplatform.jobs:To use this BatchPredictionJob in another session:\n",
- " INFO:google.cloud.aiplatform.jobs:bpj = aiplatform.BatchPredictionJob('projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328')\n",
- " INFO:google.cloud.aiplatform.jobs:View Batch Prediction Job:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/batch-predictions/181835033978339328?project=759209241365\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_SUCCEEDED\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob run completed. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_batch_prediction:mbsdk,tcn"
- },
- "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",
- " - `confidences`: The predicted confidence, between 0 and 1, per class label."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "get_batch_prediction:mbsdk,tcn"
- },
- "outputs": [],
- "source": [
- "import json\n",
- "\n",
- "import tensorflow as tf\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)\n",
- " break"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_batch_prediction:mbsdk,tcn"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " {'instance': {'content': 'gs://andy-1234-221921aip-20210803210202/test2.txt', 'mimeType': 'text/plain'}, 'prediction': {'ids': ['681905016918769664', '3564208778435887104', '8175894796863275008', '5538192790107717632', '5870051787649581056', '3232349780894023680', '926506771680329728'], 'displayNames': ['affection', 'achievement', 'bonding', 'enjoy_the_moment', 'nature', 'leisure', 'exercise'], 'confidences': [0.9977309, 0.0017838771, 0.0002530971, 0.00014939539, 4.747714e-05, 2.6297073e-05, 8.965492e-06]}}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_online_predictions:migration"
- },
- "source": [
- "## Make online predictions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "deploy_model:migration,new,mbsdk"
- },
- "source": [
- "### [predictions.deploy-model-api](https://cloud.google.com/vertex-ai/docs/predictions/deploy-model-api)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "deploy_model:mbsdk,automatic"
- },
- "source": [
- "## Deploy the model\n",
- "\n",
- "Next, deploy your model for online prediction. To deploy the model, you invoke the `deploy` method."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "deploy_model:mbsdk,automatic"
- },
- "outputs": [],
- "source": [
- "endpoint = model.deploy()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "deploy_model:mbsdk,automatic"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.models:Creating Endpoint\n",
- " INFO:google.cloud.aiplatform.models:Create Endpoint backing LRO: projects/759209241365/locations/us-central1/endpoints/4867177336350441472/operations/4087251132693348352\n",
- " INFO:google.cloud.aiplatform.models:Endpoint created. Resource name: projects/759209241365/locations/us-central1/endpoints/4867177336350441472\n",
- " INFO:google.cloud.aiplatform.models:To use this Endpoint in another session:\n",
- " INFO:google.cloud.aiplatform.models:endpoint = aiplatform.Endpoint('projects/759209241365/locations/us-central1/endpoints/4867177336350441472')\n",
- " INFO:google.cloud.aiplatform.models:Deploying model to Endpoint : projects/759209241365/locations/us-central1/endpoints/4867177336350441472\n",
- " INFO:google.cloud.aiplatform.models:Deploy Endpoint model backing LRO: projects/759209241365/locations/us-central1/endpoints/4867177336350441472/operations/1691336130932244480\n",
- " INFO:google.cloud.aiplatform.models:Endpoint model deployed. Resource name: projects/759209241365/locations/us-central1/endpoints/4867177336350441472"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "endpoints_predict:migration,new,mbsdk"
- },
- "source": [
- "## Online prediction of automl\n",
- "### [predictions.online-prediction-automl](https://cloud.google.com/vertex-ai/docs/predictions/online-predictions-automl)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_test_item"
- },
- "source": [
- "### Get test item\n",
- "\n",
- "You will use an arbitrary example out of the dataset as a test item. Don't be concerned that the example was likely used in training the model -- you just want to demonstrate how to make a prediction."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "get_test_item:automl,tcn,csv"
- },
- "outputs": [],
- "source": [
- "test_item = ! gsutil cat $IMPORT_FILE | head -n1\n",
- "if len(test_item[0]) == 3:\n",
- " _, test_item, test_label = str(test_item[0]).split(\",\")\n",
- "else:\n",
- " test_item, test_label = str(test_item[0]).split(\",\")\n",
- "\n",
- "print(test_item, test_label)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "predict_request:mbsdk,tcn"
- },
- "source": [
- "### Make the prediction\n",
- "\n",
- "Now that your `Model` resource is deployed to an `Endpoint` resource, you can do online predictions by sending prediction requests to the `Endpoint` resource.\n",
- "\n",
- "#### Request\n",
- "\n",
- "The format of each instance is:\n",
- "\n",
- " { 'content': text_string }\n",
- "\n",
- "Since the predict() method can take multiple items (instances), send your single test item as a list of one test item.\n",
- "\n",
- "#### Response\n",
- "\n",
- "The response from the predict() call is a Python dictionary with the following entries:\n",
- "\n",
- "- `ids`: The internal assigned unique identifiers for each prediction request.\n",
- "- `displayNames`: The class names for each class label.\n",
- "- `confidences`: The predicted confidence, between 0 and 1, per class label.\n",
- "- `deployed_model_id`: The Vertex AI identifier for the deployed `Model` resource which did the predictions."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "predict_request:mbsdk,tcn"
- },
- "outputs": [],
- "source": [
- "instances_list = [{\"content\": test_item}]\n",
- "\n",
- "prediction = endpoint.predict(instances_list)\n",
- "print(prediction)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "predict_request:mbsdk,tcn"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " Prediction(predictions=[{'confidences': [0.9807877540588379, 0.0029202369041740894, 0.001903864904306829, 0.013396155089139938, 0.0002868965675588697, 0.00017845185357145965, 0.0005265594809316099], 'displayNames': ['affection', 'achievement', 'enjoy_the_moment', 'bonding', 'leisure', 'nature', 'exercise'], 'ids': ['1022137895017775104', '6210284665748586496', '3327980904231469056', '8516127674962280448', '7939666922658856960', '3904441656534892544', '5633823913445163008']}], deployed_model_id='8719822099612434432', explanations=None)\n",
- "affection"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "undeploy_model:mbsdk"
- },
- "source": [
- "## Undeploy the model\n",
- "\n",
- "When you are done doing predictions, you undeploy the model from the `Endpoint` resouce. This deprovisions all compute resources and ends billing for the deployed model."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "undeploy_model:mbsdk"
- },
- "outputs": [],
- "source": [
- "endpoint.undeploy_all()"
- ]
- },
- {
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "cleanup:mbsdk"
- },
- "outputs": [],
- "source": [
- "# 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 endpoint using the Vertex endpoint object\n",
- "endpoint.delete()\n",
- "\n",
- "# Delete the batch prediction job using the Vertex batch prediction object\n",
- "batch_predict_job.delete()\n",
- "\n",
- "# Delete the bucket\n",
- "delete_bucket = False\n",
- "if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
- " ! gsutil -m rm -r $BUCKET_URI"
- ]
- }
- ],
- "metadata": {
- "colab": {
- "name": "sdk-automl-text-classification-batch-prediction.ipynb",
- "toc_visible": true
- },
- "kernelspec": {
- "display_name": "Python 3",
- "name": "python3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
diff --git a/notebooks/official/migration/sdk-automl-text-entity-extraction-batch-prediction.ipynb b/notebooks/official/migration/sdk-automl-text-entity-extraction-batch-prediction.ipynb
deleted file mode 100644
index 672897ef6..000000000
--- a/notebooks/official/migration/sdk-automl-text-entity-extraction-batch-prediction.ipynb
+++ /dev/null
@@ -1,1263 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "copyright"
- },
- "outputs": [],
- "source": [
- "# Copyright 2021 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 License 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": "90a6064798e9"
- },
- "source": [
- "Starting on September 15, 2024, you can only customize classification, entity extraction, and sentiment analysis models by moving to Vertex AI Gemini prompts and tuning. Training or updating models for Vertex AI AutoML for Text classification, entity extraction, and sentiment analysis objectives will no longer be available. You can continue using existing Vertex AI AutoML Text objectives until June 15, 2025. For more information about how Gemini offers enhanced user experience through improved prompting capabilities, see \n",
- "[Introduction to tuning](https://cloud.google.com/vertex-ai/generative-ai/docs/models/tune-gemini-overview)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "title:migration,new"
- },
- "source": [
- "# Vertex AI: Vertex AI Migration: AutoML Text Entity Extraction\n",
- "\n",
- "\n",
- " \n",
- "\n",
- " Run in Colab\n",
- " \n",
- " | \n",
- " \n",
- "\n",
- " \n",
- " View on GitHub\n",
- " \n",
- " | \n",
- " \n",
- "\n",
- " \n",
- " Open in Vertex AI Workbench\n",
- " \n",
- " | \n",
- "
\n",
- "
"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "2277f661a148"
- },
- "source": [
- "## Overview\n",
- "\n",
- "This notebook demonstrates how to create an AutoML Text Entity Extraction model, with a Vertex AI ncbi disease research dataset, and how to serve the model for batch prediction. It requires you provide a bucket where the dataset will be stored.\n",
- "\n",
- "Note: you may incur charges for training, prediction, storage or usage of other Google Cloud products in connection with testing this SDK.\n",
- "\n",
- "Learn more about [Migrate to Vertex AI](https://cloud.google.com/vertex-ai/docs/start/migrating-to-vertex-ai) and [Entity extraction for text data](https://cloud.google.com/vertex-ai/docs/training-overview#entity_extraction_for_text).\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "f926ec7acab3"
- },
- "source": [
- "### Objective\n",
- "\n",
- "The objective of this notebook is to build a AutoML Text Entity Extraction model. The following steps have been followed:\n",
- "This tutorial uses the following Google Cloud ML services :\n",
- "\n",
- "* Vertex AI Dataset resource\n",
- "* AutoML Training\n",
- "* Vertex AI Model resource\n",
- "* Vertex AI Batch Prediction\n",
- "\n",
- "The steps performed include the following:\n",
- "\n",
- "* Set your task name, and GCS prefix\n",
- "* Copy AutoML video demo train data for creating managed dataset\n",
- "* Create a dataset on Vertex AI.\n",
- "* Configure a training job\n",
- "* Launch a training job and create a model on Vertex AI\n",
- "* Copy AutoML Video Demo Prediction Data for creating batch prediction job\n",
- "* Perform batch prediction job on the model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "dataset:biomedical,ten"
- },
- "source": [
- "### Dataset\n",
- "\n",
- "The dataset used for this tutorial is the [NCBI Disease Research Abstracts dataset](https://www.ncbi.nlm.nih.gov/CBBresearch/Dogan/DISEASE/) from [National Center for Biotechnology Information](https://www.ncbi.nlm.nih.gov/). The version of the dataset you will use in this tutorial is stored in a public Cloud Storage bucket."
- ]
- },
- {
- "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": "install_aip:mbsdk"
- },
- "source": [
- "## Installation\n",
- "\n",
- "Install the packages required for executing this notebook."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "install_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "import os\n",
- "\n",
- "! pip3 install --upgrade --quiet google-cloud-aiplatform \\\n",
- " cuda-python \\\n",
- " google-cloud-storage\n",
- "if os.getenv(\"IS_TESTING\"):\n",
- " ! pip3 install --upgrade --quiet tensorflow "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "restart"
- },
- "source": [
- "### Colab only: Uncomment the following cell to restart the kernel"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "D-ZBOjErv5mM"
- },
- "outputs": [],
- "source": [
- "# Automatically restart kernel after installs so that your environment can access the new packages\n",
- "# import IPython\n",
- "\n",
- "# app = IPython.Application.instance()\n",
- "# app.kernel.do_shutdown(True)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "before_you_begin:nogpu"
- },
- "source": [
- "## Before you begin"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "before_you_begin:nogpu"
- },
- "source": [
- "### Set your project ID\n",
- "\n",
- "**If you don't know your project ID**, try the following:\n",
- "* Run `gcloud config list`.\n",
- "* Run `gcloud projects list`.\n",
- "* See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "set_project_id"
- },
- "outputs": [],
- "source": [
- "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
- "\n",
- "# Set the project id\n",
- "! gcloud config set project {PROJECT_ID}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "region"
- },
- "source": [
- "#### Region\n",
- "\n",
- "You can also change the `REGION` variable used by Vertex AI. Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "region"
- },
- "outputs": [],
- "source": [
- "REGION = \"us-central1\" # @param {type: \"string\"}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "timestamp"
- },
- "source": [
- "#### UUID\n",
- "\n",
- "If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "timestamp"
- },
- "outputs": [],
- "source": [
- "import random\n",
- "import string\n",
- "\n",
- "\n",
- "# Generate a uuid of a specifed length(default=8)\n",
- "def generate_uuid(length: int = 8) -> str:\n",
- " return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
- "\n",
- "\n",
- "UUID = generate_uuid()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "gcp_authenticate"
- },
- "source": [
- "### Authenticate your Google Cloud account\n",
- "\n",
- "Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below.\n",
- "\n",
- "**1. Vertex AI Workbench**\n",
- "* Do nothing as you are already authenticated.\n",
- "\n",
- "**2. Local JupyterLab instance, uncomment and run:**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "ce6043da7b33"
- },
- "outputs": [],
- "source": [
- "# ! gcloud auth login"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "0367eac06a10"
- },
- "source": [
- "**3. Colab, uncomment and run:**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "21ad4dbb4a61"
- },
- "outputs": [],
- "source": [
- "# from google.colab import auth\n",
- "# auth.authenticate_user()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "c13224697bfb"
- },
- "source": [
- "**4. Service account or other**\n",
- "* See how to grant Cloud Storage permissions to your service account at https://cloud.google.com/storage/docs/gsutil/commands/iam#ch-examples."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "bucket:mbsdk"
- },
- "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": "bucket"
- },
- "outputs": [],
- "source": [
- "BUCKET_URI = f\"gs://your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_bucket"
- },
- "source": [
- "**Only 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": "create_bucket"
- },
- "outputs": [],
- "source": [
- "! gsutil mb -l {REGION} {BUCKET_URI}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "setup_vars"
- },
- "source": [
- "### Set up variables\n",
- "\n",
- "Next, set up some variables used throughout the tutorial.\n",
- "### Import libraries and define constants"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "import_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "import google.cloud.aiplatform as aip"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "init_aip:mbsdk"
- },
- "source": [
- "## Initialize Vertex AI SDK for Python\n",
- "\n",
- "Initialize the Vertex SDK for Python for your project and corresponding bucket."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "init_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "aip.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "import_file:u_dataset,jsonl"
- },
- "source": [
- "#### Location of Cloud Storage training data.\n",
- "\n",
- "Now set the variable `IMPORT_FILE` to the location of the JSONL index file in Cloud Storage."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "import_file:biomedical,jsonl,ten"
- },
- "outputs": [],
- "source": [
- "IMPORT_FILE = \"gs://cloud-samples-data/language/ucaip_ten_dataset.jsonl\""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "quick_peek:jsonl"
- },
- "source": [
- "#### Quick peek at your data\n",
- "\n",
- "This tutorial uses a version of the NCBI Biomedical dataset that is stored in a public Cloud Storage bucket, using a JSONL index file.\n",
- "\n",
- "Start by doing a quick peek at the data. You count the number of examples by counting the number of objects in a JSONL index file (`wc -l`) and then peek at the first few rows."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "quick_peek:jsonl"
- },
- "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_a_dataset:migration"
- },
- "source": [
- "## Create a dataset"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "datasets_create:migration,new,mbsdk"
- },
- "source": [
- "### [datasets.create-dataset-api](https://cloud.google.com/vertex-ai/docs/datasets/create-dataset-api)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_dataset:text,ten"
- },
- "source": [
- "### Create the Dataset\n",
- "\n",
- "Next, create the `Dataset` resource using the `create` method for the `TextDataset` 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": "create_dataset:text,ten"
- },
- "outputs": [],
- "source": [
- "dataset = aip.TextDataset.create(\n",
- " display_name=\"NCBI Biomedical\" + \"_\" + UUID,\n",
- " gcs_source=[IMPORT_FILE],\n",
- " import_schema_uri=aip.schema.dataset.ioformat.text.extraction,\n",
- ")\n",
- "\n",
- "print(dataset.resource_name)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_dataset:text,ten"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Creating TextDataset\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Create TextDataset backing LRO: projects/759209241365/locations/us-central1/datasets/3704325042721521664/operations/3193181053544038400\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:TextDataset created. Resource name: projects/759209241365/locations/us-central1/datasets/3704325042721521664\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:To use this TextDataset in another session:\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:ds = aiplatform.TextDataset('projects/759209241365/locations/us-central1/datasets/3704325042721521664')\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Importing TextDataset data: projects/759209241365/locations/us-central1/datasets/3704325042721521664\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Import TextDataset data backing LRO: projects/759209241365/locations/us-central1/datasets/3704325042721521664/operations/5152246891450204160\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:TextDataset data imported. Resource name: projects/759209241365/locations/us-central1/datasets/3704325042721521664\n",
- " projects/759209241365/locations/us-central1/datasets/3704325042721521664"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "train_a_model:migration"
- },
- "source": [
- "## Train a model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "trainingpipelines_create:migration,new,mbsdk"
- },
- "source": [
- "### [training.automl-api](https://cloud.google.com/vertex-ai/docs/training/automl-api)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_automl_pipeline:text,ten"
- },
- "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 `AutoMLTextTrainingJob` 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`: A text classification model.\n",
- " - `sentiment`: A text sentiment analysis model.\n",
- " - `extraction`: A text entity extraction model.\n",
- "- `multi_label`: If a classification task, whether single (False) or multi-labeled (True).\n",
- "- `sentiment_max`: If a sentiment analysis task, the maximum sentiment value.\n",
- "\n",
- "The instantiated object is the DAG (directed acyclic graph) for the training pipeline."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "create_automl_pipeline:text,ten"
- },
- "outputs": [],
- "source": [
- "dag = aip.AutoMLTextTrainingJob(\n",
- " display_name=\"biomedical_\" + UUID, prediction_type=\"extraction\"\n",
- ")\n",
- "\n",
- "print(dag)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_automl_pipeline:text,ten"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "run_automl_pipeline:text"
- },
- "source": [
- "#### Run the training pipeline\n",
- "\n",
- "Next, you run the DAG 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",
- "\n",
- "The `run` method when completed returns the `Model` resource.\n",
- "\n",
- "The execution of the training pipeline will take upto 20 minutes."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "run_automl_pipeline:text"
- },
- "outputs": [],
- "source": [
- "model = dag.run(\n",
- " dataset=dataset,\n",
- " model_display_name=\"biomedical_\" + UUID,\n",
- " training_fraction_split=0.8,\n",
- " validation_fraction_split=0.1,\n",
- " test_fraction_split=0.1,\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "run_automl_pipeline:text"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.training_jobs:View Training:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/training/8859754745456230400?project=759209241365\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " ...\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob run completed. Resource name: projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400\n",
- " INFO:google.cloud.aiplatform.training_jobs:Model available at projects/759209241365/locations/us-central1/models/6389525951797002240"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "evaluate_the_model:migration"
- },
- "source": [
- "## Evaluate the model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "models_evaluations_list:migration,new"
- },
- "source": [
- "### [projects.locations.models.evaluations.list](https://cloud.devsite.corp.google.com/ai-platform-unified/docs/reference/rest/v1beta1/projects.locations.models.evaluations/list)"
- ]
- },
- {
- "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": "evaluate_the_model:mbsdk"
- },
- "outputs": [],
- "source": [
- "# Get model resource ID\n",
- "models = aip.Model.list(filter=\"display_name=biomedical_\" + UUID)\n",
- "\n",
- "# Get a reference to the Model Service client\n",
- "client_options = {\"api_endpoint\": f\"{REGION}-aiplatform.googleapis.com\"}\n",
- "model_service_client = aip.gapic.ModelServiceClient(client_options=client_options)\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": "evaluate_the_model:mbsdk"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " name: \"projects/759209241365/locations/us-central1/models/623915674158235648/evaluations/4280507618583117824\"\n",
- " metrics_schema_uri: \"gs://google-cloud-aiplatform/schema/modelevaluation/classification_metrics_1.0.0.yaml\"\n",
- " metrics {\n",
- " struct_value {\n",
- " fields {\n",
- " key: \"auPrc\"\n",
- " value {\n",
- " number_value: 0.9891107\n",
- " }\n",
- " }\n",
- " fields {\n",
- " key: \"confidenceMetrics\"\n",
- " value {\n",
- " list_value {\n",
- " values {\n",
- " struct_value {\n",
- " fields {\n",
- " key: \"precision\"\n",
- " value {\n",
- " number_value: 0.2\n",
- " }\n",
- " }\n",
- " fields {\n",
- " key: \"recall\"\n",
- " value {\n",
- " number_value: 1.0\n",
- " }\n",
- " }\n",
- " }\n",
- " }"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_batch_predictions:migration"
- },
- "source": [
- "## Make batch predictions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batchpredictionjobs_create:migration,new,mbsdk"
- },
- "source": [
- "### [predictions.batch-prediction](https://cloud.google.com/vertex-ai/docs/predictions/batch-predictions)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_test_items:automl,batch_prediction"
- },
- "source": [
- "### Make test items\n",
- "\n",
- "You will use synthetic data as a test data items. Don't be concerned that we are using synthetic data -- we just want to demonstrate how to make a prediction."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "make_test_items:automl,text,biomedical"
- },
- "outputs": [],
- "source": [
- "test_item_1 = 'Molecular basis of hexosaminidase A deficiency and pseudodeficiency in the Berks County Pennsylvania Dutch.\\tFollowing the birth of two infants with Tay-Sachs disease ( TSD ) , a non-Jewish , Pennsylvania Dutch kindred was screened for TSD carriers using the biochemical assay . A high frequency of individuals who appeared to be TSD heterozygotes was detected ( Kelly et al . , 1975 ) . Clinical and biochemical evidence suggested that the increased carrier frequency was due to at least two altered alleles for the hexosaminidase A alpha-subunit . We now report two mutant alleles in this Pennsylvania Dutch kindred , and one polymorphism . One allele , reported originally in a French TSD patient ( Akli et al . , 1991 ) , is a GT-- > AT transition at the donor splice-site of intron 9 . The second , a C-- > T transition at nucleotide 739 ( Arg247Trp ) , has been shown by Triggs-Raine et al . ( 1992 ) to be a clinically benign \" pseudodeficient \" allele associated with reduced enzyme activity against artificial substrate . Finally , a polymorphism [ G-- > A ( 759 ) ] , which leaves valine at codon 253 unchanged , is described'\n",
- "test_item_2 = \"Analysis of alkaptonuria (AKU) mutations and polymorphisms reveals that the CCC sequence motif is a mutational hot spot in the homogentisate 1,2 dioxygenase gene (HGO).\tWe recently showed that alkaptonuria ( AKU ) is caused by loss-of-function mutations in the homogentisate 1 , 2 dioxygenase gene ( HGO ) . Herein we describe haplotype and mutational analyses of HGO in seven new AKU pedigrees . These analyses identified two novel single-nucleotide polymorphisms ( INV4 + 31A-- > G and INV11 + 18A-- > G ) and six novel AKU mutations ( INV1-1G-- > A , W60G , Y62C , A122D , P230T , and D291E ) , which further illustrates the remarkable allelic heterogeneity found in AKU . Reexamination of all 29 mutations and polymorphisms thus far described in HGO shows that these nucleotide changes are not randomly distributed ; the CCC sequence motif and its inverted complement , GGG , are preferentially mutated . These analyses also demonstrated that the nucleotide substitutions in HGO do not involve CpG dinucleotides , which illustrates important differences between HGO and other genes for the occurrence of mutation at specific short-sequence motifs . Because the CCC sequence motifs comprise a significant proportion ( 34 . 5 % ) of all mutated bases that have been observed in HGO , we conclude that the CCC triplet is a mutational hot spot in HGO .\""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_batch_file:automl,text"
- },
- "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 only be in JSONL format. 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 file with the text item.\n",
- "- `mime_type`: The content type. In our example, it is a `text` file.\n",
- "\n",
- "For example:\n",
- "\n",
- " {'content': '[your-bucket]/file1.txt', 'mime_type': 'text'}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "make_batch_file:automl,text"
- },
- "outputs": [],
- "source": [
- "import json\n",
- "\n",
- "import tensorflow as tf\n",
- "\n",
- "gcs_test_item_1 = BUCKET_URI + \"/test1.txt\"\n",
- "with tf.io.gfile.GFile(gcs_test_item_1, \"w\") as f:\n",
- " f.write(test_item_1 + \"\\n\")\n",
- "gcs_test_item_2 = BUCKET_URI + \"/test2.txt\"\n",
- "with tf.io.gfile.GFile(gcs_test_item_2, \"w\") as f:\n",
- " f.write(test_item_2 + \"\\n\")\n",
- "\n",
- "gcs_input_uri = BUCKET_URI + \"/test.jsonl\"\n",
- "with tf.io.gfile.GFile(gcs_input_uri, \"w\") as f:\n",
- " data = {\"content\": gcs_test_item_1, \"mime_type\": \"text/plain\"}\n",
- " f.write(json.dumps(data) + \"\\n\")\n",
- " data = {\"content\": gcs_test_item_2, \"mime_type\": \"text/plain\"}\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",
- "- `sync`: If set to True, the call will block while waiting for the asynchronous batch job to complete."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "batch_request:mbsdk"
- },
- "outputs": [],
- "source": [
- "batch_predict_job = model.batch_predict(\n",
- " job_display_name=\"biomedical_\" + UUID,\n",
- " gcs_source=gcs_input_uri,\n",
- " gcs_destination_prefix=BUCKET_URI,\n",
- " sync=False,\n",
- ")\n",
- "\n",
- "print(batch_predict_job)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batch_request:mbsdk"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.jobs:Creating BatchPredictionJob\n",
- " is waiting for upstream dependencies to complete.\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob created. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296\n",
- " INFO:google.cloud.aiplatform.jobs:To use this BatchPredictionJob in another session:\n",
- " INFO:google.cloud.aiplatform.jobs:bpj = aiplatform.BatchPredictionJob('projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296')\n",
- " INFO:google.cloud.aiplatform.jobs:View Batch Prediction Job:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/batch-predictions/5110965452507447296?project=759209241365\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296 current state:\n",
- " JobState.JOB_STATE_RUNNING"
- ]
- },
- {
- "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": "batch_request_wait:mbsdk"
- },
- "outputs": [],
- "source": [
- "batch_predict_job.wait()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batch_request_wait:mbsdk"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob created. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328\n",
- " INFO:google.cloud.aiplatform.jobs:To use this BatchPredictionJob in another session:\n",
- " INFO:google.cloud.aiplatform.jobs:bpj = aiplatform.BatchPredictionJob('projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328')\n",
- " INFO:google.cloud.aiplatform.jobs:View Batch Prediction Job:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/batch-predictions/181835033978339328?project=759209241365\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_SUCCEEDED\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob run completed. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_batch_prediction:mbsdk,ten"
- },
- "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",
- " - `confidences`: The predicted confidence, between 0 and 1, per class label.\n",
- " - `textSegmentStartOffsets`: The character offset in the text to the start of the entity.\n",
- " - `textSegmentEndOffsets`: The character offset in the text to the end of the entity."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "get_batch_prediction:mbsdk,ten"
- },
- "outputs": [],
- "source": [
- "import json\n",
- "\n",
- "import tensorflow as tf\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)\n",
- " break"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_batch_prediction:mbsdk,ten"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " {'instance': {'content': 'gs://andy-1234-221921aip-20210811180202/test2.txt', 'mimeType': 'text/plain'}, 'prediction': {'ids': ['2208238262504390656', '2208238262504390656', '4827081445820334080', '4827081445820334080', '2208238262504390656', '4827081445820334080', '4827081445820334080'], 'displayNames': ['SpecificDisease', 'SpecificDisease', 'Modifier', 'Modifier', 'SpecificDisease', 'Modifier', 'Modifier'], 'textSegmentStartOffsets': ['208', '193', '381', '522', '670', '26', '12'], 'textSegmentEndOffsets': ['210', '204', '383', '524', '672', '28', '23'], 'confidences': [0.99951637, 0.9994987, 0.9994574, 0.9994488, 0.99924797, 0.9969406, 0.9692179]}}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_online_predictions:migration"
- },
- "source": [
- "## Make online predictions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "deploy_model:migration,new,mbsdk"
- },
- "source": [
- "### [predictions.deploy-model-api](https://cloud.google.com/vertex-ai/docs/predictions/deploy-model-api)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "deploy_model:mbsdk,automatic"
- },
- "source": [
- "## Deploy the model\n",
- "\n",
- "Next, deploy your model for online prediction. To deploy the model, you invoke the `deploy` method."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "deploy_model:mbsdk,automatic"
- },
- "outputs": [],
- "source": [
- "endpoint = model.deploy()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "deploy_model:mbsdk,automatic"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.models:Creating Endpoint\n",
- " INFO:google.cloud.aiplatform.models:Create Endpoint backing LRO: projects/759209241365/locations/us-central1/endpoints/4867177336350441472/operations/4087251132693348352\n",
- " INFO:google.cloud.aiplatform.models:Endpoint created. Resource name: projects/759209241365/locations/us-central1/endpoints/4867177336350441472\n",
- " INFO:google.cloud.aiplatform.models:To use this Endpoint in another session:\n",
- " INFO:google.cloud.aiplatform.models:endpoint = aiplatform.Endpoint('projects/759209241365/locations/us-central1/endpoints/4867177336350441472')\n",
- " INFO:google.cloud.aiplatform.models:Deploying model to Endpoint : projects/759209241365/locations/us-central1/endpoints/4867177336350441472\n",
- " INFO:google.cloud.aiplatform.models:Deploy Endpoint model backing LRO: projects/759209241365/locations/us-central1/endpoints/4867177336350441472/operations/1691336130932244480\n",
- " INFO:google.cloud.aiplatform.models:Endpoint model deployed. Resource name: projects/759209241365/locations/us-central1/endpoints/4867177336350441472"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "endpoints_predict:migration,new,mbsdk"
- },
- "source": [
- "### [predictions.online-prediction-automl](https://cloud.google.com/vertex-ai/docs/predictions/online-predictions-automl)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_test_item:automl,online_prediction"
- },
- "source": [
- "### Make test item\n",
- "\n",
- "You will use synthetic data as a test data item. Don't be concerned that we are using synthetic data -- we just want to demonstrate how to make a prediction."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "make_test_item:automl,text,biomedical"
- },
- "outputs": [],
- "source": [
- "test_item = 'Molecular basis of hexosaminidase A deficiency and pseudodeficiency in the Berks County Pennsylvania Dutch.\\tFollowing the birth of two infants with Tay-Sachs disease ( TSD ) , a non-Jewish , Pennsylvania Dutch kindred was screened for TSD carriers using the biochemical assay . A high frequency of individuals who appeared to be TSD heterozygotes was detected ( Kelly et al . , 1975 ) . Clinical and biochemical evidence suggested that the increased carrier frequency was due to at least two altered alleles for the hexosaminidase A alpha-subunit . We now report two mutant alleles in this Pennsylvania Dutch kindred , and one polymorphism . One allele , reported originally in a French TSD patient ( Akli et al . , 1991 ) , is a GT-- > AT transition at the donor splice-site of intron 9 . The second , a C-- > T transition at nucleotide 739 ( Arg247Trp ) , has been shown by Triggs-Raine et al . ( 1992 ) to be a clinically benign \" pseudodeficient \" allele associated with reduced enzyme activity against artificial substrate . Finally , a polymorphism [ G-- > A ( 759 ) ] , which leaves valine at codon 253 unchanged , is described'"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "predict_request:mbsdk,ten"
- },
- "source": [
- "### Make the prediction\n",
- "\n",
- "Now that your `Model` resource is deployed to an `Endpoint` resource, you can do online predictions by sending prediction requests to the `Endpoint` resource.\n",
- "\n",
- "#### Request\n",
- "\n",
- "The format of each instance is:\n",
- "\n",
- " { 'content': text_string }\n",
- "\n",
- "Since the predict() method can take multiple items (instances), send your single test item as a list of one test item.\n",
- "\n",
- "#### Response\n",
- "\n",
- "The response from the predict() call is a Python dictionary with the following entries:\n",
- "\n",
- "- `ids`: The internal assigned unique identifiers for each prediction request.\n",
- "- `displayNames`: The class names for each entity.\n",
- "- `confidences`: The predicted confidence, between 0 and 1, per entity.\n",
- "- `textSegmentStartOffsets`: The character offset in the text to the start of the entity.\n",
- "- `textSegmentEndOffsets`: The character offset in the text to the end of the entity.\n",
- "- `deployed_model_id`: The Vertex AI identifier for the deployed `Model` resource which did the predictions."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "predict_request:mbsdk,ten"
- },
- "outputs": [],
- "source": [
- "instances_list = [{\"content\": test_item}]\n",
- "\n",
- "prediction = endpoint.predict(instances_list)\n",
- "print(prediction)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "predict_request:mbsdk,ten"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " Prediction(predictions=[{'displayNames': ['SpecificDisease', 'SpecificDisease', 'SpecificDisease', 'Modifier', 'Modifier', 'Modifier'], 'confidences': [0.9995822906494141, 0.999564528465271, 0.9995641708374023, 0.9993661046028137, 0.9993420839309692, 0.9993830323219299], 'textSegmentStartOffsets': [19.0, 148.0, 168.0, 235.0, 329.0, 687.0], 'textSegmentEndOffsets': [46.0, 165.0, 171.0, 238.0, 332.0, 690.0], 'ids': ['1746900775675625472', '1746900775675625472', '1746900775675625472', '8664429803316707328', '8664429803316707328', '8664429803316707328']}], deployed_model_id='7103029833386426368', explanations=None)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "undeploy_model:mbsdk"
- },
- "source": [
- "## Undeploy the model\n",
- "\n",
- "When you are done doing predictions, you undeploy the model from the `Endpoint` resouce. This deprovisions all compute resources and ends billing for the deployed model."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "undeploy_model:mbsdk"
- },
- "outputs": [],
- "source": [
- "endpoint.undeploy_all()"
- ]
- },
- {
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "cleanup:mbsdk"
- },
- "outputs": [],
- "source": [
- "# Delete the dataset using the Vertex dataset object\n",
- "\n",
- "dataset.delete()\n",
- "\n",
- "# Delete the model using the Vertex model object\n",
- "\n",
- "model.delete()\n",
- "\n",
- "# Delete the endpoint using the Vertex endpoint object\n",
- "\n",
- "endpoint.delete()\n",
- "# Delete the AutoML or Pipeline trainig job\n",
- "\n",
- "dag.delete()\n",
- "\n",
- "# Delete the batch prediction job using the Vertex batch prediction object\n",
- "\n",
- "batch_predict_job.delete()\n",
- "\n",
- "# Delete the hyperparameter tuning job using the Vertex hyperparameter tuning object\n",
- "\n",
- "# Delete GCS bucket.\n",
- "delete_bucket = False\n",
- "if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
- " ! gsutil -m rm -r $BUCKET_URI"
- ]
- }
- ],
- "metadata": {
- "colab": {
- "name": "sdk-automl-text-entity-extraction-batch-prediction.ipynb",
- "toc_visible": true
- },
- "kernelspec": {
- "display_name": "Python 3",
- "name": "python3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
diff --git a/notebooks/official/migration/sdk-automl-text-sentiment-analysis-batch-prediction.ipynb b/notebooks/official/migration/sdk-automl-text-sentiment-analysis-batch-prediction.ipynb
deleted file mode 100644
index 2a058d8c1..000000000
--- a/notebooks/official/migration/sdk-automl-text-sentiment-analysis-batch-prediction.ipynb
+++ /dev/null
@@ -1,1436 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "copyright"
- },
- "outputs": [],
- "source": [
- "# Copyright 2021 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 License 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": "0bdfa24a80ed"
- },
- "source": [
- "Starting on September 15, 2024, you can only customize classification, entity extraction, and sentiment analysis models by moving to Vertex AI Gemini prompts and tuning. Training or updating models for Vertex AI AutoML for Text classification, entity extraction, and sentiment analysis objectives will no longer be available. You can continue using existing Vertex AI AutoML Text objectives until June 15, 2025. For more information about how Gemini offers enhanced user experience through improved prompting capabilities, see \n",
- "[Introduction to tuning](https://cloud.google.com/vertex-ai/generative-ai/docs/models/tune-gemini-overview)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "title:migration,new"
- },
- "source": [
- "# Vertex AI: Vertex AI Migration: AutoML Text Sentiment Analysis\n",
- "\n",
- "\n",
- "\n",
- " \n",
- " \n",
- " Run in Colab\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- " \n",
- " View on GitHub\n",
- " \n",
- " | \n",
- " \n",
- "\n",
- " \n",
- " Open in Vertex AI Workbench\n",
- " \n",
- " | \n",
- "
\n",
- "
"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "2277f661a148"
- },
- "source": [
- "## Overview\n",
- "\n",
- "This notebook demonstrates how to create an AutoML Text Sentiment Analysis model, with a Vertex AI ncbi disease research dataset, and how to serve the model for batch prediction. It requires you provide a bucket where the dataset will be stored.\n",
- "\n",
- "Note: you may incur charges for training, prediction, storage or usage of other Google Cloud products in connection with testing this SDK.\n",
- "\n",
- "Learn more about [Migrate to Vertex AI](https://cloud.google.com/vertex-ai/docs/start/migrating-to-vertex-ai) and [Sentiment analysis for text data](https://cloud.google.com/vertex-ai/docs/training-overview#sentiment_analysis_for_text)."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "f926ec7acab3"
- },
- "source": [
- "### Objective\n",
- "\n",
- "The objective of this notebook is to build a AutoML Text Sentiment Analysis model. The following steps have been followed:\n",
- "This tutorial uses the following Google Cloud ML services :\n",
- "\n",
- "* Vertex AI Dataset resource\n",
- "* AutoML Training\n",
- "* Vertex AI Model resource\n",
- "* Vertex AI Batch Prediction\n",
- "\n",
- "The steps performed include the following:\n",
- "\n",
- "* Copy AutoML video demo train data for creating managed dataset\n",
- "* Create a dataset on Vertex AI.\n",
- "* Configure a training job\n",
- "* Launch a training job and create a model on Vertex AI\n",
- "* Copy AutoML Video Demo Prediction Data for creating batch prediction job\n",
- "* Perform batch prediction job on the model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "dataset:claritin,tst"
- },
- "source": [
- "### Dataset\n",
- "\n",
- "The dataset used for this tutorial is the [Crowdflower Claritin-Twitter dataset](https://data.world/crowdflower/claritin-twitter) from [data.world Datasets](https://data.world). The version of the dataset you use in this tutorial is stored in a public Cloud Storage bucket."
- ]
- },
- {
- "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": "setup_local"
- },
- "source": [
- "### Set up your local development environment\n",
- "\n",
- "If you are using Colab or Google Cloud Notebooks, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
- "\n",
- "Otherwise, make sure your environment meets this notebook's requirements. You need the following:\n",
- "\n",
- "- The Cloud Storage SDK\n",
- "- Git\n",
- "- Python 3\n",
- "- virtualenv\n",
- "- Jupyter notebook running in a virtual environment with Python 3\n",
- "\n",
- "The Cloud Storage guide to [Setting up a Python development environment](https://cloud.google.com/python/setup) and the [Jupyter installation guide](https://jupyter.org/install) provide detailed instructions for meeting these requirements. The following steps provide a condensed set of instructions:\n",
- "\n",
- "1. [Install and initialize the SDK](https://cloud.google.com/sdk/docs/).\n",
- "\n",
- "2. [Install Python 3](https://cloud.google.com/python/setup#installing_python).\n",
- "\n",
- "3. [Install virtualenv](https://cloud.google.com/python/setup#installing_and_using_virtualenv) and create a virtual environment that uses Python 3. Activate the virtual environment.\n",
- "\n",
- "4. To install Jupyter, run `pip3 install jupyter` on the command-line in a terminal shell.\n",
- "\n",
- "5. To launch Jupyter, run `jupyter notebook` on the command-line in a terminal shell.\n",
- "\n",
- "6. Open this notebook in the Jupyter Notebook Dashboard.\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "install_aip:mbsdk"
- },
- "source": [
- "## Installation\n",
- "\n",
- "Install the latest version of Vertex SDK for Python."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "install_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "import os\n",
- "\n",
- "# Google Cloud Notebook\n",
- "if os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
- " USER_FLAG = \"--user\"\n",
- "else:\n",
- " USER_FLAG = \"\"\n",
- "\n",
- "! pip3 install --upgrade google-cloud-aiplatform google-cloud-storage tensorflow $USER_FLAG -q"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "restart"
- },
- "source": [
- "### Restart the kernel\n",
- "\n",
- "Once you've installed the additional packages, you need to restart the notebook kernel so it can find the packages."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "restart"
- },
- "outputs": [],
- "source": [
- "import os\n",
- "\n",
- "if not os.getenv(\"IS_TESTING\"):\n",
- " # Automatically restart kernel after installs\n",
- " import IPython\n",
- "\n",
- " app = IPython.Application.instance()\n",
- " app.kernel.do_shutdown(True)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "before_you_begin:nogpu"
- },
- "source": [
- "## Before you begin\n",
- "\n",
- "### GPU runtime\n",
- "\n",
- "This tutorial does not require a GPU runtime.\n",
- "\n",
- "### Set up your Google Cloud project\n",
- "\n",
- "**The following steps are required, regardless of your notebook environment.**\n",
- "\n",
- "1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\n",
- "\n",
- "2. [Make sure that billing is enabled for your project.](https://cloud.google.com/billing/docs/how-to/modify-project)\n",
- "\n",
- "3. [Enable the following APIs: Vertex AI APIs, Compute Engine APIs, and Cloud Storage.](https://console.cloud.google.com/flows/enableapi?apiid=ml.googleapis.com,compute_component,storage-component.googleapis.com)\n",
- "\n",
- "4. If you are running this notebook locally, you need to install the [Cloud SDK]((https://cloud.google.com/sdk)).\n",
- "\n",
- "5. Enter your project ID in the cell below. Then run the cell to make sure the\n",
- "Cloud SDK uses the right project for all the commands in this notebook.\n",
- "\n",
- "**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "set_project_id"
- },
- "outputs": [],
- "source": [
- "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "autoset_project_id"
- },
- "outputs": [],
- "source": [
- "if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
- " # Get your GCP project id from gcloud\n",
- " shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
- " PROJECT_ID = shell_output[0]\n",
- " print(\"Project ID:\", PROJECT_ID)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "set_gcloud_project_id"
- },
- "outputs": [],
- "source": [
- "! gcloud config set project $PROJECT_ID"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "region"
- },
- "source": [
- "#### Region\n",
- "\n",
- "You can also change the `REGION` variable, which is used for operations\n",
- "throughout the rest of this notebook. Below are regions supported for Vertex AI. We recommend that you choose the region closest to you.\n",
- "\n",
- "- Americas: `us-central1`\n",
- "- Europe: `europe-west4`\n",
- "- Asia Pacific: `asia-east1`\n",
- "\n",
- "You may not use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
- "\n",
- "Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "region"
- },
- "outputs": [],
- "source": [
- "REGION = \"us-central1\" # @param {type: \"string\"}\n",
- "\n",
- "if REGION == \"[your-region]\":\n",
- " REGION = \"us-central1\""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "timestamp"
- },
- "source": [
- "#### UUID\n",
- "\n",
- "If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "timestamp"
- },
- "outputs": [],
- "source": [
- "import random\n",
- "import string\n",
- "\n",
- "\n",
- "# Generate a uuid of a specifed length(default=8)\n",
- "def generate_uuid(length: int = 8) -> str:\n",
- " return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
- "\n",
- "\n",
- "UUID = generate_uuid()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "gcp_authenticate"
- },
- "source": [
- "### Authenticate your Google Cloud account\n",
- "\n",
- "**If you are using Vertex AI Workbench Notebooks**, your environment is already authenticated.\n",
- "\n",
- "**If you are using Colab**, run the cell below and follow the instructions when prompted to authenticate your account via oAuth.\n",
- "\n",
- "**Otherwise**, follow these steps:\n",
- "\n",
- "In the Cloud Console, go to the [Create service account key](https://console.cloud.google.com/apis/credentials/serviceaccountkey) page.\n",
- "\n",
- "**Click Create service account**.\n",
- "\n",
- "In the **Service account name** field, enter a name, and click **Create**.\n",
- "\n",
- "In the **Grant this service account access to project** section, click the Role drop-down list. Type \"Vertex\" into the filter box, and select **Vertex Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
- "\n",
- "Click Create. A JSON file that contains your key downloads to your local environment.\n",
- "\n",
- "Enter the path to your service account key as the GOOGLE_APPLICATION_CREDENTIALS variable in the cell below and run the cell."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "gcp_authenticate"
- },
- "outputs": [],
- "source": [
- "# If you are running this notebook in Colab, run this cell and follow the\n",
- "# instructions to authenticate your GCP account. This provides access to your\n",
- "# Cloud Storage bucket and lets you submit training jobs and prediction\n",
- "# requests.\n",
- "\n",
- "import os\n",
- "import sys\n",
- "\n",
- "# If on Vertex AI Workbench, then don't execute this code\n",
- "IS_COLAB = \"google.colab\" in sys.modules\n",
- "if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
- " \"DL_ANACONDA_HOME\"\n",
- "):\n",
- " if \"google.colab\" in sys.modules:\n",
- " from google.colab import auth as google_auth\n",
- "\n",
- " google_auth.authenticate_user()\n",
- "\n",
- " # If you are running this notebook locally, replace the string below with the\n",
- " # path to your service account key and run this cell to authenticate your GCP\n",
- " # account.\n",
- " elif not os.getenv(\"IS_TESTING\"):\n",
- " %env GOOGLE_APPLICATION_CREDENTIALS ''"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "bucket:mbsdk"
- },
- "source": [
- "### Create a Cloud Storage bucket\n",
- "\n",
- "**The following steps are required, regardless of your notebook environment.**\n",
- "\n",
- "When you initialize the Vertex SDK for Python, you specify a Cloud Storage staging bucket. The staging bucket is where all the data associated with your dataset and model resources are retained across sessions.\n",
- "\n",
- "Set the name of your Cloud Storage bucket below. Bucket names must be globally unique across all Google Cloud projects, including those outside of your organization."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "bucket"
- },
- "outputs": [],
- "source": [
- "BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
- "BUCKET_URI = f\"gs://{BUCKET_NAME}\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "autoset_bucket"
- },
- "outputs": [],
- "source": [
- "if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
- " BUCKET_NAME = PROJECT_ID + \"aip-\" + UUID\n",
- " BUCKET_URI = \"gs://\" + BUCKET_NAME"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_bucket"
- },
- "source": [
- "**Only 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": "create_bucket"
- },
- "outputs": [],
- "source": [
- "! gsutil mb -l $REGION $BUCKET_URI"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "validate_bucket"
- },
- "source": [
- "Finally, validate access to your Cloud Storage bucket by examining its contents:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "validate_bucket"
- },
- "outputs": [],
- "source": [
- "! gsutil ls -al $BUCKET_URI"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "setup_vars"
- },
- "source": [
- "### Set up variables\n",
- "\n",
- "Next, set up some variables used throughout the tutorial.\n",
- "### Import libraries and define constants"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "import_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "import google.cloud.aiplatform as aip"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "init_aip:mbsdk"
- },
- "source": [
- "## Initialize Vertex AI SDK for Python\n",
- "\n",
- "Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "init_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "aip.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
- ]
- },
- {
- "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:claritin,csv,tst"
- },
- "outputs": [],
- "source": [
- "IMPORT_FILE = \"gs://cloud-samples-data/language/claritin.csv\"\n",
- "SENTIMENT_MAX = 4"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "quick_peek:csv"
- },
- "source": [
- "#### Quick peek at your data\n",
- "\n",
- "This tutorial uses a version of the Crowdflower Claritin-Twitter dataset that is stored in a public Cloud Storage bucket, using a CSV index file.\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": "quick_peek:csv"
- },
- "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_a_dataset:migration"
- },
- "source": [
- "## Create a dataset"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "datasets_create:migration,new,mbsdk"
- },
- "source": [
- "### [datasets.create-dataset-api](https://cloud.google.com/vertex-ai/docs/datasets/create-dataset-api)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_dataset:text,tst"
- },
- "source": [
- "### Create the Dataset\n",
- "\n",
- "Next, create the `Dataset` resource using the `create` method for the `TextDataset` 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": "create_dataset:text,tst"
- },
- "outputs": [],
- "source": [
- "dataset = aip.TextDataset.create(\n",
- " display_name=\"Crowdflower Claritin-Twitter\" + \"_\" + UUID,\n",
- " gcs_source=[IMPORT_FILE],\n",
- " import_schema_uri=aip.schema.dataset.ioformat.text.sentiment,\n",
- ")\n",
- "\n",
- "print(dataset.resource_name)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_dataset:text,tst"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Creating TextDataset\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Create TextDataset backing LRO: projects/759209241365/locations/us-central1/datasets/3704325042721521664/operations/3193181053544038400\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:TextDataset created. Resource name: projects/759209241365/locations/us-central1/datasets/3704325042721521664\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:To use this TextDataset in another session:\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:ds = aiplatform.TextDataset('projects/759209241365/locations/us-central1/datasets/3704325042721521664')\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Importing TextDataset data: projects/759209241365/locations/us-central1/datasets/3704325042721521664\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Import TextDataset data backing LRO: projects/759209241365/locations/us-central1/datasets/3704325042721521664/operations/5152246891450204160\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:TextDataset data imported. Resource name: projects/759209241365/locations/us-central1/datasets/3704325042721521664\n",
- " projects/759209241365/locations/us-central1/datasets/3704325042721521664"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "train_a_model:migration"
- },
- "source": [
- "## Train a model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "trainingpipelines_create:migration,new,mbsdk"
- },
- "source": [
- "### [training.automl-api](https://cloud.google.com/vertex-ai/docs/training/automl-api)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_automl_pipeline:text,tst"
- },
- "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 `AutoMLTextTrainingJob` 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`: A text classification model.\n",
- " - `sentiment`: A text sentiment analysis model.\n",
- " - `extraction`: A text entity extraction model.\n",
- "- `multi_label`: If a classification task, whether single (False) or multi-labeled (True).\n",
- "- `sentiment_max`: If a sentiment analysis task, the maximum sentiment value.\n",
- "\n",
- "The instantiated object is the DAG (directed acyclic graph) for the training pipeline."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "create_automl_pipeline:text,tst"
- },
- "outputs": [],
- "source": [
- "dag = aip.AutoMLTextTrainingJob(\n",
- " display_name=\"claritin_\" + UUID,\n",
- " prediction_type=\"sentiment\",\n",
- " sentiment_max=SENTIMENT_MAX,\n",
- ")\n",
- "\n",
- "print(dag)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_automl_pipeline:text,tst"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "run_automl_pipeline:text"
- },
- "source": [
- "#### Run the training pipeline\n",
- "\n",
- "Next, you run the DAG 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",
- "\n",
- "The `run` method when completed returns the `Model` resource.\n",
- "\n",
- "The execution of the training pipeline take upto 20 minutes."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "run_automl_pipeline:text"
- },
- "outputs": [],
- "source": [
- "model = dag.run(\n",
- " dataset=dataset,\n",
- " model_display_name=\"claritin_\" + UUID,\n",
- " training_fraction_split=0.8,\n",
- " validation_fraction_split=0.1,\n",
- " test_fraction_split=0.1,\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "run_automl_pipeline:text"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.training_jobs:View Training:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/training/8859754745456230400?project=759209241365\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " ...\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLTextTrainingJob run completed. Resource name: projects/759209241365/locations/us-central1/trainingPipelines/8859754745456230400\n",
- " INFO:google.cloud.aiplatform.training_jobs:Model available at projects/759209241365/locations/us-central1/models/6389525951797002240"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "evaluate_the_model:migration"
- },
- "source": [
- "## Evaluate the model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "models_evaluations_list:migration,new"
- },
- "source": [
- "### [projects.locations.models.evaluations.list](https://cloud.devsite.corp.google.com/ai-platform-unified/docs/reference/rest/v1beta1/projects.locations.models.evaluations/list)"
- ]
- },
- {
- "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": "evaluate_the_model:mbsdk"
- },
- "outputs": [],
- "source": [
- "# Get model resource ID\n",
- "models = aip.Model.list(filter=\"display_name=claritin_\" + UUID)\n",
- "\n",
- "# Get a reference to the Model Service client\n",
- "client_options = {\"api_endpoint\": f\"{REGION}-aiplatform.googleapis.com\"}\n",
- "model_service_client = aip.gapic.ModelServiceClient(client_options=client_options)\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": "evaluate_the_model:mbsdk"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " name: \"projects/759209241365/locations/us-central1/models/623915674158235648/evaluations/4280507618583117824\"\n",
- " metrics_schema_uri: \"gs://google-cloud-aiplatform/schema/modelevaluation/classification_metrics_1.0.0.yaml\"\n",
- " metrics {\n",
- " struct_value {\n",
- " fields {\n",
- " key: \"auPrc\"\n",
- " value {\n",
- " number_value: 0.9891107\n",
- " }\n",
- " }\n",
- " fields {\n",
- " key: \"confidenceMetrics\"\n",
- " value {\n",
- " list_value {\n",
- " values {\n",
- " struct_value {\n",
- " fields {\n",
- " key: \"precision\"\n",
- " value {\n",
- " number_value: 0.2\n",
- " }\n",
- " }\n",
- " fields {\n",
- " key: \"recall\"\n",
- " value {\n",
- " number_value: 1.0\n",
- " }\n",
- " }\n",
- " }\n",
- " }"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_batch_predictions:migration"
- },
- "source": [
- "## Make batch predictions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batchpredictionjobs_create:migration,new,mbsdk"
- },
- "source": [
- "### [predictions.batch-prediction](https://cloud.google.com/vertex-ai/docs/predictions/batch-predictions)"
- ]
- },
- {
- "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 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,tst,csv"
- },
- "outputs": [],
- "source": [
- "test_items = ! gsutil cat $IMPORT_FILE | head -n2\n",
- "\n",
- "if len(test_items[0]) == 4:\n",
- " _, test_item_1, test_label_1, _ = str(test_items[0]).split(\",\")\n",
- " _, test_item_2, test_label_2, _ = str(test_items[1]).split(\",\")\n",
- "else:\n",
- " test_item_1, test_label_1, _ = str(test_items[0]).split(\",\")\n",
- " test_item_2, test_label_2, _ = str(test_items[1]).split(\",\")\n",
- "\n",
- "\n",
- "print(test_item_1, test_label_1)\n",
- "print(test_item_2, test_label_2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_batch_file:automl,text"
- },
- "source": [
- "### Make the batch input file\n",
- "\n",
- "Now make a batch input file, which you store in your local Cloud Storage bucket. The batch input file can only be in JSONL format. 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 file with the text item.\n",
- "- `mime_type`: The content type. In our example, it is a `text` file.\n",
- "\n",
- "For example:\n",
- "\n",
- " {'content': '[your-bucket]/file1.txt', 'mime_type': 'text'}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "make_batch_file:automl,text"
- },
- "outputs": [],
- "source": [
- "import json\n",
- "\n",
- "import tensorflow as tf\n",
- "\n",
- "gcs_test_item_1 = BUCKET_URI + \"/test1.txt\"\n",
- "with tf.io.gfile.GFile(gcs_test_item_1, \"w\") as f:\n",
- " f.write(test_item_1 + \"\\n\")\n",
- "gcs_test_item_2 = BUCKET_URI + \"/test2.txt\"\n",
- "with tf.io.gfile.GFile(gcs_test_item_2, \"w\") as f:\n",
- " f.write(test_item_2 + \"\\n\")\n",
- "\n",
- "gcs_input_uri = BUCKET_URI + \"/test.jsonl\"\n",
- "with tf.io.gfile.GFile(gcs_input_uri, \"w\") as f:\n",
- " data = {\"content\": gcs_test_item_1, \"mime_type\": \"text/plain\"}\n",
- " f.write(json.dumps(data) + \"\\n\")\n",
- " data = {\"content\": gcs_test_item_2, \"mime_type\": \"text/plain\"}\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",
- "- `sync`: If set to True, the call block while waiting for the asynchronous batch job to complete."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "batch_request:mbsdk"
- },
- "outputs": [],
- "source": [
- "batch_predict_job = model.batch_predict(\n",
- " job_display_name=\"claritin_\" + UUID,\n",
- " gcs_source=gcs_input_uri,\n",
- " gcs_destination_prefix=BUCKET_URI,\n",
- " sync=False,\n",
- ")\n",
- "\n",
- "print(batch_predict_job)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batch_request:mbsdk"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.jobs:Creating BatchPredictionJob\n",
- " is waiting for upstream dependencies to complete.\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob created. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296\n",
- " INFO:google.cloud.aiplatform.jobs:To use this BatchPredictionJob in another session:\n",
- " INFO:google.cloud.aiplatform.jobs:bpj = aiplatform.BatchPredictionJob('projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296')\n",
- " INFO:google.cloud.aiplatform.jobs:View Batch Prediction Job:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/batch-predictions/5110965452507447296?project=759209241365\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296 current state:\n",
- " JobState.JOB_STATE_RUNNING"
- ]
- },
- {
- "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": "batch_request_wait:mbsdk"
- },
- "outputs": [],
- "source": [
- "batch_predict_job.wait()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batch_request_wait:mbsdk"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob created. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328\n",
- " INFO:google.cloud.aiplatform.jobs:To use this BatchPredictionJob in another session:\n",
- " INFO:google.cloud.aiplatform.jobs:bpj = aiplatform.BatchPredictionJob('projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328')\n",
- " INFO:google.cloud.aiplatform.jobs:View Batch Prediction Job:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/batch-predictions/181835033978339328?project=759209241365\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_SUCCEEDED\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob run completed. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_batch_prediction:mbsdk,tst"
- },
- "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",
- " - `sentiment`: The sentiment."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "get_batch_prediction:mbsdk,tst"
- },
- "outputs": [],
- "source": [
- "import json\n",
- "\n",
- "import tensorflow as tf\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)\n",
- " break"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_batch_prediction:mbsdk,tst"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " {'instance': {'content': 'gs://andy-1234-221921aip-20210811220920/test2.txt', 'mimeType': 'text/plain'}, 'prediction': {'sentiment': 3}}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_online_predictions:migration"
- },
- "source": [
- "## Make online predictions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "deploy_model:migration,new,mbsdk"
- },
- "source": [
- "### [predictions.deploy-model-api](https://cloud.google.com/vertex-ai/docs/predictions/deploy-model-api)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "deploy_model:mbsdk,automatic"
- },
- "source": [
- "## Deploy the model\n",
- "\n",
- "Next, deploy your model for online prediction. To deploy the model, you invoke the `deploy` method."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "deploy_model:mbsdk,automatic"
- },
- "outputs": [],
- "source": [
- "endpoint = model.deploy()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "deploy_model:mbsdk,automatic"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.models:Creating Endpoint\n",
- " INFO:google.cloud.aiplatform.models:Create Endpoint backing LRO: projects/759209241365/locations/us-central1/endpoints/4867177336350441472/operations/4087251132693348352\n",
- " INFO:google.cloud.aiplatform.models:Endpoint created. Resource name: projects/759209241365/locations/us-central1/endpoints/4867177336350441472\n",
- " INFO:google.cloud.aiplatform.models:To use this Endpoint in another session:\n",
- " INFO:google.cloud.aiplatform.models:endpoint = aiplatform.Endpoint('projects/759209241365/locations/us-central1/endpoints/4867177336350441472')\n",
- " INFO:google.cloud.aiplatform.models:Deploying model to Endpoint : projects/759209241365/locations/us-central1/endpoints/4867177336350441472\n",
- " INFO:google.cloud.aiplatform.models:Deploy Endpoint model backing LRO: projects/759209241365/locations/us-central1/endpoints/4867177336350441472/operations/1691336130932244480\n",
- " INFO:google.cloud.aiplatform.models:Endpoint model deployed. Resource name: projects/759209241365/locations/us-central1/endpoints/4867177336350441472"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "endpoints_predict:migration,new,mbsdk"
- },
- "source": [
- "### [predictions.online-prediction-automl](https://cloud.google.com/vertex-ai/docs/predictions/online-predictions-automl)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_test_item"
- },
- "source": [
- "### Get test item\n",
- "\n",
- "You use an arbitrary example out of the dataset as a test item. Don't be concerned that the example was 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_item:automl,tst,csv"
- },
- "outputs": [],
- "source": [
- "test_item = ! gsutil cat $IMPORT_FILE | head -n1\n",
- "if len(test_item[0]) == 3:\n",
- " _, test_item, test_label, max = str(test_item[0]).split(\",\")\n",
- "else:\n",
- " test_item, test_label, max = str(test_item[0]).split(\",\")\n",
- "\n",
- "print(test_item, test_label)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "predict_request:mbsdk,tst"
- },
- "source": [
- "### Make the prediction\n",
- "\n",
- "Now that your `Model` resource is deployed to an `Endpoint` resource, you can do online predictions by sending prediction requests to the `Endpoint` resource.\n",
- "\n",
- "#### Request\n",
- "\n",
- "The format of each instance is:\n",
- "\n",
- " { 'content': text_string }\n",
- "\n",
- "Since the predict() method can take multiple items (instances), send your single test item as a list of one test item.\n",
- "\n",
- "#### Response\n",
- "\n",
- "The response from the predict() call is a Python dictionary with the following entries:\n",
- "\n",
- "- `ids`: The internal assigned unique identifiers for each prediction request.\n",
- "- `sentiment`: The sentiment value.\n",
- "- `deployed_model_id`: The Vertex AI identifier for the deployed `Model` resource which did the predictions."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "predict_request:mbsdk,tst"
- },
- "outputs": [],
- "source": [
- "instances_list = [{\"content\": test_item}]\n",
- "\n",
- "prediction = endpoint.predict(instances_list)\n",
- "print(prediction)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "predict_request:mbsdk,tst"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " Prediction(predictions=[{'sentiment': 2.0}], deployed_model_id='311601595311718400', explanations=None)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "undeploy_model:mbsdk"
- },
- "source": [
- "## Undeploy the model\n",
- "\n",
- "When you are done doing predictions, you undeploy the model from the `Endpoint` resouce. This deprovisions all compute resources and ends billing for the deployed model."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "undeploy_model:mbsdk"
- },
- "outputs": [],
- "source": [
- "endpoint.undeploy_all()"
- ]
- },
- {
- "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",
- "- Pipeline\n",
- "- Model\n",
- "- Endpoint\n",
- "- AutoML Training Job\n",
- "- Batch Job\n",
- "- Custom Job\n",
- "- Hyperparameter Tuning Job\n",
- "- Cloud Storage Bucket"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "cleanup:mbsdk"
- },
- "outputs": [],
- "source": [
- "delete_all = True\n",
- "\n",
- "if delete_all:\n",
- " # Delete the dataset using the Vertex dataset object\n",
- " try:\n",
- " if \"dataset\" in globals():\n",
- " dataset.delete()\n",
- " except Exception as e:\n",
- " print(e)\n",
- "\n",
- " # Delete the model using the Vertex model object\n",
- " try:\n",
- " if \"model\" in globals():\n",
- " model.delete()\n",
- " except Exception as e:\n",
- " print(e)\n",
- "\n",
- " # Delete the endpoint using the Vertex endpoint object\n",
- " try:\n",
- " if \"endpoint\" in globals():\n",
- " endpoint.delete()\n",
- " except Exception as e:\n",
- " print(e)\n",
- "\n",
- " # Delete the AutoML or Pipeline trainig job\n",
- " try:\n",
- " if \"dag\" in globals():\n",
- " dag.delete()\n",
- " except Exception as e:\n",
- " print(e)\n",
- "\n",
- " # Delete the custom trainig job\n",
- " try:\n",
- " if \"job\" in globals():\n",
- " job.delete()\n",
- " except Exception as e:\n",
- " print(e)\n",
- "\n",
- " # Delete the batch prediction job using the Vertex batch prediction object\n",
- " try:\n",
- " if \"batch_predict_job\" in globals():\n",
- " batch_predict_job.delete()\n",
- " except Exception as e:\n",
- " print(e)\n",
- "\n",
- " # Delete the hyperparameter tuning job using the Vertex hyperparameter tuning object\n",
- " try:\n",
- " if \"hpt_job\" in globals():\n",
- " hpt_job.delete()\n",
- " except Exception as e:\n",
- " print(e)\n",
- "\n",
- " if \"BUCKET_NAME\" in globals():\n",
- " ! gsutil rm -r $BUCKET_URI"
- ]
- }
- ],
- "metadata": {
- "colab": {
- "name": "sdk-automl-text-sentiment-analysis-batch-prediction.ipynb",
- "toc_visible": true
- },
- "kernelspec": {
- "display_name": "Python 3",
- "name": "python3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
diff --git a/notebooks/official/migration/sdk-automl-video-classification-batch-prediction.ipynb b/notebooks/official/migration/sdk-automl-video-classification-batch-prediction.ipynb
deleted file mode 100644
index a7610259e..000000000
--- a/notebooks/official/migration/sdk-automl-video-classification-batch-prediction.ipynb
+++ /dev/null
@@ -1,1094 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "copyright"
- },
- "outputs": [],
- "source": [
- "# Copyright 2021 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 License 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:migration,new"
- },
- "source": [
- "# Vertex AI Migration: AutoML Video Classification\n",
- "\n",
- "\n",
- "\n",
- " \n",
- " \n",
- "  Open in Colab\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- "  Open in Colab Enterprise\n",
- " \n",
- " | \n",
- " \n",
- "\n",
- "  Open in Workbench\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- "  View on GitHub\n",
- " \n",
- " | \n",
- "
\n",
- "
"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "7a8a13b86a8b"
- },
- "source": [
- "## Overview\n",
- "\n",
- "This tutorial demonstrates how to use the Vertex AI SDK for Python to train an AutoML video classification model and do batch prediction.\n",
- "\n",
- "Learn more about [Migrate to Vertex AI](https://cloud.google.com/vertex-ai/docs/start/migrating-to-vertex-ai) and [Classification for video data](https://cloud.google.com/vertex-ai/docs/training-overview#classification_for_videos)."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "618cfedf829a"
- },
- "source": [
- "### Objective\n",
- "\n",
- "In this tutorial, you learn to use AutoML to train a video model and use Vertex AI batch prediction to do batch predictions.\n",
- "\n",
- "\n",
- "This tutorial uses the following Google Cloud ML services:\n",
- "\n",
- "- AutoML\n",
- "- Vertex AI batch prediction\n",
- "- Vertex AI model resource\n",
- "- Vertex AI endpoint resource\n",
- "\n",
- "The steps performed include:\n",
- "\n",
- "- Train an AutoML video classification model.\n",
- "- Make a batch prediction."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "dataset:hmdb,vcn"
- },
- "source": [
- "### Dataset\n",
- "\n",
- "The dataset used for this tutorial is the [Human Motion dataset](https://TODO) from [MIT](http://cbcl.mit.edu/publications/ps/Kuehne_etal_iccv11.pdf). The version of the dataset you use in this tutorial is stored in a public Cloud Storage bucket."
- ]
- },
- {
- "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": "install_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "import os\n",
- "\n",
- "! pip3 install --upgrade --quiet google-cloud-aiplatform\n",
- "\n",
- "! pip3 install --quiet tensorflow==2.15.1\n",
- "\n",
- "if os.getenv(\"IS_TESTING\"):\n",
- " ! pip3 install --upgrade --quiet google-cloud-storage tensorflow"
- ]
- },
- {
- "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": [
- "\n",
- "⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️\n",
- "
\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": "set_project_id"
- },
- "outputs": [],
- "source": [
- "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
- "\n",
- "# Set the project id\n",
- "! gcloud config set project {PROJECT_ID}\n",
- "\n",
- "LOCATION = \"us-central1\" # @param {type: \"string\"}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "timestamp"
- },
- "source": [
- "#### UUID\n",
- "\n",
- "To avoid name collisions between users on created resources, create a uuid for each session instance. Append these uuids to the respective names of the resources \n",
- "created in this tutorial."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "timestamp"
- },
- "outputs": [],
- "source": [
- "import random\n",
- "import string\n",
- "\n",
- "\n",
- "# Generate a uuid of a specifed length(default=8)\n",
- "def generate_uuid(length: int = 8) -> str:\n",
- " return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
- "\n",
- "\n",
- "UUID = generate_uuid()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "bucket:mbsdk"
- },
- "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": "bucket"
- },
- "outputs": [],
- "source": [
- "BUCKET_URI = f\"gs://your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_bucket"
- },
- "source": [
- "**Only 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": "create_bucket"
- },
- "outputs": [],
- "source": [
- "! gsutil mb -l {LOCATION} {BUCKET_URI}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "setup_vars"
- },
- "source": [
- "### Set up variables\n",
- "\n",
- "Next, set up some variables used throughout the tutorial.\n",
- "### Import libraries and define constants"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "import_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "import os\n",
- "import sys\n",
- "\n",
- "from google.cloud import aiplatform"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "init_aip:mbsdk"
- },
- "source": [
- "## Initialize Vertex SDK for Python\n",
- "\n",
- "Initialize the Vertex SDK for Python for your project and corresponding bucket."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "init_aip:mbsdk"
- },
- "outputs": [],
- "source": [
- "aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
- ]
- },
- {
- "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:hmdb,csv,vcn"
- },
- "outputs": [],
- "source": [
- "IMPORT_FILE = \"gs://automl-video-demo-data/hmdb_split1_train_40_mp4_gs.csv\""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "quick_peek:csv"
- },
- "source": [
- "#### Quick peek at your data\n",
- "\n",
- "This tutorial uses a version of the MIT Human Motion dataset that is stored in a public Cloud Storage bucket, using a CSV index file.\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": "quick_peek:csv"
- },
- "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": "code",
- "execution_count": null,
- "metadata": {
- "id": "f483d8d80f64"
- },
- "outputs": [],
- "source": [
- "import datetime\n",
- "\n",
- "start = datetime.datetime.now()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_a_dataset:migration"
- },
- "source": [
- "## Create a dataset"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "datasets_create:migration,new,mbsdk"
- },
- "source": [
- "### [datasets.create-dataset-api](https://cloud.google.com/vertex-ai/docs/datasets/create-dataset-api)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_dataset:video,vcn"
- },
- "source": [
- "### Create the Dataset\n",
- "\n",
- "Next, create the `Dataset` resource using the `create` method for the `VideoDataset` 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",
- "\n",
- "This operation may take several minutes."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "create_dataset:video,vcn"
- },
- "outputs": [],
- "source": [
- "dataset = aiplatform.VideoDataset.create(\n",
- " display_name=\"MIT Human Motion\" + \"_\" + UUID,\n",
- " gcs_source=[IMPORT_FILE],\n",
- " import_schema_uri=aiplatform.schema.dataset.ioformat.video.classification,\n",
- ")\n",
- "\n",
- "print(dataset.resource_name)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_dataset:video,vcn"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Creating VideoDataset\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Create VideoDataset backing LRO: projects/759209241365/locations/us-central1/datasets/5948525032035581952/operations/6913187331100901376\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:VideoDataset created. Resource name: projects/759209241365/locations/us-central1/datasets/5948525032035581952\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:To use this VideoDataset in another session:\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:ds = aiplatform.VideoDataset('projects/759209241365/locations/us-central1/datasets/5948525032035581952')\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Importing VideoDataset data: projects/759209241365/locations/us-central1/datasets/5948525032035581952\n",
- " INFO:google.cloud.aiplatform.datasets.dataset:Import VideoDataset data backing LRO: projects/759209241365/locations/us-central1/datasets/5948525032035581952/operations/6800597340416638976"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "train_a_model:migration"
- },
- "source": [
- "## Train a model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "trainingpipelines_create:migration,new,mbsdk"
- },
- "source": [
- "### [training.automl-api](https://cloud.google.com/vertex-ai/docs/training/automl-api)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_automl_pipeline:video,vcn"
- },
- "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 `AutoMLVideoTrainingJob` 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`: A video classification model.\n",
- " - `object_tracking`: A video object tracking model.\n",
- " - `action_recognition`: A video action recognition model.\n",
- "\n",
- "The instantiated object is the DAG (directed acyclic graph) for the training pipeline."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "create_automl_pipeline:video,vcn"
- },
- "outputs": [],
- "source": [
- "dag = aiplatform.AutoMLVideoTrainingJob(\n",
- " display_name=\"hmdb_\" + UUID,\n",
- " prediction_type=\"classification\",\n",
- ")\n",
- "\n",
- "print(dag)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_automl_pipeline:video,vcn"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "run_automl_pipeline:video"
- },
- "source": [
- "#### Run the training pipeline\n",
- "\n",
- "Next, you run the DAG 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",
- "\n",
- "The `run` method when completed returns the `Model` resource.\n",
- "\n",
- "The execution of the training pipeline may take over 24 hrs."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "8f135100f5d9"
- },
- "outputs": [],
- "source": [
- "if os.getenv(\"IS_TESTING\"):\n",
- " sys.exit(0)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "run_automl_pipeline:video"
- },
- "outputs": [],
- "source": [
- "model = dag.run(\n",
- " dataset=dataset,\n",
- " model_display_name=\"hmdb_\" + UUID,\n",
- " training_fraction_split=0.8,\n",
- " test_fraction_split=0.2,\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "run_automl_pipeline:video"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.training_jobs:View Training:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/training/6090621516762841088?project=759209241365\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " PipelineState.PIPELINE_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088 current state:\n",
- " ...\n",
- " INFO:google.cloud.aiplatform.training_jobs:AutoMLVideoTrainingJob run completed. Resource name: projects/759209241365/locations/us-central1/trainingPipelines/6090621516762841088\n",
- " INFO:google.cloud.aiplatform.training_jobs:Model available at projects/759209241365/locations/us-central1/models/1899701006099283968"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "48ca7f0980e0"
- },
- "outputs": [],
- "source": [
- "end = datetime.datetime.now()\n",
- "end - start"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "evaluate_the_model:migration"
- },
- "source": [
- "## Evaluate the model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "models_evaluations_list:migration,new"
- },
- "source": [
- "### [projects.locations.models.evaluations.list](https://cloud.devsite.corp.google.com/ai-platform-unified/docs/reference/rest/v1beta1/projects.locations.models.evaluations/list)"
- ]
- },
- {
- "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": "evaluate_the_model:mbsdk"
- },
- "outputs": [],
- "source": [
- "# Get model resource ID\n",
- "models = aiplatform.Model.list(filter=\"display_name=hmdb_\" + UUID)\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": "evaluate_the_model:mbsdk"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " name: \"projects/759209241365/locations/us-central1/models/623915674158235648/evaluations/4280507618583117824\"\n",
- " metrics_schema_uri: \"gs://google-cloud-aiplatform/schema/modelevaluation/classification_metrics_1.0.0.yaml\"\n",
- " metrics {\n",
- " struct_value {\n",
- " fields {\n",
- " key: \"auPrc\"\n",
- " value {\n",
- " number_value: 0.9891107\n",
- " }\n",
- " }\n",
- " fields {\n",
- " key: \"confidenceMetrics\"\n",
- " value {\n",
- " list_value {\n",
- " values {\n",
- " struct_value {\n",
- " fields {\n",
- " key: \"precision\"\n",
- " value {\n",
- " number_value: 0.2\n",
- " }\n",
- " }\n",
- " fields {\n",
- " key: \"recall\"\n",
- " value {\n",
- " number_value: 1.0\n",
- " }\n",
- " }\n",
- " }\n",
- " }"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_batch_predictions:migration"
- },
- "source": [
- "## Make batch predictions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batchpredictionjobs_create:migration,new,mbsdk"
- },
- "source": [
- "### [predictions.batch-prediction](https://cloud.google.com/vertex-ai/docs/predictions/batch-predictions)"
- ]
- },
- {
- "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 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 -- the purpose here is to demonstrate how to make a prediction."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "get_test_items:automl,vcn,csv"
- },
- "outputs": [],
- "source": [
- "test_items = ! gsutil cat $IMPORT_FILE | head -n2\n",
- "\n",
- "if len(test_items[0]) == 5:\n",
- " _, test_item_1, test_label_1, _, _ = str(test_items[0]).split(\",\")\n",
- " _, test_item_2, test_label_2, _, _ = str(test_items[1]).split(\",\")\n",
- "else:\n",
- " test_item_1, test_label_1, _, _ = str(test_items[0]).split(\",\")\n",
- " test_item_2, test_label_2, _, _ = str(test_items[1]).split(\",\")\n",
- "\n",
- "\n",
- "print(test_item_1, test_label_1)\n",
- "print(test_item_2, test_label_2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "make_batch_file:automl,video"
- },
- "source": [
- "### Make a batch input file\n",
- "\n",
- "Now make a batch input file, which you store in your local Cloud Storage bucket. The batch input file can be either CSV or JSONL. You use JSONL in this tutorial. For JSONL file, you make one dictionary entry per line for each video. The dictionary contains the key/value pairs:\n",
- "\n",
- "- `content`: The Cloud Storage path to the video.\n",
- "- `mimeType`: The content type. In our example, it's a `avi` file.\n",
- "- `timeSegmentStart`: The start timestamp in the video to do prediction on. *Note*, the timestamp must be specified as a string and followed by s (second), m (minute) or h (hour).\n",
- "- `timeSegmentEnd`: The end timestamp in the video to do prediction on."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "make_batch_file:automl,video"
- },
- "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 = {\n",
- " \"content\": test_item_1,\n",
- " \"mimeType\": \"video/avi\",\n",
- " \"timeSegmentStart\": \"0.0s\",\n",
- " \"timeSegmentEnd\": \"5.0s\",\n",
- " }\n",
- " f.write(json.dumps(data) + \"\\n\")\n",
- " data = {\n",
- " \"content\": test_item_2,\n",
- " \"mimeType\": \"video/avi\",\n",
- " \"timeSegmentStart\": \"0.0s\",\n",
- " \"timeSegmentEnd\": \"5.0s\",\n",
- " }\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",
- "- `sync`: If set to True, the call blocks while waiting for the asynchronous batch job to complete."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "batch_request:mbsdk"
- },
- "outputs": [],
- "source": [
- "batch_predict_job = model.batch_predict(\n",
- " job_display_name=\"hmdb_\" + UUID,\n",
- " gcs_source=gcs_input_uri,\n",
- " gcs_destination_prefix=BUCKET_URI,\n",
- " sync=False,\n",
- ")\n",
- "\n",
- "print(batch_predict_job)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batch_request:mbsdk"
- },
- "source": [
- "*Example output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.jobs:Creating BatchPredictionJob\n",
- " is waiting for upstream dependencies to complete.\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob created. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296\n",
- " INFO:google.cloud.aiplatform.jobs:To use this BatchPredictionJob in another session:\n",
- " INFO:google.cloud.aiplatform.jobs:bpj = aiplatform.BatchPredictionJob('projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296')\n",
- " INFO:google.cloud.aiplatform.jobs:View Batch Prediction Job:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/batch-predictions/5110965452507447296?project=759209241365\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/5110965452507447296 current state:\n",
- " JobState.JOB_STATE_RUNNING"
- ]
- },
- {
- "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": "batch_request_wait:mbsdk"
- },
- "outputs": [],
- "source": [
- "batch_predict_job.wait()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "batch_request_wait:mbsdk"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob created. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328\n",
- " INFO:google.cloud.aiplatform.jobs:To use this BatchPredictionJob in another session:\n",
- " INFO:google.cloud.aiplatform.jobs:bpj = aiplatform.BatchPredictionJob('projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328')\n",
- " INFO:google.cloud.aiplatform.jobs:View Batch Prediction Job:\n",
- " https://console.cloud.google.com/ai/platform/locations/us-central1/batch-predictions/181835033978339328?project=759209241365\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_RUNNING\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328 current state:\n",
- " JobState.JOB_STATE_SUCCEEDED\n",
- " INFO:google.cloud.aiplatform.jobs:BatchPredictionJob run completed. Resource name: projects/759209241365/locations/us-central1/batchPredictionJobs/181835033978339328"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_batch_prediction:mbsdk,vcn"
- },
- "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",
- " - `confidences`: The predicted confidence, between 0 and 1, per class label.\n",
- " - `timeSegmentStart`: The time offset in the video to the start of the video sequence.\n",
- " - `timeSegmentEnd`: The time offset in the video to the end of the video sequence."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "get_batch_prediction:mbsdk,vcn"
- },
- "outputs": [],
- "source": [
- "import json\n",
- "\n",
- "import tensorflow as tf\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)\n",
- " break"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "get_batch_prediction:mbsdk,vcn"
- },
- "source": [
- "*Example Output:*\n",
- "\n",
- " {'instance': {'content': 'gs://automl-video-demo-data/hmdb51/Acrobacias_de_un_fenomeno_cartwheel_f_cm_np1_ba_bad_8.avi', 'mimeType': 'video/avi', 'timeSegmentStart': '0.0s', 'timeSegmentEnd': '5.0s'}, 'prediction': [{'id': '4517318233950257152', 'displayName': 'cartwheel', 'type': 'segment-classification', 'timeSegmentStart': '0s', 'timeSegmentEnd': '5s', 'confidence': 0.7450977}, {'id': '6823161243163951104', 'displayName': 'pullup', 'type': 'segment-classification', 'timeSegmentStart': '0s', 'timeSegmentEnd': '5s', 'confidence': 0.07339612}, {'id': '2211475224736563200', 'displayName': 'golf', 'type': 'segment-classification', 'timeSegmentStart': '0s', 'timeSegmentEnd': '5s', 'confidence': 0.065019816}, {'id': '9129004252377645056', 'displayName': 'kick_ball', 'type': 'segment-classification', 'timeSegmentStart': '0s', 'timeSegmentEnd': '5s', 'confidence': 0.06463309}, {'id': '121804997636653056', 'displayName': 'ride_horse', 'type': 'segment-classification', 'timeSegmentStart': '0s', 'timeSegmentEnd': '5s', 'confidence': 0.05185325}]}\n",
- " {'instance': {'content': 'gs://automl-video-demo-data/hmdb51/_Rad_Schlag_die_Bank__cartwheel_f_cm_np1_le_med_0.avi', 'mimeType': 'video/avi', 'timeSegmentStart': '0.0s', 'timeSegmentEnd': '5.0s'}, 'prediction': [{'id': '4517318233950257152', 'displayName': 'cartwheel', 'type': 'segment-classification', 'timeSegmentStart': '0s', 'timeSegmentEnd': '5s', 'confidence': 0.76310456}, {'id': '2211475224736563200', 'displayName': 'golf', 'type': 'segment-classification', 'timeSegmentStart': '0s', 'timeSegmentEnd': '5s', 'confidence': 0.06767218}, {'id': '6823161243163951104', 'displayName': 'pullup', 'type': 'segment-classification', 'timeSegmentStart': '0s', 'timeSegmentEnd': '5s', 'confidence': 0.05853845}, {'id': '9129004252377645056', 'displayName': 'kick_ball', 'type': 'segment-classification', 'timeSegmentStart': '0s', 'timeSegmentEnd': '5s', 'confidence': 0.055601567}, {'id': '121804997636653056', 'displayName': 'ride_horse', 'type': 'segment-classification', 'timeSegmentStart': '0s', 'timeSegmentEnd': '5s', 'confidence': 0.055083193}]}"
- ]
- },
- {
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "cleanup:mbsdk"
- },
- "outputs": [],
- "source": [
- "delete_bucket = False\n",
- "\n",
- "# Delete the dataset using the Vertex dataset object\n",
- "\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",
- "dag.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-video-classification-batch-prediction.ipynb",
- "toc_visible": true
- },
- "kernelspec": {
- "display_name": "Python 3",
- "name": "python3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
diff --git a/notebooks/official/sdk/SDK_AutoML_Video_Classification.ipynb b/notebooks/official/sdk/SDK_AutoML_Video_Classification.ipynb
deleted file mode 100644
index 6ae65022d..000000000
--- a/notebooks/official/sdk/SDK_AutoML_Video_Classification.ipynb
+++ /dev/null
@@ -1,814 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "modular-concentration"
- },
- "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 License 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": "b88c5cede17b"
- },
- "source": [
- "# Vertex AI SDK for Python: AutoML Video Classification Example\n",
- "\n",
- "\n",
- "\n",
- " \n",
- "  Open in Colab\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- "  Open in Colab Enterprise\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- "  Open in Workbench\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- "  View on GitHub\n",
- " \n",
- " | \n",
- "
"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "522e8eec0fcb"
- },
- "source": [
- "## Overview\n",
- "\n",
- "This notebook demonstrates how to create an AutoML video classification model, with a Vertex AI video dataset, and how to serve the model for batch prediction. It requires you to provide a bucket where the dataset is stored.\n",
- "\n",
- "Note: you may incur charges for training, prediction, storage or usage of other GCP products in connection with testing this SDK.\n",
- "\n",
- "Learn more about [Classification for video data](https://cloud.google.com/vertex-ai/docs/training-overview#classification_for_videos)."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "afc6017b7b45"
- },
- "source": [
- "### Objective\n",
- "\n",
- "The objective of this notebook is to build a AutoML video classification model. The following steps have been followed: \n",
- "This tutorial uses the following Google Cloud ML services :\n",
- "- Vertex AI dataset resource\n",
- "- AutoML training\n",
- "- Vertex AI model resource\n",
- "- Vertex AI batch prediction\n",
- "\n",
- "\n",
- "The steps performed include the following:\n",
- "\n",
- "- Set your task name, and GCS prefix\n",
- "- Copy AutoML video demo train data for creating managed dataset\n",
- "- Create a dataset on Vertex AI.\n",
- "- Configure a training job\n",
- "- Launch a training job and create a model on Vertex AI\n",
- "- Copy AutoML Video Demo Prediction Data for creating batch prediction job\n",
- "- Perform batch prediction job on the model\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "5bba1b08cba7"
- },
- "source": [
- "### Dataset\n",
- "\n",
- "##### HMDB: a large human motion database\n",
- "Some training data and prediction data for the demo is prepared using the [HMDB Dataset](https://serre-lab.clps.brown.edu/resource/hmdb-a-large-human-motion-database).\n",
- "\n",
- "The HMDB Dataset is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0/\n",
- "\n",
- "For more information about this dataset please visit: https://serre-lab.clps.brown.edu/resource/hmdb-a-large-human-motion-database/"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "248a51c68228"
- },
- "source": [
- "## Costs\n",
- "\n",
- "This tutorial uses the following billable components of Google Cloud:\n",
- "\n",
- "- Vertex AI\n",
- "- Cloud Storage\n",
- "\n",
- "\n",
- "Learn about [Vertex AI\n",
- "pricing](https://cloud.google.com/vertex-ai/pricing), [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": "coated-remark"
- },
- "outputs": [],
- "source": [
- "! pip3 install --upgrade --quiet google-cloud-aiplatform"
- ]
- },
- {
- "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": [
- "\n",
- "⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️\n",
- "
\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": "wsePm9c4jmpT"
- },
- "outputs": [],
- "source": [
- "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
- "\n",
- "# Set the project id\n",
- "! gcloud config set project {PROJECT_ID}\n",
- "\n",
- "LOCATION = \"us-central1\" # @param {type: \"string\"}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "57dad372c81b"
- },
- "source": [
- "#### UUID\n",
- "\n",
- "To avoid name collisions between users on created resources, create a uuid for each session instance. Append these uuids to the respective names of the resources created in this tutorial."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "4e166d927e36"
- },
- "outputs": [],
- "source": [
- "import random\n",
- "import string\n",
- "\n",
- "\n",
- "# Generate a uuid of a specifed length(default=8)\n",
- "def generate_uuid(length: int = 8) -> str:\n",
- " return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
- "\n",
- "\n",
- "UUID = generate_uuid()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "bucket:custom"
- },
- "source": [
- "### Create a Cloud Storage bucket\n",
- "\n",
- "Create a storage bucket to store intermediate artifacts such as datasets.\n",
- "\n",
- "When you submit a training job using the Cloud SDK, you upload a Python package\n",
- "containing your training code to a Cloud Storage bucket. Vertex AI runs\n",
- "the code from this package. In this tutorial, Vertex AI also saves the\n",
- "trained model that results from your job in the same bucket. Using this model artifact, you can then\n",
- "create Vertex AI Model resource and use for prediction."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "bucket"
- },
- "outputs": [],
- "source": [
- "BUCKET_URI = (\n",
- " f\"gs://your-bucket-name-{PROJECT_ID}-unique-{UUID}\" # @param {type:\"string\"}\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "create_bucket"
- },
- "source": [
- "**Only 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": "Oz8J0vmSlugt"
- },
- "outputs": [],
- "source": [
- "! gsutil mb -l $LOCATION -p $PROJECT_ID $BUCKET_URI"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "incorporated-edgar"
- },
- "source": [
- "### Import libraries and define constants\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "hispanic-macedonia"
- },
- "outputs": [],
- "source": [
- "import json\n",
- "\n",
- "from google.cloud import aiplatform, storage\n",
- "\n",
- "MY_PROJECT = PROJECT_ID\n",
- "MY_STAGING_BUCKET = BUCKET_URI"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "historical-consciousness"
- },
- "source": [
- "### Set Your Task Name, and GCS Prefix\n",
- "\n",
- "If you want to centeralize all input and output files under the gcs location."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "organizational-salad"
- },
- "outputs": [],
- "source": [
- "TASK_TYPE = \"mbsdk_automl-video-training\"\n",
- "PREDICTION_TYPE = \"classification\"\n",
- "MODEL_TYPE = \"CLOUD\"\n",
- "\n",
- "TASK_NAME = f\"{TASK_TYPE}_{PREDICTION_TYPE}\"\n",
- "BUCKET_NAME = MY_STAGING_BUCKET.split(\"gs://\")[1]\n",
- "GCS_PREFIX = TASK_NAME\n",
- "\n",
- "print(f\"Bucket Name: {BUCKET_NAME}\")\n",
- "print(f\"Task Name: {TASK_NAME}\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "professional-bulletin"
- },
- "source": [
- "### Copy AutoML Video Demo Train Data for Creating Managed Dataset"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "accurate-producer"
- },
- "outputs": [],
- "source": [
- "automl_video_demo_train_data = (\n",
- " \"gs://automl-video-demo-data/hmdb_split1_5classes_all.csv\"\n",
- ")\n",
- "\n",
- "\n",
- "gcs_source_train = f\"gs://{BUCKET_NAME}/{TASK_NAME}/data/video_classification.csv\"\n",
- "\n",
- "!gsutil cp $automl_video_demo_train_data $gcs_source_train"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "rough-alert"
- },
- "source": [
- "## Run AutoML video training with Vertex AI video dataset"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "adaptive-slovakia"
- },
- "source": [
- "## Initialize Vertex AI SDK for Python\n",
- "\n",
- "Initialize the *client* for Vertex AI."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "figured-fellow"
- },
- "outputs": [],
- "source": [
- "aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "pleasant-holmes"
- },
- "source": [
- "## Create a Dataset on Vertex AI Dataset resource\n",
- "You now create a Vertex AI video dataset using the previously prepared csv files."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "uVBfL-0TTjNS"
- },
- "outputs": [],
- "source": [
- "dataset = aiplatform.VideoDataset.create(\n",
- " display_name=f\"temp-{TASK_NAME}\",\n",
- " gcs_source=gcs_source_train,\n",
- " import_schema_uri=aiplatform.schema.dataset.ioformat.video.classification,\n",
- " sync=False,\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "3x4xuyIbVR_N"
- },
- "outputs": [],
- "source": [
- "dataset.wait()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "mexican-spending"
- },
- "source": [
- "## Launch a training job and create a model on Vertex AI"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "fdb1d50298ef"
- },
- "source": [
- "To train an AutoML model, you perform two steps: 1) create a training pipeline, and 2) run the pipeline.\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "dynamic-piece"
- },
- "source": [
- "### Configure a Training Job"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "a509028760a0"
- },
- "source": [
- "An AutoML training pipeline is created with the `AutoMLVideoTrainingJob` 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`: A video classification model.\n",
- " - `object_tracking`: A video object tracking model.\n",
- " - `action_recognition`: A video action recognition model."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "continuous-circular"
- },
- "outputs": [],
- "source": [
- "job = aiplatform.AutoMLVideoTrainingJob(\n",
- " display_name=f\"temp-{TASK_NAME}\",\n",
- " prediction_type=PREDICTION_TYPE,\n",
- " model_type=MODEL_TYPE,\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "juvenile-parameter"
- },
- "source": [
- "### Run the Training Job"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "a3b60132368e"
- },
- "source": [
- "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",
- "- `sync`: If set to True, the call blocks while waiting for the asynchronous batch job to complete.\n",
- "\n",
- "The `run` method when completed returns the model resource.\n",
- "\n",
- "The execution of the training pipeline can take over 24 hours to complete."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "8f135100f5d9"
- },
- "outputs": [],
- "source": [
- "import os\n",
- "import sys\n",
- "\n",
- "if os.getenv(\"IS_TESTING\"):\n",
- " sys.exit(0)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "human-carrier"
- },
- "outputs": [],
- "source": [
- "model = job.run(\n",
- " dataset=dataset,\n",
- " training_fraction_split=0.8,\n",
- " test_fraction_split=0.2,\n",
- " model_display_name=f\"temp-{TASK_NAME}\",\n",
- " sync=False,\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "abstract-textbook"
- },
- "outputs": [],
- "source": [
- "model.wait()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "noted-usage"
- },
- "source": [
- "# Make a Batch Prediction request"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "ruled-smith"
- },
- "source": [
- "### Copy AutoML video demo prediction data for creating batch prediction job"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "polished-dispatch"
- },
- "outputs": [],
- "source": [
- "automl_video_demo_batch_prediction_data = (\n",
- " \"gs://automl-video-demo-data/hmdb_split1_predict.jsonl\"\n",
- ")\n",
- "\n",
- "gcs_source_batch_prediction = (\n",
- " f\"gs://{BUCKET_NAME}/{TASK_NAME}/data/video_classification_batch_prediction.jsonl\"\n",
- ")\n",
- "gcs_destination_prefix_batch_prediction = (\n",
- " f\"gs://{BUCKET_NAME}/{TASK_NAME}/batch_prediction\"\n",
- ")\n",
- "\n",
- "!gsutil cp $automl_video_demo_batch_prediction_data $gcs_source_batch_prediction"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "2bd262d624e3"
- },
- "source": [
- "### Perform batch prediction job on the model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "b488cb43682a"
- },
- "source": [
- "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",
- "- `sync`: If set to True, the call blocks while waiting for the asynchronous batch job to complete."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "piano-middle"
- },
- "outputs": [],
- "source": [
- "batch_predict_job = model.batch_predict(\n",
- " job_display_name=f\"temp-{TASK_NAME}\",\n",
- " gcs_source=gcs_source_batch_prediction,\n",
- " gcs_destination_prefix=gcs_destination_prefix_batch_prediction,\n",
- " sync=False,\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "e06e39558a8e"
- },
- "outputs": [],
- "source": [
- "batch_predict_job.wait()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "8359d586c1e8"
- },
- "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",
- "\n",
- "Prediction response contains following fields\n",
- "\n",
- "- `ids`: The internal assigned unique identifiers for each prediction request.\n",
- "- `displayNames`: The class names for each class label.\n",
- "- `confidences`: The predicted confidence, between 0 and 1, per class label.\n",
- "- `timeSegmentStart`: The time offset in the video to the start of the video sequence.\n",
- "- `timeSegmentEnd`: The time offset in the video to the end of the video sequence."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "visible-scientist"
- },
- "outputs": [],
- "source": [
- "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",
- "client = storage.Client()\n",
- "bucket = client.get_bucket(BUCKET_URI.replace(\"gs://\", \"\"))\n",
- "for prediction_result in prediction_results:\n",
- " gfile_name = f\"{prediction_result}\"\n",
- " data = bucket.blob(gfile_name).download_as_string()\n",
- " data = json.loads(data)\n",
- " print(data)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "0472cd54c140"
- },
- "source": [
- "## Clean up\n",
- "\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:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "2f60a4fb2863"
- },
- "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 training job\n",
- "job.delete()\n",
- "\n",
- "# Delete the batch prediction job using the Vertex batch prediction object\n",
- "batch_predict_job.delete()\n",
- "\n",
- "# Delete the Cloud Storage bucket\n",
- "if delete_bucket:\n",
- " ! gsutil -m rm -r $BUCKET_URI"
- ]
- }
- ],
- "metadata": {
- "colab": {
- "collapsed_sections": [],
- "name": "SDK_AutoML_Video_Classification.ipynb",
- "toc_visible": true
- },
- "kernelspec": {
- "display_name": "Python 3",
- "name": "python3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
diff --git a/notebooks/official/workbench/sentiment_analysis/Sentiment_Analysis.ipynb b/notebooks/official/workbench/sentiment_analysis/Sentiment_Analysis.ipynb
deleted file mode 100644
index 040850107..000000000
--- a/notebooks/official/workbench/sentiment_analysis/Sentiment_Analysis.ipynb
+++ /dev/null
@@ -1,1144 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "18ebbd838e32"
- },
- "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 License 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": "87e8ba66a90f"
- },
- "source": [
- "# Sentiment Analysis using AutoML Natural Language and Vertex AI\n",
- "\n",
- "\n",
- " \n",
- " \n",
- "  Open in Colab\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- "  Open in Colab Enterprise\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- "  View on GitHub\n",
- " \n",
- " | \n",
- " \n",
- "\n",
- "  Open in Vertex AI Workbench\n",
- " \n",
- " | \n",
- "
\n",
- "
"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "fdbaf85c16a5"
- },
- "source": [
- "## Table of contents\n",
- "* [Overview](#section-1)\n",
- "* [Objective](#section-2)\n",
- "* [Dataset](#section-3)\n",
- "* [Costs](#section-4)\n",
- "* [Load the data](#section-5)\n",
- "* [Prepare the training data](#section-6)\n",
- "* [Create a dataset in Vertex AI](#section-7)\n",
- "* [Train the model using Vertex AI](#section-8)\n",
- "* [Deploy the model to the endpoint](#section-9)\n",
- "* [Prediction](#section-10)\n",
- "* [Review visualization](#section-11)\n",
- "* [Clean up](#section-12)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "3b9e88b02478"
- },
- "source": [
- "## Overview\n",
- "\n",
- "\n",
- "This notebook demonstrates how to perform sentiment analysis on a Stanford movie reviews dataset using AutoML Natural Language and how to deploy the sentiment analysis model on Vertex AI to get predictions. \n",
- "\n",
- "*Note: This notebook file was developed to run on a [Vertex AI Workbench managed notebooks](https://console.cloud.google.com/vertex-ai/workbench/list/managed) instance using the Python (Local) kernel. Some components of this notebook may not work in other notebook environments.*\n",
- "\n",
- "Learn more about [Vertex AI Workbench](https://cloud.google.com/vertex-ai/docs/workbench/introduction) and [Sentiment analysis for text data](https://cloud.google.com/vertex-ai/docs/training-overview#sentiment_analysis_for_text)."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "95d6c7b51af4"
- },
- "source": [
- "### Objective\n",
- "\n",
- "\n",
- "In this tutorial, you learn how to train and deploy an AutoML sentiment analysis model, and make predictions.\n",
- "\n",
- "This tutorial uses the following Google Cloud ML services:\n",
- "\n",
- "- Vertex AI dataset\n",
- "- Vertex AI model\n",
- "- AutoML training\n",
- "- Vertex AI Prediction\n",
- "\n",
- "The steps performed are:\n",
- "\n",
- "- Loading the required data. \n",
- "- Preprocessing the data.\n",
- "- Selecting the required data for the model.\n",
- "- Loading the dataset into Vertex AI managed datasets.\n",
- "- Training a sentiment model using AutoML Text training.\n",
- "- Evaluating the model.\n",
- "- Deploying the model on Vertex AI.\n",
- "- Getting predictions.\n",
- "- Clean up."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "3347ff71c571"
- },
- "source": [
- "### Dataset\n",
- "\n",
- "\n",
- "The dataset used in this notebook is a part of the [Stanford Sentiment Treebank dataset](https://nlp.stanford.edu/sentiment/), which consists of phrases from movie reviews and their corresponding sentiment scores."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "2855e681db83"
- },
- "source": [
- "### Costs\n",
- "\n",
- "\n",
- "This tutorial uses the following billable components of Google Cloud:\n",
- "\n",
- "- Vertex AI\n",
- "- Cloud Storage\n",
- "\n",
- "Learn about [Vertex AI pricing](https://cloud.google.com/vertex-ai/pricing) and \n",
- "[Cloud Storage pricing](https://cloud.google.com/storage/pricing), and use the \n",
- "[Pricing Calculator](https://cloud.google.com/products/calculator/) to generate a cost estimate based on your projected usage."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "eda7d4721c1e"
- },
- "outputs": [],
- "source": [
- "! pip3 install --upgrade --quiet wordcloud \\\n",
- " google-cloud-aiplatform \\\n",
- " fsspec \\\n",
- " gcsfs \\\n",
- " pandas "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "58707a750154"
- },
- "source": [
- "### Restart runtime (Colab only)\n",
- "To use the newly installed packages, you must restart the runtime on Google Colab."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "f200f10a1da3"
- },
- "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": "4de1bd77992b"
- },
- "source": [
- ",\n",
- "⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️,\n",
- "
"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "befa6ca14bc0"
- },
- "source": [
- "### Authenticate your notebook environment (Colab only)\n",
- "Authenticate your environment on Google Colab."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "7de6ef0fac42"
- },
- "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": "80b8daedb2c6"
- },
- "source": [
- "### Set Google Cloud project information\n",
- "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.](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "oM1iC_MfAts1"
- },
- "outputs": [],
- "source": [
- "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
- "LOCATION = \"us-central1\" # @param {type:\"string\"}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "zgPO1eR3CYjk"
- },
- "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": "MzGDU7TWdts_"
- },
- "outputs": [],
- "source": [
- "BUCKET_URI = f\"gs://your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "-EcIXiGsCePi"
- },
- "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": "NIq7R4HZCfIc"
- },
- "outputs": [],
- "source": [
- "! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "543fd0a71f4d"
- },
- "source": [
- "### Import libraries"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "5e0383e0444d"
- },
- "outputs": [],
- "source": [
- "import os\n",
- "import random\n",
- "from typing import List, Optional, Union\n",
- "\n",
- "import matplotlib.pyplot as plt\n",
- "import pandas as pd\n",
- "from google.cloud import aiplatform, storage\n",
- "from wordcloud import STOPWORDS, WordCloud"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "5563f402e958"
- },
- "source": [
- "## Load the data \n",
- "\n",
- "\n",
- "Load the phrases and scores of the dataset from the Cloud Storage."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "e9bed419ca1b"
- },
- "outputs": [],
- "source": [
- "phrases = pd.read_csv(\n",
- " \"gs://cloud-samples-data/vertex-ai/managed_notebooks/sentiment_analysis/stanford_sentiment_treebank/sentiment_phrases.txt\",\n",
- " sep=\"|\",\n",
- ")\n",
- "phrases.columns = [\"text\", \"phrase ids\"]\n",
- "scores = pd.read_csv(\n",
- " \"gs://cloud-samples-data/vertex-ai/managed_notebooks/sentiment_analysis/stanford_sentiment_treebank/sentiment_labels.txt\",\n",
- " sep=\"|\",\n",
- ")\n",
- "df = phrases.merge(scores, how=\"left\", on=\"phrase ids\")\n",
- "print(df.head(5))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "d88a752930ac"
- },
- "outputs": [],
- "source": [
- "print(max(df[\"sentiment values\"]), min(df[\"sentiment values\"]))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "c83b75fb3279"
- },
- "source": [
- "Rename the columns because the data itself doesn't contain any feature names. The dataframe **phrases** contains all phrases and their IDs separated by `|` whereas **scores** contains all phrase ids and the corresponding sentiment scores separated by a `|`."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "44443815a2fc"
- },
- "source": [
- "### Create labels \n",
- "\n",
- "The label scores are bucketed into four classes by mapping them into the following ranges:\n",
- "\n",
- "[0, 0.25], (0.25, 0.5], (0.5, 0.75],(0.75, 1.0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "97bbb7218788"
- },
- "outputs": [],
- "source": [
- "# Define the class labels\n",
- "VERYNEGATIVE = 0\n",
- "NEGATIVE = 1\n",
- "POSITIVE = 2\n",
- "VERYPOSITIVE = 3"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "e971f9f576cf"
- },
- "outputs": [],
- "source": [
- "bins = [0, 0.25, 0.5, 0.75, 1]\n",
- "labels = [VERYNEGATIVE, NEGATIVE, POSITIVE, VERYPOSITIVE]\n",
- "df[\"label\"] = pd.cut(df[\"sentiment values\"], bins=bins, labels=labels)\n",
- "print(df.head())"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "87b38faf9a85"
- },
- "source": [
- "## Prepare the training data\n",
- "\n",
- "\n",
- "To train a sentiment analysis model using AutoML Natural Language, you provide samples of the type of content you want to train labeled with a value indicating how the sentiment varies.\n",
- "\n",
- "The sentiment label is encoded as integer ranging from 0 (relatively negative) to a maximum value of your choice (positive). For example, if you want to identify whether the sentiment is negative, positive, or neutral, you would label the training data with sentiment scores of 0 (negative), 1 (neutral), and 2 (positive). If you want to capture more granularity with five levels of sentiment, you can still label documents with the most negative sentiment as 0 and use 4 for the most positive sentiment. In such case, the maximum sentiment score (sentiment_max) for the dataset would be 4."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "edcf89a00954"
- },
- "source": [
- "For training, you select a subset of the orginal data that consists of extreme positive and negative samples. Here the maximum sentiment would be 1. In the **ml_use** column, you provide if the instance belongs to TRAIN, VALIDATION or TEST sample or Vertex AI randomly assign. \n",
- "\n",
- "Each line or instance in the CSV file refers to a single document. The following example shows the general format of a valid CSV file:\n",
- "\n",
- "**[ml_use]**,**gcs_file_uri**|**\"inline_text\"**,**sentiment**,**sentimentMax**\n",
- "\n",
- "For more information visit the [official documentation on preparing the sentiment analysis data.](https://cloud.google.com/vertex-ai/docs/datasets/prepare-text#sentiment-analysis) \n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "245c07c7df7b"
- },
- "source": [
- "### Select a subset of the data"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "e1db0f24d1da"
- },
- "outputs": [],
- "source": [
- "subset_data = df[df[\"label\"].isin([VERYNEGATIVE, VERYPOSITIVE])].reset_index(drop=True)\n",
- "subset_data.head()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "99f314e5a437"
- },
- "outputs": [],
- "source": [
- "subset_data[\"label\"] = subset_data[\"label\"].apply(lambda x: 1 if x == 3 else 0)\n",
- "subset_data[\"ml_use\"] = \"\"\n",
- "subset_data[\"sentimentMax\"] = 1\n",
- "subset_data = subset_data[[\"ml_use\", \"text\", \"label\", \"sentimentMax\"]]\n",
- "print(subset_data.head())"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "c3e54c1f430a"
- },
- "source": [
- "### Create a csv"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "806303530d77"
- },
- "outputs": [],
- "source": [
- "FILE_NAME = \"sentiment_data.csv\"\n",
- "subset_data.to_csv(FILE_NAME, index=False)\n",
- "# Upload the saved model file to Cloud Storage\n",
- "BLOB_PATH = \"sentiment_analysis/\"\n",
- "BLOB_NAME = os.path.join(BLOB_PATH, FILE_NAME)\n",
- "bucket = storage.Client().bucket(BUCKET_URI[5:])\n",
- "blob = bucket.blob(BLOB_NAME)\n",
- "blob.upload_from_filename(FILE_NAME)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "453938b7c88b"
- },
- "source": [
- "## Create a dataset in Vertex AI\n",
- "\n",
- "\n",
- "The following code uses the Vertex AI SDK for Python to create a dataset and to import data. "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "e359a68f6295"
- },
- "outputs": [],
- "source": [
- "def import_data_text_sentiment_analysis(\n",
- " project: str,\n",
- " location: str,\n",
- " display_name: str,\n",
- " src_uris: Union[str, List[str]],\n",
- " sync: bool = True,\n",
- "):\n",
- " aiplatform.init(project=project, location=location)\n",
- "\n",
- " ds = aiplatform.TextDataset.create(\n",
- " display_name=display_name,\n",
- " gcs_source=src_uris,\n",
- " import_schema_uri=aiplatform.schema.dataset.ioformat.text.sentiment,\n",
- " sync=sync,\n",
- " )\n",
- "\n",
- " print(ds.display_name)\n",
- " print(ds.resource_name)\n",
- " return ds"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "bd1f01b376b2"
- },
- "source": [
- "Set a display name for the managed dataset. "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "de95111cf709"
- },
- "outputs": [],
- "source": [
- "DATASET_NAME = \"[your-dataset-display-name]\" # @param {type:\"string\"}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "989313dc7b94"
- },
- "outputs": [],
- "source": [
- "if DATASET_NAME == \"\" or DATASET_NAME == \"[your-dataset-display-name]\":\n",
- " DATASET_NAME = \"sentimentanalysis\"\n",
- "\n",
- "# Create a Vertex AI managed dataset resource\n",
- "src_uris = [f\"{BUCKET_URI}/sentiment_analysis/sentiment_data.csv\"]\n",
- "dataset = import_data_text_sentiment_analysis(\n",
- " PROJECT_ID, LOCATION, DATASET_NAME, src_uris\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "a7c140cb93c0"
- },
- "source": [
- "## Train the model using Vertex AI\n",
- "\n",
- "\n",
- "The following code uses the Vertex AI SDK for Python to train the model on the above created dataset. You can get the dataset id from the dataset section of Vertex AI in the Cloud Console or from the resource name in the dataset object created above. You can specify how the training data is split between the training, validation, and test sets by setting the fraction_split variables.\n",
- "\n",
- "**Note**: The model training job can take more than 120 minutes to finish."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "bd06f6f67a56"
- },
- "outputs": [],
- "source": [
- "def create_training_pipeline_text_sentiment_analysis(\n",
- " project: str,\n",
- " location: str,\n",
- " display_name: str,\n",
- " dataset_id: str,\n",
- " model_display_name: Optional[str] = None,\n",
- " sentiment_max: int = 10,\n",
- " training_fraction_split: float = 0.8,\n",
- " validation_fraction_split: float = 0.1,\n",
- " test_fraction_split: float = 0.1,\n",
- " sync: bool = True,\n",
- "):\n",
- " aiplatform.init(project=project, location=location)\n",
- "\n",
- " job = aiplatform.AutoMLTextTrainingJob(\n",
- " display_name=display_name,\n",
- " prediction_type=\"sentiment\",\n",
- " sentiment_max=sentiment_max,\n",
- " )\n",
- "\n",
- " text_dataset = aiplatform.TextDataset(dataset_id)\n",
- "\n",
- " model = job.run(\n",
- " dataset=text_dataset,\n",
- " model_display_name=model_display_name,\n",
- " training_fraction_split=training_fraction_split,\n",
- " validation_fraction_split=validation_fraction_split,\n",
- " test_fraction_split=test_fraction_split,\n",
- " sync=sync,\n",
- " )\n",
- "\n",
- " print(model.display_name)\n",
- " print(model.resource_name)\n",
- " print(model.uri)\n",
- " return model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "e7d09a8e2a01"
- },
- "source": [
- "Set a display name for the AutoML training job."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "22de78a6fee1"
- },
- "outputs": [],
- "source": [
- "JOB_NAME = \"[your-training-job-display-name]\" # @param {type:\"string\"}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "a0a83ba2be4b"
- },
- "outputs": [],
- "source": [
- "if JOB_NAME == \"\" or JOB_NAME == \"[your-training-job-display-name]\":\n",
- " JOB_NAME = \"sentimentanalysis_job\"\n",
- "\n",
- "# Using the created Text dataset, create and run the training job\n",
- "dataset_id = dataset.resource_name.split(\"/\")[-1]\n",
- "print(dataset_id)\n",
- "model = create_training_pipeline_text_sentiment_analysis(\n",
- " PROJECT_ID, LOCATION, JOB_NAME, dataset_id, sentiment_max=1\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "077f319a4218"
- },
- "source": [
- "## Deploy the model to the endpoint\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "cd4463310e5a"
- },
- "source": [
- "### Create endpoint\n",
- "Create a Vertex AI endpoint to deploy the model."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "473ff65e0b88"
- },
- "outputs": [],
- "source": [
- "def create_endpoint(\n",
- " project: str,\n",
- " display_name: str,\n",
- " location: str,\n",
- "):\n",
- " aiplatform.init(project=project, location=location)\n",
- "\n",
- " endpoint = aiplatform.Endpoint.create(\n",
- " display_name=display_name,\n",
- " project=project,\n",
- " location=location,\n",
- " )\n",
- "\n",
- " print(endpoint.display_name)\n",
- " print(endpoint.resource_name)\n",
- " return endpoint"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "8fc75934c34c"
- },
- "source": [
- "Set a display name for the endpoint."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "fbef44c69575"
- },
- "outputs": [],
- "source": [
- "ENDPOINT_NAME = \"[your-endpoint-name]\" # @param {type:\"string\"}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "54785032708f"
- },
- "outputs": [],
- "source": [
- "if ENDPOINT_NAME == \"[your-endpoint-name]\":\n",
- " ENDPOINT_NAME = \"sentiment-analysis-endpoint\"\n",
- "\n",
- "# Create Endpoint\n",
- "endpoint = create_endpoint(PROJECT_ID, ENDPOINT_NAME, LOCATION)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "f84df96c9828"
- },
- "source": [
- "### Deploy the model\n",
- "\n",
- "The following code uses the Vertex AI SDK for Python to deploy the model to a endpoint."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "6c520e2f7f8b"
- },
- "outputs": [],
- "source": [
- "model.deploy(endpoint=endpoint)\n",
- "\n",
- "print(model.display_name)\n",
- "print(model.resource_name)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "9bcd4d7a11de"
- },
- "source": [
- "## Prediction\n",
- "\n",
- "\n",
- "After deploying the model to an endpoint use the Vertex AI SDK to request an online prediction. Filter the data that you haven't used for the training and pick longer reviews to test the model."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "7fa5e1a01a77"
- },
- "outputs": [],
- "source": [
- "def predict_text_sentiment_analysis_sample(endpoint, content):\n",
- " print(content)\n",
- " response = endpoint.predict(instances=[{\"content\": content}], parameters={})\n",
- "\n",
- " for prediction_ in response.predictions:\n",
- " print(prediction_)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "817ba5968e22"
- },
- "outputs": [],
- "source": [
- "test_data_pos = df[df[\"label\"].isin([POSITIVE])].reset_index(drop=True)\n",
- "test_data_neg = df[df[\"label\"].isin([NEGATIVE])].reset_index(drop=True)\n",
- "\n",
- "test_data_neg = test_data_neg.text.values[200:]\n",
- "random.shuffle(test_data_neg)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "3fed98fbb6d7"
- },
- "outputs": [],
- "source": [
- "test_data_pos = test_data_pos.text.values[200:]\n",
- "random.shuffle(test_data_pos)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "963102b04a59"
- },
- "source": [
- "Here is the prediction results on the positive samples. The model did a good job on predicting positive sentiment for positive reviews. The first and last review predictions are false negatives. "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "5d7b1d59c0d0"
- },
- "outputs": [],
- "source": [
- "for review in test_data_pos[0:10]:\n",
- " predict_text_sentiment_analysis_sample(endpoint, review)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "6fc9dbc3436e"
- },
- "source": [
- "Here is the prediction results on the negative reviews. Out of 10 reviews below 7 negative reviews are correctly predicted with negative sentiment"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "53830cc82fc5"
- },
- "outputs": [],
- "source": [
- "for review in test_data_neg[0:10]:\n",
- " predict_text_sentiment_analysis_sample(endpoint, review)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "aa6a2abc2006"
- },
- "source": [
- "## Review visualization\n",
- "\n",
- "\n",
- "Visualize the positive and negative reviews in the data."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "b08c4be897ce"
- },
- "outputs": [],
- "source": [
- "data_pos = df[df[\"label\"].isin([VERYPOSITIVE])].reset_index(drop=True)\n",
- "data_neg = df[df[\"label\"].isin([VERYNEGATIVE])].reset_index(drop=True)\n",
- "\n",
- "data_neg = data_neg.text.values"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "27d61dcee56a"
- },
- "outputs": [],
- "source": [
- "data_pos = data_pos.text.values"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "9ddc8e1dfaab"
- },
- "source": [
- "Create the word cloud by removing the common words to highlight the words representing positive and negative samples."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "8f48fa9258c3"
- },
- "outputs": [],
- "source": [
- "# Python program to generate WordCloud\n",
- "def plot_word_cloud(data, common_words):\n",
- " comment_words = \"\"\n",
- " stopwords = set(STOPWORDS)\n",
- " for val in data:\n",
- " tokens = val.split()\n",
- " for i in range(len(tokens)):\n",
- " tokens[i] = tokens[i].lower()\n",
- " for each in common_words:\n",
- " if each in tokens[i]:\n",
- " tokens[i] = \"\"\n",
- " break\n",
- "\n",
- " comment_words += \" \".join(tokens) + \" \"\n",
- "\n",
- " wordcloud = WordCloud(\n",
- " width=800,\n",
- " height=800,\n",
- " background_color=\"white\",\n",
- " stopwords=stopwords,\n",
- " min_font_size=10,\n",
- " ).generate(comment_words)\n",
- "\n",
- " plt.figure(figsize=(8, 8), facecolor=None)\n",
- " plt.imshow(wordcloud)\n",
- " plt.axis(\"off\")\n",
- " plt.tight_layout(pad=0)\n",
- "\n",
- " plt.show()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "09e568ad3526"
- },
- "source": [
- "Plot a word cloud of negative reviews."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "4fd9bf528bb4"
- },
- "outputs": [],
- "source": [
- "plot_word_cloud(\n",
- " data_neg,\n",
- " [\n",
- " \"movie\",\n",
- " \"film\",\n",
- " \"story\",\n",
- " \"audience\",\n",
- " \"director\",\n",
- " \"watch\",\n",
- " \"seem\",\n",
- " \"world\",\n",
- " \"one\",\n",
- " \"make\",\n",
- " \"way\",\n",
- " \"character\",\n",
- " \"much\",\n",
- " \"time\",\n",
- " \"even\",\n",
- " \"take\",\n",
- " \"s\",\n",
- " \"n't\",\n",
- " \"will\",\n",
- " \"may\",\n",
- " \"re\",\n",
- " \"plot\",\n",
- " \"good\",\n",
- " \"comedy\",\n",
- " \"made\",\n",
- " ],\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "cd2c9686efbc"
- },
- "source": [
- "Plot a word cloud of positive reviews."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "2fb8c50e991b"
- },
- "outputs": [],
- "source": [
- "plot_word_cloud(\n",
- " data_pos,\n",
- " [\n",
- " \"movie\",\n",
- " \"film\",\n",
- " \"story\",\n",
- " \"audience\",\n",
- " \"director\",\n",
- " \"watch\",\n",
- " \"seem\",\n",
- " \"world\",\n",
- " \"one\",\n",
- " \"make\",\n",
- " \"way\",\n",
- " \"character\",\n",
- " \"much\",\n",
- " \"time\",\n",
- " \"even\",\n",
- " \"take\",\n",
- " \"s\",\n",
- " \"n't\",\n",
- " \"will\",\n",
- " \"may\",\n",
- " \"re\",\n",
- " \"plot\",\n",
- " \"made\",\n",
- " ],\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "c169667d92e8"
- },
- "source": [
- "## Clean up\n",
- "\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:"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "577c85781223"
- },
- "source": [
- "Set `delete_bucket` to **True** to delete the Cloud Storage bucket."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "c72329be87c8"
- },
- "outputs": [],
- "source": [
- "# Undeploy the model from the endpoint\n",
- "endpoint.undeploy_all()\n",
- "\n",
- "# Delete the endpoint\n",
- "endpoint.delete()\n",
- "\n",
- "# Delete the model\n",
- "model.delete()\n",
- "\n",
- "# Delete the dataset\n",
- "dataset.delete()\n",
- "\n",
- "# Removes locally generated file\n",
- "! rm -r $FILE_NAME\n",
- "\n",
- "delete_bucket = False\n",
- "# Delete the Cloud storge bucket\n",
- "if delete_bucket:\n",
- " ! gsutil -m rm -r $BUCKET_URI"
- ]
- }
- ],
- "metadata": {
- "colab": {
- "name": "Sentiment_Analysis.ipynb",
- "toc_visible": true
- },
- "kernelspec": {
- "display_name": "Python 3",
- "name": "python3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}