diff --git a/notebooks/official/automl/automl-text-classification.ipynb b/notebooks/official/automl/automl-text-classification.ipynb deleted file mode 100644 index 2372b7bc7..000000000 --- a/notebooks/official/automl/automl-text-classification.ipynb +++ /dev/null @@ -1,920 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "ur8xi4C7S06n" - }, - "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 Lice`nse is distributed on an \"AS IS\" BASIS,\n", - "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", - "# See the License for the specific language governing permissions and\n", - "# limitations under the License." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "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": "0d2298941703" - }, - "source": [ - "# Vertex AI: Create, train, and deploy an AutoML text classification model" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "JAPoU8Sm5E6e" - }, - "source": [ - "\n", - "\n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \"Colab Run in Colab\n", - " \n", - " \n", - " \n", - " \"GitHub\n", - " View on GitHub\n", - " \n", - " \n", - " \n", - " \"Vertex\n", - " Open in Vertex AI Workbench\n", - " \n", - "
" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "1adb10a59bc3" - }, - "source": [ - "## Overview\n", - "\n", - "This notebook walks you through the major phases of building and using an AutoML text classification model on [Vertex AI](https://cloud.google.com/vertex-ai/docs/). \n", - "\n", - "Learn more about [Classification for text data](https://cloud.google.com/vertex-ai/docs/training-overview#classification_for_text)." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "9b9824ae2c91" - }, - "source": [ - "### Objective\n", - "\n", - "In this tutorial, you learn how to use AutoML to train a text classification model.\n", - "\n", - "This tutorial uses the following Google Cloud ML services:\n", - "\n", - "- AutoML training\n", - "- Vertex AI model resource\n", - "\n", - "The steps performed include:\n", - "\n", - "* Create a Vertex AI dataset.\n", - "* Train an AutoML text classification model resource.\n", - "* Obtain the evaluation metrics for the model resource.\n", - "* Create an endpoint resource.\n", - "* Deploy the model resource to the endpoint resource.\n", - "* Make an online prediction\n", - "* Make a batch prediction" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "f67c62885df4" - }, - "source": [ - "### Dataset\n", - "\n", - "In this notebook, you use the \"Happy Moments\" sample dataset to train a model. The resulting model classifies happy moments into categores that reflect the causes of happiness. " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "0259a7ce8120" - }, - "source": [ - "### Costs\n", - "\n", - "This tutorial uses billable components of Google Cloud:\n", - "\n", - "* Vertex AI training and serving\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": "db52a0a61fca" - }, - "source": [ - "### Installation\n", - "\n", - "Install the following packages for executing this notebook." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "b75757581291" - }, - "outputs": [], - "source": [ - "# install packages\n", - "! pip3 install --upgrade --quiet google-cloud-aiplatform \\\n", - " google-cloud-storage \\\n", - " jsonlines " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "e9255e3b156f" - }, - "source": [ - "### Colab Only: Uncomment the following cell to restart the kernel" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "0c0b2427998a" - }, - "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": "435b8e413535" - }, - "source": [ - "### Before you begin\n", - "\n", - "#### 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": "be175254a715" - }, - "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": "2e6b8b324ce1" - }, - "source": [ - "#### Region\n", - "\n", - "You can also change the `REGION` variable used by Vertex AI. \n", - "Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "ae43d96c4b1b" - }, - "outputs": [], - "source": [ - "REGION = \"[your-region]\" # @param {type: \"string\"}" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "6c43a8673066" - }, - "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 since you're already authenticated.\n", - "\n", - "**2. Local JupyterLab Instance,** uncomment and run." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "fbc9cd30cc4b" - }, - "outputs": [], - "source": [ - "# ! gcloud auth login" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cd0da2c26879" - }, - "source": [ - "**3. Colab,** uncomment and run:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "a336a05c6149" - }, - "outputs": [], - "source": [ - "# from google.colab import auth\n", - "# auth.authenticate_user()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "0461097edfa5" - }, - "source": [ - "**4. Service Account or other**\n", - "- See all the authentication options here: [Google Cloud Platform Jupyter Notebook Authentication Guide](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/notebook_authentication_guide.ipynb)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "e5755d1a554f" - }, - "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": "d2de92accb67" - }, - "outputs": [], - "source": [ - "BUCKET_NAME = f\"your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}\n", - "BUCKET_URI = f\"gs://{BUCKET_NAME}\"" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "b72bfdf29dae" - }, - "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": "a4453435d115" - }, - "outputs": [], - "source": [ - "! gsutil mb -l {REGION} {BUCKET_URI}" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "93d685084cf2" - }, - "source": [ - "### Import libraries and define constants" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "152013538e59" - }, - "outputs": [], - "source": [ - "import jsonlines\n", - "from google.cloud import aiplatform, storage\n", - "from google.cloud.aiplatform import jobs" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "03101a4492f3" - }, - "source": [ - "### Initialize Vertex AI \n", - "\n", - "Initialize the Vertex AI SDK for Python for your project." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "740cd5c67c79" - }, - "outputs": [], - "source": [ - "aiplatform.init(project=PROJECT_ID, location=REGION)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "32c971919605" - }, - "source": [ - "## Create a dataset resource and import your data\n", - "\n", - "The notebook uses the 'Happy Moments' dataset for demonstration purposes. You can change it to another text classification dataset that [conforms to the data preparation requirements](https://cloud.google.com/vertex-ai/docs/datasets/prepare-text#classification).\n", - "\n", - "Using the Python SDK, create a dataset and import the dataset in one call to `TextDataset.create()`, as shown in the following cell.\n", - "\n", - "Creating and importing data is a long running operation. This next step can take a while. The `create()` method waits for the operation to complete, outputting statements as the operation progresses. The statements contain the full name of the dataset used in the following section.\n", - "\n", - "**Note**: You can close the noteboook while waiting for this operation to complete. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "d35b8b6b94ae" - }, - "outputs": [], - "source": [ - "# Use a timestamp to ensure unique resources\n", - "src_uris = \"gs://cloud-ml-data/NL-classification/happiness.csv\"\n", - "display_name = \"e2e-text-dataset-unique\"\n", - "\n", - "text_dataset = aiplatform.TextDataset.create(\n", - " display_name=display_name,\n", - " gcs_source=src_uris,\n", - " import_schema_uri=aiplatform.schema.dataset.ioformat.text.single_label_classification,\n", - " sync=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "68f10356cab9" - }, - "source": [ - "## Train your text classification model\n", - "\n", - "Now you can begin training your model. Training the model is a two part process:\n", - "\n", - "1. **Define the training job.** You must provide a display name and the type of training you want when defining the training job.\n", - "2. **Run the training job.** When you run the training job, you need to supply a reference to the dataset to use for training. You can also configure the data split percentages.\n", - "\n", - "You don't need to specify [data splits](https://cloud.google.com/vertex-ai/docs/general/ml-use). The training job has a default setting of training 80%/ testing 10%/ validate 10% if you don't provide values.\n", - "\n", - "To train your model, you call `AutoMLTextTrainingJob.run()` as shown in the following snippets. The method returns a reference to your new model object.\n", - "\n", - "As with importing data into the dataset, training your model can take a substantial amount of time. The client library prints out operation status messages while the training pipeline operation processes. You must wait for the training process to complete before you can get the resource name and ID of your new model, which is required for model evaluation and model deployment.\n", - "\n", - "**Note**: You can close the notebook while waiting for the operation to complete." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "0aa0f01805ea" - }, - "outputs": [], - "source": [ - "# Define the training job\n", - "training_job_display_name = \"e2e-text-training-job-unique\"\n", - "job = aiplatform.AutoMLTextTrainingJob(\n", - " display_name=training_job_display_name,\n", - " prediction_type=\"classification\",\n", - " multi_label=False,\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "1ec60baf2c51" - }, - "outputs": [], - "source": [ - "model_display_name = \"e2e-text-classification-model-unique\"\n", - "\n", - "# Run the training job\n", - "model = job.run(\n", - " dataset=text_dataset,\n", - " model_display_name=model_display_name,\n", - " training_fraction_split=0.1,\n", - " validation_fraction_split=0.1,\n", - " test_fraction_split=0.1,\n", - " sync=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "caaa3f32b12e" - }, - "source": [ - "## Review model evaluation scores\n", - "\n", - "After your model training has finished, you can review the evaluation scores for it using the `list_model_evaluations()` method. This method returns an iterator for each evaluation slice." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "b0bb6be8621a" - }, - "outputs": [], - "source": [ - "model_evaluations = model.list_model_evaluations()\n", - "\n", - "for model_evaluation in model_evaluations:\n", - " print(model_evaluation.to_dict())" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "b5dbe4dbaa60" - }, - "source": [ - "## Deploy your text classification model\n", - "\n", - "Once your model has completed training, you must deploy it to an _endpoint_ to get online predictions from the model. When you deploy the model to an endpoint, a copy of the model is made on the endpoint with a new resource name and display name.\n", - "\n", - "You can deploy multiple models to the same endpoint and split traffic between the various models assigned to the endpoint. However, you must deploy one model at a time to the endpoint. To change the traffic split percentages, you must assign new values on your second (and subsequent) models each time you deploy a new model.\n", - "\n", - "The following code block demonstrates how to deploy a model. The code snippet relies on the Python SDK to create a new endpoint for deployment. The call to `modely.deploy()` returns a reference to an endpoint object--you need this reference for online predictions in the next section." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "19bc4a55ccfe" - }, - "outputs": [], - "source": [ - "deployed_model_display_name = \"e2e-deployed-text-classification-model-unique\"\n", - "\n", - "endpoint = model.deploy(\n", - " deployed_model_display_name=deployed_model_display_name, sync=True\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "351a6e8be3a5" - }, - "source": [ - "## Get online predictions from your model\n", - "\n", - "Now that you have your endpoint, you can get online predictions from the text classification model. To get the online prediction, you send a prediction request to your endpoint." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "953b333fc0fc" - }, - "outputs": [], - "source": [ - "content = \"I got a high score on my math final!\"\n", - "\n", - "response = endpoint.predict(instances=[{\"content\": content}])\n", - "\n", - "for prediction_ in response.predictions:\n", - " ids = prediction_[\"ids\"]\n", - " display_names = prediction_[\"displayNames\"]\n", - " confidence_scores = prediction_[\"confidences\"]\n", - " for count, id in enumerate(ids):\n", - " print(f\"Prediction ID: {id}\")\n", - " print(f\"Prediction display name: {display_names[count]}\")\n", - " print(f\"Prediction confidence score: {confidence_scores[count]}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "f18811cd0477" - }, - "source": [ - "## Get batch predictions from your model\n", - "\n", - "You can get batch predictions from a text classification model without deploying it. You must first format all of your prediction instances (prediction input) in JSONL format and store the JSONL file in a Google Cloud storage bucket. You must also provide a Google Cloud storage bucket to hold your prediction output.\n", - "\n", - "To start, you must first create your predictions input file in JSONL format. Each line in the JSONL document needs to be formatted as follows:\n", - "\n", - "```\n", - "{ \"content\": \"gs://sourcebucket/datasets/texts/source_text.txt\", \"mimeType\": \"text/plain\"}\n", - "```\n", - "\n", - "The `content` field in the JSON structure must be a Google Cloud Storage URI to another document that contains the text input for prediction.\n", - "[See the documentation for more information.](https://cloud.google.com/ai-platform-unified/docs/predictions/batch-predictions#text)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "e4b838cbcd99" - }, - "outputs": [], - "source": [ - "instances = [\n", - " \"We hiked through the woods and up the hill to the ice caves\",\n", - " \"My kitten is so cute\",\n", - "]\n", - "input_file_name = \"batch-prediction-input.jsonl\"" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "76ac422ab8dd" - }, - "source": [ - "For batch prediction, supply the following:\n", - "\n", - "+ All of your prediction instances as individual files on Google Cloud Storage, as TXT files for your instances.\n", - "+ A JSONL file that lists the URIs of all your prediction instances.\n", - "+ A Cloud Storage bucket to hold the output from batch prediction.\n", - "\n", - "For this tutorial, the following cells create a new Storage bucket, upload individual prediction instances as text files to the bucket, and then create the JSONL file with the URIs of your prediction instances." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "8b7cabbb86ad" - }, - "outputs": [], - "source": [ - "# Instantiate the Storage client and create the new bucket\n", - "# from google.cloud import storage\n", - "storage_client = storage.Client()\n", - "bucket = storage_client.get_bucket(BUCKET_NAME)\n", - "# Iterate over the prediction instances, creating a new TXT file\n", - "# for each.\n", - "input_file_data = []\n", - "for count, instance in enumerate(instances):\n", - " instance_name = f\"input_{count}.txt\"\n", - " instance_file_uri = f\"{BUCKET_URI}/{instance_name}\"\n", - " # Add the data to store in the JSONL input file.\n", - " tmp_data = {\"content\": instance_file_uri, \"mimeType\": \"text/plain\"}\n", - " input_file_data.append(tmp_data)\n", - "\n", - " # Create the new instance file\n", - " blob = bucket.blob(instance_name)\n", - " blob.upload_from_string(instance)\n", - "\n", - "input_str = \"\\n\".join([str(d) for d in input_file_data])\n", - "file_blob = bucket.blob(f\"{input_file_name}\")\n", - "file_blob.upload_from_string(input_str)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "31c262320610" - }, - "source": [ - "Now that you have the bucket with the prediction instances ready, you can send a batch prediction https://storage.googleapis.com/upload/storage/v1/b/gs://vertex-ai-devaip-20220728004429/o?uploadType=multipartequest to Vertex AI. When you send a request to the service, you must provide the URI of your JSONL file and your output bucket, including the `gs://` protocols.\n", - "\n", - "With the Python SDK, you can create a batch prediction job by calling `Model.batch_predict()`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "f5ab2139d52d" - }, - "outputs": [], - "source": [ - "job_display_name = \"e2e-text-classification-batch-prediction-job\"\n", - "# model = aiplatform.Model(model_name=model.name)\n", - "batch_prediction_job = model.batch_predict(\n", - " job_display_name=job_display_name,\n", - " gcs_source=f\"{BUCKET_URI}/{input_file_name}\",\n", - " gcs_destination_prefix=f\"{BUCKET_URI}/output\",\n", - " sync=True,\n", - ")\n", - "batch_prediction_job_name = batch_prediction_job.resource_name" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "11503f2e08a2" - }, - "source": [ - "Once the batch prediction job completes, the Python SDK prints out the resource name of the batch prediction job in the format `projects/[PROJECT_ID]/locations/[LOCATION]/batchPredictionJobs/[BATCH_PREDICTION_JOB_ID]`. You can query the Vertex AI service for the status of the batch prediction job using its ID.\n", - "\n", - "The following code snippet demonstrates how to create an instance of the `BatchPredictionJob` class to review its status. Note that you need the full resource name printed out from the Python SDK for this snippet.\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cd014de40e2f" - }, - "source": [ - "## Batch prediction job" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "bf6e614723ed" - }, - "outputs": [], - "source": [ - "batch_job = jobs.BatchPredictionJob(batch_prediction_job_name)\n", - "print(f\"Batch prediction job state: {str(batch_job.state)}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "1f9a12dadf6f" - }, - "source": [ - "After the batch job has completed, you can view the results of the job in your output Storage bucket. You might want to first list all of the files in your output bucket to find the URI of the output file." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "8ff1ec03205c" - }, - "outputs": [], - "source": [ - "BUCKET_OUTPUT = f\"{BUCKET_URI}/output\"\n", - "\n", - "! gsutil ls -a $BUCKET_OUTPUT" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "52f3f8af2e41" - }, - "source": [ - "The output from the batch prediction job should be contained in a folder (or _prefix_) that includes the name of the batch prediction job plus a time stamp for when it was created.\n", - "\n", - "For example, if your batch prediction job name is `my-job` and your bucket name is `my-bucket`, the URI of the folder containing your output might look like the following:\n", - "\n", - "```\n", - "gs://my-bucket/output/prediction-my-job-2021-06-04T19:54:25.889262Z/\n", - "```\n", - "\n", - "To read the batch prediction results, you must download the file locally and open the file. The next cell copies all of the files in the `BUCKET_OUTPUT_FOLDER` into a local folder." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "4bb16e040942" - }, - "outputs": [], - "source": [ - "import os\n", - "\n", - "RESULTS_DIRECTORY = \"prediction_results\"\n", - "RESULTS_DIRECTORY_FULL = f\"{RESULTS_DIRECTORY}/output\"\n", - "\n", - "# Create missing directories\n", - "os.makedirs(RESULTS_DIRECTORY, exist_ok=True)\n", - "\n", - "# Get the Cloud Storage paths for each result\n", - "! gsutil -m cp -r $BUCKET_OUTPUT $RESULTS_DIRECTORY\n", - "\n", - "# Get most recently modified directory\n", - "latest_directory = max(\n", - " (\n", - " os.path.join(RESULTS_DIRECTORY_FULL, d)\n", - " for d in os.listdir(RESULTS_DIRECTORY_FULL)\n", - " ),\n", - " key=os.path.getmtime,\n", - ")\n", - "\n", - "print(f\"Local results folder: {latest_directory}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "e375109b7e40" - }, - "source": [ - "## Review results" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "f406e1e4d5ec" - }, - "source": [ - "With all of the results files downloaded locally, you can open them and read the results. In this tutorial, you use the [`jsonlines`](https://jsonlines.readthedocs.io/en/latest/) library to read the output results.\n", - "\n", - "The following cell opens up the JSONL output file and then prints the predictions for each instance." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "91d7f2a74a7c" - }, - "outputs": [], - "source": [ - "# Get downloaded results in directory\n", - "results_files = []\n", - "for dirpath, _, files in os.walk(latest_directory):\n", - " for file in files:\n", - " if file.find(\"predictions\") >= 0:\n", - " results_files.append(os.path.join(dirpath, file))\n", - "\n", - "\n", - "# Consolidate all the results into a list\n", - "results = []\n", - "for results_file in results_files:\n", - " # Open each result\n", - " with jsonlines.open(results_file) as reader:\n", - " for result in reader.iter(type=dict, skip_invalid=True):\n", - " instance = result[\"instance\"]\n", - " prediction = result[\"prediction\"]\n", - " print(f\"\\ninstance: {instance['content']}\")\n", - " for key, output in prediction.items():\n", - " print(f\"\\n{key}: {output}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "af3874f08502" - }, - "source": [ - "## Cleaning up\n", - "\n", - "To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud 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", - "* Training job\n", - "* Model\n", - "* Endpoint\n", - "* Batch prediction\n", - "* Batch prediction bucket" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "adce73b48b72" - }, - "outputs": [], - "source": [ - "delete_bucket = False\n", - "\n", - "if delete_bucket or os.getenv(\"IS_TESTING\"):\n", - " ! gsutil rm -r $BUCKET_URI\n", - "\n", - "# Delete batch\n", - "batch_job.delete()\n", - "\n", - "# Undeploy endpoint\n", - "endpoint.undeploy_all()\n", - "\n", - "# `force` parameter ensures that models are undeployed before deletion\n", - "endpoint.delete()\n", - "\n", - "# Delete model\n", - "model.delete()\n", - "\n", - "# Delete text dataset\n", - "text_dataset.delete()\n", - "\n", - "# Delete training job\n", - "job.delete()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "fa6a8c434c79" - }, - "source": [ - "## Next steps\n", - "\n", - "After completing this tutorial, see the following documentation pages to learn more about Vertex AI:\n", - "\n", - "* [Preparing text training data](https://cloud.google.com/vertex-ai/docs/training-overview#text_data)\n", - "* [Training an AutoML model using the API](https://cloud.google.com/vertex-ai/docs/training-overview#automl)\n", - "* [Evaluating AutoML models](https://cloud.google.com/vertex-ai/docs/training-overview#automl)\n", - "* [Deploying a model using ther Vertex AI API](https://cloud.google.com/vertex-ai/docs/predictions/overview#model_deployment)\n", - "* [Getting online predictions from AutoML models](https://cloud.google.com/vertex-ai/docs/predictions/overview#model_deployment)\n", - "* [Getting batch predictions](https://cloud.google.com/vertex-ai/docs/predictions/overview#batch_predictions)" - ] - } - ], - "metadata": { - "colab": { - "name": "automl-text-classification.ipynb", - "toc_visible": true - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/notebooks/official/automl/automl_text_sentiment_analysis_batch_prediction.ipynb b/notebooks/official/automl/automl_text_sentiment_analysis_batch_prediction.ipynb deleted file mode 100644 index a5c6b2279..000000000 --- a/notebooks/official/automl/automl_text_sentiment_analysis_batch_prediction.ipynb +++ /dev/null @@ -1,834 +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" - }, - "source": [ - "# AutoML training text sentiment analysis model for batch prediction\n", - "\n", - "\n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \"Colab Run in Colab\n", - " \n", - " \n", - " \n", - " \"GitHub\n", - " View on GitHub\n", - " \n", - " \n", - " \n", - " \"Vertex\n", - " Open in Vertex AI Workbench\n", - " \n", - "
\n", - "


" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "overview:automl" - }, - "source": [ - "## Overview\n", - "\n", - "\n", - "This tutorial demonstrates how to use the Vertex AI SDK to create text sentiment analysis models and do batch prediction using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "objective:automl,training,batch_prediction" - }, - "source": [ - "### Objective\n", - "\n", - "In this tutorial, you create an AutoML text sentiment analysis model from a Python script, and then do a batch prediction using the Vertex SDK. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n", - "\n", - "The steps performed include:\n", - "\n", - "- Create a Vertex `Dataset` resource.\n", - "- Train the model.\n", - "- View the model evaluation.\n", - "- Make a batch prediction.\n", - "\n", - "There is one key difference between using batch prediction and using online prediction:\n", - "\n", - "* Prediction Service: Does an on-demand prediction for the entire set of instances (i.e., one or more data items) and returns the results in real-time.\n", - "\n", - "* Batch Prediction Service: Does a queued (batch) prediction for the entire set of instances in the background and stores the results in a Cloud Storage bucket when ready." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "dataset: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 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": "db52a0a61fca" - }, - "source": [ - "### Installation\n", - "\n", - "Install the following packages for executing this notebook." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "install_aip:mbsdk" - }, - "outputs": [], - "source": [ - "! pip3 install --upgrade google-cloud-aiplatform -q\n", - "\n", - "! pip3 install --upgrade tensorflow -q" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "e9255e3b156f" - }, - "source": [ - "### Colab Only: Uncomment the following cell to restart the kernel" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "0c0b2427998a" - }, - "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": "435b8e413535" - }, - "source": [ - "### Before you begin\n", - "\n", - "#### 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": "2e6b8b324ce1" - }, - "source": [ - "#### Region\n", - "\n", - "You can also change the `REGION` variable used by Vertex AI. \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\"}" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "6c43a8673066" - }, - "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": "fbc9cd30cc4b" - }, - "outputs": [], - "source": [ - "# ! gcloud auth login" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cd0da2c26879" - }, - "source": [ - "**3. Colab,** uncomment and run:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "a336a05c6149" - }, - "outputs": [], - "source": [ - "# from google.colab import auth\n", - "# auth.authenticate_user()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "0461097edfa5" - }, - "source": [ - "**4. Service Account or other**\n", - "- See all the authentication options here: [Google Cloud Platform Jupyter Notebook Authentication Guide](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/notebook_authentication_guide.ipynb)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "e5755d1a554f" - }, - "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": "d2de92accb67" - }, - "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 aiplatform" - ] - }, - { - "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": [ - "aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "tutorial_start:automl" - }, - "source": [ - "# Tutorial\n", - "\n", - "Now you are ready to start creating your own AutoML text sentiment analysis model." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "import_file:u_dataset,csv" - }, - "source": [ - "#### Location of Cloud Storage training data.\n", - "\n", - "Now set the variable `IMPORT_FILE` to the location of the CSV index file in Cloud Storage." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "import_file: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_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 = aiplatform.TextDataset.create(\n", - " display_name=\"Crowdflower Claritin-Twitter\",\n", - " gcs_source=[IMPORT_FILE],\n", - " import_schema_uri=aiplatform.schema.dataset.ioformat.text.sentiment,\n", - ")\n", - "\n", - "print(dataset.resource_name)" - ] - }, - { - "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 = aiplatform.AutoMLTextTrainingJob(\n", - " display_name=\"claritin\",\n", - " prediction_type=\"sentiment\",\n", - " sentiment_max=SENTIMENT_MAX,\n", - ")\n", - "\n", - "print(dag)" - ] - }, - { - "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=\"claritin\",\n", - " training_fraction_split=0.8,\n", - " validation_fraction_split=0.1,\n", - " test_fraction_split=0.1,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "caaa3f32b12e" - }, - "source": [ - "## Review model evaluation scores\n", - "\n", - "After your model training has finished, you can review the evaluation scores for it using the `list_model_evaluations()` method. This method will return an iterator for each evaluation slice." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "b0bb6be8621a" - }, - "outputs": [], - "source": [ - "model_evaluations = model.list_model_evaluations()\n", - "\n", - "for model_evaluation in model_evaluations:\n", - " print(model_evaluation.to_dict())" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "make_prediction" - }, - "source": [ - "## Send a batch prediction request\n", - "\n", - "Send a batch prediction to your model." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "get_test_items:batch_prediction" - }, - "source": [ - "### Get test item(s)\n", - "\n", - "Now do a batch prediction to your Vertex model. You will use arbitrary examples out of the dataset as a test items. Don't be concerned that the examples were likely used in training the model -- we just want to demonstrate how to make a prediction." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "get_test_items:automl,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 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=\"claritin\",\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_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": "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": "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", - "if delete_bucket or os.getenv(\"IS_TESTING\"):\n", - " ! gsutil rm -r $BUCKET_URI\n", - "\n", - "# Delete batch\n", - "batch_predict_job.delete()\n", - "\n", - "# Delete model\n", - "model.delete()\n", - "\n", - "# Delete text dataset\n", - "dataset.delete()\n", - "\n", - "# Delete training job\n", - "dag.delete()" - ] - } - ], - "metadata": { - "colab": { - "name": "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/automl/get_started_automl_training.ipynb b/notebooks/official/automl/get_started_automl_training.ipynb index ebca9d9a9..84dcaa26c 100644 --- a/notebooks/official/automl/get_started_automl_training.ipynb +++ b/notebooks/official/automl/get_started_automl_training.ipynb @@ -1428,863 +1428,6 @@ "endpoint.delete()" ] }, - { - "cell_type": "markdown", - "metadata": { - "id": "automl_text_intro" - }, - "source": [ - "## AutoML text models\n", - "\n", - "AutoML can train the following types of text models:\n", - "\n", - "- classification\n", - "- sentiment analysis\n", - "- entity extraction\n", - "\n", - "Learn more about [AutoML Model Types](https://cloud.google.com/vertex-ai/docs/start/automl-model-types)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "data_preparation:text,u_dataset" - }, - "source": [ - "### Data preparation\n", - "\n", - "The Vertex AI `Dataset` resource for text has a couple of requirements for your text data.\n", - "\n", - "- Text examples must be stored in a CSV or JSONL file.\n", - "\n", - "Learn more about [Preparing text data](https://cloud.google.com/vertex-ai/docs/datasets/prepare-text)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "data_import_format:tcn,u_dataset,csv" - }, - "source": [ - "#### CSV\n", - "\n", - "For text classification, the CSV file has a few requirements:\n", - "\n", - "- No heading.\n", - "- First column is the text example or Cloud Storage path to text file (.txt suffix).\n", - "- Second column the label.\n", - "- Any remaining columns are additional labels for multi-label text classification.\n", - "\n", - "For text sentiment analysis, the CSV file has a few requirements:\n", - "\n", - "- No heading.\n", - "- First column is the text example or Cloud Storage path to text file (.txt suffix).\n", - "- Second column is the sentiment value.\n", - "- Third column is the maximum possible sentiment value.\n", - "\n", - "##### ML_USE\n", - "\n", - "Each row may additionally specify which split to assign the data item to when the dataset is split for training; otherwise, the dataset will be randomly split: 80/10/10.\n", - "\n", - "The `ml_use` assignment is specified by prepending a column for specifying the assignment -- as the first column. The value may be one of: training, test, or validation." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "766c838de8a0" - }, - "source": [ - "#### JSONL \n", - "\n", - "For text classification, the JSONL file has a few requirements:\n", - "\n", - "- Each data item is a separate JSON object, on a separate line.\n", - "- The key/value pair `text_gcs_uri` is the Cloud Storage path to the text file.\n", - "- The key/value pair `text_content` is the alternate way of specifying the text as inlined.\n", - "- The key/value pair `display_name` is the label for the text.\n", - "\n", - "{\n", - " \"classification_annotation\": {\n", - " \"display_name\": label\n", - " },\n", - " \"text_content\": text\n", - "}\n", - "{\n", - " \"classification_annotation\": {\n", - " \"display_name\": label\n", - " },\n", - " \"text_gcs_uri\": \"gcs_uri_to_file\"\n", - "}\n", - "\n", - " \n", - "For multi-label, the labels are specified as a list of `display_name` key/value pairs:\n", - "\n", - " 'classification_annotations': [\n", - " { 'display_name': label1\n", - " },\n", - " { 'display_name': labelN\n", - " },\n", - " ]\n", - "\n", - "For text sentiment analysis, the JSONL file has a few requirements:\n", - "\n", - "- Each data item is a separate JSON object, on a separate line.\n", - "- The key/value pair `text_gcs_uri` is the Cloud Storage path to the text file.\n", - "- The key/value pair `text_content` is the alternate way of specifying the text as inlined.\n", - "- The key/value pair `sentiment` is the sentiment value as an integer value greater than 0.\n", - "- The key/value pair `sentiment_max`is the maximum possible value for the sentiment.\n", - "\n", - "{\n", - " \"sentiment_annotation\": {\n", - " \"sentiment\": number,\n", - " \"sentiment_max\": number\n", - " },\n", - " \"text_content\": text,\n", - "}\n", - "{\n", - " \"sentiment_annotation\": {\n", - " \"sentiment\": number,\n", - " \"sentiment_max\": number\n", - " },\n", - " \"text_gcs_uri\": \"gcs_uri_to_file\"\n", - "}\n", - "\n", - "\n", - "For text entity extraction, the JSONL file has a few requirements:\n", - "\n", - "- Each data item is a separate JSON object, on a separate line.\n", - "- The key/value pair `text_gcs_uri` is the Cloud Storage path to the text file.\n", - "- The key/value pair `text_content` is the alternate way of specifying the text as inlined.\n", - "- The key/value pair `start_offset` is the character offset of the start of the text.\n", - "- The key/value pair `end_offset` is the character offset of the end of the text.\n", - "- The key/value pair `display_name` is the label for the text.\n", - "\n", - "{\n", - " \"text_segment_annotations\": [\n", - " {\n", - " \"start_offset\":number,\n", - " \"end_offset\":number,\n", - " \"display_name\": label\n", - " },\n", - " ...\n", - " ],\n", - " \"textContent\": \"inline_text\"\n", - "}\n", - "{\n", - " \"textSegmentAnnotations\": [\n", - " {\n", - " \"start_offset\": number,\n", - " \"end_offset\": number,\n", - " \"displayName\": label\n", - " },\n", - " ...\n", - " ],\n", - " \"text_gcs_uri\": \"gcs_uri_to_file\"\n", - "}\n", - "\n", - "##### ML_USE\n", - "\n", - "Each JSONL object may additionally specify which split to assign the data item to when the dataset is split for training; otherwise, the dataset will be randomly split: 80/10/10.\n", - "\n", - "\"data_item_resource_labels\": {\n", - " \"aiplatform.googleapis.com/ml_use\": \"training|test|validation\"\n", - " }\n", - "\n", - "*Note*: The dictionary key fields may alternatively be in camelCase. For example, 'text_gcs_uri' can also be 'textGcsUri'." - ] - }, - { - "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": [ - "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", - "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", - " - `single_label`: Binary and multi-class classification\n", - " - `multi_label`: Multi-label multi-class classification\n", - " - `sentiment`: Sentiment analysis\n", - " - `extraction`: Entity extraction\n", - "\n", - "Learn more about [TextDataset](https://cloud.google.com/vertex-ai/docs/datasets/prepare-text)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "create_dataset:text,tcn" - }, - "outputs": [], - "source": [ - "dataset = aiplatform.TextDataset.create(\n", - " display_name=\"happydb\",\n", - " gcs_source=[IMPORT_FILE],\n", - " import_schema_uri=aiplatform.schema.dataset.ioformat.text.single_label_classification,\n", - ")\n", - "\n", - "print(dataset.resource_name)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "create_automl_pipeline:text,tcn" - }, - "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" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "create_automl_pipeline:text,tcn" - }, - "outputs": [], - "source": [ - "dag = aiplatform.AutoMLTextTrainingJob(\n", - " display_name=\"happydb\",\n", - " prediction_type=\"classification\",\n", - " multi_label=False,\n", - ")\n", - "\n", - "print(dag)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "run_automl_pipeline:text" - }, - "source": [ - "#### Run the training pipeline\n", - "\n", - "Next, you run the created 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 > 30 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=\"happydb\",\n", - " training_fraction_split=0.8,\n", - " validation_fraction_split=0.1,\n", - " test_fraction_split=0.1,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "source": [ - "## Review model evaluation scores\n", - "\n", - "After your model training has finished, you can review the evaluation scores for it using the `list_model_evaluations()` method. This method will return an iterator for each evaluation slice." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "outputs": [], - "source": [ - "model_evaluations = model.list_model_evaluations()\n", - "\n", - "for model_evaluation in model_evaluations:\n", - " print(model_evaluation.to_dict())" - ] - }, - { - "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": "undeploy_model:mbsdk" - }, - "source": [ - "#### Undeploy the model\n", - "\n", - "When you're done doing predictions, undeploy the model from the `Endpoint` resource. 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": "model_delete:mbsdk" - }, - "source": [ - "#### Delete the model\n", - "\n", - "The method 'delete()' will delete the model." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "model_delete:mbsdk" - }, - "outputs": [], - "source": [ - "model.delete()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "dataset_delete:mbsdk" - }, - "source": [ - "#### Delete the dataset\n", - "\n", - "The method 'delete()' will delete the dataset." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "dataset_delete:mbsdk" - }, - "outputs": [], - "source": [ - "dataset.delete()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "endpoint_delete:mbsdk" - }, - "source": [ - "#### Delete the endpoint\n", - "\n", - "The method 'delete()' will delete the endpoint." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "endpoint_delete:mbsdk" - }, - "outputs": [], - "source": [ - "endpoint.delete()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "automl_video_intro" - }, - "source": [ - "## AutoML video models\n", - "\n", - "AutoML can train the following types of video models:\n", - "\n", - "- classification\n", - "- object tracking\n", - "- action recognition\n", - "\n", - "A model can be trained for either deployment to the cloud or exported to the edge.\n", - "\n", - "Learn more about [AutoML Model Types](https://cloud.google.com/vertex-ai/docs/start/automl-model-types)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "data_preparation:text,u_dataset" - }, - "source": [ - "### Data preparation\n", - "\n", - "The Vertex AI `Dataset` resource for text has a couple of requirements for your text data.\n", - "\n", - "- Text examples must be stored in a CSV or JSONL file.\n", - "\n", - "Learn more about [Preparing video data](https://cloud.google.com/vertex-ai/docs/datasets/prepare-video)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "427212b48840" - }, - "source": [ - "#### CSV\n", - "\n", - "For video classification, the CSV file has a few requirements:\n", - "\n", - "- No heading.\n", - "- First column is the Cloud Storage path to video file.\n", - "- Second column the label.\n", - "- Third column is the start time (seconds) in the video to classify.\n", - "- Fourth column is the end time (seconds) in the video to classify.\n", - "\n", - "For multi-label classification, each label is a separate row entry.\n", - "\n", - "For video object tracking, the CSV file has a few requirements:\n", - "\n", - "- No heading.\n", - "- First column is the Cloud Storage path to video file.\n", - "- Second column the label.\n", - "- Third column is unused (blank).\n", - "- Fourth column is the start time (seconds) in the video to start tracking the object.\n", - "- The fifth through eighth columns are the vertices of the object to track.\n", - " - x_min\n", - " - y_min\n", - " - x_max\n", - " - y_max\n", - " \n", - "For action recognition, the CSV file has a few requirements:\n", - "\n", - "- No heading.\n", - "- Each row can be one of the following four formats:\n", - "\n", - "VIDEO_URI, TIME_SEGMENT_START, TIME_SEGMENT_END, LABEL, ANNOTATION_FRAME_TIMESTAMP\n", - "\n", - "VIDEO_URI, , , LABEL, ANNOTATION_FRAME_TIMESTAMP\n", - "\n", - "VIDEO_URI, TIME_SEGMENT_START, TIME_SEGMENT_END, LABEL, ANNOTATION_SEGMENT_START, ANNOTATION_SEGMENT_END\n", - "\n", - "VIDEO_URI, , , LABEL, ANNOTATION_SEGMENT_START, ANNOTATION_SEGMENT_END\n", - "\n", - "\n", - "##### ML_USE\n", - "\n", - "Each row may additionally specify which split to assign the data item to when the dataset is split for training; otherwise, the dataset will be randomly split: 80/10/10.\n", - "\n", - "The `ml_use` assignment is specified by prepending a column for specifying the assignment -- as the first column. The value may be one of: training, or test." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "461301339727" - }, - "source": [ - "#### JSONL\n", - "\n", - "For video classification, the CSV file has a few requirements:\n", - "\n", - "- Each data item is a separate JSON object, on a separate line.\n", - "- The key/value pair `video_gcs_uri` is the Cloud Storage path to the text file.\n", - "- The key/value pair `display_name` is the label for the text.\n", - "- The key/value pair `start_time` is the start time (seconds) for classifying.\n", - "- The key/value pair `end_time` is the end time (seconds) for classifying.\n", - "\n", - "\n", - " {\n", - " \"video_gcs_uri\": video,\n", - " \"time_segment_annotations\": [{\n", - " \"display_name\": label,\n", - " \"start_time\": \"start_time_of_segment\",\n", - " \"end_time\": \"end_time_of_segment\"\n", - " }]\n", - " }\n", - "\n", - "For video object tracking, the CSV file has a few requirements:\n", - "\n", - "- Each data item is a separate JSON object, on a separate line.\n", - "- The key/value pair `video_gcs_uri` is the Cloud Storage path to the text file.\n", - "\n", - " {\n", - " \"video_gcs_uri\": video,\n", - " \"temporal_bounding_box_annotations\": [{\n", - " \"display_name\": label,\n", - " \"x_min\": \"leftmost_coordinate_of_the_bounding box\",\n", - " \"x_max\": \"rightmost_coordinate_of_the_bounding box\",\n", - " \"y_min\": \"topmost_coordinate_of_the_bounding box\",\n", - " \"y_max\": \"bottommost_coordinate_of_the_bounding box\",\n", - " \"time_offset\": \"timeframe_object-detected\"\n", - " }]\n", - " }\n", - "\n", - "For video action recognition, the CSV file has a few requirements:\n", - "\n", - "- Each data item is a separate JSON object, on a separate line.\n", - "- The key/value pair `video_gcs_uri` is the Cloud Storage path to the text file.\n", - "\n", - " {\n", - " \"video_gcs_uri': video,\n", - " \"time_segments\": [{\n", - " \"start_time\": \"start_time_of_fully_annotated_segment\",\n", - " \"end_time\": \"end_time_of_segment\"}],\n", - " \"time_segment_annotations\": [{\n", - " \"display_name\": label,\n", - " \"start_time\": \"start_time_of_segment\",\n", - " \"end_time\": \"end_time_of_segment\"\n", - " }]\n", - " }\n", - "\n", - "##### ML_USE\n", - "\n", - "Each JSONL object may additionally specify which split to assign the data item to when the dataset is split for training; otherwise, the dataset will be randomly split: 80/20.\n", - "\n", - "\"data_item_resource_labels\": {\n", - " \"aiplatform.googleapis.com/ml_use\": \"training|test\"\n", - " }\n", - "\n", - "*Note*: The dictionary key fields may alternatively be in camelCase. For example, 'video_gcs_uri' can also be 'videoGcsUri'." - ] - }, - { - "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_5classes_train_inf.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": [ - "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: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", - "- `import_schema_uri`: The data labeling schema for the data items.\n", - " - `classification`: Binary and multi-class classification\n", - " - `object_tracking`: Object tracking\n", - " - `action_recognition`: Action recognition\n", - "\n", - "Learn more about [VideoDataset](https://cloud.google.com/vertex-ai/docs/datasets/prepare-video)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "create_dataset:video,vcn" - }, - "outputs": [], - "source": [ - "dataset = aiplatform.VideoDataset.create(\n", - " display_name=\"human_motion\",\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_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." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "create_automl_pipeline:video,vcn" - }, - "outputs": [], - "source": [ - "dag = aiplatform.AutoMLVideoTrainingJob(\n", - " display_name=\"human_motion\",\n", - " prediction_type=\"classification\",\n", - ")\n", - "\n", - "print(dag)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "run_automl_pipeline:video" - }, - "source": [ - "#### Run the training pipeline\n", - "\n", - "Next, you run the created 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 will take upto > 30 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=\"human_motion\",\n", - " training_fraction_split=0.8,\n", - " test_fraction_split=0.2,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "source": [ - "## Review model evaluation scores\n", - "\n", - "After your model training has finished, you can review the evaluation scores for it using the `list_model_evaluations()` method. This method will return an iterator for each evaluation slice." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "outputs": [], - "source": [ - "model_evaluations = model.list_model_evaluations()\n", - "\n", - "for model_evaluation in model_evaluations:\n", - " print(model_evaluation.to_dict())" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "model_delete:mbsdk" - }, - "source": [ - "#### Delete the model\n", - "\n", - "The method 'delete()' will delete the model." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "model_delete:mbsdk" - }, - "outputs": [], - "source": [ - "model.delete()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "dataset_delete:mbsdk" - }, - "source": [ - "#### Delete the dataset\n", - "\n", - "The method 'delete()' will delete the dataset." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "dataset_delete:mbsdk" - }, - "outputs": [], - "source": [ - "dataset.delete()" - ] - }, { "cell_type": "markdown", "metadata": { diff --git a/notebooks/official/automl/sdk_automl_text_entity_extraction_online.ipynb b/notebooks/official/automl/sdk_automl_text_entity_extraction_online.ipynb deleted file mode 100644 index ef2a53108..000000000 --- a/notebooks/official/automl/sdk_automl_text_entity_extraction_online.ipynb +++ /dev/null @@ -1,775 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "copyright" - }, - "outputs": [], - "source": [ - "# Copyright 2022 Google LLC\n", - "#\n", - "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", - "# you may not use this file except in compliance with the License.\n", - "# You may obtain a copy of the License at\n", - "#\n", - "# https://www.apache.org/licenses/LICENSE-2.0\n", - "#\n", - "# Unless required by applicable law or agreed to in writing, software\n", - "# distributed under the 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": "64a4f198313d" - }, - "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" - }, - "source": [ - "# Vertex AI SDK for Python: AutoML training text entity extraction model for online prediction\n", - "\n", - "\n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \"Colab Run in Colab\n", - " \n", - " \n", - " \n", - " \"GitHub\n", - " View on GitHub\n", - " \n", - " \n", - " \n", - " \"Vertex\n", - " Open in Vertex AI Workbench\n", - " \n", - "
\n", - "


" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "overview:automl" - }, - "source": [ - "## Overview\n", - "\n", - "\n", - "This tutorial demonstrates how to use the Vertex AI SDK for Python to create text entity extraction models and do online prediction using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model.\n", - "\n", - "Learn more about [Entity extraction for text data](https://cloud.google.com/vertex-ai/docs/training-overview#entity_extraction_for_text)." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "objective:automl,training,online_prediction" - }, - "source": [ - "### Objective\n", - "\n", - "In this tutorial, you learn how to create an AutoML text entity extraction model and deploy for online prediction from a Python script using the Vertex AI SDK. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n", - "\n", - "This tutorial uses the following Google Cloud ML services:\n", - "\n", - "- `AutoML Training`\n", - "- `Vertex AI Datasets`\n", - "\n", - "The steps performed include:\n", - "\n", - "- Create a Vertex `Dataset` resource.\n", - "- Train the model.\n", - "- View the model evaluation.\n", - "- Deploy the `Model` resource to a serving `Endpoint` resource.\n", - "- Make a prediction.\n", - "- Undeploy 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 latest version of Vertex AI SDK for Python." - ] - }, - { - "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", - " google-cloud-storage" - ] - }, - { - "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": "yfEglUHQk9S3" - }, - "source": [ - "## Before you begin\n", - "\n", - "### 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": "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 -p $PROJECT_ID $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", - "\n", - "import google.cloud.aiplatform as 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": "tutorial_start:automl" - }, - "source": [ - "# Tutorial\n", - "\n", - "Now you are ready to start creating your own AutoML text entity extraction model." - ] - }, - { - "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": [ - "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,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 = aiplatform.TextDataset.create(\n", - " display_name=\"NCBI Biomedical\",\n", - " gcs_source=[IMPORT_FILE],\n", - " import_schema_uri=aiplatform.schema.dataset.ioformat.text.extraction,\n", - ")\n", - "\n", - "print(dataset.resource_name)" - ] - }, - { - "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" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "create_automl_pipeline:text,ten" - }, - "outputs": [], - "source": [ - "job = aiplatform.AutoMLTextTrainingJob(\n", - " display_name=\"biomedical\", prediction_type=\"extraction\"\n", - ")\n", - "\n", - "print(job)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "run_automl_pipeline:text" - }, - "source": [ - "#### Run the training pipeline\n", - "\n", - "Next, you 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 4 hours." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "run_automl_pipeline:text" - }, - "outputs": [], - "source": [ - "model = job.run(\n", - " dataset=dataset,\n", - " model_display_name=\"biomedical\",\n", - " training_fraction_split=0.8,\n", - " validation_fraction_split=0.1,\n", - " test_fraction_split=0.1,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "source": [ - "## Review model evaluation scores\n", - "After your model has finished training, you can review the evaluation scores for it.\n", - "\n", - "First, you need to get a reference to the new model. As with datasets, you can either use the reference to the model variable you created when you deployed the model or you can list all of the models in your project." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "outputs": [], - "source": [ - "# Get model resource ID\n", - "models = aiplatform.Model.list(filter=\"display_name=biomedical\")\n", - "\n", - "# Get a reference to the Model Service client\n", - "client_options = {\"api_endpoint\": f\"{REGION}-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": "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": "make_prediction" - }, - "source": [ - "## Send a online prediction request\n", - "\n", - "Send a online prediction to your deployed model." - ] - }, - { - "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": "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 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 AutoML or Pipeline training job\n", - "job.delete()\n", - "\n", - "if os.getenv(\"IS_TESTING\"):\n", - " ! gsutil -m rm -r $BUCKET_URI" - ] - } - ], - "metadata": { - "colab": { - "name": "sdk_automl_text_entity_extraction_online.ipynb", - "toc_visible": true - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/notebooks/official/automl/sdk_automl_text_sentiment_analysis_online.ipynb b/notebooks/official/automl/sdk_automl_text_sentiment_analysis_online.ipynb deleted file mode 100644 index 8befdc296..000000000 --- a/notebooks/official/automl/sdk_automl_text_sentiment_analysis_online.ipynb +++ /dev/null @@ -1,773 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "copyright" - }, - "outputs": [], - "source": [ - "# Copyright 2022 Google LLC\n", - "#\n", - "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", - "# you may not use this file except in compliance with the License.\n", - "# You may obtain a copy of the License at\n", - "#\n", - "# https://www.apache.org/licenses/LICENSE-2.0\n", - "#\n", - "# Unless required by applicable law or agreed to in writing, software\n", - "# distributed under the 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": "148c71404373" - }, - "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" - }, - "source": [ - "# Vertex AI SDK: Training an AutoML text sentiment analysis model for online predictions\n", - "\n", - "\n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \"Colab Run in Colab\n", - " \n", - " \n", - " \n", - " \"GitHub\n", - " View on GitHub\n", - " \n", - " \n", - "\n", - " \"Vertex\n", - " Open in Vertex AI Workbench\n", - " \n", - "
\n", - "


" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "overview:automl" - }, - "source": [ - "## Overview\n", - "\n", - "This tutorial demonstrates how to use the Vertex AI SDK to train and deploy an [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) text sentiment analysis model and get online predictions from it.\n", - "\n", - "Learn more about [Sentiment analysis for text data](https://cloud.google.com/vertex-ai/docs/training-overview#sentiment_analysis_for_text)." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "objective:automl,training,online_prediction" - }, - "source": [ - "### Objective\n", - "\n", - "In this tutorial, you learn how to create an AutoML text sentiment analysis model and deploy it for online predictions from a Python script using the Vertex AI SDK. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n", - "\n", - "This tutorial uses the following Google Cloud ML services and resources:\n", - "- Vertex AI Datasets\n", - "- Vertex AI Training (AutoML)\n", - "- Vertex AI Model Registry\n", - "- Vertex AI Endpoints\n", - "\n", - "The steps performed include:\n", - "\n", - "- Create a `Vertex AI Dataset` resource.\n", - "- Create a training job for the AutoML model on the dataset.\n", - "- View the model evaluation metrics.\n", - "- Deploy the `Vertex AI Model` resource to a serving `Vertex AI Endpoint`.\n", - "- Make a prediction request to the deployed model.\n", - "- Undeploy the model from endpoint.\n", - "- Perform clean up process." - ] - }, - { - "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) that consists of tweets tagged with sentiment, the author's gender, and whether or not they mention any of the top 10 adverse events reported to the FDA. The version of the dataset you use in this tutorial is stored in a public Cloud Storage bucket. In this tutorial, you use the tweets data to build an AutoML text sentiment analysis model on Google Cloud platform." - ] - }, - { - "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 latest version of Vertex AI SDK for Python." - ] - }, - { - "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", - " google-cloud-storage" - ] - }, - { - "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": "yfEglUHQk9S3" - }, - "source": [ - "## Before you begin\n", - "\n", - "### 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": "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 -p $PROJECT_ID $BUCKET_URI" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "setup_vars" - }, - "source": [ - "### Import libraries" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "import_aip:mbsdk" - }, - "outputs": [], - "source": [ - "import google.cloud.aiplatform as aiplatform" - ] - }, - { - "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": [ - "aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "import_file:u_dataset,csv" - }, - "source": [ - "### Define the constants\n", - "\n", - "Set the constants that you use in this tutorial." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "import_file:claritin,csv,tst" - }, - "outputs": [], - "source": [ - "# Set the location of the CSV index file in Cloud Storage.\n", - "IMPORT_FILE = \"gs://cloud-samples-data/language/claritin.csv\"\n", - "# Set the max. sentiment score\n", - "SENTIMENT_MAX = 4" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "quick_peek:csv" - }, - "source": [ - "## Take a quick peek at your data\n", - "\n", - "This tutorial uses a version of the `Crowdflower Claritin-Twitter` dataset which is stored in a public Cloud Storage bucket, using a CSV index file.\n", - "\n", - "Start by taking a quick peek at the data. Further, count the number of examples by counting the number of rows in the CSV index file (`wc -l`) and then print the first few rows." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "quick_peek:csv" - }, - "outputs": [], - "source": [ - "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,tst" - }, - "source": [ - "## Create the Dataset\n", - "\n", - "Now, create a `Vertex AI Dataset` resource using the `create` method of 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 = aiplatform.TextDataset.create(\n", - " display_name=\"Crowdflower Claritin-Twitter\",\n", - " gcs_source=[IMPORT_FILE],\n", - " import_schema_uri=aiplatform.schema.dataset.ioformat.text.sentiment,\n", - ")\n", - "\n", - "print(dataset.resource_name)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "create_automl_pipeline:text,tst" - }, - "source": [ - "## Create and run training job\n", - "\n", - "In this section, to train an AutoML model, you perform these steps:\n", - "\n", - "1) create a training job.\n", - "2) run the job.\n", - "\n", - "### Create a training job\n", - "\n", - "An AutoML training job is created with the `AutoMLTextTrainingJob` class, with the following parameters:\n", - "\n", - "- `display_name`: The human readable name for the training job 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." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "create_automl_pipeline:text,tst" - }, - "outputs": [], - "source": [ - "job = aiplatform.AutoMLTextTrainingJob(\n", - " display_name=\"claritin\",\n", - " prediction_type=\"sentiment\",\n", - " sentiment_max=SENTIMENT_MAX,\n", - ")\n", - "\n", - "print(job)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "run_automl_pipeline:text" - }, - "source": [ - "### Run the training job\n", - "\n", - "Next, you run 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 180 minutes." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "run_automl_pipeline:text" - }, - "outputs": [], - "source": [ - "model = job.run(\n", - " dataset=dataset,\n", - " model_display_name=\"claritin\",\n", - " training_fraction_split=0.8,\n", - " validation_fraction_split=0.1,\n", - " test_fraction_split=0.1,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "source": [ - "## Review model evaluation scores\n", - "\n", - "Once your model training has finished, you can review the evaluation scores.\n", - "\n", - "Firstly, you need to get a reference to the newly created 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 and filter." - ] - }, - { - "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=claritin\")\n", - "\n", - "# Get a reference to the Model Service client\n", - "client_options = {\"api_endpoint\": f\"{REGION}-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": "deploy_model:mbsdk,automatic" - }, - "source": [ - "## Deploy the model\n", - "\n", - "Next, deploy your model to serve online predictions. To deploy the model, you invoke the `deploy` method of the model resource which in turn returns you the deployed endpoint.\n", - "\n", - "**Note:** Normally, an endpoint is created beforehand and is given as a reference while model deployment. By default, `deploy()` method creates an endpoint when an endpoint reference is not given." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "deploy_model:mbsdk,automatic" - }, - "outputs": [], - "source": [ - "endpoint = model.deploy()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "make_prediction" - }, - "source": [ - "## Send online prediction requests\n", - "\n", - "In this step, you prepare some test instances from the dataset and send an online prediction request to your deployed model." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "get_test_item" - }, - "source": [ - "### Create test instances\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. It is just 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 request\n", - "\n", - "Now that your model is deployed to an endpoint, you can send online prediction requests to the endpoint resource.\n", - "\n", - "#### Request format\n", - "\n", - "The format of each instance should be in JSON as below:\n", - "\n", - " { 'content': text_string }\n", - "\n", - "Since the `predict()` method can take multiple instances, send your request as a list of one test instance.\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": "undeploy_model:mbsdk" - }, - "source": [ - "## Undeploy the model\n", - "\n", - "After you explore the 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", - "- Vertex AI Dataset\n", - "- Vertex AI Model\n", - "- Vertex AI Endpoint\n", - "- AutoML Training Job\n", - "- Cloud Storage Bucket (set `delete_bucket` to **True** to delete the bucket)" - ] - }, - { - "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", - "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 AutoML or Pipeline training job\n", - "job.delete()\n", - "\n", - "# Delete the Cloud storage bucket\n", - "if delete_bucket or os.getenv(\"IS_TESTING\"):\n", - " ! gsutil rm -r $BUCKET_URI" - ] - } - ], - "metadata": { - "colab": { - "name": "sdk_automl_text_sentiment_analysis_online.ipynb", - "toc_visible": true - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/notebooks/official/automl/sdk_automl_video_action_recognition_batch.ipynb b/notebooks/official/automl/sdk_automl_video_action_recognition_batch.ipynb deleted file mode 100644 index beb01b22f..000000000 --- a/notebooks/official/automl/sdk_automl_video_action_recognition_batch.ipynb +++ /dev/null @@ -1,787 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "copyright" - }, - "outputs": [], - "source": [ - "# Copyright 2022 Google LLC\n", - "#\n", - "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", - "# you may not use this file except in compliance with the License.\n", - "# You may obtain a copy of the License at\n", - "#\n", - "# https://www.apache.org/licenses/LICENSE-2.0\n", - "#\n", - "# Unless required by applicable law or agreed to in writing, software\n", - "# distributed under the 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" - }, - "source": [ - "# Vertex AI SDK: AutoML training video action recognition model for batch prediction\n", - "\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \"Colab
Open in Colab\n", - "
\n", - "
\n", - " \n", - " \"Google
Open in Colab Enterprise\n", - "
\n", - "
\n", - " \n", - " \"Vertex
\n", - " Open in Vertex AI Workbench\n", - "
\n", - "
\n", - " \n", - " \"GitHub
\n", - " View on GitHub\n", - "
\n", - "
\n", - "


" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "overview:automl" - }, - "source": [ - "## Overview\n", - "\n", - "\n", - "This tutorial demonstrates how to use the Vertex AI SDK to create video action recognition models and do batch prediction using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model.\n", - "\n", - "Learn more about [Action recognition for video data](https://cloud.google.com/vertex-ai/docs/training-overview#action_recognition_for_videos)." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "objective:automl,training,batch_prediction" - }, - "source": [ - "### Objective\n", - "\n", - "In this tutorial, you learn how to create an AutoML video action recognition model from a Python script, and then do a batch prediction using the Vertex AI SDK. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n", - "\n", - "This tutorial uses the following Google Cloud Vertex AI services and resources:\n", - "\n", - "- Vertex AI dataset\n", - "- Vertex AI model\n", - "- Vertex AI batch prediction\n", - "\n", - "\n", - "The steps performed include:\n", - "\n", - "- Create a Vertex AI dataset resource.\n", - "- Train the model.\n", - "- View the model evaluation.\n", - "- Make a batch prediction.\n", - "\n", - "There is one key difference between using batch prediction and using online prediction:\n", - "\n", - "**Prediction service**: Does an on-demand prediction for the entire set of instances (i.e., one or more data items) and returns the results in real-time.\n", - "\n", - "**Batch prediction service**: Does a queued (batch) prediction for the entire set of instances in the background and stores the results in a Cloud Storage bucket when ready." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "dataset:golf,var" - }, - "source": [ - "### Dataset\n", - "\n", - "The dataset used for this tutorial is the golf swing recognition portion of the [Human Motion dataset 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. The trained model will predict the start frame where an action of golf swing begins." - ] - }, - { - "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": "dae013b807c7" - }, - "outputs": [], - "source": [ - "! pip3 install --upgrade --quiet google-cloud-aiplatform \\\n", - " google-cloud-storage" - ] - }, - { - "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": "294fe4e5a671" - }, - "outputs": [], - "source": [ - "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n", - "LOCATION = \"us-central1\" # @param {type:\"string\"}" - ] - }, - { - "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": "autoset_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": "91c46850b49b" - }, - "outputs": [], - "source": [ - "! gsutil mb -l $LOCATION -p $PROJECT_ID $BUCKET_URI" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "a56633b047ee" - }, - "source": [ - "### Initialize Vertex AI SDK for Python" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "673ef17c3cff" - }, - "outputs": [], - "source": [ - "from google.cloud import aiplatform\n", - "\n", - "aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "setup_vars" - }, - "source": [ - "### Import libraries and define constants" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "import_aip:mbsdk" - }, - "outputs": [], - "source": [ - "import json\n", - "\n", - "from google.cloud import storage" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "tutorial_start:automl" - }, - "source": [ - "# Tutorial\n", - "\n", - "Now you are ready to start creating your own AutoML video action recognition model.\n", - "\n", - "#### Location of Cloud Storage training data.\n", - "\n", - "Now set the variable `IMPORT_FILES` to the location of the CSV index files in Cloud Storage." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "import_file:golf,csv,var" - }, - "outputs": [], - "source": [ - "IMPORT_FILES = [\n", - " \"gs://automl-video-demo-data/hmdb_golf_swing_train.csv\",\n", - " \"gs://automl-video-demo-data/hmdb_golf_swing_test.csv\",\n", - "]" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "quick_peek:csv" - }, - "source": [ - "#### Quick peek at your data\n", - "\n", - "This tutorial uses a version of the Golf Swings 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": [ - "FILE = IMPORT_FILES[0]\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:video,var" - }, - "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,var" - }, - "outputs": [], - "source": [ - "dataset = aiplatform.VideoDataset.create(\n", - " display_name=\"Golf Swings\",\n", - " gcs_source=IMPORT_FILES,\n", - " import_schema_uri=aiplatform.schema.dataset.ioformat.video.action_recognition,\n", - ")\n", - "\n", - "print(dataset.resource_name)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "create_automl_pipeline:video,var" - }, - "source": [ - "### Create and run training pipeline\n", - "\n", - "To train an AutoML model, you perform two steps: \n", - "1. create a training pipeline.\n", - "2. run the pipeline.\n", - "\n", - "#### Create the 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." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "create_automl_pipeline:video,var" - }, - "outputs": [], - "source": [ - "job = aiplatform.AutoMLVideoTrainingJob(\n", - " display_name=\"golf\",\n", - " prediction_type=\"action_recognition\",\n", - ")\n", - "\n", - "print(job)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "run_automl_pipeline:video" - }, - "source": [ - "#### Run the training pipeline\n", - "\n", - "Next, you run 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 can take upto 40 minutes." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "run_automl_pipeline:video" - }, - "outputs": [], - "source": [ - "model = job.run(\n", - " dataset=dataset,\n", - " model_display_name=\"golf\",\n", - " training_fraction_split=0.8,\n", - " test_fraction_split=0.2,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "source": [ - "## Review model evaluation scores\n", - "After your model has finished training, you can review the evaluation scores for it.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "outputs": [], - "source": [ - "# Get evaluations\n", - "model_evaluations = model.list_model_evaluations()\n", - "\n", - "model_evaluation = list(model_evaluations)[0]\n", - "print(model_evaluation)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "make_prediction" - }, - "source": [ - "## Send a batch prediction request\n", - "\n", - "Send a batch prediction request to your registered model.\n", - "\n", - "### Get test item(s)\n", - "\n", - "Now send a batch prediction request to your Vertex AI 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 as we just want to demonstrate how to make a prediction." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "get_test_items:automl,var,csv" - }, - "outputs": [], - "source": [ - "import_file = IMPORT_FILES[0]\n", - "test_items = ! gsutil cat $import_file | head -n2\n", - "\n", - "cols = str(test_items[0]).split(\",\")\n", - "test_item_1 = str(cols[0])\n", - "test_label_1 = str(cols[-1])\n", - "\n", - "cols = str(test_items[1]).split(\",\")\n", - "test_item_2 = str(cols[0])\n", - "test_label_2 = str(cols[-1])\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": "13a02e5067ab" - }, - "outputs": [], - "source": [ - "test_filename = \"test.jsonl\"\n", - "gcs_input_uri = BUCKET_URI + \"/\" + test_filename\n", - "\n", - "# Configure the test-data\n", - "data_1 = {\n", - " \"content\": test_item_1,\n", - " \"mimeType\": \"video/avi\",\n", - " \"timeSegmentStart\": \"0.0s\",\n", - " \"timeSegmentEnd\": \"5.0s\",\n", - "}\n", - "data_2 = {\n", - " \"content\": test_item_2,\n", - " \"mimeType\": \"video/avi\",\n", - " \"timeSegmentStart\": \"0.0s\",\n", - " \"timeSegmentEnd\": \"5.0s\",\n", - "}\n", - "\n", - "# Upload the test-data to Cloud storage bucket\n", - "bucket = storage.Client(project=PROJECT_ID).bucket(BUCKET_URI.replace(\"gs://\", \"\"))\n", - "blob = bucket.blob(blob_name=test_filename)\n", - "data = json.dumps(data_1) + \"\\n\" + json.dumps(data_2) + \"\\n\"\n", - "blob.upload_from_string(data)\n", - "\n", - "# Check the Cloud storage uri\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 Vertex AI 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=\"golf\",\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_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": "get_batch_prediction:mbsdk,var" - }, - "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 name for the predicted 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", - " - `confidence`: The predicted confidence, between 0 and 1." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "76f7d62916bc" - }, - "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", - "\n", - "for prediction_result in prediction_results:\n", - " gfile_name = f\"gs://{bp_iter_outputs.bucket.name}/{prediction_result}\".replace(\n", - " BUCKET_URI + \"/\", \"\"\n", - " )\n", - " data = bucket.get_blob(gfile_name).download_as_string()\n", - " data = json.loads(data)\n", - " print(data)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cleanup:mbsdk" - }, - "source": [ - "# Cleaning up\n", - "\n", - "To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n", - "project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n", - "\n", - "Otherwise, you can delete the individual resources you created in this tutorial:\n", - "\n", - "- Dataset\n", - "- Model\n", - "- AutoML Training Job\n", - "- Batch Job\n", - "- Cloud Storage Bucket" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "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 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 Cloud Storage objects\n", - "delete_bucket = False\n", - "if delete_bucket:\n", - " ! gsutil -m rm -r $BUCKET_URI" - ] - } - ], - "metadata": { - "colab": { - "name": "sdk_automl_video_action_recognition_batch.ipynb", - "toc_visible": true - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/notebooks/official/automl/sdk_automl_video_classification_batch.ipynb b/notebooks/official/automl/sdk_automl_video_classification_batch.ipynb deleted file mode 100644 index dad8761ed..000000000 --- a/notebooks/official/automl/sdk_automl_video_classification_batch.ipynb +++ /dev/null @@ -1,810 +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" - }, - "source": [ - "# Vertex AI SDK: AutoML training video classification model for batch prediction\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \"Colab
Open in Colab\n", - "
\n", - "
\n", - " \n", - " \"Google
Open in Colab Enterprise\n", - "
\n", - "
\n", - " \n", - " \"GitHub
\n", - " View on GitHub\n", - "
\n", - "
\n", - " \n", - " \"Vertex
\n", - "Open in Vertex AI Workbench \n", - "
\n", - "
\n", - "


" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "overview:automl" - }, - "source": [ - "## Overview\n", - "\n", - "\n", - "This tutorial demonstrates how to use the Vertex AI SDK to create video classification models and do batch prediction using a Vertex AI [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model.\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": "objective:automl,training,batch_prediction" - }, - "source": [ - "### Objective\n", - "\n", - "In this tutorial, you learn how to create an AutoML video classification model from a Python script, and then do a batch prediction using the Vertex AI SDK.\n", - "Alternatively, you can create and deploy models using the `gcloud` command-line tool or online using the Google Cloud Console.\n", - "\n", - "This tutorial uses the following Google Cloud ML services and resources:\n", - "\n", - "- Vertex AI Training\n", - "- Vertex AI models\n", - "- Vertex AI batch prediction\n", - "\n", - "The steps performed include:\n", - "\n", - "- Create a Vertex dataset resource.\n", - "- Train the model.\n", - "- View the model evaluation.\n", - "- Make a batch prediction.\n", - "\n", - "There is one key difference between using batch prediction and using online prediction:\n", - "\n", - "**Prediction service**: Does an on-demand prediction for the entire set of instances (that is, one or more data items) and returns the results in real-time.\n", - "\n", - "**Batch prediction service**: Does a queued (batch) prediction for the entire set of instances in the background and stores the results in a Cloud Storage bucket when ready." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "dataset:hmdb,vcn" - }, - "source": [ - "### Dataset\n", - "\n", - "The dataset used for this tutorial is the golf swing recognition portion of the [Human Motion dataset from MIT](http://cbcl.mit.edu/publications/ps/Kuehne_etal_iccv11.pdf). The version of the dataset used in this tutorial is stored in a public Cloud Storage bucket. The trained model predicts the start frame where a golf swing begins.\n" - ] - }, - { - "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": "514a03ed1a82" - }, - "outputs": [], - "source": [ - "! pip3 install --upgrade --quiet google-cloud-aiplatform google-cloud-storage" - ] - }, - { - "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": "294fe4e5a671" - }, - "outputs": [], - "source": [ - "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n", - "LOCATION = \"us-central1\" # @param {type:\"string\"}" - ] - }, - { - "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 -p $PROJECT_ID $BUCKET_URI" - ] - }, - { - "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": [ - "from google.cloud import aiplatform\n", - "\n", - "aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "tutorial_start:automl" - }, - "source": [ - "## Tutorial\n", - "\n", - "Now you are ready to start creating your own AutoML video classification model." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "import_file:u_dataset,csv" - }, - "source": [ - "#### Location of Cloud Storage training data.\n", - "\n", - "Now set the variable `IMPORT_FILE` to the location of the CSV index file in Cloud Storage." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "import_file:hmdb,csv,vcn" - }, - "outputs": [], - "source": [ - "IMPORT_FILE = \"gs://automl-video-demo-data/hmdb_split1_5classes_train_inf.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": [ - "count = ! gsutil cat $IMPORT_FILE | wc -l\n", - "print(\"Number of Examples\", int(count[0]))\n", - "\n", - "print(\"First 10 rows\")\n", - "! gsutil cat $IMPORT_FILE | head" - ] - }, - { - "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\",\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_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" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "create_automl_pipeline:video,vcn" - }, - "outputs": [], - "source": [ - "job = aiplatform.AutoMLVideoTrainingJob(\n", - " display_name=\"hmdb\",\n", - " prediction_type=\"classification\",\n", - ")\n", - "\n", - "print(job)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "run_automl_pipeline:video" - }, - "source": [ - "#### Run the training pipeline\n", - "\n", - "Next, you run the job to start the training job by invoking the method `run`, with the following parameters:\n", - "\n", - "- `dataset`: The dataset resource to train the model.\n", - "- `model_display_name`: The human readable name for the trained model.\n", - "- `training_fraction_split`: The percentage of the dataset to use for training.\n", - "- `test_fraction_split`: The percentage of the dataset to use for test (holdout data).\n", - "\n", - "The `run` method when completed returns the model resource.\n", - "\n", - "The execution of the training pipeline can take over 3 hours to complete." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "run_automl_pipeline:video" - }, - "outputs": [], - "source": [ - "model = job.run(\n", - " dataset=dataset,\n", - " model_display_name=\"hmdb\",\n", - " training_fraction_split=0.8,\n", - " test_fraction_split=0.2,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "source": [ - "## Review model evaluation scores\n", - "After your model has finished training, you can review the evaluation scores for it.\n", - "\n", - "You can check the model's evaluation results using the `get_model_evaluation` method of the Vertex AI model resource.\n", - "\n", - "Just like Vertex AI datasets, you can either use the reference to the model variable you created when you deployed the model or you can filter from the list of all of the models in your project using the model's display name as given below." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "outputs": [], - "source": [ - "# Get model resource ID using the display_name\n", - "models = aiplatform.Model.list(filter=\"display_name=hmdb\")\n", - "\n", - "if len(models) != 0:\n", - "\n", - " # Get the model object\n", - " model_rsc_name = models[0].resource_name\n", - " print(\"Model resource name:\", model_rsc_name)\n", - " model = aiplatform.Model(model_rsc_name)\n", - "\n", - " # Print the model evaluation\n", - " model_eval = model.get_model_evaluation()\n", - " print(model_eval.to_dict())" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "make_prediction" - }, - "source": [ - "## Send a batch prediction request\n", - "\n", - "Send a batch prediction to your deployed model." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "get_test_items:batch_prediction" - }, - "source": [ - "### Get test item(s)\n", - "\n", - "Now do a batch prediction to your Vertex AI model. Use arbitrary examples from the dataset as test items.\n", - "Don't be concerned that the examples were likely used in training the model since this tutorial is just about how to make a batch 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.\n", - "You use JSONL in this tutorial. In the JSONL file, 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", - "from google.cloud import storage\n", - "\n", - "test_filename = \"test.jsonl\"\n", - "gcs_input_uri = BUCKET_URI + \"/\" + test_filename\n", - "\n", - "data_1 = {\n", - " \"content\": test_item_1,\n", - " \"mimeType\": \"video/avi\",\n", - " \"timeSegmentStart\": \"0.0s\",\n", - " \"timeSegmentEnd\": \"5.0s\",\n", - "}\n", - "data_2 = {\n", - " \"content\": test_item_2,\n", - " \"mimeType\": \"video/avi\",\n", - " \"timeSegmentStart\": \"0.0s\",\n", - " \"timeSegmentEnd\": \"5.0s\",\n", - "}\n", - "\n", - "\n", - "bucket = storage.Client(project=PROJECT_ID).bucket(BUCKET_URI[5:])\n", - "blob = bucket.blob(blob_name=test_filename)\n", - "data = json.dumps(data_1) + \"\\n\" + json.dumps(data_2) + \"\\n\"\n", - "blob.upload_from_string(data)\n", - "print(gcs_input_uri)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "d56366168ec5" - }, - "source": [ - "### Check input content\n", - "Check the contents of the `test.jsonl`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "378131e21a7e" - }, - "outputs": [], - "source": [ - "! gsutil cat $gcs_input_uri" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "batch_request:mbsdk" - }, - "source": [ - "### Make the batch prediction request\n", - "\n", - "Now that your Vertex AI 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\",\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_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": "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.\n", - "Call the method `iter_outputs()` to get a list of each Cloud Storage file generated with the results.\n", - "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": [ - "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", - "for prediction_result in prediction_results:\n", - " gfile_name = f\"gs://{bp_iter_outputs.bucket.name}/{prediction_result}\".replace(\n", - " BUCKET_URI + \"/\", \"\"\n", - " )\n", - " data = bucket.get_blob(gfile_name).download_as_string()\n", - " data = json.loads(data)\n", - " print(data)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cleanup:mbsdk" - }, - "source": [ - "## Clean up\n", - "\n", - "To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n", - "project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n", - "\n", - "Otherwise, you can delete the individual resources you created in this tutorial:\n", - "\n", - "- dataset\n", - "- model\n", - "- AutoML training job\n", - "- batch job\n", - "- Cloud Storage bucket\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "cleanup:mbsdk" - }, - "outputs": [], - "source": [ - "# If the bucket needs to be deleted too, please set \"delete_bucket\" to True\n", - "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 rm -r $BUCKET_URI" - ] - } - ], - "metadata": { - "colab": { - "name": "sdk_automl_video_classification_batch.ipynb", - "toc_visible": true - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/notebooks/official/automl/sdk_automl_video_object_tracking_batch.ipynb b/notebooks/official/automl/sdk_automl_video_object_tracking_batch.ipynb deleted file mode 100644 index 75ef9125b..000000000 --- a/notebooks/official/automl/sdk_automl_video_object_tracking_batch.ipynb +++ /dev/null @@ -1,819 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "copyright" - }, - "outputs": [], - "source": [ - "# Copyright 2022 Google LLC\n", - "#\n", - "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", - "# you may not use this file except in compliance with the License.\n", - "# You may obtain a copy of the License at\n", - "#\n", - "# https://www.apache.org/licenses/LICENSE-2.0\n", - "#\n", - "# Unless required by applicable law or agreed to in writing, software\n", - "# distributed under the 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" - }, - "source": [ - "# Vertex AI SDK for Python: AutoML training video object tracking model for batch prediction\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \"Google
Open in Colab\n", - "
\n", - "
\n", - " \n", - " \"Google
Open in Colab Enterprise\n", - "
\n", - "
\n", - "\n", - " \"Vertex
\n", - " Open in Vertex AI Workbench\n", - "
\n", - "
\n", - " \n", - " \"GitHub
\n", - " View on GitHub\n", - "
\n", - "
\n", - "


" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "overview:automl" - }, - "source": [ - "## Overview\n", - "\n", - "\n", - "This tutorial demonstrates how to use the Vertex AI SDK for Python to create video object tracking models and do batch prediction using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model.\n", - "\n", - "Learn more about [Object tracking for video data](https://cloud.google.com/vertex-ai/docs/training-overview#object_tracking_for_videos)." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "objective:automl,training,batch_prediction" - }, - "source": [ - "### Objective\n", - "\n", - "In this tutorial, you learn how to create an AutoML video object tracking model from a Python script, and then do a batch prediction using the Vertex AI SDK for Python. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n", - "\n", - "This tutorial uses the following Google Cloud services and resources:\n", - "\n", - "- Vertex AI\n", - "- Google Cloud Storage\n", - "\n", - "The steps performed include:\n", - "\n", - "- Create a Vertex dataset resource.\n", - "- Train the model.\n", - "- View the model evaluation.\n", - "- Make a batch prediction.\n", - "\n", - "There is one key difference between using batch prediction and using online prediction:\n", - "\n", - "* Prediction Service: Does an on-demand prediction for the entire set of instances (i.e., one or more data items) and returns the results in real-time.\n", - "\n", - "* Batch Prediction Service: Does a queued (batch) prediction for the entire set of instances in the background and stores the results in a Cloud Storage bucket when ready." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "dataset:traffic,vot" - }, - "source": [ - "### Dataset\n", - "\n", - "The dataset used for this tutorial is the [Traffic](https://storage.googleapis.com/automl-video-demo-data/traffic_videos/traffic_videos_labels.csv) dataset. 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": "514a03ed1a82" - }, - "outputs": [], - "source": [ - "! pip3 install --upgrade --quiet google-cloud-aiplatform google-cloud-storage" - ] - }, - { - "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": "294fe4e5a671" - }, - "outputs": [], - "source": [ - "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n", - "LOCATION = \"us-central1\" # @param {type:\"string\"}" - ] - }, - { - "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": "91c46850b49b" - }, - "outputs": [], - "source": [ - "! gsutil mb -l $LOCATION -p $PROJECT_ID $BUCKET_URI" - ] - }, - { - "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": [ - "from google.cloud import aiplatform\n", - "\n", - "aiplatform.init(project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_URI)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "setup_vars" - }, - "source": [ - "### Import libraries and define constants" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "import_aip:mbsdk" - }, - "outputs": [], - "source": [ - "import json\n", - "\n", - "from google.cloud import storage" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "tutorial_start:automl" - }, - "source": [ - "# Tutorial\n", - "\n", - "Now you are ready to start creating your own AutoML video object tracking model." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "import_file:u_dataset,csv" - }, - "source": [ - "#### Location of Cloud Storage training data.\n", - "\n", - "Now set the variable `IMPORT_FILE` to the location of the CSV index file in Cloud Storage." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "import_file:traffic,csv,vot" - }, - "outputs": [], - "source": [ - "IMPORT_FILE = \"gs://cloud-samples-data/ai-platform-unified/video/traffic/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": [ - "FILE = IMPORT_FILE\n", - "\n", - "count = ! gsutil cat $FILE | wc -l\n", - "print(\"Number of Examples\", int(count[0]))\n", - "\n", - "! gsutil cat $FILE | head" - ] - }, - { - "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": [ - "dataset = aiplatform.VideoDataset.create(\n", - " display_name=\"Traffic\",\n", - " gcs_source=[IMPORT_FILE],\n", - " import_schema_uri=aiplatform.schema.dataset.ioformat.video.object_tracking,\n", - ")\n", - "\n", - "print(dataset.resource_name)" - ] - }, - { - "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: 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" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "create_automl_pipeline:video,vot" - }, - "outputs": [], - "source": [ - "job = aiplatform.AutoMLVideoTrainingJob(\n", - " display_name=\"traffic\",\n", - " prediction_type=\"object_tracking\",\n", - ")\n", - "\n", - "print(job)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "run_automl_pipeline:video" - }, - "source": [ - "#### Run the training pipeline\n", - "\n", - "Next, you 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 will take upto 4 hours." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "run_automl_pipeline:video" - }, - "outputs": [], - "source": [ - "model = job.run(\n", - " dataset=dataset,\n", - " model_display_name=\"traffic\",\n", - " training_fraction_split=0.8,\n", - " test_fraction_split=0.2,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "source": [ - "## Review model evaluation scores\n", - "After your model has finished training, you can review the evaluation scores for it.\n", - "\n", - "First, you need to get a reference to the new model. As with datasets, you can either use the reference to the model variable you created when you deployed the model or you can list all of the models in your project." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "evaluate_the_model:mbsdk" - }, - "outputs": [], - "source": [ - "model_evaluations = model.list_model_evaluations()\n", - "model_evaluation = list(model_evaluations)[0]\n", - "print(model_evaluation)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "b3377983702b" - }, - "outputs": [], - "source": [ - "# Print the evaluation metrics\n", - "for evaluation in model_evaluations:\n", - " evaluation = evaluation.to_dict()\n", - " print(\"Model's evaluation metrics from Training:\\n\")\n", - " metrics = evaluation[\"metrics\"]\n", - " for metric in metrics.keys():\n", - " print(f\"metric: {metric}, value: {metrics[metric]}\\n\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "make_prediction" - }, - "source": [ - "## Send a batch prediction request\n", - "\n", - "Send a batch prediction to your deployed model." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "get_test_items:batch_prediction" - }, - "source": [ - "### Get test item(s)\n", - "\n", - "Now do a batch prediction to your Vertex model. You will use arbitrary examples out of the dataset as a test items. Don't be concerned that the examples were likely used in training the model -- we just want to demonstrate how to make a prediction." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "get_test_items:automl,vot,csv" - }, - "outputs": [], - "source": [ - "test_items = ! gsutil cat $IMPORT_FILE | head -n2\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])" - ] - }, - { - "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 will 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": [ - "test_filename = \"test.jsonl\"\n", - "gcs_input_uri = BUCKET_URI + \"/test.jsonl\"\n", - "# making data_1 and data_2 variables using the structure mentioned above\n", - "data_1 = {\n", - " \"content\": test_item_1,\n", - " \"mimeType\": \"video/avi\",\n", - " \"timeSegmentStart\": \"0.0s\",\n", - " \"timeSegmentEnd\": \"5.0s\",\n", - "}\n", - "\n", - "data_2 = {\n", - " \"content\": test_item_2,\n", - " \"mimeType\": \"video/avi\",\n", - " \"timeSegmentStart\": \"0.0s\",\n", - " \"timeSegmentEnd\": \"5.0s\",\n", - "}\n", - "\n", - "# getting reference to bucket\n", - "bucket = storage.Client(project=PROJECT_ID).bucket(BUCKET_URI.replace(\"gs://\", \"\"))\n", - "\n", - "# creating a blob\n", - "blob = bucket.blob(blob_name=test_filename)\n", - "\n", - "# creating data variable\n", - "data = json.dumps(data_1) + \"\\n\" + json.dumps(data_2) + \"\\n\"\n", - "\n", - "# uploading data variable content to bucket\n", - "blob.upload_from_string(data)\n", - "\n", - "# printing path of uploaded file\n", - "print(gcs_input_uri)\n", - "\n", - "# printing content of uploaded file\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=\"traffic\",\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_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": "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": [ - "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}\".replace(\n", - " BUCKET_URI + \"/\", \"\"\n", - " )\n", - " data = bucket.get_blob(gfile_name).download_as_string()\n", - " data = json.loads(data)\n", - " print(data)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cleanup:mbsdk" - }, - "source": [ - "# Cleaning up\n", - "\n", - "To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n", - "project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n", - "\n", - "Otherwise, you can delete the individual resources you created in this tutorial:\n", - "\n", - "- Dataset\n", - "- Model\n", - "- AutoML Training Job\n", - "- Batch Job\n", - "- Cloud Storage Bucket" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "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 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_bucket = False\n", - "if delete_bucket:\n", - " ! gsutil -m rm -r $BUCKET_URI" - ] - } - ], - "metadata": { - "colab": { - "name": "sdk_automl_video_object_tracking_batch.ipynb", - "toc_visible": true - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -}