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

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

ResNet 50 模型

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

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.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. Verify 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. Verify 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 工作類型。

    建立資料集

    建立名為 resnet_inference_test 的資料集:

    SQL

    1. 前往「BigQuery」頁面

      前往 BigQuery

    2. 在「編輯器」窗格中,執行下列 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」窗格中,按一下 「新增資料」

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

    3. 在「Filter By」(依條件篩選) 窗格的「Data Source Type」(資料來源類型) 區段中,選取「Databases」(資料庫)

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

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

    5. 按一下「Vertex AI Models: BigQuery Federation」解決方案資訊卡。

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

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

    8. 點選「建立連線」

    9. 在「連線資訊」窗格中,複製「服務帳戶 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 值區

    建立 Cloud Storage 值區,存放模型檔案。

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

    主控台

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

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

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

      「新增主體」對話方塊隨即開啟。

    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 改成您先前建立的值區名稱。

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

    建立物件資料表

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

    SQL

    1. 前往「BigQuery」頁面

      前往 BigQuery

    2. 在「編輯器」窗格中,執行下列 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 資料夾到先前建立的 bucket。

    將模型載入 BigQuery ML

    1. 前往「BigQuery」頁面

      前往 BigQuery

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

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

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

    檢查模型

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

    1. 前往「BigQuery」頁面

      前往 BigQuery

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

    3. 按一下 resnet 模型。

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

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

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

    執行推論

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

    1. 前往「BigQuery」頁面

      前往 BigQuery

    2. 在「編輯器」窗格中,執行下列 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. 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.