mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
copy notebooks/community from ai-platform-samples (#3)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
+1468
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+1766
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+1471
File diff suppressed because it is too large
Load Diff
+1593
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+1811
File diff suppressed because it is too large
Load Diff
+1687
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+1765
File diff suppressed because it is too large
Load Diff
+1618
File diff suppressed because it is too large
Load Diff
+1664
File diff suppressed because it is too large
Load Diff
+1839
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+1563
File diff suppressed because it is too large
Load Diff
+1779
File diff suppressed because it is too large
Load Diff
+1552
File diff suppressed because it is too large
Load Diff
+1777
File diff suppressed because it is too large
Load Diff
+1553
File diff suppressed because it is too large
Load Diff
+1771
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+2465
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+2444
File diff suppressed because it is too large
Load Diff
+2294
File diff suppressed because it is too large
Load Diff
+2514
File diff suppressed because it is too large
Load Diff
+2795
File diff suppressed because it is too large
Load Diff
+2261
File diff suppressed because it is too large
Load Diff
+2179
File diff suppressed because it is too large
Load Diff
+2269
File diff suppressed because it is too large
Load Diff
+2188
File diff suppressed because it is too large
Load Diff
+2339
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+2342
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+2215
File diff suppressed because it is too large
Load Diff
+2498
File diff suppressed because it is too large
Load Diff
+2187
File diff suppressed because it is too large
Load Diff
+2190
File diff suppressed because it is too large
Load Diff
+2112
File diff suppressed because it is too large
Load Diff
+2090
File diff suppressed because it is too large
Load Diff
+2192
File diff suppressed because it is too large
Load Diff
+2159
File diff suppressed because it is too large
Load Diff
+2169
File diff suppressed because it is too large
Load Diff
+1305
File diff suppressed because it is too large
Load Diff
+2081
File diff suppressed because it is too large
Load Diff
+2104
File diff suppressed because it is too large
Load Diff
+2079
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+1635
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,889 @@
|
||||
{
|
||||
"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 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": "JAPoU8Sm5E6e"
|
||||
},
|
||||
"source": [
|
||||
"<table align=\"left\">\n",
|
||||
"\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/community/hyperparameter_tuning/distributed-hyperparameter-tuning.ipynb\"\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/community/hyperparameter_tuning/distributed-hyperparameter-tuning.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "tvgnzT1CKxrO"
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"This notebook demonstrates how to run a hyperparameter tuning job with Vertex Training to discover optimal hyperparameter values for an ML model. To speed up the training process, `MirroredStrategy` from the `tf.distribute` module is used to distribute training across multiple GPUs on a single machine.\n",
|
||||
"\n",
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"The dataset used for this tutorial is the [horses or humans dataset](https://www.tensorflow.org/datasets/catalog/horses_or_humans) from [TensorFlow Datasets](https://www.tensorflow.org/datasets). The trained model predicts if an image is of a horse or a human.\n",
|
||||
"\n",
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this notebook, you create a custom-trained model from a Python script in a Docker container. You learn how to modify training application code for hyperparameter tuning and submit a Vertex Training hyperparameter tuning job with the Python SDK.\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"* Create a Vertex AI custom job for training a model.\n",
|
||||
"* Launch hyperparameter tuning job with the Python SDK.\n",
|
||||
"* Cleanup resources.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"### Costs \n",
|
||||
"\n",
|
||||
"This tutorial uses billable components of Google Cloud:\n",
|
||||
"\n",
|
||||
"* Vertex AI\n",
|
||||
"* Cloud Storage\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Learn about [Vertex AI\n",
|
||||
"pricing](https://cloud.google.com/vertex-ai/pricing) 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": "ze4-nDLfK4pw"
|
||||
},
|
||||
"source": [
|
||||
"### Set up your local development environment\n",
|
||||
"\n",
|
||||
"**If you are using Colab or Google Cloud Notebooks**, your environment already meets\n",
|
||||
"all the requirements to run this notebook. You can skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "gCuSR8GkAgzl"
|
||||
},
|
||||
"source": [
|
||||
"**Otherwise**, make sure your environment meets this notebook's requirements.\n",
|
||||
"You need the following:\n",
|
||||
"\n",
|
||||
"* The Google Cloud SDK\n",
|
||||
"* Git\n",
|
||||
"* Python 3\n",
|
||||
"* virtualenv\n",
|
||||
"* Jupyter notebook running in a virtual environment with Python 3\n",
|
||||
"\n",
|
||||
"The Google Cloud guide to [Setting up a Python development\n",
|
||||
"environment](https://cloud.google.com/python/setup) and the [Jupyter\n",
|
||||
"installation guide](https://jupyter.org/install) provide detailed instructions\n",
|
||||
"for meeting these requirements. The following steps provide a condensed set of\n",
|
||||
"instructions:\n",
|
||||
"\n",
|
||||
"1. [Install and initialize the Cloud SDK.](https://cloud.google.com/sdk/docs/)\n",
|
||||
"\n",
|
||||
"1. [Install Python 3.](https://cloud.google.com/python/setup#installing_python)\n",
|
||||
"\n",
|
||||
"1. [Install\n",
|
||||
" virtualenv](https://cloud.google.com/python/setup#installing_and_using_virtualenv)\n",
|
||||
" and create a virtual environment that uses Python 3. Activate the virtual environment.\n",
|
||||
"\n",
|
||||
"1. To install Jupyter, run `pip3 install jupyter` on the\n",
|
||||
"command-line in a terminal shell.\n",
|
||||
"\n",
|
||||
"1. To launch Jupyter, run `jupyter notebook` on the command-line in a terminal shell.\n",
|
||||
"\n",
|
||||
"1. Open this notebook in the Jupyter Notebook Dashboard."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "i7EUnXsZhAGF"
|
||||
},
|
||||
"source": [
|
||||
"### Install additional packages\n",
|
||||
"\n",
|
||||
"Install the latest version of Vertex SDK for Python."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "2b4ef9b72d43"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"# The Google Cloud Notebook product has specific requirements\n",
|
||||
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
|
||||
"\n",
|
||||
"# Google Cloud Notebook requires dependencies to be installed with '--user'\n",
|
||||
"USER_FLAG = \"\"\n",
|
||||
"if IS_GOOGLE_CLOUD_NOTEBOOK:\n",
|
||||
" USER_FLAG = \"--user\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "wyy5Lbnzg5fi"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "hhq5zEbGg0XX"
|
||||
},
|
||||
"source": [
|
||||
"### Restart the kernel\n",
|
||||
"\n",
|
||||
"After you install the additional packages, you need to restart the notebook kernel so it can find the packages."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "EzrelQZ22IZj"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
" # Automatically restart kernel after installs\n",
|
||||
" import IPython\n",
|
||||
"\n",
|
||||
" app = IPython.Application.instance()\n",
|
||||
" app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "lWEdiXsJg0XY"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"\n",
|
||||
"### Select a GPU runtime\n",
|
||||
"\n",
|
||||
"**Make sure you're running this notebook in a GPU runtime if you have that option. In Colab, select \"Runtime --> Change runtime type > GPU\"**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "BF1j6f9HApxa"
|
||||
},
|
||||
"source": [
|
||||
"### Set up your Google Cloud project\n",
|
||||
"\n",
|
||||
"**The following steps are required, regardless of your notebook environment.**\n",
|
||||
"\n",
|
||||
"1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\n",
|
||||
"\n",
|
||||
"1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
|
||||
"\n",
|
||||
"1. [Enable the Vertex AI API and Compute Engine API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com,compute_component).\n",
|
||||
"1. If you are running this notebook locally, you will need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
|
||||
"\n",
|
||||
"1. Enter your project ID in the cell below. Then run the cell to make sure the\n",
|
||||
"Cloud SDK uses the right project for all the commands in this notebook.\n",
|
||||
"\n",
|
||||
"**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$` into these commands."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "WReHDGG5g0XY"
|
||||
},
|
||||
"source": [
|
||||
"#### Set your project ID\n",
|
||||
"\n",
|
||||
"**If you don't know your project ID**, you may be able to get your project ID using `gcloud`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "oM1iC_MfAts1"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"PROJECT_ID = \"\"\n",
|
||||
"\n",
|
||||
"# Get your Google Cloud project ID from gcloud\n",
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
" shell_output=!gcloud config list --format 'value(core.project)' 2>/dev/null\n",
|
||||
" PROJECT_ID = shell_output[0]\n",
|
||||
" print(\"Project ID: \", PROJECT_ID)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "qJYoRfYng0XZ"
|
||||
},
|
||||
"source": [
|
||||
"Otherwise, set your project ID here."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "riG_qUokg0XZ"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
|
||||
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "06571eb4063b"
|
||||
},
|
||||
"source": [
|
||||
"#### Timestamp\n",
|
||||
"\n",
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append it onto the name of resources you create in this tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "697568e92bd6"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "dr--iN2kAylZ"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Google Cloud Notebooks**, your environment is already\n",
|
||||
"authenticated. Skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "sBCra4QMA2wR"
|
||||
},
|
||||
"source": [
|
||||
"**If you are using Colab**, run the cell below and follow the instructions\n",
|
||||
"when prompted to authenticate your account via oAuth.\n",
|
||||
"\n",
|
||||
"**Otherwise**, follow these steps:\n",
|
||||
"\n",
|
||||
"1. In the Cloud Console, go to the [**Create service account key**\n",
|
||||
" page](https://console.cloud.google.com/apis/credentials/serviceaccountkey).\n",
|
||||
"\n",
|
||||
"2. Click **Create service account**.\n",
|
||||
"\n",
|
||||
"3. In the **Service account name** field, enter a name, and\n",
|
||||
" click **Create**.\n",
|
||||
"\n",
|
||||
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"Vertex AI\"\n",
|
||||
"into the filter box, and select\n",
|
||||
" **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
|
||||
"\n",
|
||||
"5. Click *Create*. A JSON file that contains your key downloads to your\n",
|
||||
"local environment.\n",
|
||||
"\n",
|
||||
"6. Enter the path to your service account key as the\n",
|
||||
"`GOOGLE_APPLICATION_CREDENTIALS` variable in the cell below and run the cell."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "PyQmSRbKA8r-"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"# If you are running this notebook in Colab, run this cell and follow the\n",
|
||||
"# instructions to authenticate your GCP account. This provides access to your\n",
|
||||
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
|
||||
"# requests.\n",
|
||||
"\n",
|
||||
"# The Google Cloud Notebook product has specific requirements\n",
|
||||
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
|
||||
"\n",
|
||||
"# If on Google Cloud Notebooks, then don't execute this code\n",
|
||||
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
|
||||
" if \"google.colab\" in sys.modules:\n",
|
||||
" from google.colab import auth as google_auth\n",
|
||||
"\n",
|
||||
" google_auth.authenticate_user()\n",
|
||||
"\n",
|
||||
" # If you are running this notebook locally, replace the string below with the\n",
|
||||
" # path to your service account key and run this cell to authenticate your GCP\n",
|
||||
" # account.\n",
|
||||
" elif not os.getenv(\"IS_TESTING\"):\n",
|
||||
" %env GOOGLE_APPLICATION_CREDENTIALS ''"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "zgPO1eR3CYjk"
|
||||
},
|
||||
"source": [
|
||||
"### Create a Cloud Storage bucket\n",
|
||||
"\n",
|
||||
"**The following steps are required, regardless of your notebook environment.**\n",
|
||||
"\n",
|
||||
"When you submit a custom training job using the Cloud SDK, you will need to provide a staging bucket.\n",
|
||||
"\n",
|
||||
"Set the name of your Cloud Storage bucket below. It must be unique across all\n",
|
||||
"Cloud Storage buckets.\n",
|
||||
"\n",
|
||||
"You may also change the `REGION` variable, which is used for operations\n",
|
||||
"throughout the rest of this notebook. Make sure to [choose a region where Vertex AI services are\n",
|
||||
"available](https://cloud.google.com/vertex-ai/docs/general/locations#available_regions). You may\n",
|
||||
"not use a Multi-Regional Storage bucket for training with Vertex AI."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "MzGDU7TWdts_"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"BUCKET_NAME = \"gs://[your-bucket-name]\" # @param {type:\"string\"}\n",
|
||||
"REGION = \"[your-region]\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "cf221059d072"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"gs://[your-bucket-name]\":\n",
|
||||
" BUCKET_NAME = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "-EcIXiGsCePi"
|
||||
},
|
||||
"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": "NIq7R4HZCfIc"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION $BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ucvCsknMCims"
|
||||
},
|
||||
"source": [
|
||||
"Finally, validate access to your Cloud Storage bucket by examining its contents:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "vhOb7YnwClBb"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil ls -al $BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "XoEqT2Y4DJmf"
|
||||
},
|
||||
"source": [
|
||||
"### Import libraries and define constants"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "pRUOFELefqf1"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"from google.cloud import aiplatform\n",
|
||||
"from google.cloud.aiplatform import hyperparameter_tuning as hpt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "reBCSTKOg47l"
|
||||
},
|
||||
"source": [
|
||||
"### Write Dockerfile\n",
|
||||
"\n",
|
||||
"The first step in containerizing your code is to create a Dockerfile. In the Dockerfile, you'll include all the commands needed to run the image such as installing the necessary libraries and setting up the entry point for the training code.\n",
|
||||
"\n",
|
||||
"This Dockerfile uses the Deep Learning Container TensorFlow Enterprise 2.5 GPU Docker image. The Deep Learning Containers on Google Cloud come with many common ML and data science frameworks pre-installed. After downloading that image, this Dockerfile installs the [CloudML Hypertune](https://github.com/GoogleCloudPlatform/cloudml-hypertune) library and sets up the entrypoint for the training code.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%writefile Dockerfile\n",
|
||||
"\n",
|
||||
"FROM gcr.io/deeplearning-platform-release/tf2-gpu.2-5\n",
|
||||
"WORKDIR /\n",
|
||||
"\n",
|
||||
"# Installs hypertune library\n",
|
||||
"RUN pip install cloudml-hypertune\n",
|
||||
"\n",
|
||||
"# Copies the trainer code to the docker image.\n",
|
||||
"COPY trainer /trainer\n",
|
||||
"\n",
|
||||
"# Sets up the entry point to invoke the trainer.\n",
|
||||
"ENTRYPOINT [\"python\", \"-m\", \"trainer.task\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Create training application code\n",
|
||||
"\n",
|
||||
"Next, you create a trainer directory with a `task.py` script that contains the code for your training application."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "MjJTYC86hPOZ"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Create trainer directory\n",
|
||||
"\n",
|
||||
"! mkdir trainer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"In the next cell, you write the contents of the training script, `task.py`. This file downloads the _horses or humans_ dataset from TensorFlow datasets and trains a `tf.keras` functional model using `MirroredStrategy` from the `tf.distribute` module.\n",
|
||||
"\n",
|
||||
"There are a few components that are specific to using the hyperparameter tuning service:\n",
|
||||
"\n",
|
||||
"* The script imports the `hypertune` library. Note that the Dockerfile included instructions to pip install the hypertune library.\n",
|
||||
"* The function `get_args()` defines a command-line argument for each hyperparameter you want to tune. In this example, the hyperparameters that will be tuned are the learning rate, the momentum value in the optimizer, and the number of units in the last hidden layer of the model. The value passed in those arguments is then used to set the corresponding hyperparameter in the code.\n",
|
||||
"* At the end of the `main()` function, the hypertune library is used to define the metric to optimize. In this example, the metric that will be optimized is the the validation accuracy. This metric is passed to an instance of `HyperTune`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%writefile trainer/task.py\n",
|
||||
"\n",
|
||||
"import argparse\n",
|
||||
"import hypertune\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import tensorflow_datasets as tfds\n",
|
||||
"\n",
|
||||
"def get_args():\n",
|
||||
" \"\"\"Parses args. Must include all hyperparameters you want to tune.\"\"\"\n",
|
||||
"\n",
|
||||
" parser = argparse.ArgumentParser()\n",
|
||||
" parser.add_argument(\n",
|
||||
" '--learning_rate', required=True, type=float, help='learning rate')\n",
|
||||
" parser.add_argument(\n",
|
||||
" '--momentum', required=True, type=float, help='SGD momentum value')\n",
|
||||
" parser.add_argument(\n",
|
||||
" '--units',\n",
|
||||
" required=True,\n",
|
||||
" type=int,\n",
|
||||
" help='number of units in last hidden layer')\n",
|
||||
" parser.add_argument(\n",
|
||||
" '--epochs',\n",
|
||||
" required=False,\n",
|
||||
" type=int,\n",
|
||||
" default=10,\n",
|
||||
" help='number of training epochs')\n",
|
||||
" args = parser.parse_args()\n",
|
||||
" return args\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def preprocess_data(image, label):\n",
|
||||
" \"\"\"Resizes and scales images.\"\"\"\n",
|
||||
"\n",
|
||||
" image = tf.image.resize(image, (150, 150))\n",
|
||||
" return tf.cast(image, tf.float32) / 255., label\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def create_dataset(batch_size):\n",
|
||||
" \"\"\"Loads Horses Or Humans dataset and preprocesses data.\"\"\"\n",
|
||||
"\n",
|
||||
" data, info = tfds.load(\n",
|
||||
" name='horses_or_humans', as_supervised=True, with_info=True)\n",
|
||||
"\n",
|
||||
" # Create train dataset\n",
|
||||
" train_data = data['train'].map(preprocess_data)\n",
|
||||
" train_data = train_data.shuffle(1000)\n",
|
||||
" train_data = train_data.batch(batch_size)\n",
|
||||
"\n",
|
||||
" # Create validation dataset\n",
|
||||
" validation_data = data['test'].map(preprocess_data)\n",
|
||||
" validation_data = validation_data.batch(64)\n",
|
||||
"\n",
|
||||
" return train_data, validation_data\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def create_model(units, learning_rate, momentum):\n",
|
||||
" \"\"\"Defines and compiles model.\"\"\"\n",
|
||||
"\n",
|
||||
" inputs = tf.keras.Input(shape=(150, 150, 3))\n",
|
||||
" x = tf.keras.layers.Conv2D(16, (3, 3), activation='relu')(inputs)\n",
|
||||
" x = tf.keras.layers.MaxPooling2D((2, 2))(x)\n",
|
||||
" x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(x)\n",
|
||||
" x = tf.keras.layers.MaxPooling2D((2, 2))(x)\n",
|
||||
" x = tf.keras.layers.Conv2D(64, (3, 3), activation='relu')(x)\n",
|
||||
" x = tf.keras.layers.MaxPooling2D((2, 2))(x)\n",
|
||||
" x = tf.keras.layers.Flatten()(x)\n",
|
||||
" x = tf.keras.layers.Dense(units, activation='relu')(x)\n",
|
||||
" outputs = tf.keras.layers.Dense(1, activation='sigmoid')(x)\n",
|
||||
" model = tf.keras.Model(inputs, outputs)\n",
|
||||
" model.compile(\n",
|
||||
" loss='binary_crossentropy',\n",
|
||||
" optimizer=tf.keras.optimizers.SGD(\n",
|
||||
" learning_rate=learning_rate, momentum=momentum),\n",
|
||||
" metrics=['accuracy'])\n",
|
||||
" return model\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" args = get_args()\n",
|
||||
"\n",
|
||||
" # Create Strategy\n",
|
||||
" strategy = tf.distribute.MirroredStrategy()\n",
|
||||
"\n",
|
||||
" # Scale batch size\n",
|
||||
" GLOBAL_BATCH_SIZE = 64 * strategy.num_replicas_in_sync \n",
|
||||
" train_data, validation_data = create_dataset(GLOBAL_BATCH_SIZE)\n",
|
||||
"\n",
|
||||
" # Wrap model variables within scope\n",
|
||||
" with strategy.scope():\n",
|
||||
" model = create_model(args.units, args.learning_rate, args.momentum)\n",
|
||||
"\n",
|
||||
" # Train model\n",
|
||||
" history = model.fit(\n",
|
||||
" train_data, epochs=args.epochs, validation_data=validation_data)\n",
|
||||
"\n",
|
||||
" # Define Metric\n",
|
||||
" hp_metric = history.history['val_accuracy'][-1]\n",
|
||||
"\n",
|
||||
" hpt = hypertune.HyperTune()\n",
|
||||
" hpt.report_hyperparameter_tuning_metric(\n",
|
||||
" hyperparameter_metric_tag='accuracy',\n",
|
||||
" metric_value=hp_metric,\n",
|
||||
" global_step=args.epochs)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Build the Container\n",
|
||||
"\n",
|
||||
"In the next cells, you build the container and push it to Google Container Registry."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Set the IMAGE_URI\n",
|
||||
"IMAGE_URI=f\"gcr.io/{PROJECT_ID}/horse-human:hypertune\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Build the docker image\n",
|
||||
"! docker build -f Dockerfile -t $IMAGE_URI ./"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Push it to Google Container Registry:\n",
|
||||
"! docker push $IMAGE_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Create and run hyperparameter tuning job on Vertex AI\n",
|
||||
"\n",
|
||||
"Once your container is pushed to Google Container Registry, you use the Vertex SDK to create and run the hyperparameter tuning job.\n",
|
||||
"\n",
|
||||
"You define the following specifications:\n",
|
||||
"* `worker_pool_specs`: Dictionary specifying the machine type and Docker image. This example defines a single node cluster with one `n1-standard-4` machine with two `NVIDIA_TESLA_T4` GPUs.\n",
|
||||
"* `parameter_spec`: Dictionary specifying the parameters to optimize. The dictionary key is the string assigned to the command line argument for each hyperparameter in your training application code, and the dictionary value is the parameter specification. The parameter specification includes the type, min/max values, and scale for the hyperparameter.\n",
|
||||
"* `metric_spec`: Dictionary specifying the metric to optimize. The dictionary key is the `hyperparameter_metric_tag` that you set in your training application code, and the value is the optimization goal."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"worker_pool_specs = [{\n",
|
||||
" 'machine_spec': {\n",
|
||||
" 'machine_type': 'n1-standard-4',\n",
|
||||
" 'accelerator_type': 'NVIDIA_TESLA_T4',\n",
|
||||
" 'accelerator_count': 2\n",
|
||||
" },\n",
|
||||
" 'replica_count': 1,\n",
|
||||
" 'container_spec': {\n",
|
||||
" 'image_uri': IMAGE_URI\n",
|
||||
" }\n",
|
||||
"}]\n",
|
||||
"\n",
|
||||
"metric_spec = {'accuracy': 'maximize'}\n",
|
||||
"\n",
|
||||
"parameter_spec = {\n",
|
||||
" 'learning_rate': hpt.DoubleParameterSpec(min=0.001, max=1, scale='log'),\n",
|
||||
" 'momentum': hpt.DoubleParameterSpec(min=0, max=1, scale='linear'),\n",
|
||||
" 'units': hpt.DiscreteParameterSpec(values=[64, 128, 512], scale=None)\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Create a `CustomJob`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Create a CustomJob\n",
|
||||
"\n",
|
||||
"JOB_NAME = 'horses-humans-hyperparam-job' + TIMESTAMP\n",
|
||||
"\n",
|
||||
"my_custom_job = aiplatform.CustomJob(display_name=JOB_NAME,\n",
|
||||
" worker_pool_specs=worker_pool_specs,\n",
|
||||
" staging_bucket=BUCKET_NAME)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Then, create and run a `HyperparameterTuningJob`.\n",
|
||||
"\n",
|
||||
"There are a few arguments to note:\n",
|
||||
"\n",
|
||||
"* `max_trial_count`: Sets an upper bound on the number of trials the service will run. The recommended practice is to start with a smaller number of trials and get a sense of how impactful your chosen hyperparameters are before scaling up.\n",
|
||||
"\n",
|
||||
"* `parallel_trial_count`: If you use parallel trials, the service provisions multiple training processing clusters. The worker pool spec that you specify when creating the job is used for each individual training cluster. Increasing the number of parallel trials reduces the amount of time the hyperparameter tuning job takes to run; however, it can reduce the effectiveness of the job overall. This is because the default tuning strategy uses results of previous trials to inform the assignment of values in subsequent trials.\n",
|
||||
" \n",
|
||||
"* `search_algorithm`: The available search algorithms are grid, random, or default (None). The default option applies Bayesian optimization to search the space of possible hyperparameter values and is the recommended algorithm."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Create and run HyperparameterTuningJob\n",
|
||||
"\n",
|
||||
"hp_job = aiplatform.HyperparameterTuningJob(\n",
|
||||
" display_name=JOB_NAME,\n",
|
||||
" custom_job=my_custom_job,\n",
|
||||
" metric_spec=metric_spec,\n",
|
||||
" parameter_spec=parameter_spec,\n",
|
||||
" max_trial_count=15,\n",
|
||||
" parallel_trial_count=3,\n",
|
||||
" search_algorithm=None)\n",
|
||||
"\n",
|
||||
"hp_job.run()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Click on the generated link to see your run in the Cloud Console. When the job completes, you will see the results of the tuning trials."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "TpV-iwP9qw9c"
|
||||
},
|
||||
"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": "sx_vKniMq9ZX"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Delete Cloud Storage objects that were created\n",
|
||||
"! gsutil -m rm -r $BUCKET_NAME"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"collapsed_sections": [],
|
||||
"name": "notebook_template.ipynb",
|
||||
"toc_visible": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 266 KiB |
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1942
File diff suppressed because it is too large
Load Diff
+2649
File diff suppressed because it is too large
Load Diff
+1398
File diff suppressed because it is too large
Load Diff
+1373
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+1902
File diff suppressed because one or more lines are too long
+2065
File diff suppressed because it is too large
Load Diff
+2253
File diff suppressed because it is too large
Load Diff
+2001
File diff suppressed because it is too large
Load Diff
+2333
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+1839
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
+1691
File diff suppressed because it is too large
Load Diff
+2535
File diff suppressed because it is too large
Load Diff
+1633
File diff suppressed because it is too large
Load Diff
+2417
File diff suppressed because it is too large
Load Diff
+1634
File diff suppressed because it is too large
Load Diff
+2355
File diff suppressed because it is too large
Load Diff
+1542
File diff suppressed because it is too large
Load Diff
+2063
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+877
@@ -0,0 +1,877 @@
|
||||
{
|
||||
"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 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": "JAPoU8Sm5E6e"
|
||||
},
|
||||
"source": [
|
||||
"<table align=\"left\">\n",
|
||||
"\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/community/ml_metadata/sdk-metric-parameter-tracking-for-locally-trained-models.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/community/ml_metadata/sdk-metric-parameter-tracking-for-locally-trained-models.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "WBFL9LagqmwT"
|
||||
},
|
||||
"source": [
|
||||
"#Vertex AI: Track parameters and metrics for locally trained models"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "tvgnzT1CKxrO"
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"This notebook demonstrates how to track metrics and parameters for ML training jobs and analyze this metadata using Vertex SDK for Python.\n",
|
||||
"\n",
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"In this notebook, we will train a simple distributed neural network (DNN) model to predict automobile's miles per gallon (MPG) based on automobile information in the [auto-mpg dataset](https://www.kaggle.com/devanshbesain/exploration-and-analysis-auto-mpg).\n",
|
||||
"\n",
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this notebook, you will learn how to use Vertex SDK for Python to:\n",
|
||||
"\n",
|
||||
" * Track parameters and metrics for a locally trainined model.\n",
|
||||
" * Extract and perform analysis for all parameters and metrics within an Experiment.\n",
|
||||
"\n",
|
||||
"### Costs \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"This tutorial uses billable components of Google Cloud:\n",
|
||||
"\n",
|
||||
"* Vertex AI\n",
|
||||
"* Cloud Storage\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Learn about [Vertex AI\n",
|
||||
"pricing](https://cloud.google.com/vertex-ai/pricing) 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": "ze4-nDLfK4pw"
|
||||
},
|
||||
"source": [
|
||||
"### Set up your local development environment\n",
|
||||
"\n",
|
||||
"**If you are using Colab or Google Cloud Notebooks**, your environment already meets\n",
|
||||
"all the requirements to run this notebook. You can skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "gCuSR8GkAgzl"
|
||||
},
|
||||
"source": [
|
||||
"**Otherwise**, make sure your environment meets this notebook's requirements.\n",
|
||||
"You need the following:\n",
|
||||
"\n",
|
||||
"* The Google Cloud SDK\n",
|
||||
"* Git\n",
|
||||
"* Python 3\n",
|
||||
"* virtualenv\n",
|
||||
"* Jupyter notebook running in a virtual environment with Python 3\n",
|
||||
"\n",
|
||||
"The Google Cloud guide to [Setting up a Python development\n",
|
||||
"environment](https://cloud.google.com/python/setup) and the [Jupyter\n",
|
||||
"installation guide](https://jupyter.org/install) provide detailed instructions\n",
|
||||
"for meeting these requirements. The following steps provide a condensed set of\n",
|
||||
"instructions:\n",
|
||||
"\n",
|
||||
"1. [Install and initialize the Cloud SDK.](https://cloud.google.com/sdk/docs/)\n",
|
||||
"\n",
|
||||
"1. [Install Python 3.](https://cloud.google.com/python/setup#installing_python)\n",
|
||||
"\n",
|
||||
"1. [Install\n",
|
||||
" virtualenv](https://cloud.google.com/python/setup#installing_and_using_virtualenv)\n",
|
||||
" and create a virtual environment that uses Python 3. Activate the virtual environment.\n",
|
||||
"\n",
|
||||
"1. To install Jupyter, run `pip install jupyter` on the\n",
|
||||
"command-line in a terminal shell.\n",
|
||||
"\n",
|
||||
"1. To launch Jupyter, run `jupyter notebook` on the command-line in a terminal shell.\n",
|
||||
"\n",
|
||||
"1. Open this notebook in the Jupyter Notebook Dashboard."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "i7EUnXsZhAGF"
|
||||
},
|
||||
"source": [
|
||||
"### Install additional packages\n",
|
||||
"\n",
|
||||
"Run the following commands to install the Vertex SDK for Python."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "IaYsrh0Tc17L"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
" USER_FLAG = \"\"\n",
|
||||
"else:\n",
|
||||
" USER_FLAG = \"--user\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "wyy5Lbnzg5fi"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!python3 -m pip install {USER_FLAG} google-cloud-aiplatform --upgrade"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "hhq5zEbGg0XX"
|
||||
},
|
||||
"source": [
|
||||
"### Restart the kernel\n",
|
||||
"\n",
|
||||
"After you install the additional packages, you need to restart the notebook kernel so it can find the packages."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "EzrelQZ22IZj"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
" # Automatically restart kernel after installs\n",
|
||||
" import IPython\n",
|
||||
"\n",
|
||||
" app = IPython.Application.instance()\n",
|
||||
" app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "lWEdiXsJg0XY"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"\n",
|
||||
"### Select a GPU runtime\n",
|
||||
"\n",
|
||||
"**Make sure you're running this notebook in a GPU runtime if you have that option. In Colab, select \"Runtime --> Change runtime type > GPU\"**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "BF1j6f9HApxa"
|
||||
},
|
||||
"source": [
|
||||
"### Set up your Google Cloud project\n",
|
||||
"\n",
|
||||
"**The following steps are required, regardless of your notebook environment.**\n",
|
||||
"\n",
|
||||
"1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\n",
|
||||
"\n",
|
||||
"1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
|
||||
"\n",
|
||||
"1. [Enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
|
||||
"\n",
|
||||
"1. If you are running this notebook locally, you will need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
|
||||
"\n",
|
||||
"1. Enter your project ID in the cell below. Then run the cell to make sure the\n",
|
||||
"Cloud SDK uses the right project for all the commands in this notebook.\n",
|
||||
"\n",
|
||||
"**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$` into these commands."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "WReHDGG5g0XY"
|
||||
},
|
||||
"source": [
|
||||
"#### Set your project ID\n",
|
||||
"\n",
|
||||
"**If you don't know your project ID**, you may be able to get your project ID using `gcloud`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "oM1iC_MfAts1"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"PROJECT_ID = \"\"\n",
|
||||
"\n",
|
||||
"# Get your Google Cloud project ID from gcloud\n",
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
" shell_output=!gcloud config list --format 'value(core.project)' 2>/dev/null\n",
|
||||
" PROJECT_ID = shell_output[0]\n",
|
||||
" print(\"Project ID: \", PROJECT_ID)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "qJYoRfYng0XZ"
|
||||
},
|
||||
"source": [
|
||||
"Otherwise, set your project ID here."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "riG_qUokg0XZ"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
|
||||
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "06571eb4063b"
|
||||
},
|
||||
"source": [
|
||||
"#### Timestamp\n",
|
||||
"\n",
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append it onto the name of resources you create in this tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "697568e92bd6"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "dr--iN2kAylZ"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Google Cloud Notebooks**, your environment is already\n",
|
||||
"authenticated. Skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "sBCra4QMA2wR"
|
||||
},
|
||||
"source": [
|
||||
"**If you are using Colab**, run the cell below and follow the instructions\n",
|
||||
"when prompted to authenticate your account via oAuth.\n",
|
||||
"\n",
|
||||
"**Otherwise**, follow these steps:\n",
|
||||
"\n",
|
||||
"1. In the Cloud Console, go to the [**Create service account key**\n",
|
||||
" page](https://console.cloud.google.com/apis/credentials/serviceaccountkey).\n",
|
||||
"\n",
|
||||
"2. Click **Create service account**.\n",
|
||||
"\n",
|
||||
"3. In the **Service account name** field, enter a name, and\n",
|
||||
" click **Create**.\n",
|
||||
"\n",
|
||||
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"Vertex AI\"\n",
|
||||
"into the filter box, and select\n",
|
||||
" **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
|
||||
"\n",
|
||||
"5. Click *Create*. A JSON file that contains your key downloads to your\n",
|
||||
"local environment.\n",
|
||||
"\n",
|
||||
"6. Enter the path to your service account key as the\n",
|
||||
"`GOOGLE_APPLICATION_CREDENTIALS` variable in the cell below and run the cell."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "PyQmSRbKA8r-"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"# If you are running this notebook in Colab, run this cell and follow the\n",
|
||||
"# instructions to authenticate your GCP account. This provides access to your\n",
|
||||
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
|
||||
"# requests.\n",
|
||||
"\n",
|
||||
"# If on Google Cloud Notebooks, then don't execute this code\n",
|
||||
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
|
||||
" if \"google.colab\" in sys.modules:\n",
|
||||
" from google.colab import auth as google_auth\n",
|
||||
"\n",
|
||||
" google_auth.authenticate_user()\n",
|
||||
"\n",
|
||||
" # If you are running this notebook locally, replace the string below with the\n",
|
||||
" # path to your service account key and run this cell to authenticate your GCP\n",
|
||||
" # account.\n",
|
||||
" elif not os.getenv(\"IS_TESTING\"):\n",
|
||||
" %env GOOGLE_APPLICATION_CREDENTIALS ''"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "XoEqT2Y4DJmf"
|
||||
},
|
||||
"source": [
|
||||
"### Import libraries and define constants"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "Y9Uo3tifg1kx"
|
||||
},
|
||||
"source": [
|
||||
"Import required libraries."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "pRUOFELefqf1"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import pandas as pd\n",
|
||||
"from google.cloud import aiplatform\n",
|
||||
"from tensorflow.python.keras import Sequential, layers\n",
|
||||
"from tensorflow.python.keras.utils import data_utils"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "xtXZWmYqJ1bh"
|
||||
},
|
||||
"source": [
|
||||
"Define some constants"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "JIOrI-hoJ46P"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"EXPERIMENT_NAME = \"\" # @param {type:\"string\"}\n",
|
||||
"REGION = \"[your-region]\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "jWQLXXNVN4Lv"
|
||||
},
|
||||
"source": [
|
||||
"If EXEPERIMENT_NAME is not set, set a default one below:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "Q1QInYWOKsmo"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if EXPERIMENT_NAME == \"\" or EXPERIMENT_NAME is None:\n",
|
||||
" EXPERIMENT_NAME = \"my-experiment-\" + TIMESTAMP"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "Xuny18aMcWDb"
|
||||
},
|
||||
"source": [
|
||||
"## Concepts\n",
|
||||
"\n",
|
||||
"To better understanding how parameters and metrics are stored and organized, we'd like to introduce the following concepts:\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "NThDci5bp0Uw"
|
||||
},
|
||||
"source": [
|
||||
"### Experiment\n",
|
||||
"Experiments describe a context that groups your runs and the artifacts you create into a logical session. For example, in this notebook you create an Experiment and log data to that experiment."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "SAyRR3Ydp4X5"
|
||||
},
|
||||
"source": [
|
||||
"### Run\n",
|
||||
"A run represents a single path/avenue that you executed while performing an experiment. A run includes artifacts that you used as inputs or outputs, and parameters that you used in this execution. An Experiment can contain multiple runs. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "l1YW2pgyegFP"
|
||||
},
|
||||
"source": [
|
||||
"## Getting started tracking parameters and metrics\n",
|
||||
"\n",
|
||||
"You can use the Vertex SDK for Python to track metrics and parameters for models trained locally. \n",
|
||||
"\n",
|
||||
"In the following example, you train a simple distributed neural network (DNN) model to predict automobile's miles per gallon (MPG) based on automobile information in the [auto-mpg dataset](https://www.kaggle.com/devanshbesain/exploration-and-analysis-auto-mpg)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "KPY41M9_AhZU"
|
||||
},
|
||||
"source": [
|
||||
"### Load and process the training dataset"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "bfMQSmRuUuX-"
|
||||
},
|
||||
"source": [
|
||||
"Download and process the dataset."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "RiQuMv4bmpuV"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def read_data(uri):\n",
|
||||
" dataset_path = data_utils.get_file(\"auto-mpg.data\", uri)\n",
|
||||
" column_names = [\n",
|
||||
" \"MPG\",\n",
|
||||
" \"Cylinders\",\n",
|
||||
" \"Displacement\",\n",
|
||||
" \"Horsepower\",\n",
|
||||
" \"Weight\",\n",
|
||||
" \"Acceleration\",\n",
|
||||
" \"Model Year\",\n",
|
||||
" \"Origin\",\n",
|
||||
" ]\n",
|
||||
" raw_dataset = pd.read_csv(\n",
|
||||
" dataset_path,\n",
|
||||
" names=column_names,\n",
|
||||
" na_values=\"?\",\n",
|
||||
" comment=\"\\t\",\n",
|
||||
" sep=\" \",\n",
|
||||
" skipinitialspace=True,\n",
|
||||
" )\n",
|
||||
" dataset = raw_dataset.dropna()\n",
|
||||
" dataset[\"Origin\"] = dataset[\"Origin\"].map(\n",
|
||||
" lambda x: {1: \"USA\", 2: \"Europe\", 3: \"Japan\"}.get(x)\n",
|
||||
" )\n",
|
||||
" dataset = pd.get_dummies(dataset, prefix=\"\", prefix_sep=\"\")\n",
|
||||
" return dataset\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"dataset = read_data(\n",
|
||||
" \"http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "Y06J7A7yU21t"
|
||||
},
|
||||
"source": [
|
||||
"Split dataset for training and testing."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "p5JBCBKyH-NC"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def train_test_split(dataset, split_frac=0.8, random_state=0):\n",
|
||||
" train_dataset = dataset.sample(frac=split_frac, random_state=random_state)\n",
|
||||
" test_dataset = dataset.drop(train_dataset.index)\n",
|
||||
" train_labels = train_dataset.pop(\"MPG\")\n",
|
||||
" test_labels = test_dataset.pop(\"MPG\")\n",
|
||||
"\n",
|
||||
" return train_dataset, test_dataset, train_labels, test_labels\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"train_dataset, test_dataset, train_labels, test_labels = train_test_split(dataset)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "gaNNTFPaU7KT"
|
||||
},
|
||||
"source": [
|
||||
"Normalize the features in the dataset for better model performance."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "VGq5QCoyIEWJ"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def normalize_dataset(train_dataset, test_dataset):\n",
|
||||
" train_stats = train_dataset.describe()\n",
|
||||
" train_stats = train_stats.transpose()\n",
|
||||
"\n",
|
||||
" def norm(x):\n",
|
||||
" return (x - train_stats[\"mean\"]) / train_stats[\"std\"]\n",
|
||||
"\n",
|
||||
" normed_train_data = norm(train_dataset)\n",
|
||||
" normed_test_data = norm(test_dataset)\n",
|
||||
"\n",
|
||||
" return normed_train_data, normed_test_data\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"normed_train_data, normed_test_data = normalize_dataset(train_dataset, test_dataset)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "UBXUgxgqA_GB"
|
||||
},
|
||||
"source": [
|
||||
"### Define ML model and training function"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "66odBYKrIN4q"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def train(\n",
|
||||
" train_data,\n",
|
||||
" train_labels,\n",
|
||||
" num_units=64,\n",
|
||||
" activation=\"relu\",\n",
|
||||
" dropout_rate=0.0,\n",
|
||||
" validation_split=0.2,\n",
|
||||
" epochs=1000,\n",
|
||||
"):\n",
|
||||
"\n",
|
||||
" model = Sequential(\n",
|
||||
" [\n",
|
||||
" layers.Dense(\n",
|
||||
" num_units,\n",
|
||||
" activation=activation,\n",
|
||||
" input_shape=[len(train_dataset.keys())],\n",
|
||||
" ),\n",
|
||||
" layers.Dropout(rate=dropout_rate),\n",
|
||||
" layers.Dense(num_units, activation=activation),\n",
|
||||
" layers.Dense(1),\n",
|
||||
" ]\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" model.compile(loss=\"mse\", optimizer=\"adam\", metrics=[\"mae\", \"mse\"])\n",
|
||||
" print(model.summary())\n",
|
||||
"\n",
|
||||
" history = model.fit(\n",
|
||||
" train_data, train_labels, epochs=epochs, validation_split=validation_split\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" return model, history"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "O8XJZB3gR8eL"
|
||||
},
|
||||
"source": [
|
||||
"### Initialize the Model Builder SDK and create an Experiment\n",
|
||||
"\n",
|
||||
"Initialize the *client* for Vertex AI and create an experiment."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "o_wnT10RJ7-W"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION, experiment=EXPERIMENT_NAME)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "u-iTnzt3B6Z_"
|
||||
},
|
||||
"source": [
|
||||
"### Start several model training runs\n",
|
||||
"\n",
|
||||
"Training parameters and metrics are logged for each run."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "i2wnpu8_7JfV"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"parameters = [\n",
|
||||
" {\"num_units\": 16, \"epochs\": 3, \"dropout_rate\": 0.1},\n",
|
||||
" {\"num_units\": 16, \"epochs\": 10, \"dropout_rate\": 0.1},\n",
|
||||
" {\"num_units\": 16, \"epochs\": 10, \"dropout_rate\": 0.2},\n",
|
||||
" {\"num_units\": 32, \"epochs\": 10, \"dropout_rate\": 0.1},\n",
|
||||
" {\"num_units\": 32, \"epochs\": 10, \"dropout_rate\": 0.2},\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"for i, params in enumerate(parameters):\n",
|
||||
" aiplatform.start_run(run=f\"auto-mpg-local-run-{i}\")\n",
|
||||
" aiplatform.log_params(params)\n",
|
||||
" model, history = train(\n",
|
||||
" normed_train_data,\n",
|
||||
" train_labels,\n",
|
||||
" num_units=params[\"num_units\"],\n",
|
||||
" activation=\"relu\",\n",
|
||||
" epochs=params[\"epochs\"],\n",
|
||||
" dropout_rate=params[\"dropout_rate\"],\n",
|
||||
" )\n",
|
||||
" aiplatform.log_metrics(\n",
|
||||
" {metric: values[-1] for metric, values in history.history.items()}\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" loss, mae, mse = model.evaluate(normed_test_data, test_labels, verbose=2)\n",
|
||||
" aiplatform.log_metrics({\"eval_loss\": loss, \"eval_mae\": mae, \"eval_mse\": mse})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "jZLrJZTfL7tE"
|
||||
},
|
||||
"source": [
|
||||
"### Extract parameters and metrics into a dataframe for analysis"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "A1PqKxlpOZa2"
|
||||
},
|
||||
"source": [
|
||||
"We can also extract all parameters and metrics associated with any Experiment into a dataframe for further analysis."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "jbRf1WoH_vbY"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"experiment_df = aiplatform.get_experiment_df()\n",
|
||||
"experiment_df"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "EYuYgqVCMKU1"
|
||||
},
|
||||
"source": [
|
||||
"### Visualizing an experiment's parameters and metrics"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "r8orCj8iJuO1"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.rcParams[\"figure.figsize\"] = [15, 5]\n",
|
||||
"\n",
|
||||
"ax = pd.plotting.parallel_coordinates(\n",
|
||||
" experiment_df.reset_index(level=0),\n",
|
||||
" \"run_name\",\n",
|
||||
" cols=[\n",
|
||||
" \"param.num_units\",\n",
|
||||
" \"param.dropout_rate\",\n",
|
||||
" \"param.epochs\",\n",
|
||||
" \"metric.loss\",\n",
|
||||
" \"metric.val_loss\",\n",
|
||||
" \"metric.eval_loss\",\n",
|
||||
" ],\n",
|
||||
" color=[\"blue\", \"green\", \"pink\", \"red\"],\n",
|
||||
")\n",
|
||||
"ax.set_yscale(\"symlog\")\n",
|
||||
"ax.legend(bbox_to_anchor=(1.0, 0.5))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "WTHvPMweMlP1"
|
||||
},
|
||||
"source": [
|
||||
"## Visualizing experiments in Cloud Console"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "F19_5lw0MqXv"
|
||||
},
|
||||
"source": [
|
||||
"Run the following to get the URL of Vertex AI Experiments for your project.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "GmN9vE9pqqzt"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(\"Vertex AI Experiments:\")\n",
|
||||
"print(\n",
|
||||
" f\"https://console.cloud.google.com/ai/platform/experiments/experiments?folder=&organizationId=&project={PROJECT_ID}\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "TpV-iwP9qw9c"
|
||||
},
|
||||
"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."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"collapsed_sections": [],
|
||||
"name": "sdk-metric-parameter-tracking-for-locally-trained-models.ipynb",
|
||||
"toc_visible": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -0,0 +1,547 @@
|
||||
{
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"name": "Vertex AI SDK - AutoML Forecasting Model Training Example",
|
||||
"provenance": [],
|
||||
"collapsed_sections": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
}
|
||||
},
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "ur8xi4C7S06n"
|
||||
},
|
||||
"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."
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "eHLV0D7Y5jtU"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex AI Model Builder SDK: AutoML Forecasting Model Training Example\n",
|
||||
"\n",
|
||||
"To use this Colaboratory notebook, you copy the notebook to your own Google Drive and open it with Colaboratory (or Colab). You can run each step, or cell, and see its results. To run a cell, use Shift+Enter. Colab automatically displays the return value of the last line in each cell. For more information about running notebooks in Colab, see the [Colab welcome page](https://colab.research.google.com/notebooks/welcome.ipynb).\n",
|
||||
"\n",
|
||||
"This notebook demonstrates how to create an AutoML model based on a time series dataset. It will require you provide a bucket where the dataset will be stored.\n",
|
||||
"\n",
|
||||
"Note: you may incur charges for training, prediction, storage or usage of other GCP products in connection with testing this SDK."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "lld3eeJUs5yM"
|
||||
},
|
||||
"source": [
|
||||
"# Install Vertex AI SDK, Authenticate, and upload of a Dataset to your GCS bucket\n",
|
||||
"\n",
|
||||
"After the SDK installation the kernel will be automatically restarted. You may see this error message `Your session crashed for an unknown reason` which is normal."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "cMZLb8Arr2AG"
|
||||
},
|
||||
"source": [
|
||||
"%%capture\n",
|
||||
"!pip3 uninstall -y google-cloud-aiplatform\n",
|
||||
"!pip3 install google-cloud-aiplatform\n",
|
||||
" \n",
|
||||
"import IPython\n",
|
||||
" \n",
|
||||
"app = IPython.Application.instance()\n",
|
||||
"app.kernel.do_shutdown(True)"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "ApsLDJjdsGPN"
|
||||
},
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "c0SNmTBeD2nV"
|
||||
},
|
||||
"source": [
|
||||
"### Enter your project and GCS bucket\n",
|
||||
"\n",
|
||||
"Enter your Project ID in the cell below. Then run the cell to make sure the Cloud SDK uses the right project for all the commands in this notebook."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "s19AzYSGLIb9"
|
||||
},
|
||||
"source": [
|
||||
"**If you don't know your project ID**, you may be able to get your project ID using gcloud."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "nwlVqT6RKxG7"
|
||||
},
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"PROJECT_ID = \"\"\n",
|
||||
"\n",
|
||||
"# Get your Google Cloud project ID from gcloud\n",
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
" shell_output=!gcloud config list --format 'value(core.project)' 2>/dev/null\n",
|
||||
" PROJECT_ID = shell_output[0]\n",
|
||||
" print(\"Project ID: \", PROJECT_ID)"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "H5E8VB3jLOFC"
|
||||
},
|
||||
"source": [
|
||||
"Otherwise, set your project ID here."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "DrED76XTK9OB"
|
||||
},
|
||||
"source": [
|
||||
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
|
||||
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "zkJk7agzT6F9"
|
||||
},
|
||||
"source": [
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append it onto the name of resources you create in this tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "qcRkdZBaUAz4"
|
||||
},
|
||||
"source": [
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "TFfpJs3DQsfo"
|
||||
},
|
||||
"source": [
|
||||
"Set the name of your Cloud Storage bucket below. It must be unique across all Cloud Storage buckets.\n",
|
||||
"\n",
|
||||
"You may also change the REGION variable, which is used for operations throughout the rest of this notebook. Make sure to [choose a region where Vertex AI services are available](https://cloud.google.com/vertex-ai/docs/general/locations#available_regions). You may not use a Multi-Regional Storage bucket for training with Vertex AI."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "iqSQT6Z6bekX"
|
||||
},
|
||||
"source": [
|
||||
"BUCKET_NAME = \"gs://[your-bucket-name]\" # @param {type:\"string\"}\n",
|
||||
"REGION = \"[your-region]\" # @param {type:\"string\"}"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "ukGsLjm-Ki14"
|
||||
},
|
||||
"source": [
|
||||
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"gs://[your-bucket-name]\":\n",
|
||||
" BUCKET_NAME = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "-6AQjKlnx0mf"
|
||||
},
|
||||
"source": [
|
||||
"The datasets we are using are samples from the [Iowa Liquor Retail Sales](https://pantheon.corp.google.com/marketplace/product/iowa-department-of-commerce/iowa-liquor-sales) dataset. The training sample contains the sales from 2020 and the prediction sample (used in the batch prediction step) contains the January - April sales from 2021."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "V_T10yTTqcS_"
|
||||
},
|
||||
"source": [
|
||||
"TRAINING_DATASET_BQ_PATH = 'bq://bigquery-public-data:iowa_liquor_sales_forecasting.2020_sales_train'"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "rk43VP_IqcTE"
|
||||
},
|
||||
"source": [
|
||||
"# Initialize Vertex AI SDK\n",
|
||||
"\n",
|
||||
"Initialize the *client* for Vertex AI."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "VCiC9gBWqcTF"
|
||||
},
|
||||
"source": [
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_NAME)"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "35QVNhACqcTJ"
|
||||
},
|
||||
"source": [
|
||||
"# Create a Managed Time Series Dataset from BigQuery\n",
|
||||
"\n",
|
||||
"This section will create a dataset from a BigQuery table."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "4OfCqaYRqcTJ"
|
||||
},
|
||||
"source": [
|
||||
"ds = aiplatform.datasets.TimeSeriesDataset.create(\n",
|
||||
" display_name='iowa_liquor_sales_train',\n",
|
||||
" bq_source=[TRAINING_DATASET_BQ_PATH])\n",
|
||||
"\n",
|
||||
"ds.resource_name"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "6-bBqipfqcTS"
|
||||
},
|
||||
"source": [
|
||||
"# Launch a Training Job to Create a Model\n",
|
||||
"\n",
|
||||
"Once we have defined your training script, we will create a model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "aA41rT_mb-rV"
|
||||
},
|
||||
"source": [
|
||||
"time_column = \"date\"\n",
|
||||
"time_series_identifier_column=\"store_name\"\n",
|
||||
"target_column=\"sale_dollars\"\n",
|
||||
"\n",
|
||||
"job = aiplatform.AutoMLForecastingTrainingJob(\n",
|
||||
" display_name='train-iowa-liquor-sales-automl_1',\n",
|
||||
" optimization_objective='minimize-rmse', \n",
|
||||
" column_transformations=[\n",
|
||||
" {\"timestamp\": {\"column_name\": time_column}},\n",
|
||||
" {\"numeric\": {\"column_name\": target_column}},\n",
|
||||
" {\"categorical\": {\"column_name\": \"city\"}},\n",
|
||||
" {\"categorical\": {\"column_name\": \"zip_code\"}},\n",
|
||||
" {\"categorical\": {\"column_name\": \"county\"}},\n",
|
||||
" ]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# This will take around an hour to run\n",
|
||||
"model = job.run(\n",
|
||||
" dataset=ds,\n",
|
||||
" target_column=target_column,\n",
|
||||
" time_column=time_column,\n",
|
||||
" time_series_identifier_column=time_series_identifier_column,\n",
|
||||
" available_at_forecast_columns=[time_column],\n",
|
||||
" unavailable_at_forecast_columns=[target_column],\n",
|
||||
" time_series_attribute_columns=[\"city\", \"zip_code\", \"county\"],\n",
|
||||
" forecast_horizon=30,\n",
|
||||
" context_window=30,\n",
|
||||
" data_granularity_unit=\"day\",\n",
|
||||
" data_granularity_count=1,\n",
|
||||
" weight_column=None,\n",
|
||||
" budget_milli_node_hours=1000,\n",
|
||||
" model_display_name=\"iowa-liquor-sales-forecast-model\", \n",
|
||||
" predefined_split_column_name=None,\n",
|
||||
")"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "muSC-mvgHno7",
|
||||
"cellView": "form"
|
||||
},
|
||||
"source": [
|
||||
"#@title # Fetch Model Evaluation Metrics\n",
|
||||
"#@markdown Fetch the model evaluation metrics calculated during training on the test set.\n",
|
||||
"\n",
|
||||
"import pandas as pd\n",
|
||||
"\n",
|
||||
"list_evaluation_pager = model.api_client.list_model_evaluations(parent=model.resource_name)\n",
|
||||
"for model_evaluation in list_evaluation_pager:\n",
|
||||
" metrics_dict = {m[0]: m[1] for m in model_evaluation.metrics.items()}\n",
|
||||
" df = pd.DataFrame(metrics_dict.items(), columns=[\"Metric\", \"Value\"])\n",
|
||||
" print(df.to_string(index=False))"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "nIw1ifPuqcTb"
|
||||
},
|
||||
"source": [
|
||||
"# Run Batch Prediction"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "nT-bZ1autijD"
|
||||
},
|
||||
"source": [
|
||||
"#@markdown ## Create Output BigQuery Dataset\n",
|
||||
"#@markdown First, create a new BigQuery dataset for the batch prediction output in the same region as the batch prediction input dataset. \n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"from google.cloud import bigquery\n",
|
||||
"\n",
|
||||
"os.environ[\"GOOGLE_CLOUD_PROJECT\"] = PROJECT_ID\n",
|
||||
"\n",
|
||||
"batch_predict_bq_input_uri = \"bq://bigquery-public-data.iowa_liquor_sales_forecasting.2021_sales_predict\"\n",
|
||||
"batch_predict_bq_output_dataset_name = \"iowa_liquor_sales_predictions\"\n",
|
||||
"batch_predict_bq_output_dataset_path = \"{}.{}\".format(PROJECT_ID, batch_predict_bq_output_dataset_name)\n",
|
||||
"batch_predict_bq_output_uri_prefix = \"bq://{}.{}\".format(PROJECT_ID, batch_predict_bq_output_dataset_name)\n",
|
||||
"# Must be the same region as batch_predict_bq_input_uri\n",
|
||||
"client = bigquery.Client()\n",
|
||||
"dataset = bigquery.Dataset(batch_predict_bq_output_dataset_path)\n",
|
||||
"dataset_region = \"US\" # @param {type : \"string\"}\n",
|
||||
"dataset.location = dataset_region\n",
|
||||
"dataset = client.create_dataset(dataset)\n",
|
||||
"print(\"Created bigquery dataset {} in {}\".format(batch_predict_bq_output_dataset_path, dataset_region))"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "krKRn9W0xxI2"
|
||||
},
|
||||
"source": [
|
||||
"Run a batch prediction job to generate liquor sales forecasts for stores in Iowa from an input dataset containing historical sales."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "8I8aRjRh6GGG"
|
||||
},
|
||||
"source": [
|
||||
"model.batch_predict(\n",
|
||||
" bigquery_source=batch_predict_bq_input_uri,\n",
|
||||
" instances_format=\"bigquery\",\n",
|
||||
" bigquery_destination_prefix=batch_predict_bq_output_uri_prefix,\n",
|
||||
" predictions_format=\"bigquery\",\n",
|
||||
" job_display_name=\"predict-iowa-liquor-sales-automl_1\")"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "CTQl3fH6Ur2Z",
|
||||
"cellView": "form"
|
||||
},
|
||||
"source": [
|
||||
"#@title # Visualize the Forecasts\n",
|
||||
"#@markdown Follow the given link to visualize the generated forecasts in [Data Studio](https://support.google.com/datastudio/answer/6283323?hl=en).\n",
|
||||
"\n",
|
||||
"import urllib\n",
|
||||
"\n",
|
||||
"tables = client.list_tables(batch_predict_bq_output_dataset_path)\n",
|
||||
"\n",
|
||||
"prediction_table_id = \"\"\n",
|
||||
"for table in tables:\n",
|
||||
" if table.table_id.startswith(\n",
|
||||
" \"predictions_\") and table.table_id > prediction_table_id:\n",
|
||||
" prediction_table_id = table.table_id\n",
|
||||
"batch_predict_bq_output_uri = \"{}.{}\".format(\n",
|
||||
" batch_predict_bq_output_dataset_path, prediction_table_id)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def _sanitize_bq_uri(bq_uri):\n",
|
||||
" if bq_uri.startswith(\"bq://\"):\n",
|
||||
" bq_uri = bq_uri[5:]\n",
|
||||
" return bq_uri.replace(\":\", \".\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_data_studio_link(batch_prediction_bq_input_uri,\n",
|
||||
" batch_prediction_bq_output_uri, time_column,\n",
|
||||
" time_series_identifier_column, target_column):\n",
|
||||
" batch_prediction_bq_input_uri = _sanitize_bq_uri(\n",
|
||||
" batch_prediction_bq_input_uri)\n",
|
||||
" batch_prediction_bq_output_uri = _sanitize_bq_uri(\n",
|
||||
" batch_prediction_bq_output_uri)\n",
|
||||
" base_url = \"https://datastudio.google.com/c/u/0/reporting\"\n",
|
||||
" query = \"SELECT \\\\n\" \\\n",
|
||||
" \" CAST(input.{} as DATETIME) timestamp_col,\\\\n\" \\\n",
|
||||
" \" CAST(input.{} as STRING) time_series_identifier_col,\\\\n\" \\\n",
|
||||
" \" CAST(input.{} as NUMERIC) historical_values,\\\\n\" \\\n",
|
||||
" \" CAST(predicted_{}.value as NUMERIC) predicted_values,\\\\n\" \\\n",
|
||||
" \" * \\\\n\" \\\n",
|
||||
" \"FROM `{}` input\\\\n\" \\\n",
|
||||
" \"LEFT JOIN `{}` output\\\\n\" \\\n",
|
||||
" \"ON\\\\n\" \\\n",
|
||||
" \"CAST(input.{} as DATETIME) = CAST(output.{} as DATETIME)\\\\n\" \\\n",
|
||||
" \"AND CAST(input.{} as STRING) = CAST(output.{} as STRING)\"\n",
|
||||
" query = query.format(time_column, time_series_identifier_column,\n",
|
||||
" target_column, target_column,\n",
|
||||
" batch_prediction_bq_input_uri,\n",
|
||||
" batch_prediction_bq_output_uri, time_column, time_column,\n",
|
||||
" time_series_identifier_column,\n",
|
||||
" time_series_identifier_column)\n",
|
||||
" params = {\n",
|
||||
" \"templateId\": \"067f70d2-8cd6-4a4c-a099-292acd1053e8\",\n",
|
||||
" \"ds0.connector\": \"BIG_QUERY\",\n",
|
||||
" \"ds0.projectId\": PROJECT_ID,\n",
|
||||
" \"ds0.billingProjectId\": PROJECT_ID,\n",
|
||||
" \"ds0.type\": \"CUSTOM_QUERY\",\n",
|
||||
" \"ds0.sql\": query\n",
|
||||
" }\n",
|
||||
" params_str_parts = []\n",
|
||||
" for k, v in params.items():\n",
|
||||
" params_str_parts.append(\"\\\"{}\\\":\\\"{}\\\"\".format(k, v))\n",
|
||||
" params_str = \"\".join([\"{\", \",\".join(params_str_parts), \"}\"])\n",
|
||||
" return \"{}?{}\".format(base_url,\n",
|
||||
" urllib.parse.urlencode({\"params\": params_str}))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"print(\n",
|
||||
" get_data_studio_link(batch_predict_bq_input_uri,\n",
|
||||
" batch_predict_bq_output_uri, time_column,\n",
|
||||
" time_series_identifier_column, target_column))"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "24NPJ7nCRchZ"
|
||||
},
|
||||
"source": [
|
||||
"\n",
|
||||
"# 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"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "gq3ZSsAkRnXh"
|
||||
},
|
||||
"source": [
|
||||
"# Delete model resource\n",
|
||||
"model.delete(sync=True)\n",
|
||||
"\n",
|
||||
"# Delete Cloud Storage objects that were created\n",
|
||||
"! gsutil -m rm -r $BUCKET_NAME"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,522 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "VBOfRw7ifk8w"
|
||||
},
|
||||
"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": "w3LR4Lj8fk8x"
|
||||
},
|
||||
"source": [
|
||||
"# Feedback or issues?\n",
|
||||
"For any feedback or questions, please open an [issue](https://github.com/googleapis/python-aiplatform/issues)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "mUMzY5W9fk8y"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex SDK for Python: AutoML Image Classfication Training with Customer Managed Encryption Keys (CMEK) Example\n",
|
||||
"To use this Jupyter notebook, create a copy of the notebook in Colab and open it. You can run each step, or cell, and see its results. To run a cell, use Shift+Enter. Colab automatically displays the return value of the last line in each cell.\n",
|
||||
"\n",
|
||||
"This notebook demonstrate how to train an AutoML Image Classification model with CMEK. It will require you provide a bucket where the dataset will be stored.\n",
|
||||
"\n",
|
||||
"Note: you may incur charges for training, prediction, storage or usage of other GCP products in connection with testing this SDK."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "lld3eeJUs5yM"
|
||||
},
|
||||
"source": [
|
||||
"# Install SDK\n",
|
||||
" \n",
|
||||
"After the SDK installation the kernel will be automatically restarted."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "sBfZtR4X1Dr_"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip3 uninstall -y google-cloud-aiplatform\n",
|
||||
"!pip3 install --upgrade google-cloud-kms\n",
|
||||
"!pip3 install google-cloud-aiplatform\n",
|
||||
"import IPython\n",
|
||||
"\n",
|
||||
"app = IPython.Application.instance()\n",
|
||||
"app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "c0SNmTBeD2nV"
|
||||
},
|
||||
"source": [
|
||||
"### Enter your project and GCS bucket\n",
|
||||
"\n",
|
||||
"Enter your Project Id in the cell below. Then run the cell to make sure the Cloud SDK uses the right project for all the commands in this notebook."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "YcwsEwXPivBZ"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "iqSQT6Z6bekX"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"YOUR REGION\" # e.g. us-central1\n",
|
||||
"MY_PROJECT = \"YOUR PROJECT ID\"\n",
|
||||
"MY_STAGING_BUCKET = \"gs://YOUR BUCKET\" # bucket should be in same region as ucaip"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "mRk9eoTm6Pyi"
|
||||
},
|
||||
"source": [
|
||||
"## Setting up Customer Managed Encryption Keys\n",
|
||||
"\n",
|
||||
"By default, Google Cloud automatically encrypts data when it is at rest using encryption keys managed by Google. If you have specific compliance or regulatory requirements related to the keys that protect your data, you can use customer-managed encryption keys (CMEK) for your training jobs.\n",
|
||||
"\n",
|
||||
"For more info on using CMEK on Vertex AI, please see: [https://cloud.google.com/vertex-ai/docs/general/cmek#before_you_begin](https://cloud.google.com/vertex-ai/docs/general/cmek#before_you_begin)\n",
|
||||
"\n",
|
||||
"You can create a key using the guide above or executing the the Notebook cells below."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "RD_Pvrg584X3"
|
||||
},
|
||||
"source": [
|
||||
"1. Register your application for Cloud Key Management Service (KMS) API in Google Cloud Platform at https://console.cloud.google.com/flows/enableapi?apiid=cloudkms.googleapis.com\n",
|
||||
"\n",
|
||||
"2. Create a key ring\n",
|
||||
"\n",
|
||||
"Create a key ring"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "dxRZzbvQnZC7"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"KEY_RING_ID = \"your_key_ring_name\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Reference: https://cloud.google.com/kms/docs/samples/kms-create-key-ring\n",
|
||||
"def create_key_ring(project_id, location_id, id):\n",
|
||||
" \"\"\"\n",
|
||||
" Creates a new key ring in Cloud KMS\n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" project_id (string): Google Cloud project ID (e.g. 'my-project').\n",
|
||||
" location_id (string): Cloud KMS location (e.g. 'us-east1').\n",
|
||||
" id (string): ID of the key ring to create (e.g. 'my-key-ring').\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" KeyRing: Cloud KMS key ring.\n",
|
||||
"\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" # Import the client library.\n",
|
||||
" from google.cloud import kms\n",
|
||||
"\n",
|
||||
" # Create the client.\n",
|
||||
" client = kms.KeyManagementServiceClient()\n",
|
||||
"\n",
|
||||
" # Build the parent location name.\n",
|
||||
" location_name = f\"projects/{project_id}/locations/{location_id}\"\n",
|
||||
"\n",
|
||||
" # Build the key ring.\n",
|
||||
" key_ring = {}\n",
|
||||
"\n",
|
||||
" # Call the API.\n",
|
||||
" created_key_ring = client.create_key_ring(\n",
|
||||
" request={\"parent\": location_name, \"key_ring_id\": id, \"key_ring\": key_ring}\n",
|
||||
" )\n",
|
||||
" print(\"Created key ring: {}\".format(created_key_ring.name))\n",
|
||||
" return created_key_ring\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"create_key_ring(project_id=MY_PROJECT, location_id=REGION, id=KEY_RING_ID)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "gCL1-IfFtWXl"
|
||||
},
|
||||
"source": [
|
||||
"Create a key"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "LXcagdmSnYYW"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"KEY_ID = \"your_key_id\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Reference: https://cloud.google.com/kms/docs/samples/kms-create-key-symmetric-encrypt-decrypt\n",
|
||||
"def create_key_symmetric_encrypt_decrypt(project_id, location_id, key_ring_id, id):\n",
|
||||
" \"\"\"\n",
|
||||
" Creates a new symmetric encryption/decryption key in Cloud KMS.\n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" project_id (string): Google Cloud project ID (e.g. 'my-project').\n",
|
||||
" location_id (string): Cloud KMS location (e.g. 'us-east1').\n",
|
||||
" key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').\n",
|
||||
" id (string): ID of the key to create (e.g. 'my-symmetric-key').\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" CryptoKey: Cloud KMS key.\n",
|
||||
"\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" # Import the client library.\n",
|
||||
" from google.cloud import kms\n",
|
||||
"\n",
|
||||
" # Create the client.\n",
|
||||
" client = kms.KeyManagementServiceClient()\n",
|
||||
"\n",
|
||||
" # Build the parent key ring name.\n",
|
||||
" key_ring_name = client.key_ring_path(project_id, location_id, key_ring_id)\n",
|
||||
"\n",
|
||||
" # Build the key.\n",
|
||||
" purpose = kms.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT\n",
|
||||
" algorithm = (\n",
|
||||
" kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION\n",
|
||||
" )\n",
|
||||
" key = {\n",
|
||||
" \"purpose\": purpose,\n",
|
||||
" \"version_template\": {\n",
|
||||
" \"algorithm\": algorithm,\n",
|
||||
" },\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" # Call the API.\n",
|
||||
" created_key = client.create_crypto_key(\n",
|
||||
" request={\"parent\": key_ring_name, \"crypto_key_id\": id, \"crypto_key\": key}\n",
|
||||
" )\n",
|
||||
" print(\"Created symmetric key: {}\".format(created_key.name))\n",
|
||||
" return created_key\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"create_key_symmetric_encrypt_decrypt(\n",
|
||||
" project_id=MY_PROJECT, location_id=REGION, key_ring_id=KEY_RING_ID, id=KEY_ID\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "3gKDBOqC8Gl5"
|
||||
},
|
||||
"source": [
|
||||
"Give permissions to key to the Vertex AI service account"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "6QrRg08Vqfru"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Reference: https://cloud.google.com/vertex-ai/docs/general/cmek#granting_permissions\n",
|
||||
"# Get the service account\n",
|
||||
"SERVICE_ACCOUNT = ! gcloud projects get-iam-policy {MY_PROJECT} \\\n",
|
||||
" --flatten=\"bindings[].members\" \\\n",
|
||||
" --format=\"table(bindings.members)\" \\\n",
|
||||
" --filter=\"bindings.role:roles/aiplatform.serviceAgent\" \\\n",
|
||||
" | grep -oP \"service-.+?@gcp-sa-aiplatform.iam.gserviceaccount.com\"\n",
|
||||
"SERVICE_ACCOUNT = SERVICE_ACCOUNT[0]\n",
|
||||
"\n",
|
||||
"print(f\"Service account is: {SERVICE_ACCOUNT}\")\n",
|
||||
"\n",
|
||||
"# Give permissions\n",
|
||||
"!gcloud kms keys add-iam-policy-binding {KEY_ID} \\\n",
|
||||
" --keyring={KEY_RING_ID} \\\n",
|
||||
" --location={REGION} \\\n",
|
||||
" --project={MY_PROJECT} \\\n",
|
||||
" --member=serviceAccount:{SERVICE_ACCOUNT} \\\n",
|
||||
" --role=roles/cloudkms.cryptoKeyEncrypterDecrypter"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ebAHZg2vlhXL"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Create the full resource identifier for the created key\n",
|
||||
"ENCRYPTION_SPEC_KEY_NAME = f\"projects/{MY_PROJECT}/locations/{REGION}/keyRings/{KEY_RING_ID}/cryptoKeys/{KEY_ID}\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "Aa_8wrqSkamz"
|
||||
},
|
||||
"source": [
|
||||
"## Initialize Vertex SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the *client* for Vertex AI\n",
|
||||
"\n",
|
||||
"All resources created during this Notebook run will encrypted with the encryption key created above.\n",
|
||||
"\n",
|
||||
"You can override the encryption key at each function call."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ohdgOs69kGNU"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"aiplatform.init(\n",
|
||||
" project=MY_PROJECT,\n",
|
||||
" staging_bucket=MY_STAGING_BUCKET,\n",
|
||||
" location=REGION,\n",
|
||||
" encryption_spec_key_name=ENCRYPTION_SPEC_KEY_NAME,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "35QVNhACqcTJ"
|
||||
},
|
||||
"source": [
|
||||
"# Create Managed Image Dataset from CSV\n",
|
||||
"\n",
|
||||
"This section will create a managed Image dataset from the Flowers dataset. For more imformation on this dataset please visit https://www.tensorflow.org/datasets/catalog/tf_flowers."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "4OfCqaYRqcTJ"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"IMPORT_FILE = (\n",
|
||||
" \"gs://cloud-samples-data/vision/automl_classification/flowers/all_data_v2.csv\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"ds = aiplatform.ImageDataset.create(\n",
|
||||
" display_name=\"flowers\",\n",
|
||||
" gcs_source=[IMPORT_FILE],\n",
|
||||
" import_schema_uri=aiplatform.schema.dataset.ioformat.image.single_label_classification,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"ds.resource_name"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "6-bBqipfqcTS"
|
||||
},
|
||||
"source": [
|
||||
"# Launch a Training Job to Create a Model\n",
|
||||
"\n",
|
||||
"Train an AutoML Image Classification model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "aA41rT_mb-rV"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"job = aiplatform.AutoMLImageTrainingJob(\n",
|
||||
" display_name=\"train-iris-automl-mbsdk-1\",\n",
|
||||
" prediction_type=\"classification\",\n",
|
||||
" multi_label=False,\n",
|
||||
" model_type=\"CLOUD\",\n",
|
||||
" base_model=None,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# This will take around half an hour to run\n",
|
||||
"model = job.run(\n",
|
||||
" dataset=ds,\n",
|
||||
" model_display_name=\"iris-classification-model-mbsdk\",\n",
|
||||
" training_fraction_split=0.6,\n",
|
||||
" validation_fraction_split=0.2,\n",
|
||||
" test_fraction_split=0.2,\n",
|
||||
" budget_milli_node_hours=8000,\n",
|
||||
" disable_early_stopping=False,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "5vhDsMJNqcTW"
|
||||
},
|
||||
"source": [
|
||||
"# Deploy Your Model\n",
|
||||
"\n",
|
||||
"Deploy your model, then wait until the model FINISHES deployment before proceeding to prediction."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "Y9GH72wWqcTX"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"endpoint = model.deploy()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "nIw1ifPuqcTb"
|
||||
},
|
||||
"source": [
|
||||
"# Predict on Endpoint\n",
|
||||
"- Take one sample from the data imported to the dataset\n",
|
||||
"- This sample will be encoded to base64 and passed to the endpoint for prediction"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "H23ISHdHVIZM"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"test_item = !gsutil cat $IMPORT_FILE | head -n1\n",
|
||||
"test_item, test_label = str(test_item[0]).split(\",\")\n",
|
||||
"\n",
|
||||
"print(test_item, test_label)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "TF_N0kqZU768"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import base64\n",
|
||||
"\n",
|
||||
"import tensorflow as tf\n",
|
||||
"\n",
|
||||
"with tf.io.gfile.GFile(test_item, \"rb\") as f:\n",
|
||||
" content = f.read()\n",
|
||||
"\n",
|
||||
"# The format of each instance should conform to the deployed model's prediction input schema.\n",
|
||||
"instances_list = [{\"content\": base64.b64encode(content).decode(\"utf-8\")}]\n",
|
||||
"\n",
|
||||
"prediction = endpoint.predict(instances=instances_list)\n",
|
||||
"\n",
|
||||
"prediction"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "nWA3qocXfk82"
|
||||
},
|
||||
"source": [
|
||||
"# Undeploy Model from Endpoint"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "V1brMaO_fk82"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"endpoint.undeploy_all()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"collapsed_sections": [],
|
||||
"name": "AI_Platform_(Unified)_SDK_AutoML_Image_Classification_Training_with_CMEK.ipynb",
|
||||
"toc_visible": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "bguORR-uVtyk"
|
||||
},
|
||||
"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": "f1KuZ_LBcHee"
|
||||
},
|
||||
"source": [
|
||||
"# Feedback or issues?\n",
|
||||
"\n",
|
||||
"For any feedback or questions, please open an [issue](https://github.com/googleapis/python-aiplatform/issues)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "A4QHhG05cJD9"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex SDK for Python: AutoML Text Extraction Example\n",
|
||||
"To use this Jupyter notebook, copy the notebook to a Google Cloud Notebooks instance with Tensorflow installed and open it. You can run each step, or cell, and see its results. To run a cell, use Shift+Enter. Jupyter automatically displays the return value of the last line in each cell. For more information about running notebooks in Google Cloud Notebook, see the [Google Cloud Notebook guide](https://cloud.google.com/vertex-ai/docs/general/notebooks).\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"This notebook demonstrate how to create an AutoML Text Extraction Model, with a Vertex AI text dataset, and how to serve the model for online prediction.\n",
|
||||
"\n",
|
||||
"Note: you may incur charges for training, prediction, storage or usage of other GCP products in connection with testing this SDK."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "lld3eeJUs5yM"
|
||||
},
|
||||
"source": [
|
||||
"### Install Vertex SDK for Python\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"After the SDK installation the kernel will be automatically restarted."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "sBfZtR4X1Dr_"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip3 uninstall -y google-cloud-aiplatform\n",
|
||||
"!pip3 install google-cloud-aiplatform\n",
|
||||
"import IPython\n",
|
||||
"\n",
|
||||
"app = IPython.Application.instance()\n",
|
||||
"app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "kBFvlCFh5Yij"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "Yz_rkDIteP5M"
|
||||
},
|
||||
"source": [
|
||||
"### Enter Your Project and GCS Bucket\n",
|
||||
"\n",
|
||||
"Enter your Project Id in the cell below. Then run the cell to make sure the Cloud SDK uses the right project for all the commands in this notebook."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "iqSQT6Z6bekX"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MY_PROJECT = \"YOUR PROJECT ID\"\n",
|
||||
"MY_STAGING_BUCKET = \"gs://YOUR BUCKET\" # bucket should be in same region as ucaip"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "rk43VP_IqcTE"
|
||||
},
|
||||
"source": [
|
||||
"## Initialize Vertex SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the *client* for Vertex AI."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "VCiC9gBWqcTF"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=MY_PROJECT, staging_bucket=MY_STAGING_BUCKET)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "35QVNhACqcTJ"
|
||||
},
|
||||
"source": [
|
||||
"## Create a Dataset on Vertex AI\n",
|
||||
"We will now create a Vertex AI text dataset using the previously prepared jsonl files. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "XGKZ3bdyTMcd"
|
||||
},
|
||||
"source": [
|
||||
"### The biomedical dataset\n",
|
||||
"To create an entity extraction model, use a corpus of biomedical research abstracts that mention hundreds of diseases and concepts. The resulting model identifies these medical entities in other documents.\n",
|
||||
"\n",
|
||||
"The goal of the corpus is to advance the understanding of the causes of happiness through text-based reflection.\n",
|
||||
"\n",
|
||||
"Please reference [AutoML Documentation](https://cloud.google.com/natural-language/automl/docs/quickstart#model_objectives) for more information."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "KeNtSVjRxVCC"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Text Extraction\n",
|
||||
"IMPORT_FILE = \"gs://ucaip-test-us-central1/dataset/ucaip_ten_dataset.jsonl\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "4OfCqaYRqcTJ"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"ds = aiplatform.TextDataset.create(\n",
|
||||
" display_name=\"text-extraction\",\n",
|
||||
" gcs_source=[IMPORT_FILE],\n",
|
||||
" import_schema_uri=aiplatform.schema.dataset.ioformat.text.extraction,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"ds.resource_name"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "6-bBqipfqcTS"
|
||||
},
|
||||
"source": [
|
||||
"## Launch a Training Job and Create a Model on Vertex AI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "aA41rT_mb-rV"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"job = aiplatform.AutoMLTextTrainingJob(\n",
|
||||
" display_name=\"text-extraction\", prediction_type=\"extraction\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# This will take around an hour to run\n",
|
||||
"model = job.run(\n",
|
||||
" dataset=ds,\n",
|
||||
" training_fraction_split=0.6,\n",
|
||||
" validation_fraction_split=0.2,\n",
|
||||
" test_fraction_split=0.2,\n",
|
||||
" model_display_name=\"text-extraction\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "5vhDsMJNqcTW"
|
||||
},
|
||||
"source": [
|
||||
"# Deploy Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "Y9GH72wWqcTX"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"endpoint = model.deploy()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "nIw1ifPuqcTb"
|
||||
},
|
||||
"source": [
|
||||
"# Predict on Endpoint"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "3k6-rSZPqcTc"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"input_text = \"\"\"\n",
|
||||
"Phenotypic variation including retinitis pigmentosa, pattern dystrophy, and fundus flavimaculatus in a single family with a deletion of codon 153 or 154 of the peripherin/RDS gene.\\tBACKGROUND AND OBJECTIVES Mutations of the peripherin / RDS gene have been reported in autosomal dominant retinitis pigmentosa , pattern macular dystrophy , and retinitis punctata albescens . We report herein the occurrence of three separate phenotypes within a single family with a novel 3-base pair deletion of codon 153 or 154 of the peripherin / RDS gene . DESIGN Case reports with clinical features , fluorescein angiography , kinetic perimetry , electrophysiological studies , and molecular genetics . SETTING University medical centers . PATIENTS A 75-year-old woman , her two daughters ( aged 44 and 50 years ) , and her 49-year-old son were screened for peripherin / RDS mutations because of the presence of multiple phenotypes within the same family . RESULTS The mother presented at age 63 years with a profoundly abnormal electroretinogram ( ERG ) and adult-onset retinitis pigmentosa that progressed dramatically over 12 years , with marked loss of peripheral visual field . One daughter developed pattern macular dystrophy at age 31 years . At age 44 years , her ERG was moderately abnormal but her clinical disease was limited to the macula . Another daughter presented at age 42 years with macular degeneration and over 10 years developed the clinical picture of fundus flavimaculatus . Her peripheral visual field was preserved but her ERG was moderately abnormal . The son had onset of macular degeneration at age 44 years . Pericentral scotomas were present and the ERG was markedly abnormal . Fluorescein angiography revealed punctate pigment epithelial transmission defects . CONCLUSIONS A 3-base pair deletion of codon 153 or 154 of the peripherin / RDS gene can produce clinically disparate phenotypes even within the same family\n",
|
||||
"Splicing defects in the ataxia-telangiectasia gene, ATM: underlying mutations and consequences.\\tMutations resulting in defective splicing constitute a significant proportion ( 30 / 62 [ 48 % ] ) of a new series of mutations in the ATM gene in patients with ataxia-telangiectasia ( AT ) that were detected by the protein-truncation assay followed by sequence analysis of genomic DNA . Fewer than half of the splicing mutations involved the canonical AG splice-acceptor site or GT splice-donor site . A higher percentage of mutations occurred at less stringently conserved sites , including silent mutations of the last nucleotide of exons , mutations in nucleotides other than the conserved AG and GT in the consensus splice sites , and creation of splice-acceptor or splice-donor sites in either introns or exons . These splicing mutations led to a variety of consequences , including exon skipping and , to a lesser degree , intron retention , activation of cryptic splice sites , or creation of new splice sites . In addition , 5 of 12 nonsense mutations and 1 missense mutation were associated with deletion in the cDNA of the exons in which the mutations occurred . No ATM protein was detected by western blotting in any AT cell line in which splicing mutations were identified . Several cases of exon skipping in both normal controls and patients for whom no underlying defect could be found in genomic DNA were also observed , suggesting caution in the interpretation of exon deletions observed in ATM cDNA when there is no accompanying identification of genomic mutations .\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"instances_list = [{\"content\": input_text}]\n",
|
||||
"\n",
|
||||
"prediction = endpoint.predict(instances_list)\n",
|
||||
"prediction"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "RXpchK0oWqWq"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prediction_instance = prediction.predictions[0]\n",
|
||||
"\n",
|
||||
"extractions = zip(\n",
|
||||
" prediction_instance[\"ids\"],\n",
|
||||
" prediction_instance[\"textSegmentStartOffsets\"],\n",
|
||||
" prediction_instance[\"textSegmentEndOffsets\"],\n",
|
||||
" prediction_instance[\"confidences\"],\n",
|
||||
" prediction_instance[\"displayNames\"],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"for id, start, end, confidence, display_name in extractions:\n",
|
||||
" print(\n",
|
||||
" f\"{id}: '{input_text[int(start):int(end)]}' predicted as '{display_name}'' with confidence {confidence}\"\n",
|
||||
" )"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"collapsed_sections": [],
|
||||
"name": "AI_Platform_(Unified)_SDK_AutoML_Text_Extraction_Training.ipynb",
|
||||
"toc_visible": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -0,0 +1,501 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "humanitarian-petite"
|
||||
},
|
||||
"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": "corporate-remains"
|
||||
},
|
||||
"source": [
|
||||
"# Feedback or issues?\n",
|
||||
"\n",
|
||||
"For any feedback or questions, please open an [issue](https://github.com/googleapis/python-aiplatform/issues)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "first-dietary"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex SDK for Python: AutoML Video Action Recognition Example\n",
|
||||
"To use this Jupyter notebook, copy the notebook to a Google Cloud Notebooks instance with Tensorflow installed and open it. You can run each step, or cell, and see its results. To run a cell, use Shift+Enter. Jupyter automatically displays the return value of the last line in each cell. For more information about running notebooks in Google Cloud Notebook, see the [Google Cloud Notebook guide](https://cloud.google.com/vertex-ai/docs/general/notebooks).\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"This notebook demonstrate how to create an AutoML Video Action Recognition Model, with a Vertex AI video dataset, and how to serve the model for batch prediction. It will require you provide a bucket where the dataset will be stored.\n",
|
||||
"\n",
|
||||
"Note: you may incur charges for training, prediction, storage or usage of other GCP products in connection with testing this SDK."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "critical-twenty"
|
||||
},
|
||||
"source": [
|
||||
"### Install Vertex SDK for Python\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"After the SDK installation the kernel will be automatically restarted."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "precious-produce"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip3 uninstall -y google-cloud-aiplatform\n",
|
||||
"!pip3 install google-cloud-aiplatform\n",
|
||||
"import IPython\n",
|
||||
"\n",
|
||||
"app = IPython.Application.instance()\n",
|
||||
"app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "finished-roller"
|
||||
},
|
||||
"source": [
|
||||
"### Enter Your Project and GCS Bucket\n",
|
||||
"\n",
|
||||
"Enter your Project Id in the cell below. Then run the cell to make sure the Cloud SDK uses the right project for all the commands in this notebook."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "limiting-costume"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MY_PROJECT = \"YOUR PROJECT ID\"\n",
|
||||
"MY_STAGING_BUCKET = \"gs://YOUR BUCKET\" # bucket should be in same region as ucaip"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "b3GKWBB_Y6So"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
" import os\n",
|
||||
"\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()\n",
|
||||
" os.environ[\"GOOGLE_CLOUD_PROJECT\"] = MY_PROJECT"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "entire-fleece"
|
||||
},
|
||||
"source": [
|
||||
"### Set Your Task Name, and GCS Prefix\n",
|
||||
"\n",
|
||||
"If you want to centeralize all input and output files under the gcs location."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "micro-administration"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"TASK_TYPE = \"mbsdk_automl-video-training\"\n",
|
||||
"PREDICTION_TYPE = \"action_recognition\"\n",
|
||||
"MODEL_TYPE = \"CLOUD\"\n",
|
||||
"\n",
|
||||
"TASK_NAME = f\"{TASK_TYPE}_{PREDICTION_TYPE}\"\n",
|
||||
"BUCKET_NAME = MY_STAGING_BUCKET.split(\"gs://\")[1]\n",
|
||||
"GCS_PREFIX = TASK_NAME\n",
|
||||
"\n",
|
||||
"print(f\"Bucket Name: {BUCKET_NAME}\")\n",
|
||||
"print(f\"Task Name: {TASK_NAME}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "capable-sitting"
|
||||
},
|
||||
"source": [
|
||||
"# HMDB: a large human motion database\n",
|
||||
"We prepared some training data and prediction data for the demo using the [HMDB Dataset](https://serre-lab.clps.brown.edu/resource/hmdb-a-large-human-motion-database).\n",
|
||||
"\n",
|
||||
"The HMDB Dataset is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0/\n",
|
||||
"\n",
|
||||
"For more information about this dataset please visit: https://serre-lab.clps.brown.edu/resource/hmdb-a-large-human-motion-database/"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "_NQwynxpDMrN"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"automl_video_demo_train_data = \"gs://automl-video-demo-data/hmdb_golf_swing_all.csv\"\n",
|
||||
"automl_video_demo_batch_prediction_data = (\n",
|
||||
" \"gs://automl-video-demo-data/hmdb_golf_swing_predict.jsonl\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "catholic-financing"
|
||||
},
|
||||
"source": [
|
||||
"### Copy AutoML Video Demo Train Data for Creating Managed Dataset"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "coastal-engineering"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gcs_source_train = f\"gs://{BUCKET_NAME}/{TASK_NAME}/data/video_action_recognition.csv\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "acoustic-wonder"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!gsutil cp $automl_video_demo_train_data $gcs_source_train"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "accepting-setup"
|
||||
},
|
||||
"source": [
|
||||
"# Run AutoML Video Training with Managed Video Dataset"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "terminal-better"
|
||||
},
|
||||
"source": [
|
||||
"## Initialize Vertex SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the *client* for Vertex AI."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "trying-mixture"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=MY_PROJECT, staging_bucket=MY_STAGING_BUCKET)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "nonprofit-client"
|
||||
},
|
||||
"source": [
|
||||
"## Create a Dataset on Vertex AI\n",
|
||||
"We will now create a Vertex AI video dataset using the previously prepared csv files. Choose one of the options below. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "4rXbKPQHT_YS"
|
||||
},
|
||||
"source": [
|
||||
"Option 1: Using MBSDK VideoDataset class"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "labeled-heating"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dataset = aiplatform.VideoDataset.create(\n",
|
||||
" display_name=f\"temp-{TASK_NAME}\",\n",
|
||||
" gcs_source=gcs_source_train,\n",
|
||||
" import_schema_uri=aiplatform.schema.dataset.ioformat.video.action_recognition,\n",
|
||||
" sync=False,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "_RwF8f8yURIb"
|
||||
},
|
||||
"source": [
|
||||
"Option 2: Using MBSDK Dataset class\n",
|
||||
"```\n",
|
||||
"dataset = aiplatform.Dataset.create(\n",
|
||||
" display_name=f'temp-{TASK_NAME}',\n",
|
||||
" metadata_schema_uri=aiplatform.schema.dataset.metadata.video,\n",
|
||||
" gcs_source=gcs_source_train, \n",
|
||||
" import_schema_uri=aiplatform.schema.dataset.ioformat.video.action_recognition,\n",
|
||||
" sync=False\n",
|
||||
")\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "Kl4mKHXgVWcS"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dataset.wait()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "above-police"
|
||||
},
|
||||
"source": [
|
||||
"## Launch a Training Job and Create a Model on Vertex AI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "headed-saturn"
|
||||
},
|
||||
"source": [
|
||||
"### Config a Training Job"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "sexual-hayes"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"job = aiplatform.AutoMLVideoTrainingJob(\n",
|
||||
" display_name=f\"temp-{TASK_NAME}\",\n",
|
||||
" prediction_type=PREDICTION_TYPE,\n",
|
||||
" model_type=MODEL_TYPE,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "determined-report"
|
||||
},
|
||||
"source": [
|
||||
"### Run the Training Job"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "exciting-vision"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = job.run(\n",
|
||||
" dataset=dataset,\n",
|
||||
" training_fraction_split=0.8,\n",
|
||||
" test_fraction_split=0.2,\n",
|
||||
" model_display_name=f\"temp-{TASK_NAME}\",\n",
|
||||
" sync=False,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "fundamental-guess"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model.wait()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "pregnant-charlotte"
|
||||
},
|
||||
"source": [
|
||||
"# Batch Prediction Job on the Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "outdoor-courtesy"
|
||||
},
|
||||
"source": [
|
||||
"### Copy AutoML Video Demo Prediction Data for Creating Batch Prediction Job"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "quarterly-sheep"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gcs_source_batch_prediction = f\"gs://{BUCKET_NAME}/{TASK_NAME}/data/video_action_recognition_batch_prediction.jsonl\"\n",
|
||||
"gcs_destination_prefix_batch_prediction = (\n",
|
||||
" f\"gs://{BUCKET_NAME}/{TASK_NAME}/batch_prediction\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "declared-mexico"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!gsutil cp $automl_video_demo_batch_prediction_data $gcs_source_batch_prediction"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "hollywood-clearing"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"batch_predict_job = model.batch_predict(\n",
|
||||
" job_display_name=f\"temp-{TASK_NAME}\",\n",
|
||||
" gcs_source=gcs_source_batch_prediction,\n",
|
||||
" gcs_destination_prefix=gcs_destination_prefix_batch_prediction,\n",
|
||||
" sync=False,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "thorough-yellow"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"batch_predict_job.wait()\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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "instructional-assumption"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"\n",
|
||||
"import tensorflow as tf\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",
|
||||
" break"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "processed-brooklyn"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"line"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"collapsed_sections": [],
|
||||
"name": "AI_Platform_(Unified)_SDK_AutoML_Video_Action_Recognition.ipynb",
|
||||
"toc_visible": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -0,0 +1,496 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "modular-concentration"
|
||||
},
|
||||
"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": "insured-graduation"
|
||||
},
|
||||
"source": [
|
||||
"# Feedback or issues?\n",
|
||||
"\n",
|
||||
"For any feedback or questions, please open an [issue](https://github.com/googleapis/python-aiplatform/issues)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "pregnant-going"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex SDK for Python: AutoML Video Classification Example\n",
|
||||
"To use this Jupyter notebook, copy the notebook to a Google Cloud Notebooks instance with Tensorflow installed and open it. You can run each step, or cell, and see its results. To run a cell, use Shift+Enter. Jupyter automatically displays the return value of the last line in each cell. For more information about running notebooks in Google Cloud Notebook, see the [Google Cloud Notebook guide](https://cloud.google.com/vertex-ai/docs/general/notebooks).\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"This notebook demonstrate how to create an AutoML Video Classification Model, with a Vertex AI video dataset, and how to serve the model for batch prediction. It will require you provide a bucket where the dataset will be stored.\n",
|
||||
"\n",
|
||||
"Note: you may incur charges for training, prediction, storage or usage of other GCP products in connection with testing this SDK."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "pending-chamber"
|
||||
},
|
||||
"source": [
|
||||
"### Install Vertex SDK for Python\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"After the SDK installation the kernel will be automatically restarted."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "coated-remark"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip3 uninstall -y google-cloud-aiplatform\n",
|
||||
"!pip3 install google-cloud-aiplatform\n",
|
||||
"import IPython\n",
|
||||
"\n",
|
||||
"app = IPython.Application.instance()\n",
|
||||
"app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "incorporated-edgar"
|
||||
},
|
||||
"source": [
|
||||
"### Enter Your Project and GCS Bucket\n",
|
||||
"\n",
|
||||
"Enter your Project Id in the cell below. Then run the cell to make sure the Cloud SDK uses the right project for all the commands in this notebook."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "hispanic-macedonia"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MY_PROJECT = \"YOUR PROJECT\"\n",
|
||||
"MY_STAGING_BUCKET = \"gs://YOUR BUCKET\" # bucket should be in same region as ucaip"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "efovKMU5WW7u"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
" import os\n",
|
||||
"\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()\n",
|
||||
" os.environ[\"GOOGLE_CLOUD_PROJECT\"] = MY_PROJECT"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "historical-consciousness"
|
||||
},
|
||||
"source": [
|
||||
"### Set Your Task Name, and GCS Prefix\n",
|
||||
"\n",
|
||||
"If you want to centeralize all input and output files under the gcs location."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "organizational-salad"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"TASK_TYPE = \"mbsdk_automl-video-training\"\n",
|
||||
"PREDICTION_TYPE = \"classification\"\n",
|
||||
"MODEL_TYPE = \"CLOUD\"\n",
|
||||
"\n",
|
||||
"TASK_NAME = f\"{TASK_TYPE}_{PREDICTION_TYPE}\"\n",
|
||||
"BUCKET_NAME = MY_STAGING_BUCKET.split(\"gs://\")[1]\n",
|
||||
"GCS_PREFIX = TASK_NAME\n",
|
||||
"\n",
|
||||
"print(f\"Bucket Name: {BUCKET_NAME}\")\n",
|
||||
"print(f\"Task Name: {TASK_NAME}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "compact-engagement"
|
||||
},
|
||||
"source": [
|
||||
"# HMDB: a large human motion database\n",
|
||||
"We prepared some training data and prediction data for the demo using the [HMDB Dataset](https://serre-lab.clps.brown.edu/resource/hmdb-a-large-human-motion-database).\n",
|
||||
"\n",
|
||||
"The HMDB Dataset is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0/\n",
|
||||
"\n",
|
||||
"For more information about this dataset please visit: https://serre-lab.clps.brown.edu/resource/hmdb-a-large-human-motion-database/"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "SPDHQoFRD-vM"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"automl_video_demo_train_data = (\n",
|
||||
" \"gs://automl-video-demo-data/hmdb_split1_5classes_all.csv\"\n",
|
||||
")\n",
|
||||
"automl_video_demo_batch_prediction_data = (\n",
|
||||
" \"gs://automl-video-demo-data/hmdb_split1_predict.jsonl\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "professional-bulletin"
|
||||
},
|
||||
"source": [
|
||||
"### Copy AutoML Video Demo Train Data for Creating Managed Dataset"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "accurate-producer"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gcs_source_train = f\"gs://{BUCKET_NAME}/{TASK_NAME}/data/video_classification.csv\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "sticky-casino"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!gsutil cp $automl_video_demo_train_data $gcs_source_train"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "rough-alert"
|
||||
},
|
||||
"source": [
|
||||
"# Run AutoML Video Training with Managed Video Dataset"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "adaptive-slovakia"
|
||||
},
|
||||
"source": [
|
||||
"## Initialize Vertex SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the *client* for Vertex AI."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "figured-fellow"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=MY_PROJECT, staging_bucket=MY_STAGING_BUCKET)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "pleasant-holmes"
|
||||
},
|
||||
"source": [
|
||||
"## Create a Dataset on Vertex AI\n",
|
||||
"We will now create a Vertex AI video dataset using the previously prepared csv files. Choose one of the options below. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "Ln-8NdHjTfbH"
|
||||
},
|
||||
"source": [
|
||||
"Option 1: Using MBSDK VideoDataset class"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "uVBfL-0TTjNS"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dataset = aiplatform.VideoDataset.create(\n",
|
||||
" display_name=f\"temp-{TASK_NAME}\",\n",
|
||||
" gcs_source=gcs_source_train,\n",
|
||||
" import_schema_uri=aiplatform.schema.dataset.ioformat.video.classification,\n",
|
||||
" sync=False,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "lXCA_nvHTp_I"
|
||||
},
|
||||
"source": [
|
||||
"Option 2: Using MBSDK Dataset class\n",
|
||||
"```\n",
|
||||
"dataset = aiplatform.Dataset.create(\n",
|
||||
" display_name=f'temp-{TASK_NAME}',\n",
|
||||
" metadata_schema_uri=aiplatform.schema.dataset.metadata.video,\n",
|
||||
" gcs_source=gcs_source_train, \n",
|
||||
" import_schema_uri=aiplatform.schema.dataset.ioformat.video.classification,\n",
|
||||
" sync=False\n",
|
||||
")\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "3x4xuyIbVR_N"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dataset.wait()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "mexican-spending"
|
||||
},
|
||||
"source": [
|
||||
"## Launch a Training Job and Create a Model on Vertex AI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "dynamic-piece"
|
||||
},
|
||||
"source": [
|
||||
"### Config a Training Job"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "continuous-circular"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"job = aiplatform.AutoMLVideoTrainingJob(\n",
|
||||
" display_name=f\"temp-{TASK_NAME}\",\n",
|
||||
" prediction_type=PREDICTION_TYPE,\n",
|
||||
" model_type=MODEL_TYPE,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "juvenile-parameter"
|
||||
},
|
||||
"source": [
|
||||
"### Run the Training Job"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "human-carrier"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = job.run(\n",
|
||||
" dataset=dataset,\n",
|
||||
" training_fraction_split=0.8,\n",
|
||||
" test_fraction_split=0.2,\n",
|
||||
" model_display_name=f\"temp-{TASK_NAME}\",\n",
|
||||
" sync=False,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "abstract-textbook"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model.wait()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "noted-usage"
|
||||
},
|
||||
"source": [
|
||||
"# Batch Prediction Job on the Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ruled-smith"
|
||||
},
|
||||
"source": [
|
||||
"### Copy AutoML Video Demo Prediction Data for Creating Batch Prediction Job"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "polished-dispatch"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gcs_source_batch_prediction = (\n",
|
||||
" f\"gs://{BUCKET_NAME}/{TASK_NAME}/data/video_classification_batch_prediction.jsonl\"\n",
|
||||
")\n",
|
||||
"gcs_destination_prefix_batch_prediction = (\n",
|
||||
" f\"gs://{BUCKET_NAME}/{TASK_NAME}/batch_prediction\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "objective-soldier"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!gsutil cp $automl_video_demo_batch_prediction_data $gcs_source_batch_prediction"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "piano-middle"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"batch_predict_job = model.batch_predict(\n",
|
||||
" job_display_name=f\"temp-{TASK_NAME}\",\n",
|
||||
" gcs_source=gcs_source_batch_prediction,\n",
|
||||
" gcs_destination_prefix=gcs_destination_prefix_batch_prediction,\n",
|
||||
" sync=False,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "visible-scientist"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"batch_predict_job.wait()\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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "moving-geneva"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"\n",
|
||||
"import tensorflow as tf\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",
|
||||
" break\n",
|
||||
"\n",
|
||||
"print(line)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"collapsed_sections": [],
|
||||
"name": "AI_Platform_(Unified)_SDK_AutoML_Video_Classification.ipynb",
|
||||
"toc_visible": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user