Adds the updated managed_notebooks/predictive_maintenance notebook to the official folder (#521)

* adds the updated predictive-maintenance (managed)notebook from community to official folder

* ran linter test

* resubmitting during phase2

* ran linter test

* addresses the review comments: updates based on the new template, sets delete_bucket to False

* ran linter test

* replaces timestamp with uuid

* ran linter test

Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com>
Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
This commit is contained in:
Krishna Chaitanya Movva
2022-08-16 09:39:07 -07:00
committed by GitHub
co-authored by Karl Weinmeister Andrew Ferlitsch
parent fa27309134
commit fa91e45018
2 changed files with 435 additions and 146 deletions
@@ -1,17 +1,62 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "18ebbd838e32"
},
"outputs": [],
"source": [
"# Copyright 2022 Google LLC\n",
"#\n",
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aef73cfa8725"
},
"source": [
"# Predictive Maintenance \n",
"# Predictive Maintenance using Vertex AI\n",
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/predictive_maintainance/predictive_maintenance_usecase.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",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/predictive_maintainance/predictive_maintenance_usecase.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://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/predictive_maintainance/predictive_maintenance_usecase.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
"</table>\n",
"<br/><br/><br/>\n",
"\n",
"\n",
"## Table of contents\n",
"* [Overview](#section-1)\n",
"* [Dataset](#section-2)\n",
"* [Objective](#section-3)\n",
"* [Objective](#section-2)\n",
"* [Dataset](#section-3)\n",
"* [Costs](#section-4)\n",
"* [Data analysis](#section-5)\n",
"* [Fit a regression model](#section-6)\n",
@@ -22,24 +67,32 @@
" * [Create an endpoint](#section-11)\n",
" * [Deploy the model to the created endpoint](#section-12)\n",
" * [Test calling the endpoint](#section-13)\n",
"* [Clean up](#section-14)\n",
"\n",
"\n",
"* [Clean up](#section-14)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "e10c5167a061"
},
"source": [
"## Overview\n",
"<a name=\"section-1\"></a>\n",
"\n",
"This notebook demonstrates how to perform predictive maintenance on industrial data using machine learning techniques, deploy the machine learning model on Vertex AI, and automate the workflow using the executor feature of Vertex AI Workbench.\n",
"In this notebook, you go through a predictive maintenance usecase on industrial data using machine learning techniques, deploy the machine learning model on Vertex AI, and automate the workflow using the executor feature of Vertex AI Workbench.\n",
"\n",
"*Note: This notebook file was developed to run in a [Vertex AI Workbench managed notebooks](https://console.cloud.google.com/vertex-ai/workbench/list/managed) instance using the XGBoost (Local) kernel. Some components of this notebook may not work in other notebook environments.*\n",
"\n",
"## Dataset\n",
"*Note: This notebook file is developed to run in a [Vertex AI Workbench managed notebooks](https://console.cloud.google.com/vertex-ai/workbench/list/managed) instance using the XGBoost (Local) kernel. Some components of this notebook may not work in other notebook environments.*"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fead9e83ebd7"
},
"source": [
"### Objective\n",
"<a name=\"section-2\"></a>\n",
"\n",
"The dataset used in this notebook is a part of the [NASA Turbofan Engine Degradation Simulation dataset](https://ti.arc.nasa.gov/tech/dash/groups/pcoe/prognostic-data-repository/), which consists of simulated time-series data for four sets of fleet engines under different combinations of operational conditions and fault modes. In this notebook, only one of the engine's simulated data (FD001) has been used to analyze and train a model that can predict the engine's remaining useful life.\n",
"\n",
"## Objectives\n",
"<a name=\"section-3\"></a>\n",
"\n",
"The objectives of this notebook include:\n",
"\n",
"- Loading the required dataset from a Cloud Storage bucket.\n",
@@ -49,9 +102,28 @@
"- Evaluating the model.\n",
"- Running the notebook end-to-end as a training job using Executor.\n",
"- Deploying the model on Vertex AI.\n",
"- Clean up.\n",
"- Clean up."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "a71f4d96bf80"
},
"source": [
"### Dataset\n",
"<a name=\"section-3\"></a>\n",
"\n",
"## Costs\n",
"The dataset used in this notebook is a part of the [NASA Turbofan Engine Degradation Simulation dataset](https://ti.arc.nasa.gov/tech/dash/groups/pcoe/prognostic-data-repository/), which consists of simulated time-series data for four sets of fleet engines under different combinations of operational conditions and fault modes. A version of this dataset which is saved to a public Cloud Storage bucket is used in this notebook. In this notebook, one of the engine's simulated data (FD001) is used to analyze and train a model that can predict the engine's remaining useful life."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "36c53c95b4b9"
},
"source": [
"### Costs\n",
"<a name=\"section-4\"></a>\n",
"\n",
"This tutorial uses the following billable components of Google Cloud:\n",
@@ -69,24 +141,126 @@
{
"cell_type": "markdown",
"metadata": {
"id": "5b15a97278df"
"id": "629f52f6efe1"
},
"source": [
"## Before you begin\n",
"\n",
"### Kernel selection\n",
"Select <b>XGBoost</b> kernel while running this notebook on Vertex AI Workbench managed notebooks instances or ensure that the following libraries are installed in the environment where this notebook is being run.\n",
"Select <b>XGBoost</b> kernel while running this notebook on Vertex AI Workbench's managed instances. Otherwise, ensure that the following libraries are installed in the environment where this notebook is being run.\n",
"- XGBoost\n",
"- Pandas\n",
"- Seaborn\n",
"- Sklearn\n",
"\n",
"Along with the above libraries, the following google-cloud libraries are also used in this notebook.\n",
"Along with the above libraries, th`e following google-cloud libraries are also used in this notebook.\n",
"\n",
"- google.cloud.aiplatform\n",
"- google.cloud.storage\n",
"- google.cloud.storage"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "16bee0754628"
},
"source": [
"## Installation\n",
"\n",
"### Set your project ID\n",
"Install the following packages to run this notebook outside Vertex AI Workbench's managed instances."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "69520a67e54c"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
" \n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform \\\n",
" google-cloud-storage \\\n",
" xgboost \\\n",
" seaborn \\\n",
" sklearn \\\n",
" fsspec \\\n",
" gcsfs \\\n",
" pandas -q"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eda79cca981d"
},
"source": [
"### Restart the kernel\n",
"\n",
"Once you've installed the additional packages, you need to restart the notebook kernel so it can find the packages."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "e200999cabe5"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" # Automatically restart kernel after installs\n",
" import IPython\n",
"\n",
" app = IPython.Application.instance()\n",
" app.kernel.do_shutdown(True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5b15a97278df"
},
"source": [
"## Before you begin \n",
"\n",
"### Set up your Google Cloud project\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\n",
"\n",
"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 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": "5aee4379e8e5"
},
"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`."
]
@@ -99,36 +273,67 @@
},
"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": "750bf2883c2d"
},
"source": [
"Otherwise, set your project ID here."
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3c6db1ca88b9"
"id": "5bf9979b96ff"
},
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
"if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
" # Get your GCP project id from gcloud\n",
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID:\", PROJECT_ID)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "09021c90b34c"
},
"outputs": [],
"source": [
"! gcloud config set project $PROJECT_ID"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9658ecf524b1"
},
"source": [
"#### Region\n",
"\n",
"You can also change the `REGION` variable, which is used for operations\n",
"throughout the rest of this notebook. Below are regions supported for Vertex AI. It is recommended that you choose the region closest to you.\n",
"\n",
"- Americas: `us-central1`\n",
"- Europe: `europe-west4`\n",
"- Asia Pacific: `asia-east1`\n",
"\n",
"You may not use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
"\n",
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "5c615e53149f"
},
"outputs": [],
"source": [
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
@@ -137,9 +342,9 @@
"id": "f66f96816fd0"
},
"source": [
"#### Timestamp\n",
"#### UUID\n",
"\n",
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append it onto the name of resources you create in this tutorial."
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
]
},
{
@@ -150,9 +355,84 @@
},
"outputs": [],
"source": [
"from datetime import datetime\n",
"import random\n",
"import string\n",
"\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
"\n",
"# Generate a uuid of a specifed length(default=8)\n",
"def generate_uuid(length: int = 8) -> str:\n",
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
"\n",
"\n",
"UUID = generate_uuid()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "df899ce9999c"
},
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
"authenticated.\n",
"\n",
"**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": "201e8e760d22"
},
"outputs": [],
"source": [
"# If you are running this notebook in Colab, run this cell and follow the\n",
"# instructions to authenticate your GCP account. This provides access to your\n",
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
"\n",
"import os\n",
"import sys\n",
"\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
" google_auth.authenticate_user()\n",
"\n",
" # If you are running this notebook locally, replace the string below with the\n",
" # path to your service account key and run this cell to authenticate your GCP\n",
" # account.\n",
" elif not os.getenv(\"IS_TESTING\"):\n",
" %env GOOGLE_APPLICATION_CREDENTIALS ''"
]
},
{
@@ -161,11 +441,18 @@
"id": "ea53caa30628"
},
"source": [
"## Select or Create a Cloud Storage Bucket for storing the model\n",
"### Create a Cloud Storage bucket\n",
"\n",
"When you create a model resource on Vertex AI using the Cloud SDK, you need to give a Cloud Storage bucket URI of the model where the model is stored. Using the model saved, you can then create Vertex AI model and endpoint resources in order to serve online predictions.\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"Set the name of your Cloud Storage bucket below. It must be unique across all Cloud Storage buckets. 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."
"\n",
"When you create a model in Vertex AI using the Cloud SDK, you give a Cloud Storage path where the trained model is saved. \n",
"In this tutorial, Vertex AI saves the trained model to a Cloud Storage bucket. Using this model artifact, you can then\n",
"create Vertex AI model and endpoint resources in order to serve\n",
"online predictions.\n",
"\n",
"Set the name of your Cloud Storage bucket below. It must be unique across all\n",
"Cloud Storage buckets."
]
},
{
@@ -176,9 +463,8 @@
},
"outputs": [],
"source": [
"BUCKET_NAME = \"[your-bucket-name]\"\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\"\n",
"REGION = \"us-central1\""
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -189,13 +475,9 @@
},
"outputs": [],
"source": [
"# Set a default bucketname in case bucket name is not given\n",
"if BUCKET_NAME == \"\" or BUCKET_NAME is None:\n",
" from datetime import datetime\n",
"\n",
" TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + UUID\n",
" BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -215,7 +497,7 @@
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_NAME"
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
@@ -235,7 +517,7 @@
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_NAME"
"! gsutil ls -al $BUCKET_URI"
]
},
{
@@ -244,7 +526,7 @@
"id": "4c0f6aac282a"
},
"source": [
"## Import the required libraries"
"### Import the required libraries"
]
},
{
@@ -287,7 +569,7 @@
"outputs": [],
"source": [
"# load the data from the source\n",
"INPUT_PATH = \"gs://vertex_ai_managed_services_demo/mfg_predictive_maintenance/train_FD001.txt\" # data source\n",
"INPUT_PATH = \"gs://cloud-samples-data/ai-platform-unified/datasets/tabular/predictive_maintenance.csv\" # data source\n",
"raw_data = pd.read_csv(INPUT_PATH, sep=\" \", header=None)\n",
"# check the data\n",
"print(raw_data.shape)\n",
@@ -492,7 +774,7 @@
"id": "8197cdef2cff"
},
"source": [
"As the current objective is to predict the remaining useful life (RUL) of each unit (ID), the target variable needs to be identified. Since we're dealing with a timeseries data that represents the lifetime of a unit, remaining useful life of a unit can be calculated by subtracting the current cycle from the maximum cycle of that unit.\n",
"As the current objective is to predict the remaining useful life (RUL) of each unit (ID), the target variable needs to be identified. Since you're dealing with a timeseries data that represents the lifetime of a unit, remaining useful life of a unit can be calculated by subtracting the current cycle from the maximum cycle of that unit.\n",
"\n",
"\t\t\t\t\tRUL = Max. Cycle - Current Cycle \n",
"## RUL calculation and Feature selection"
@@ -810,6 +1092,7 @@
"## Running a notebook end-to-end using executor\n",
"<a name=\"section-9\"></a>\n",
"\n",
"**Note:** This section can only be considered when running this notebook on Managed instances from Vertex AI Workbench.\n",
"### Automating the notebook execution\n",
"All the steps followed until now can be run as a training job without using any additional code using the Vertex AI Workbench executor. The executor can help you run a notebook file from start to end, with your choice of the environment, machine type, input parameters, and other characteristics. After setting up an execution, the notebook is executed as a job in Vertex AI custom training. Your jobs can be monitored from the Executor pane in the left sidebar.\n",
"\n",
@@ -817,13 +1100,13 @@
"\n",
"The executor also lets you choose the environment and machine type while automating the runs similar to Vertex AI training jobs without switching to the training jobs UI. Apart from the custom container that replicates the existing kernel by default, pre-built environments like TensorFlow Enterprise, PyTorch, and others can also be selected to run the notebook. The required compute power can be specified by choosing from the list of machine types available, including GPUs.\n",
"\n",
"## Scheduled runs on executor\n",
"### Scheduled runs on executor\n",
"\n",
"Notebook runs can also be scheduled recurringly with the executor. To do so, select Schedule-based recurring executions as the run type instead of One-time execution. The frequency of the job and the time when it executes is provided when you create the execution.\n",
"\n",
"<img src=\"https://storage.googleapis.com/gweb-cloudblog-publish/images/7_Vertex_AI_Workbench.max-1100x1100.jpg\">\n",
"\n",
"## Parameterizing the variables\n",
"### Parameterizing the variables\n",
"\n",
"The executor lets you run a notebook with different sets of input parameters. If you’ve added parameter tags to any of your notebook cells, you can pass in your parameter values to the executor. More about how to use this feature can be found on this [blog](https://cloud.google.com/blog/products/ai-machine-learning/schedule-and-execute-notebooks-with-vertex-ai-workbench).\n",
"\n",
@@ -855,6 +1138,37 @@
"ARTIFACT_GCS_PATH = f\"gs://{BUCKET_NAME}/{BLOB_PATH}\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1aa75b3d4616"
},
"source": [
"Give a display name to the Vertex AI model resource."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "02ca350dba6c"
},
"outputs": [],
"source": [
"# Set the model-dsiplay-name\n",
"MODEL_DISPLAY_NAME = \"[your-model-display-name]\" # @param {type:\"string\"}\n",
"\n",
"# Otherwise, use the default name\n",
"if (\n",
" MODEL_DISPLAY_NAME == \"[your-model-display-name]\"\n",
" or MODEL_DISPLAY_NAME is None\n",
" or MODEL_DISPLAY_NAME == \"\"\n",
"):\n",
" MODEL_DISPLAY_NAME = \"pred_maint_model_\" + UUID\n",
"\n",
"print(MODEL_DISPLAY_NAME)"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -891,6 +1205,28 @@
"Next, create an endpoint resource for deploying the model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "e1e0cd571992"
},
"outputs": [],
"source": [
"# Set the endpoint-dsiplay-name\n",
"ENDPOINT_DISPLAY_NAME = \"[your-endpoint-display-name]\" # @param {type:\"string\"}\n",
"\n",
"# Otherwise, use the default name\n",
"if (\n",
" ENDPOINT_DISPLAY_NAME == \"[your-endpoint-display-name]\"\n",
" or ENDPOINT_DISPLAY_NAME is None\n",
" or ENDPOINT_DISPLAY_NAME == \"\"\n",
"):\n",
" ENDPOINT_DISPLAY_NAME = \"pred_maint_endpoint_\" + UUID\n",
"\n",
"print(ENDPOINT_DISPLAY_NAME)"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -899,6 +1235,7 @@
},
"outputs": [],
"source": [
"# Create the Endpoint resource\n",
"endpoint = aiplatform.Endpoint.create(display_name=ENDPOINT_DISPLAY_NAME)\n",
"\n",
"print(endpoint.display_name)\n",
@@ -915,18 +1252,11 @@
"<a name=\"section-12\"></a>\n",
"\n",
"\n",
"Configure the deployment name, machine type, and other parameters for the deployment and deploy the model to the created endpoint."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ca41cac871d6"
},
"outputs": [],
"source": [
"MACHINE_TYPE = \"n1-standard-2\""
"Configure the following parameters and deploy the model to the created endpoint.\n",
"\n",
"- `endpoint`: The `Endpoint` object created using Vertex AI SDK.\n",
"- `deployed_model_display_name`: A display-name for the deployment.\n",
"- `machine_type`: Type of the machine required for the deployment environment. See [here](https://cloud.google.com/vertex-ai/docs/predictions/configure-compute) for references."
]
},
{
@@ -940,8 +1270,8 @@
"# deploy the model to the endpoint\n",
"model.deploy(\n",
" endpoint=endpoint,\n",
" deployed_model_display_name=DEPLOYED_MODEL_NAME,\n",
" machine_type=MACHINE_TYPE,\n",
" deployed_model_display_name=MODEL_DISPLAY_NAME + \"_deployment\",\n",
" machine_type=\"n1-standard-2\",\n",
")\n",
"\n",
"model.wait()\n",
@@ -984,7 +1314,15 @@
"## Clean up\n",
"<a name=\"section-14\"></a>\n",
"\n",
"Undeploy the model from the endpoint."
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial:\n",
"* Vertex AI Model\n",
"* Vertex AI Endpoint\n",
"* Cloud Storage bucket\n",
"\n",
"Set `delete_bucket` to **True** to delete the Cloud Storage bucket."
]
},
{
@@ -995,68 +1333,19 @@
},
"outputs": [],
"source": [
"DEPLOYED_MODEL_ID = \"\"\n",
"endpoint.undeploy(deployed_model_id=DEPLOYED_MODEL_ID)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "96e427b77791"
},
"source": [
"Delete the endpoint."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ace028ac23ea"
},
"outputs": [],
"source": [
"endpoint.delete()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4b77998d0512"
},
"source": [
"Delete the model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "e034150a4c94"
},
"outputs": [],
"source": [
"model.delete()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "23cb2deb122d"
},
"source": [
"Remove the contents of the Cloud Storage bucket."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "98aaac27d85d"
},
"outputs": [],
"source": [
"! gsutil -m rm -r $BUCKET_URI"
"# Undeploy all the models from the endpoint\n",
"endpoint.undeploy_all()\n",
"\n",
"# Delete the endpoint resource\n",
"endpoint.delete()\n",
"\n",
"# Delete the model resource\n",
"model.delete()\n",
"\n",
"# Delete the Cloud Storage bucket\n",
"delete_bucket = False\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil -m rm -r $BUCKET_URI"
]
}
],