mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
refactor, chore(egen): Removes boilerplate, heading fixes, and other corrections from template (#3020)
* Completed code fixing for file prediction/pytorch_image_classification_with_prebuilt_serving_containers.ipynb. Please note that we need to execute file end to end. * Added Colab Enterprice link and testead once again with different environment as previously it was throwing an expecption of libraries * Added below comment: # @title Copyright & License (click to expand) * Fixed issue in notebook based on feedback given by reviewer in PR * Fixed issue based on feedback give on PR
This commit is contained in:
+102
-155
@@ -33,25 +33,27 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"<table align=\"left\">\n",
|
"<table align=\"left\">\n",
|
||||||
"\n",
|
" <td style=\"text-align: center\">\n",
|
||||||
" <td>\n",
|
|
||||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/prediction/pytorch_image_classification_with_prebuilt_serving_containers.ipynb\">\n",
|
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/prediction/pytorch_image_classification_with_prebuilt_serving_containers.ipynb\">\n",
|
||||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||||
" </a>\n",
|
" </a>\n",
|
||||||
" </td>\n",
|
" </td>\n",
|
||||||
" <td>\n",
|
" <td style=\"text-align: center\">\n",
|
||||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/prediction/pytorch_image_classification_with_prebuilt_serving_containers.ipynb\">\n",
|
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fprediction%2Fpytorch_image_classification_with_prebuilt_serving_containers.ipynb\">\n",
|
||||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||||
" View on GitHub\n",
|
|
||||||
" </a>\n",
|
" </a>\n",
|
||||||
" </td>\n",
|
" </td> \n",
|
||||||
" <td>\n",
|
" <td style=\"text-align: center\">\n",
|
||||||
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/official/prediction/pytorch_image_classification_with_prebuilt_serving_containers.ipynb\">\n",
|
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/official/prediction/pytorch_image_classification_with_prebuilt_serving_containers.ipynb\">\n",
|
||||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
|
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Workbench\n",
|
||||||
" Open in Vertex AI Workbench\n",
|
|
||||||
" </a>\n",
|
" </a>\n",
|
||||||
" </td>\n",
|
" </td>\n",
|
||||||
"</table>"
|
" <td style=\"text-align: center\">\n",
|
||||||
|
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/prediction/pytorch_image_classification_with_prebuilt_serving_containers.ipynb\">\n",
|
||||||
|
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> View on GitHub\n",
|
||||||
|
" </a>\n",
|
||||||
|
" </td>\n",
|
||||||
|
"</table>\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -140,19 +142,26 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "i7EUnXsZhAGF"
|
"id": "7cc240e9c1a6"
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Installation\n",
|
"## Get started"
|
||||||
"\n",
|
]
|
||||||
"Install the following packages required to execute this notebook."
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "9d6f71d52f3d"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"### Install Vertex AI SDK for Python and other required packages\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "2b4ef9b72d43"
|
"id": "39883d03df60"
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -167,155 +176,92 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "58707a750154"
|
"id": "16220914acc5"
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"### Colab only: Uncomment the following cell to restart the kernel."
|
"### Restart runtime (Colab only)\n",
|
||||||
|
"\n",
|
||||||
|
"To use the newly installed packages, you must restart the runtime on Google Colab."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "f200f10a1da3"
|
"id": "157953ab28f0"
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
"import sys\n",
|
||||||
"# import IPython\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"# app = IPython.Application.instance()\n",
|
"if \"google.colab\" in sys.modules:\n",
|
||||||
"# app.kernel.do_shutdown(True)"
|
"\n",
|
||||||
|
" import IPython\n",
|
||||||
|
"\n",
|
||||||
|
" app = IPython.Application.instance()\n",
|
||||||
|
" app.kernel.do_shutdown(True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "BF1j6f9HApxa"
|
"id": "e669f8088ac3"
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Before you begin\n",
|
"<div class=\"alert alert-block alert-warning\">\n",
|
||||||
"\n",
|
"<b>⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️</b>\n",
|
||||||
"### Set up your Google Cloud project\n",
|
"</div>\n"
|
||||||
"\n",
|
|
||||||
"**The following steps are required, regardless of your notebook environment.**\n",
|
|
||||||
"\n",
|
|
||||||
"1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\n",
|
|
||||||
"\n",
|
|
||||||
"2. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
|
|
||||||
"\n",
|
|
||||||
"3. [Enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
|
|
||||||
"\n",
|
|
||||||
"4. If you are running this notebook locally, you need to install the [Cloud SDK](https://cloud.google.com/sdk)."
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "WReHDGG5g0XY"
|
"id": "5dccb1c8feb6"
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"#### Set your project ID\n",
|
"### Authenticate your notebook environment (Colab only)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"**If you don't know your project ID**, see the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)"
|
"Authenticate your environment on Google Colab.\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "oM1iC_MfAts1"
|
"id": "cc7251520a07"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import sys\n",
|
||||||
|
"\n",
|
||||||
|
"if \"google.colab\" in sys.modules:\n",
|
||||||
|
"\n",
|
||||||
|
" from google.colab import auth\n",
|
||||||
|
"\n",
|
||||||
|
" auth.authenticate_user()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "60763ee24ce0"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"### Set Google Cloud project information and initialize Vertex AI SDK for Python\n",
|
||||||
|
"\n",
|
||||||
|
"To get started using Vertex AI, you must have an existing Google Cloud project and [enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com). Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "f02130bff721"
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
|
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
|
||||||
"\n",
|
"LOCATION = \"us-central1\" # @param {type:\"string\"}"
|
||||||
"# Set the project id\n",
|
|
||||||
"! gcloud config set project {PROJECT_ID}"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"id": "region"
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"#### Region\n",
|
|
||||||
"\n",
|
|
||||||
"You can also change the `REGION` variable used by Vertex AI. Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"id": "tTy1gX11kCJY"
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"id": "sBCra4QMA2wR"
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"### Authenticate your Google Cloud account\n",
|
|
||||||
"\n",
|
|
||||||
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"id": "74ccc9e52986"
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"**1. Vertex AI Workbench**\n",
|
|
||||||
"* Do nothing as you are already authenticated."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"id": "de775a3773ba"
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"**2. Local JupyterLab instance, uncomment and run:**"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"id": "254614fa0c46"
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# ! gcloud auth login"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"id": "ef21552ccea8"
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"**3. Colab, uncomment and run:**"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"id": "603adbbf0532"
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# from google.colab import auth\n",
|
|
||||||
"# auth.authenticate_user()"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -324,7 +270,7 @@
|
|||||||
"id": "zgPO1eR3CYjk"
|
"id": "zgPO1eR3CYjk"
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"### Create a Cloud Storage bucket\n",
|
"#### Create a Cloud Storage bucket\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Create a storage bucket to store intermediate artifacts such as datasets."
|
"Create a storage bucket to store intermediate artifacts such as datasets."
|
||||||
]
|
]
|
||||||
@@ -346,7 +292,7 @@
|
|||||||
"id": "-EcIXiGsCePi"
|
"id": "-EcIXiGsCePi"
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
"**If your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -357,7 +303,31 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"! gsutil mb -l {REGION} -p {PROJECT_ID} {BUCKET_URI}"
|
"! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "init_aip:mbsdk,all"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"#### Initialize Vertex AI SDK for Python\n",
|
||||||
|
"\n",
|
||||||
|
"Initialize the Vertex AI SDK for Python for your project."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "BnBAXs5XkCJZ"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from google.cloud import aiplatform\n",
|
||||||
|
"\n",
|
||||||
|
"aiplatform.init(project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_URI)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -385,33 +355,10 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"import tensorflow as tf\n",
|
"import tensorflow as tf\n",
|
||||||
"import torch\n",
|
"import torch\n",
|
||||||
"from google.cloud import aiplatform\n",
|
|
||||||
"from PIL import Image\n",
|
"from PIL import Image\n",
|
||||||
"from torchvision import models"
|
"from torchvision import models"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"id": "init_aip:mbsdk,all"
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"### Initialize Vertex AI SDK for Python\n",
|
|
||||||
"\n",
|
|
||||||
"Initialize the Vertex AI SDK for Python for your project."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"id": "BnBAXs5XkCJZ"
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|||||||
Reference in New Issue
Block a user