Hello 自訂訓練:設定專案和環境

本頁將逐步說明如何設定 Google Cloud 專案以使用 Vertex AI,以及下載一些用於訓練的 TensorFlow 程式碼。您也會下載網頁應用程式的程式碼,用於取得預測結果。

本教學課程包含多個頁面:

  1. 設定專案和環境。

  2. 訓練自訂圖片分類模型。

  3. 透過自訂圖片分類模型提供預測結果。

  4. 清除專案所用資源。

各頁面都假設您已按照教學課程先前頁面的指示操作。

事前準備

在本教學課程中,請使用 Google Cloud 控制台和 Cloud Shell 與 Google Cloud互動。或者,您也可以使用已安裝 Google Cloud CLI 的其他 Bash 殼層,而不使用 Cloud Shell。

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  8. 在 Google Cloud 控制台中啟用 Cloud Shell。

    啟用 Cloud Shell

    Google Cloud 主控台底部會開啟一個 Cloud Shell 工作階段,並顯示指令列提示。Cloud Shell 是已安裝 Google Cloud CLI 的殼層環境,並已針對您目前的專案設定好相關值。工作階段可能要幾秒鐘的時間才能初始化。

  9. 如果 Cloud Shell 的提示未顯示 (PROJECT_ID)$ (其中 PROJECT_ID 會替換為您的 Google Cloud 專案 ID),請執行下列指令,將 Cloud Shell 設定為使用您的專案:

    gcloud config set project PROJECT_ID
    

建立 Cloud Storage 值區

us-central1 區域建立 Regional Cloud Storage bucket,以供本教學課程的其餘部分使用。按照本教學課程操作時,請將儲存空間用於下列用途:

  • 儲存訓練程式碼,供 Vertex AI 用於自訂訓練工作。
  • 儲存自訂訓練工作輸出的模型構件。
  • 代管可從 Vertex AI 端點取得預測結果的網頁應用程式。

如要建立 Cloud Storage bucket,請在 Cloud Shell 工作階段中執行下列指令:

gcloud storage buckets create gs://BUCKET_NAME --project=PROJECT_ID --location=us-central1

更改下列內容:

  • PROJECT_ID: Google Cloud 專案的 ID。
  • BUCKET_NAME:您為 bucket 選擇的名稱。例如: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/ 目錄封裝至 Python 來源發布套件的設定檔,供 Vertex AI 使用。

  • function/:Python 程式碼目錄,適用於可接收及預先處理來自網頁瀏覽器的預測要求、將要求傳送至 Vertex AI、處理預測回應,以及將回應傳回瀏覽器的 Cloud Run 函式

  • webapp/:這個目錄包含網頁應用程式的程式碼和標記,可從 Vertex AI 取得花卉分類預測結果。

後續步驟

請按照本教學課程的下一頁操作,在 Vertex AI 中執行自訂訓練工作。