本页面逐步演示了如何设置 Google Cloud 项目以使用 Vertex AI,以及如何下载一些 TensorFlow 代码以进行训练。您还将下载获取预测结果的 Web 应用的代码。
本教程包含多个页面:设置项目和环境。
清理项目。
每个页面均假定您已经按照本教程中之前页面的说明操作。
准备工作
在本教程中,您将使用 Google Cloud 控制台和 Cloud Shell 与 Google Cloud进行互动。或者,您也可以使用其他安装了 Google Cloud CLI 的 Bash Shell(而不是 Cloud Shell)。
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Vertex AI and Cloud Run functions APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Vertex AI and Cloud Run functions APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
在 Google Cloud 控制台中,激活 Cloud Shell。
Cloud Shell 会话随即会在 Google Cloud 控制台的底部启动,并显示命令行提示符。Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 Shell 环境。该会话可能需要几秒钟时间来完成初始化。
-
如果 Cloud Shell 未在提示符中显示
(PROJECT_ID)$(其中 PROJECT_ID 已替换为您的 Google Cloud 项目 ID),请运行以下命令将 Cloud Shell 配置为使用您的项目:gcloud config set project PROJECT_ID
创建 Cloud Storage 存储桶
在 us-central1 区域创建区域 Cloud Storage 存储桶,供本教程的其余部分使用。在本教程中,存储桶有以下用途:
- 存储 Vertex AI 的训练代码,以便在自定义训练作业中使用。
- 存储自定义训练作业输出的模型工件。
- 托管从 Vertex AI 端点获取预测结果的 Web 应用。
要创建 Cloud Storage 存储桶,请在 Cloud Shell 会话中运行以下命令:
gcloud storage buckets create gs://BUCKET_NAME --project=PROJECT_ID --location=us-central1
替换以下内容:
- PROJECT_ID:您的 Google Cloud 项目的 ID。
- BUCKET_NAME:您为存储桶选择的名称。例如
hello_custom_PROJECT_ID。了解存储桶名称要求。
下载示例代码
下载示例代码供本教程的其余部分使用。
gcloud storage cp gs://cloud-samples-data/ai-platform/hello-custom/hello-custom-sample-v1.tar.gz - | tar -xzv
(可选)如需查看示例代码文件,请运行以下命令:
ls -lpR hello-custom-sample
hello-custom-sample 目录包含四项:
trainer/:用于训练花卉分类模型的 TensorFlow Keras 代码的目录。setup.py:用于将trainer/目录封装为 Vertex AI 可以使用的 Python 源分发软件包的配置文件。function/:Cloud Run 函数的 Python 代码的目录,可以从网络浏览器接收和预处理预测请求,将其发送到 Vertex AI,处理预测响应,并将其发送回浏览器。webapp/:包含从 Vertex AI 获取花卉分类预测的 Web 应用的代码和标记的目录。
后续步骤
按照本教程的下一页,在 Vertex AI 上运行自定义训练作业。