教學課程:使用分類模型對物件資料表執行推論
本教學課程將說明如何根據公開資料集中的圖片建立物件資料表,然後使用 ResNet 50 模型對該物件資料表執行推論。
ResNet 50 模型
ResNet 50 模型會分析圖片檔案,並輸出代表圖片屬於相應類別的向量 (邏輯值)。詳情請參閱模型的 TensorFlow Hub 頁面中的「用途」部分。
ResNet 50 模型輸入會採用形狀為 [-1, 224, 224, 3]
的 DType
= 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.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 根據預測用量產生預估費用。
如要進一步瞭解 BigQuery 儲存空間定價,請參閱 BigQuery 說明文件中的「儲存空間定價」一節。
如要進一步瞭解 BigQuery ML 定價,請參閱 BigQuery 說明文件中的「BigQuery ML 定價」一節。
如要進一步瞭解 Cloud Storage 的計價方式,請參閱 Cloud Storage 定價頁面。
事前準備
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the BigQuery and BigQuery Connection API APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the BigQuery and BigQuery Connection API APIs.
前往「BigQuery」頁面
在「編輯器」窗格中執行下列 SQL 陳述式:
CREATE SCHEMA `PROJECT_ID.resnet_inference_test`;
將
PROJECT_ID
替換為您的專案 ID。在 Google Cloud 控制台中啟用 Cloud Shell。
執行
bq mk
指令建立資料集:bq mk --dataset --location=us PROJECT_ID:resnet_inference_test
將
PROJECT_ID
替換為您的專案 ID。前往「BigQuery」頁面
在「Explorer」窗格中,按一下
「Add data」。「Add data」對話方塊隨即開啟。
在「Filter By」窗格中的「Data Source Type」部分,選取「Databases」。
或者,您也可以在「Search for data sources」欄位中輸入
Vertex AI
。在「精選資料來源」部分,按一下「Vertex AI」。
按一下「Vertex AI 模型:BigQuery 聯盟」解決方案資訊卡。
在「連線類型」清單中,選取「Vertex AI 遠端模型、遠端函式和 BigLake (Cloud 資源)」。
在「連線 ID」欄位中輸入
lake-connection
。點選「建立連線」。
在「連線資訊」窗格中,複製「服務帳戶 ID」欄位的值,並儲存在其他位置。您需要這些資訊,才能授予連線服務帳戶權限。
在 Cloud Shell 中執行
bq mk
指令,建立連線:bq mk --connection --location=us --connection_type=CLOUD_RESOURCE \ lake-connection
執行
bq show
指令,擷取連線相關資訊:bq show --connection us.lake-connection
從
properties
欄複製serviceAccountId
屬性的值,並儲存在其他位置。您需要這些資訊,才能授予連線服務帳戶權限。前往「IAM & Admin」(IAM 與管理) 頁面。
點選「授予存取權」。
系統會開啟「Add principals」對話方塊。
在「新增主體」欄位,輸入先前複製的服務帳戶 ID。
在「Select a role」(請選擇角色) 欄位中,依序選取「Cloud Storage」和「Storage Object Viewer」(Storage 物件檢視者)。
按一下 [儲存]。
前往「BigQuery」頁面
在「編輯器」窗格中執行下列 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'] );
- 下載 ResNet 50 模型至本機電腦。這會為模型提供
saved_model.pb
檔案和variables
資料夾。 - 上傳
saved_model.pb
檔案和variables
資料夾至先前建立的值區。 前往「BigQuery」頁面
在「編輯器」窗格中執行下列 SQL 陳述式:
CREATE MODEL `resnet_inference_test.resnet` OPTIONS( model_type = 'TENSORFLOW', model_path = 'gs://BUCKET_NAME/*');
將
BUCKET_NAME
替換為您先前建立的值區名稱。前往「BigQuery」頁面
在「Explorer」窗格中,依序展開專案、
resnet_inference_test
資料集,然後展開「Models」節點。按一下
resnet
模型。在隨即開啟的模型面板中,按一下「結構定義」分頁標籤。
請查看「標籤」部分。這會識別模型輸出的欄位。在此情況下,欄位名稱值為
activation_49
。請查看「功能」部分。這會指出必須輸入模型的欄位。您可以在
ML.DECODE_IMAGE
函式的SELECT
陳述式中參照這些項目。在此情況下,欄位名稱值為input_1
。前往「BigQuery」頁面
在「編輯器」窗格中執行下列 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 | —------------------------------------------------------------------------------------------------------------------------------------
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
建立保留項目
如要使用匯入的模型與物件資料表,您必須建立預留空間,並使用 BigQuery Enterprise 或 Enterprise Plus 版,然後建立預留空間指派作業,使用 QUERY
工作類型。
建立資料集
建立名為 resnet_inference_test
的資料集:
SQL
bq
建立連線
建立名為 lake-connection
的連線:
主控台
bq
建立 Cloud Storage bucket
建立 Cloud Storage 值區,用於儲存模型檔案。
將權限授予連線的服務帳戶
主控台
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
值區中的圖片檔案,建立名為 vision_images
的物件資料表:
SQL
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 中提供:
將模型載入 BigQuery ML
檢查模型
檢查已上傳的模型,瞭解其輸入和輸出欄位:
執行推論
使用 resnet
模型對 vision_images
物件資料表執行推論: