教學課程:使用分類模型對物件資料表執行推論

本教學課程說明如何根據公開資料集的圖片建立物件資料表,然後使用 ResNet 50 模型對該物件資料表執行推論。

ResNet 50 模型

ResNet 50 模型會分析圖片檔案,並輸出一批向量,代表圖片屬於對應類別 (logits) 的可能性。詳情請參閱模型 TensorFlow Hub 頁面的「Usage」部分。

ResNet 50 模型輸入會採用 DType = float32 的張量,形狀為 [-1, 224, 224, 3]。輸出內容是形狀為 [-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
  • 如要建立 bucket,您必須具備 storage.buckets.create 權限。

  • 如要將模型上傳至 Cloud Storage,您需要 storage.objects.create 和 storage.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. 登入 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 BigQuery and BigQuery Connection API APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). 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 BigQuery and BigQuery Connection API APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

建立預留項目

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

建立資料集

建立名為 resnet_inference_test 的資料集:

SQL

  1. 前往「BigQuery」頁面

    前往「BigQuery」

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

    CREATE SCHEMA `PROJECT_ID.resnet_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」。

    醒目顯示的「Explorer」窗格按鈕。

    如果沒有看到左側窗格,請按一下 「Expand left pane」(展開左側窗格),開啟窗格。

  3. 在「Explorer」窗格中,點選「 新增資料」。

    「新增資料」對話方塊隨即開啟。

  4. 在「篩選依據」窗格的「資料來源類型」部分,選取「資料庫」。

    或者,您也可以在「Search for data sources」(搜尋資料來源) 欄位中輸入 Vertex AI。

  5. 在「精選資料來源」部分,點選「Vertex AI」。

  6. 點選「Vertex AI Models: BigQuery Federation」解決方案資訊卡。

  7. 在「Connection type」(連線類型) 清單中,選取「Vertex AI remote models, remote functions, BigLake and Spanner (Cloud Resource)」(Vertex AI 遠端模型、遠端函式、BigLake 和 Spanner (Cloud 資源))。

  8. 在「連線 ID」欄位輸入 lake-connection。

  9. 點選「建立連線」。

  10. 在「連線資訊」窗格中,複製「服務帳戶 ID」欄位的值,並儲存到任意位置。您需要這項資訊,才能將權限授予連線的服務帳戶。

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 bucket

建立 Cloud Storage bucket,用於存放模型檔案。

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

控制台

  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 替換為先前建立的值區名稱。

詳情請參閱「將主體新增至 bucket 層級政策」。

建立物件資料表

根據公開 gs://cloud-samples-data/vision bucket 中的圖片檔,建立名為 vision_images 的物件資料表:

SQL

  1. 前往「BigQuery」頁面

    前往「BigQuery」

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

    CREATE EXTERNAL TABLE resnet_inference_test.vision_images
    WITH CONNECTION `us.lake-connection`
    OPTIONS(
      object_metadata = 'SIMPLE',
      uris = ['gs://cloud-samples-data/vision/*.jpg']
    );

bq

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

bq mk --table \
--external_table_definition='gs://cloud-samples-data/vision/*.jpg@us.lake-connection' \
--object_metadata=SIMPLE \
resnet_inference_test.vision_images

將模型上傳至 Cloud Storage

取得模型檔案,並將其儲存在 Cloud Storage:

  1. 下載 ResNet 50 模型至本機電腦。這樣一來,您就會取得模型的 saved_model.pb 檔案和 variables 資料夾。
  2. 將 saved_model.pb 檔案和 variables 資料夾上傳至先前建立的值區。

將模型載入 BigQuery ML

  1. 前往「BigQuery」頁面

    前往「BigQuery」

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

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

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

檢查模型

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

  1. 前往「BigQuery」頁面

    前往「BigQuery」

  2. 點選左側窗格中的 「Explorer」。

    醒目顯示的「Explorer」窗格按鈕。

  3. 在「Explorer」窗格中展開專案,依序點選「Datasets」(資料集) 和「resnet_inference_test」資料集。

  4. 前往「模型」分頁。

  5. 按一下「resnet」模型。

  6. 在開啟的模型窗格中,按一下「結構定義」分頁標籤。

  7. 查看「標籤」部分。這會識別模型輸出的欄位。在本例中,欄位名稱值為 activation_49。

  8. 查看「功能」部分。這會指出必須輸入模型的欄位。您可以在 ML.DECODE_IMAGE 函式的 SELECT 陳述式中參照這些引數。在本例中,欄位名稱值為 input_1。

執行推論

使用 resnet 模型,對 vision_images 物件資料表執行推論:

  1. 前往「BigQuery」頁面

    前往「BigQuery」

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

    SELECT *
    FROM ML.PREDICT(
      MODEL `resnet_inference_test.resnet`,
      (SELECT uri, ML.RESIZE_IMAGE(ML.DECODE_IMAGE(data), 224, 224, FALSE) AS input_1
      FROM resnet_inference_test.vision_images)
    );

    結果應大致如下:

    -------------------------------------------------------------------------------------------------------------------------------------
    | activation_49           | uri                                                                                           | input_1 |
    —------------------------------------------------------------------------------------------------------------------------------------
    | 1.0254175464297077e-07  | gs://cloud-samples-data/vision/automl_classification/flowers/daisy/21652746_cc379e0eea_m.jpg  | 0.0     |
    —------------------------------------------------------------------------------------------------------------------------------------
    | 2.1671139620593749e-06  |                                                                                               | 0.0     |
    —--------------------------                                                                                               -----------
    | 8.346052027263795e-08   |                                                                                               | 0.0     |
    —--------------------------                                                                                               -----------
    | 1.159310958342985e-08   |                                                                                               | 0.0     |
    —------------------------------------------------------------------------------------------------------------------------------------
    

清除所用資源

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。