教學課程:使用特徵向量模型對物件資料表執行推論

本教學課程將說明如何根據花卉資料集中的圖片建立物件資料表,然後使用 MobileNet V3 模型對該物件資料表執行推論。

MobileNet V3 模型

MobileNet V3 模型會分析圖片檔案,並傳回特徵向量陣列。特徵向量陣列是數值元素清單,用於描述分析圖片的特徵。每個特徵向量都會描述一個多維特徵空間,並提供圖像在這個空間中的座標。您可以使用圖片的特徵向量資訊進一步分類圖片,例如使用餘弦相似度將相似圖片分組。

MobileNet V3 模型輸入會採用形狀為 [-1, 224, 224, 3]DType tf.float32 張量。輸出內容是形狀為 [-1, 1024]tf.float32 張量陣列。

所需權限

  • 您必須具備 bigquery.datasets.create 權限,才能建立資料集。
  • 如要建立連線資源,您必須具備下列權限:

    • bigquery.connections.create
    • bigquery.connections.get
  • 如要將權限授予連線的服務帳戶,您必須具備下列權限:

    • resourcemanager.projects.setIamPolicy
  • 如要建立物件資料表,您必須具備下列權限:

    • bigquery.tables.create
    • bigquery.tables.update
    • bigquery.connections.delegate
  • 您必須具備 storage.buckets.create 權限,才能建立值區。

  • 如要將資料集和模型上傳至 Cloud Storage,您必須具備 storage.objects.createstorage.objects.get 權限。

  • 如要將模型載入 BigQuery ML,您需要具備下列權限:

    • bigquery.jobs.create
    • bigquery.models.create
    • bigquery.models.getData
    • bigquery.models.updateData
  • 如要執行推論,您需要具備下列權限:

    • 物件資料表上的 bigquery.tables.getData
    • 模型的 bigquery.models.getData
    • bigquery.jobs.create

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

  • BigQuery: You incur storage costs for the object table you create in BigQuery.
  • BigQuery ML: You incur costs for the model you create and the inference you perform in BigQuery ML.
  • Cloud Storage: You incur costs for the objects you store in Cloud Storage.

您可以使用 Pricing Calculator 根據預測用量產生預估費用。 新 Google Cloud 使用者可能符合申請免費試用的資格。

如要進一步瞭解 BigQuery 儲存空間定價,請參閱 BigQuery 說明文件中的「儲存空間定價」一節。

如要進一步瞭解 BigQuery ML 定價,請參閱 BigQuery 說明文件中的「BigQuery ML 定價」一節。

如要進一步瞭解 Cloud Storage 的計價方式,請參閱 Cloud Storage 定價頁面。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the BigQuery and BigQuery Connection API APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the BigQuery and BigQuery Connection API APIs.

    Enable the APIs

  8. 建立保留項目

    如要使用匯入的模型與物件資料表,您必須建立預留空間,並使用 BigQuery Enterprise 或 Enterprise Plus 版,然後建立預留空間指派,使用 QUERY 工作類型。

    建立資料集

    建立名為 mobilenet_inference_test 的資料集:

    SQL

    1. 前往「BigQuery」頁面

      前往 BigQuery

    2. 在「編輯器」窗格中執行下列 SQL 陳述式:

      CREATE SCHEMA `PROJECT_ID.mobilenet_inference_test`;

      PROJECT_ID 替換為您的專案 ID。

    bq

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

      啟用 Cloud Shell

    2. 執行 bq mk 指令建立資料集:

      bq mk --dataset --location=us PROJECT_ID:resnet_inference_test

      PROJECT_ID 替換為您的專案 ID。

    建立連線

    建立名為 lake-connection 的連線:

    主控台

    1. 前往「BigQuery」頁面

      前往 BigQuery

    2. 在「Explorer」窗格中,按一下 「Add data」

      「Add data」對話方塊隨即開啟。

    3. 在「Filter By」窗格中的「Data Source Type」部分,選取「Databases」

      或者,您也可以在「Search for data sources」欄位中輸入 Vertex AI

    4. 在「精選資料來源」部分,按一下「Vertex AI」

    5. 按一下「Vertex AI 模型:BigQuery 聯盟」解決方案資訊卡。

    6. 在「連線類型」清單中,選取「Vertex AI 遠端模型、遠端函式和 BigLake (Cloud 資源)」

    7. 在「連線 ID」欄位中輸入 lake-connection

    8. 點選「建立連線」

    9. 在「Explorer」窗格中展開專案,展開「External connections」節點,然後選取 us.lake-connection 連線。

    10. 在「連線資訊」窗格中,複製「服務帳戶 ID」欄位的值。您需要這些資訊,才能在下一個步驟中建立 Cloud Storage 值區,並授予連線服務帳戶權限

    bq

    1. 在 Cloud Shell 中執行 bq mk 指令,建立連線:

      bq mk --connection --location=us --connection_type=CLOUD_RESOURCE \
      lake-connection
      
    2. 執行 bq show 指令,擷取連線相關資訊:

      bq show --connection us.lake-connection
      
    3. properties 欄複製 serviceAccountId 屬性的值,並儲存在其他位置。您需要這些資訊,才能授予連線服務帳戶權限

    建立 Cloud Storage 值區

    1. 建立 Cloud Storage 值區
    2. 在儲存區中建立兩個資料夾,一個命名為 mobilenet 用於模型檔案,另一個命名為 flowers 用於資料集。

    將權限授予連線的服務帳戶

    主控台

    1. 前往「IAM & Admin」(IAM 與管理) 頁面。

      前往「IAM & Admin」(IAM 與管理)

    2. 點選「授予存取權」。

      系統會開啟「Add principals」對話方塊。

    3. 在「新增主體」欄位,輸入先前複製的服務帳戶 ID。

    4. 在「Select a role」(請選擇角色) 欄位中,依序選取「Cloud Storage」和「Storage Object Viewer」(Storage 物件檢視者)

    5. 按一下 [儲存]

    gcloud

    在 Cloud Shell 中執行 gcloud storage buckets add-iam-policy-binding 指令

    gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
    --member=serviceAccount:MEMBER \
    --role=roles/storage.objectViewer

    MEMBER 替換為先前複製的服務帳戶 ID。將 BUCKET_NAME 替換為您先前建立的值區名稱。

    詳情請參閱「在值區層級政策中新增授權對象」。

    將資料集上傳至 Cloud Storage

    取得資料集檔案,並將檔案儲存在 Cloud Storage 中:

    1. 下載花卉資料集至本機電腦。
    2. 解壓縮 flower_photos.tgz 檔案。
    3. 上傳 flower_photos 資料夾至先前建立的值區中的 flowers 資料夾。
    4. 上傳完成後,請刪除 flower_photos 資料夾中的 LICENSE.txt 檔案。

    建立物件資料表

    根據上傳的花朵資料集,建立名為 sample_images 的物件資料表:

    SQL

    1. 前往「BigQuery」頁面

      前往 BigQuery

    2. 在「編輯器」窗格中執行下列 SQL 陳述式:

      CREATE EXTERNAL TABLE mobilenet_inference_test.sample_images
      WITH CONNECTION `us.lake-connection`
      OPTIONS(
        object_metadata = 'SIMPLE',
        uris = ['gs://BUCKET_NAME/flowers/*']);

      請將 BUCKET_NAME 替換為您先前建立的值區名稱。

    bq

    在 Cloud Shell 中執行 bq mk 指令,建立連線:

    bq mk --table \
    --external_table_definition='gs://BUCKET_NAME/flowers/*@us.lake-connection' \
    --object_metadata=SIMPLE \
    mobilenet_inference_test.sample_images

    請將 BUCKET_NAME 替換為您先前建立的值區名稱。

    將模型上傳至 Cloud Storage

    取得模型檔案並在 Cloud Storage 中提供:

    1. 下載 MobileNet V3 模型至本機電腦。這會為模型提供 saved_model.pb 檔案和 variables 資料夾。
    2. 上傳 saved_model.pb 檔案和 variables 資料夾至先前建立的值區中的 mobilenet 資料夾。

    將模型載入 BigQuery ML

    1. 前往「BigQuery」頁面

      前往 BigQuery

    2. 在「編輯器」窗格中執行下列 SQL 陳述式:

      CREATE MODEL `mobilenet_inference_test.mobilenet`
      OPTIONS(
        model_type = 'TENSORFLOW',
        model_path = 'gs://BUCKET_NAME/mobilenet/*');

      BUCKET_NAME 替換為您先前建立的值區名稱。

    檢查模型

    檢查已上傳的模型,瞭解其輸入和輸出欄位:

    1. 前往「BigQuery」頁面

      前往 BigQuery

    2. 在「Explorer」窗格中,依序展開專案、mobilenet_inference_test 資料集,然後展開「Models」節點。

    3. 按一下 mobilenet 模型。

    4. 在隨即開啟的模型面板中,按一下「結構定義」分頁標籤。

    5. 請查看「標籤」部分。這會標示模型輸出的欄位。在此情況下,欄位名稱值為 feature_vector

    6. 請查看「功能」部分。這會指出必須輸入模型的欄位。您可以在 ML.DECODE_IMAGE 函式的 SELECT 陳述式中參照這些項目。在此情況下,欄位名稱值為 inputs

    執行推論

    使用 mobilenet 模型對 sample_images 物件資料表執行推論:

    1. 前往「BigQuery」頁面

      前往 BigQuery

    2. 在「編輯器」窗格中執行下列 SQL 陳述式:

      SELECT *
      FROM ML.PREDICT(
        MODEL `mobilenet_inference_test.mobilenet`,
        (SELECT uri, ML.RESIZE_IMAGE(ML.DECODE_IMAGE(data), 224, 224, FALSE) AS inputs
        FROM mobilenet_inference_test.sample_images)
      );

      結果應如下所示:

      --------------------------------------------------------------------------------------------------------------
      | feature_vector         | uri                                                        | inputs               |
      -------------------------------------------------------------------------------------------------------------
      | 0.850297749042511      | gs://mybucket/flowers/dandelion/3844111216_742ea491a0.jpg  | 0.29019609093666077  |
      -------------------------------------------------------------------------------------------------------------
      | -0.27427938580513      |                                                            | 0.31372550129890442  |
      -------------------------                                                            ------------------------
      | -0.23189745843410492   |                                                            | 0.039215687662363052 |
      -------------------------                                                            ------------------------
      | -0.058292809873819351  |                                                            | 0.29985997080802917  |
      -------------------------------------------------------------------------------------------------------------
      

    清除所用資源

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.